This repository has been archived by the owner on Aug 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyRetrieve.java
136 lines (122 loc) · 4.66 KB
/
KeyRetrieve.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import com.sleepycat.db.*;
import java.util.concurrent.TimeUnit;
public class KeyRetrieve {
//examples
// Random Key: thndjefjyfwgpbmhzbfsfkubphiyvqirxwggmuxhvqnfmczshjaldffddmqyylwfmvbttcpvdjjffawzrdmwzykaspfugguntavetgdszamyogibkekcrvjuf
// Random Data: djjtchkroyzbyzqycjbjfvkxuwuywywkcvqltyagjavmhpewjuhfqsaawwzwvusrobrzmkbstekgkbawzkl
// Last key: zzzzevhwlhknxjmuxosuaunkzvqaynhihjpryfhwzziekyjpwhtsffnvywrrcwpxpmavfbqzwbxwkwwodfxiwcxsvkfznsgymytctmjleuxohjezlyeodccqletvf
// Last Data: woqlahmzrqlyhhjzdklmjqolkbhhiasczpgukpyobcwztbsffleukvbfdnqnubmorshieukeclbbtie
// DATA BELOW I'M 100% SURE IS RIGHT AS I PRINTED IT AS THE DATABASE WAS BEING CREATED.
// key: oohiqwurgzsllzvhgigpxqwzbenyyjxuczmewrecjmxuvgjlzrnfxlmgzoilphatfquyyaadzvnztflneudhykt
// data: hnrkmhfcwwzplsykkcaxqdtsnlenyanztgszjgnzvdgpidenkicynfdsgeyvgbnbaxdxlodtwexdlp
// key: hgfjlbziiqxoqcpdsfginenvozqvyhnsdzwnzmubbxybauzdsolvbtxqqsbdqznvkjixrnaxfmcx
// data: zqmornwieprpscrhbldbvrnexrqwxtkzufejctzshhhmblhfiwzwnhcadcotyueskadatwtowrfkxf
// key: szysjqctdhewbmldkyzojvnpatqlhofdiavalosfwidkrppmshbcmrihpxuqdmyhvyvpuplerjsqxaseuetlsznwfclfytahorfqfgagjslzkqmlebh
// data: fgzqeklegbkhfmvybrrlaeqprkwchikudsvdgksxkoxpmeaeqchluypdvatveqreeevnnbqszeykoyddsflsgnktspcfpgggmvkricdrlzfamhisqyyuljptcsnc
Scan scan = Scan.getInstance();
// creates database or gets instance if it was already created
DataBase db = DataBase.getInstance();
Database database = null;
String filename = "answers";
WriteToFile fileWrite = new WriteToFile();
public KeyRetrieve(){
if(Pref.getDbType() != 3){
database = db.getPrimaryDb();
if(database == null){
db.initDataBase();
database = db.getPrimaryDb();
}
}else{
database = db.getIndexTree();
if(database == null){
db.initDataBase();
database = db.getIndexTree();
}
}
}
// main of KeyRetrieve
public void getRecords(boolean test, String testKey) {
String key = null;
// check if databse was populated by user yet
if (database == null) {
System.out.println("Database needs to be populated first!");
return;
}
if(!test){
// gets user input for record to search for
System.out.print("Please enter key you want to search for: ");
key = scan.getString();
}else{
key = testKey;
}
// set up inputed key and data
DatabaseEntry dbKey = new DatabaseEntry();
dbKey.setData(key.getBytes());
dbKey.setSize(key.length());
DatabaseEntry data = new DatabaseEntry();
// start timer, end before returns
long timeStart = System.nanoTime();
System.out.println("Searching for key in database");
// get the data record
try {
database.get(null, dbKey, data, LockMode.DEFAULT);
}
catch (DatabaseException e) {
System.err.println("unable to get key/value record!");
}
// end timer and print time
long timeEnd = System.nanoTime();
long time = TimeUnit.MICROSECONDS.convert(timeEnd - timeStart, TimeUnit.NANOSECONDS);
// prints the records found
if (data.getData() == null) {
System.out.println("Records found: 0");
}
else {
System.out.println("Records found: 1");
String sData = new String (data.getData());
String fKey = new String (dbKey.getData());
System.out.println("Key: " + fKey);
System.out.println("Data: " + sData);
fileWrite.writeString(filename, fKey);
fileWrite.writeString(filename, sData);
fileWrite.writeString(filename, "");
}
System.out.println("Search time = " + time + " Microseconds");
return;
}
public void getIndexFileRecord(boolean test, String testKey) throws DatabaseException{
String key;
if(!test){
System.out.print("Enter key: ");
key = scan.getString();
}
else{
key = testKey;
}
String data = null;
int recordsFound = 0;
Cursor c = database.openCursor(null, null);
DatabaseEntry cKey = new DatabaseEntry();
DatabaseEntry cData = new DatabaseEntry();
cKey.setSize(key.length());
cKey.setData(key.getBytes());
OperationStatus status;
long startTime = System.nanoTime();
status = c.getSearchKey(cKey, cData, LockMode.DEFAULT);
if(status == OperationStatus.SUCCESS){
data = new String (cData.getData());
}
long endTime = System.nanoTime();
long duration = TimeUnit.MICROSECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS);
if(status == OperationStatus.SUCCESS){
recordsFound = 1;
fileWrite.writeString(filename, key);
fileWrite.writeString(filename, data);
fileWrite.writeString(filename, "");
}
System.out.println("Records found: " + recordsFound + " Search time: " + duration + " Microseconds");
System.out.println("Record information");
System.out.println("key: " + key);
System.out.println("data: " + data);
}
}