00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.ByteArrayInputStream;
00042
import java.io.ByteArrayOutputStream;
00043
import java.io.IOException;
00044
import java.io.PrintWriter;
00045
00046
import java.util.Hashtable;
00047
import java.util.Iterator;
00048
import java.util.Enumeration;
00049
00050
import com.quadcap.sql.io.ObjectInputStream;
00051
import com.quadcap.sql.io.ObjectOutputStream;
00052
00053
import com.quadcap.sql.index.Btree;
00054
00055
import com.quadcap.util.Debug;
00056
import com.quadcap.util.Util;
00057
00058
00059
00060
00061
00062
00063 public class MultiSet {
00064 Btree
map;
00065
00066 public MultiSet(Btree map) {
00067
this.map = map;
00068 }
00069
00070 public void put(String a, String b)
throws IOException {
00071
00072 byte[] ba =
bytes(a);
00073 Hashtable t =
map(map.get(ba));
00074 t.put(b, b);
00075 map.set(ba, bytes(t));
00076 }
00077
00078 public void delete(String a, String b)
throws IOException {
00079
00080 byte[] ba =
bytes(a);
00081 Hashtable t =
map(map.get(ba));
00082 t.remove(b);
00083 map.set(ba, bytes(t));
00084 }
00085
00086 public void rename(String oldN, String newN,
MultiSet rev)
throws IOException {
00087 byte[] oldK =
bytes(oldN);
00088 byte[] oldB =
map.get(bytes(oldN));
00089
if (oldB != null && rev != null) {
00090 Iterator iter =
map(oldB).keySet().iterator();
00091
while (iter.hasNext()) {
00092 String nam = iter.next().toString();
00093 rev.delete(nam, oldN);
00094 rev.put(nam, newN);
00095 }
00096 map.delete(oldK);
00097 map.set(bytes(newN), oldB);
00098 }
00099 }
00100
00101 public Enumeration
get(String key)
throws IOException {
00102 Hashtable t =
map(map.get(
bytes(key)));
00103
return t.keys();
00104 }
00105
00106 static byte[]
bytes(Hashtable t) {
00107
try {
00108
ObjectOutputStream os =
new ObjectOutputStream(null);
00109 os.
writeInt(t.size());
00110 Enumeration e = t.keys();
00111
while (e.hasMoreElements()) {
00112 os.
writeObject(e.nextElement().toString());
00113 }
00114
return os.
toByteArray();
00115 }
catch (IOException e) {
00116
Debug.print(e);
00117
throw new RuntimeException(e.toString());
00118 }
00119 }
00120
00121 static Hashtable
map(byte[] b) {
00122 Hashtable t =
new Hashtable();
00123
if (b != null) {
00124
try {
00125 ByteArrayInputStream bis =
new ByteArrayInputStream(b);
00126
ObjectInputStream is =
new ObjectInputStream(bis);
00127
int len = is.
readInt();
00128
for (
int i = 0; i < len; i++) {
00129 String s = (String)is.
readObject();
00130 t.put(s, s);
00131 }
00132 }
catch (Exception e) {
00133
Debug.print(e);
00134
throw new RuntimeException(e.toString());
00135 }
00136 }
00137
return t;
00138 }
00139
00140 static byte[] bytes(String s) {
00141
return Util.bytes(s);
00142 }
00143
00144 public void delete(String a)
throws IOException {
00145 map.delete(bytes(a));
00146 }
00147
00148
00149 public void display(PrintWriter w)
throws IOException {
00150 map.display(w);
00151 }
00152
00153
00154 }