1 package com.quadcap.sql; 2 3 40 41 import java.io.ByteArrayInputStream ; 42 import java.io.ByteArrayOutputStream ; 43 import java.io.IOException ; 44 import java.io.PrintWriter ; 45 46 import java.util.Hashtable ; 47 import java.util.Iterator ; 48 import java.util.Enumeration ; 49 50 import com.quadcap.sql.io.ObjectInputStream; 51 import com.quadcap.sql.io.ObjectOutputStream; 52 53 import com.quadcap.sql.index.Btree; 54 55 import com.quadcap.util.Debug; 56 import com.quadcap.util.Util; 57 58 63 public class MultiSet { 64 Btree map; 65 66 public MultiSet(Btree map) { 67 this.map = map; 68 } 69 70 public void put(String a, String b) throws IOException { 71 byte[] ba = bytes(a); 73 Hashtable t = map(map.get(ba)); 74 t.put(b, b); 75 map.set(ba, bytes(t)); 76 } 77 78 public void delete(String a, String b) throws IOException { 79 byte[] ba = bytes(a); 81 Hashtable t = map(map.get(ba)); 82 t.remove(b); 83 map.set(ba, bytes(t)); 84 } 85 86 public void rename(String oldN, String newN, MultiSet rev) throws IOException { 87 byte[] oldK = bytes(oldN); 88 byte[] oldB = map.get(bytes(oldN)); 89 if (oldB != null && rev != null) { 90 Iterator iter = map(oldB).keySet().iterator(); 91 while (iter.hasNext()) { 92 String nam = iter.next().toString(); 93 rev.delete(nam, oldN); 94 rev.put(nam, newN); 95 } 96 map.delete(oldK); 97 map.set(bytes(newN), oldB); 98 } 99 } 100 101 public Enumeration get(String key) throws IOException { 102 Hashtable t = map(map.get(bytes(key))); 103 return t.keys(); 104 } 105 106 static byte[] bytes(Hashtable t) { 107 try { 108 ObjectOutputStream os = new ObjectOutputStream(null); 109 os.writeInt(t.size()); 110 Enumeration e = t.keys(); 111 while (e.hasMoreElements()) { 112 os.writeObject(e.nextElement().toString()); 113 } 114 return os.toByteArray(); 115 } catch (IOException e) { 116 Debug.print(e); 117 throw new RuntimeException (e.toString()); 118 } 119 } 120 121 static Hashtable map(byte[] b) { 122 Hashtable t = new Hashtable (); 123 if (b != null) { 124 try { 125 ByteArrayInputStream bis = new ByteArrayInputStream (b); 126 ObjectInputStream is = new ObjectInputStream(bis); 127 int len = is.readInt(); 128 for (int i = 0; i < len; i++) { 129 String s = (String )is.readObject(); 130 t.put(s, s); 131 } 132 } catch (Exception e) { 133 Debug.print(e); 134 throw new RuntimeException (e.toString()); 135 } 136 } 137 return t; 138 } 139 140 static byte[] bytes(String s) { 141 return Util.bytes(s); 142 } 143 144 public void delete(String a) throws IOException { 145 map.delete(bytes(a)); 146 } 147 148 public void display(PrintWriter w) throws IOException { 150 map.display(w); 151 } 152 154 } 155 | Popular Tags |