--- /dev/null
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
+import java.util.Hashtable;\r
+import java.util.Set;\r
+\r
+public class MultiHash{\r
+ private int[] views;\r
+ private Hashtable viewTable = new Hashtable();\r
+\r
+ public MultiHash(){\r
+ }\r
+ \r
+ // Pass in the look up map\r
+ public MultiHash(int[] bitmapArray){\r
+ this.views = bitmapArray;\r
+ for(int i = 0; i < views.length; i++){\r
+ Hashtable ht = new Hashtable();\r
+ viewTable.put(views[i], ht);\r
+ }\r
+ }\r
+\r
+ // For each view add it to its view hashtable\r
+ public void put(Tuples o){\r
+ // Tune the Tuple for each view and add it to its designated hashtable\r
+ for(int i = 0; i < views.length; i++){\r
+ int tupleKey = generateTupleKey(o, views[i]);\r
+ Hashtable tuplesTable = (Hashtable) viewTable.get(views[i]);\r
+ if(tuplesTable.containsKey(tupleKey)){\r
+ Set tupleSet = (Set) tuplesTable.get(tupleKey);\r
+ tupleSet.add(o);\r
+ }else{\r
+ Set tupleSet = new HashSet();\r
+ tupleSet.add(o);\r
+ tuplesTable.put(tupleKey, tupleSet);\r
+ }\r
+ }\r
+ }\r
+\r
+ public int generateTupleKey(Tuples o, int viewIndex){\r
+ ArrayList<Integer> indices = findIndices(viewIndex);\r
+ ArrayList obj = new ArrayList();\r
+ for(int i = 0; i < indices.size(); i++){\r
+ obj.add(o.get(indices.get(i)));\r
+ }\r
+ return obj.hashCode()^29;\r
+ }\r
+\r
+ private ArrayList<Integer> findIndices(int viewIndex){\r
+ int mask = 1;\r
+ ArrayList<Integer> indices = new ArrayList<Integer>();\r
+ for(int i = 0; i < 31; i++){\r
+ if((mask & viewIndex) != 0){\r
+ indices.add(i);\r
+ }\r
+ mask = mask << 1;\r
+ }\r
+ return indices;\r
+ }\r
+\r
+ public Tuples get(int bitmap, Tuple o){\r
+ Tuples tuple = new Tuple(); //\r
+ int tupleKey = generateTupleKey(o, bitmap);\r
+ Hashtable tuplesTable = (Hashtable) viewTable.get(bitmap);\r
+ if(tuplesTable.containsKey(tupleKey)){\r
+ Set tupleSet = (Set) tuplesTable.get(tupleKey);\r
+ tuple = convertToTuple(tupleSet);\r
+ return tuple;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private Tuples convertToTuple(Set tupleSet){\r
+ Object[] tuples = tupleSet.toArray();\r
+ ArrayList o = new ArrayList();\r
+ for(int i = 0; i < tuples.length; i++){\r
+ o.add(tuples[i]);\r
+ }\r
+ Tuples tuple = new Tuple(o);\r
+ return tuple;\r
+ }\r
+\r
+ public void remove(Tuples o){\r
+// System.out.println("removed called"+viewTable.toString());\r
+ for(int i = 0; i < views.length; i++){\r
+ int tupleKey = generateTupleKey(o, views[i]);\r
+ Hashtable tuplesTable = (Hashtable) viewTable.get(views[i]);\r
+ if(tuplesTable.containsKey(tupleKey)){\r
+ tuplesTable.remove(tupleKey);\r
+ }else{\r
+ System.out.println("Cannot find such key");\r
+ }\r
+ }\r
+ }\r
+\r
+ public OptimizedView getOptimizedView(int bitMapView){\r
+ Hashtable tmp = (Hashtable) viewTable.get(bitMapView);\r
+ OptimizedView ov = new OptimizedView(bitMapView, tmp, this);\r
+ return ov;\r
+ }\r
+\r
+ /* Debug visualizations */\r
+ public void drawTierTwoTable(){\r
+ for(int i = 0; i < views.length; i++){\r
+ Hashtable tmp = (Hashtable) viewTable.get(views[i]);\r
+ System.out.println("Hashtable "+i+":\t"+tmp.keySet().toString());\r
+ Object[] keySets = tmp.keySet().toArray();\r
+ for(int j = 0; j < keySets.length; j++){\r
+ System.out.println(tmp.get(keySets[j]));\r
+ }\r
+ }\r
+ }\r
+ \r
+ public int[] getViews(){\r
+ return views;\r
+ }\r
+ \r
+ public Hashtable getTable(){\r
+ return viewTable;\r
+ }\r
+}\r
--- /dev/null
+import java.util.ArrayList;\r
+import java.util.Hashtable;\r
+import java.util.Set;\r
+\r
+public class OptimizedView extends MultiHash\r
+{\r
+ private int bitMapView;\r
+ private Hashtable table;\r
+ private MultiHash parent;\r
+\r
+ public OptimizedView(int bitMapView, Hashtable table, MultiHash parent) {\r
+ this.bitMapView = bitMapView;\r
+ this.table = table;\r
+ this.parent = parent;\r
+ }\r
+\r
+ public void remove(Tuple o){\r
+ parent.remove(o);\r
+ }\r
+ public Tuples get(Tuples o){\r
+ Tuples tuple = new Tuple();\r
+\r
+ int tupleKey = generateTupleKey(o);\r
+ if(table.containsKey(tupleKey)){\r
+ Set tupleSet = (Set) table.get(tupleKey);\r
+ tuple = convertToTuple(tupleSet);\r
+ return tuple;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private Tuples convertToTuple(Set tupleSet){\r
+ Object[] tuples = tupleSet.toArray();\r
+ ArrayList o = new ArrayList();\r
+ for(int i = 0; i < tuples.length; i++){\r
+ o.add(tuples[i]);\r
+ }\r
+ Tuples tuple = new Tuple(o);\r
+ return tuple;\r
+ }\r
+\r
+ public int generateTupleKey(Tuples o){\r
+ ArrayList<Integer> indices = findIndices(bitMapView);\r
+ ArrayList obj = new ArrayList();\r
+ for(int i = 0; i < indices.size(); i++){\r
+ obj.add(o.get(indices.get(i)));\r
+ }\r
+ return obj.hashCode()^29;\r
+ }\r
+\r
+ private ArrayList<Integer> findIndices(int viewIndex){\r
+ int mask = 1;\r
+ ArrayList<Integer> indices = new ArrayList<Integer>();\r
+ for(int i = 0; i < 31; i++){\r
+ if((mask & viewIndex) != 0){\r
+ indices.add(i);\r
+ }\r
+ mask = mask << 1;\r
+ }\r
+ return indices;\r
+ }\r
+\r
+ public String toString(){\r
+ return table.toString();\r
+ }\r
+}\r