import simpledb.index.*; import simpledb.index.query.*; import simpledb.index.hash.HashIndex; import simpledb.query.*; import simpledb.record.*; import simpledb.server.SimpleDB; import simpledb.tx.Transaction; import java.util.*; public class HashTest { private static Schema idxsch = new Schema(); private static Transaction tx; public static void test() { System.out.println("BEGIN HASH PACKAGE TEST"); tx = new Transaction(); idxsch.addIntField("majorid"); idxsch.addIntField("dataval"); idxsch.addIntField("block"); idxsch.addIntField("id"); testInsert(); testDelete(); testIndexSelect(); testIndexJoin(); tx.rollback(); } private static void testInsert() { Index idx = new HashIndex("hashIdx", idxsch, tx); Plan p = new TablePlan("student", tx); UpdateScan s = (UpdateScan) p.open(); while (s.next()) idx.insert(s.getVal("majorid"), s.getRid()); s.beforeFirst(); Constant searchkey = new IntConstant(10); List rids = new ArrayList(); while (s.next()) if (s.getVal("majorid").equals(searchkey)) rids.add(s.getRid()); s.close(); int ridcount = 0; idx.beforeFirst(searchkey); while (idx.next()) { ridcount++; RID rid = idx.getDataRid(); if (!rids.contains(rid)) System.out.println("*****HashTest: rid not inserted"); } idx.close(); if (ridcount != rids.size()) System.out.println("*****HashTest: not enough rids inserted"); } private static void testDelete() { Index idx = new HashIndex("hashIdx", idxsch, tx); Plan p = new TablePlan("student", tx); UpdateScan s = (UpdateScan) p.open(); Constant searchkey = new IntConstant(10); while (s.next()) if (s.getVal("majorid").equals(searchkey)) { idx.delete(searchkey, s.getRid()); s.delete(); } s.close(); idx.beforeFirst(searchkey); while (idx.next()) System.out.println("*****HashTest: rid not deleted"); idx.close(); } private static void testIndexSelect() { Index idx = new HashIndex("hashIdx", idxsch, tx); Plan p = new TablePlan("student", tx); TableScan s1 = (TableScan) p.open(); Constant searchkey = new IntConstant(20); Scan s2 = new IndexSelectScan(idx, searchkey, s1); List vals = new ArrayList(); while (s2.next()) vals.add(s2.getVal("sid")); s2.close(); // also closes s1 and idx int count = 0; Scan s3 = p.open(); while (s3.next()) if (s3.getVal("majorid").equals(searchkey)) { count++; if (!vals.contains(s3.getVal("sid"))) System.out.println("*****HashTest: bad index select"); } s3.close(); if (count != vals.size()) System.out.println("*****HashTest: bad index select count"); } private static void testIndexJoin() { Index idx = new HashIndex("hashIdx", idxsch, tx); Plan p1 = new TablePlan("student", tx); TableScan s1 = (TableScan) p1.open(); Plan p2 = new TablePlan("dept", tx); Scan s2 = p2.open(); Scan s3 = new IndexJoinScan(s2, idx, "did", s1); Plan p4 = new ProductPlan(p1, p2); Expression e1 = new FieldNameExpression("majorid"); Expression e2 = new FieldNameExpression("did"); Term t = new Term(e1, e2); Predicate pred = new Predicate(t); Plan p5 = new SelectPlan(p4, pred); Scan s5 = p5.open(); // check to see if s5 is the same as s3 List vals = new ArrayList(); while (s5.next()) vals.add(s5.getVal("sid")); s5.close(); int count = 0; while (s3.next()) { count++; if (!vals.contains(s3.getVal("sid"))) System.out.println("*****HashTest: bad index join"); } s3.close(); if (count != vals.size()) System.out.println("*****HashTest: bad index join count"); } }