KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > MultiSet


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.PrintWriter JavaDoc;
45
46 import java.util.Hashtable JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Enumeration JavaDoc;
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 /**
59  * Implement a multi-valued map over a Btree.
60  *
61  * @author Stan Bailes
62  */

63 public class MultiSet {
64     Btree map;
65
66     public MultiSet(Btree map) {
67     this.map = map;
68     }
69     
70     public void put(String JavaDoc a, String JavaDoc b) throws IOException JavaDoc {
71     //Debug.println("put(" + a + ", " + b + ")");
72
byte[] ba = bytes(a);
73     Hashtable JavaDoc t = map(map.get(ba));
74     t.put(b, b);
75     map.set(ba, bytes(t));
76     }
77
78     public void delete(String JavaDoc a, String JavaDoc b) throws IOException JavaDoc {
79     //Debug.println("delete(" + a + ", " + b + ")");
80
byte[] ba = bytes(a);
81     Hashtable JavaDoc t = map(map.get(ba));
82     t.remove(b);
83     map.set(ba, bytes(t));
84     }
85
86     public void rename(String JavaDoc oldN, String JavaDoc newN, MultiSet rev) throws IOException JavaDoc {
87         byte[] oldK = bytes(oldN);
88         byte[] oldB = map.get(bytes(oldN));
89         if (oldB != null && rev != null) {
90             Iterator JavaDoc iter = map(oldB).keySet().iterator();
91             while (iter.hasNext()) {
92                 String JavaDoc 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 JavaDoc get(String JavaDoc key) throws IOException JavaDoc {
102     Hashtable JavaDoc t = map(map.get(bytes(key)));
103     return t.keys();
104     }
105
106     static byte[] bytes(Hashtable JavaDoc t) {
107     try {
108         ObjectOutputStream os = new ObjectOutputStream(null);
109         os.writeInt(t.size());
110         Enumeration JavaDoc e = t.keys();
111         while (e.hasMoreElements()) {
112         os.writeObject(e.nextElement().toString());
113         }
114         return os.toByteArray();
115     } catch (IOException JavaDoc e) {
116         Debug.print(e);
117         throw new RuntimeException JavaDoc(e.toString());
118     }
119     }
120
121     static Hashtable JavaDoc map(byte[] b) {
122     Hashtable JavaDoc t = new Hashtable JavaDoc();
123     if (b != null) {
124         try {
125         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(b);
126         ObjectInputStream is = new ObjectInputStream(bis);
127         int len = is.readInt();
128         for (int i = 0; i < len; i++) {
129             String JavaDoc s = (String JavaDoc)is.readObject();
130             t.put(s, s);
131         }
132         } catch (Exception JavaDoc e) {
133         Debug.print(e);
134         throw new RuntimeException JavaDoc(e.toString());
135         }
136     }
137     return t;
138     }
139
140     static byte[] bytes(String JavaDoc s) {
141     return Util.bytes(s);
142     }
143     
144     public void delete(String JavaDoc a) throws IOException JavaDoc {
145     map.delete(bytes(a));
146     }
147
148     //#ifdef DEBUG
149
public void display(PrintWriter JavaDoc w) throws IOException JavaDoc {
150         map.display(w);
151     }
152     //#endif
153

154 }
155
Popular Tags