-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashDriver.java
63 lines (60 loc) · 1.84 KB
/
HashDriver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import simpledb.server.*;
import simpledb.index.*;
import simpledb.index.hash.*;
import simpledb.query.*;
import simpledb.record.RID;
import simpledb.record.Schema;
import simpledb.record.TableInfo;
import simpledb.tx.Transaction;
import java.util.Scanner;
public class HashDriver{
private static Schema idxsch = new Schema();
private static Schema messySch = new Schema();
private static Transaction tx;
private static Transaction tx2;
private static TableScan ts;
public static void main(String[] args) {
SimpleDB.init("studentdb");
System.out.println("BUILD LINEAR HASH INDEX");
tx = new Transaction();
tx2 = new Transaction();
// Defines the schema for an index record for Col2 of the messy table
idxsch.addStringField("dataval",64);
idxsch.addIntField("block");
idxsch.addIntField("id");
messySch.addIntField("col1");
messySch.addStringField("col2",64);
// Builds a Linear Hash Index on Col2 of the messy table
LinearHash idx = new LinearHash("hashIdxTest", idxsch, tx);
Plan p = new TablePlan("messy", tx);
UpdateScan s = (UpdateScan) p.open();
while (s.next()){
idx.insert(s.getVal("col2"), s.getRid());
}
s.close();
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("Enter searchkey: (q to quit)");
String str = scan.next();
if((str.equals("q"))||(str.equals("quit"))){
break;
}
Constant searchkey = new StringConstant(str);
RID rid = idx.search(searchkey);
if (rid==null){
System.out.println("Not Found");
}
else{
String tblname = "messy";
TableInfo ti = new TableInfo(tblname, messySch);
ts = new TableScan(ti, tx2);
ts.moveToRid(rid);
System.out.println(ts.getInt("col1")+"\t"+ts.getVal("col2"));
ts.close();
}
}
idx.close();
tx.rollback();
tx2.rollback();
}
}