Quadcap Embeddable Database

com/quadcap/sql/MultiSet.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * Implement a multi-valued map over a Btree. 00060 * 00061 * @author Stan Bailes 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 //Debug.println("put(" + a + ", " + b + ")"); 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 //Debug.println("delete(" + a + ", " + b + ")"); 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 //#ifdef DEBUG 00149 public void display(PrintWriter w) throws IOException { 00150 map.display(w); 00151 } 00152 //#endif 00153 00154 }