001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.ByteArrayInputStream;
042: import java.io.ByteArrayOutputStream;
043: import java.io.IOException;
044: import java.io.PrintWriter;
045:
046: import java.util.Hashtable;
047: import java.util.Iterator;
048: import java.util.Enumeration;
049:
050: import com.quadcap.sql.io.ObjectInputStream;
051: import com.quadcap.sql.io.ObjectOutputStream;
052:
053: import com.quadcap.sql.index.Btree;
054:
055: import com.quadcap.util.Debug;
056: import com.quadcap.util.Util;
057:
058: /**
059: * Implement a multi-valued map over a Btree.
060: *
061: * @author Stan Bailes
062: */
063: public class MultiSet {
064: Btree map;
065:
066: public MultiSet(Btree map) {
067: this .map = map;
068: }
069:
070: public void put(String a, String b) throws IOException {
071: //Debug.println("put(" + a + ", " + b + ")");
072: byte[] ba = bytes(a);
073: Hashtable t = map(map.get(ba));
074: t.put(b, b);
075: map.set(ba, bytes(t));
076: }
077:
078: public void delete(String a, String b) throws IOException {
079: //Debug.println("delete(" + a + ", " + b + ")");
080: byte[] ba = bytes(a);
081: Hashtable t = map(map.get(ba));
082: t.remove(b);
083: map.set(ba, bytes(t));
084: }
085:
086: public void rename(String oldN, String newN, MultiSet rev)
087: throws IOException {
088: byte[] oldK = bytes(oldN);
089: byte[] oldB = map.get(bytes(oldN));
090: if (oldB != null && rev != null) {
091: Iterator iter = map(oldB).keySet().iterator();
092: while (iter.hasNext()) {
093: String nam = iter.next().toString();
094: rev.delete(nam, oldN);
095: rev.put(nam, newN);
096: }
097: map.delete(oldK);
098: map.set(bytes(newN), oldB);
099: }
100: }
101:
102: public Enumeration get(String key) throws IOException {
103: Hashtable t = map(map.get(bytes(key)));
104: return t.keys();
105: }
106:
107: static byte[] bytes(Hashtable t) {
108: try {
109: ObjectOutputStream os = new ObjectOutputStream(null);
110: os.writeInt(t.size());
111: Enumeration e = t.keys();
112: while (e.hasMoreElements()) {
113: os.writeObject(e.nextElement().toString());
114: }
115: return os.toByteArray();
116: } catch (IOException e) {
117: Debug.print(e);
118: throw new RuntimeException(e.toString());
119: }
120: }
121:
122: static Hashtable map(byte[] b) {
123: Hashtable t = new Hashtable();
124: if (b != null) {
125: try {
126: ByteArrayInputStream bis = new ByteArrayInputStream(b);
127: ObjectInputStream is = new ObjectInputStream(bis);
128: int len = is.readInt();
129: for (int i = 0; i < len; i++) {
130: String s = (String) is.readObject();
131: t.put(s, s);
132: }
133: } catch (Exception e) {
134: Debug.print(e);
135: throw new RuntimeException(e.toString());
136: }
137: }
138: return t;
139: }
140:
141: static byte[] bytes(String s) {
142: return Util.bytes(s);
143: }
144:
145: public void delete(String a) throws IOException {
146: map.delete(bytes(a));
147: }
148:
149: //#ifdef DEBUG
150: public void display(PrintWriter w) throws IOException {
151: map.display(w);
152: }
153: //#endif
154:
155: }
|