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
/
DataRetrieve.java
177 lines (133 loc) · 5.44 KB
/
DataRetrieve.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import com.sleepycat.db.*;
import java.util.*;
import java.lang.Object;
import java.io.*;
public class DataRetrieve {
//example key that has data: thndjefjyfwgpbmhzbfsfkubphiyvqirxwggmuxhvqnfmczshjaldffddmqyylwfmvbttcpvdjjffawzrdmwzykaspfugguntavetgdszamyogibkekcrvjuf
// 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();
Database database = null;
SecondaryDatabase db2 = null;
String filename = "answers";
WriteToFile fileWrite = new WriteToFile();
Map<String, String> records = new HashMap<String, String>();
OperationStatus oprStat;
DataBase db = DataBase.getInstance();
public DataRetrieve(){
DataBase db = DataBase.getInstance();
if (Pref.getDbType() == 3) {
//if(index.getDataSecondary() == null){
// index.configureDataSecondary();
//}
db2 = db.getIndexSecondary();
}else{
database = db.getPrimaryDb();
//if(database == null){
// throw new RuntimeException("database is null in data retrieve line 40");
//}
}
}
//
public void getRecords(boolean test, String testData) {
//check if database is null, if true than return to main menu
if((Pref.getDbType() == 3 && db2 == null) || (Pref.getDbType() != 3 && database == null)){
System.out.println("Database has not been created!");
return;
}
String searchData;
if(!test){
// gets user input for record to search for
System.out.print("Please enter data you want to search for: ");
searchData = scan.getString();
}
else{
searchData = testData;
}
// start timer, end before returns
long timeStart = System.nanoTime();
DatabaseEntry secKey = new DatabaseEntry();
DatabaseEntry pKey = new DatabaseEntry();
DatabaseEntry dataEntry = new DatabaseEntry();
//OperationStatus success = ;
secKey.setReuseBuffer(false);
pKey.setReuseBuffer(false);
dataEntry.setReuseBuffer(false);
SecondaryDatabase database2 = null;
String data = null;
String key = null;
System.out.println("Searching for data: " + searchData + " in database");
try {
//if there is a index file then we can use key search
if (Pref.getDbType() == 3) {
System.out.println("searching in index file");
SecondaryCursor c = db2.openSecondaryCursor(null, null);
//set dbKey to the data value we are searching for then move cursor
secKey.setData(searchData.getBytes());
secKey.setSize(searchData.length());
if(c.getSearchKey(secKey, pKey, dataEntry, LockMode.DEFAULT)==OperationStatus.SUCCESS){
data = new String (secKey.getData());
key = new String (pKey.getData());
records.put(key, data);
//next if there are duplicate keys after the first get them
while(c.getNextDup(secKey, pKey, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS){
data = new String (secKey.getData());
key = new String (pKey.getData());
records.put(key, data);
}
}
else{
System.out.println("data " + searchData + " is not found");
}
c.close();
}
else{
//if BTREE or HASH then search all records using cursor and return matches
Cursor c = database.openCursor(null, null);
oprStat = c.getFirst(pKey, dataEntry, LockMode.DEFAULT);
//loop through database until we find a matching key
while(oprStat == OperationStatus.SUCCESS){
data = new String (dataEntry.getData());
key = new String (pKey.getData());
if(data.equals(searchData)){
records.put(key, data);
}
oprStat = c.getNext(pKey, dataEntry, LockMode.DEFAULT);
}
c.close();
}
}
catch (DatabaseException e) {
System.err.println("unable to get key/value record!");
}
// end timer and print time
long timeEnd = System.nanoTime();
long time = timeEnd - timeStart;
time = time/1000;
System.out.println();
System.out.println("Records found: "+ records.size());
//get the key values of all records returned
Set keys = records.keySet();
Integer len = keys.size();
String[] hashMapKeys = records.keySet().toArray(new String[len]);
//iterate through hashMapKeys and write key/data pairs to answers.txt
System.out.println();
for(int i = 0; i < len; i++){
System.out.println("Key " + (i+1) + ": " + hashMapKeys[i]);
System.out.println("Data " + (i+1) + ": " + records.get(hashMapKeys[i]));
System.out.println();
fileWrite.writeString(filename, hashMapKeys[i]);
fileWrite.writeString(filename, records.get(hashMapKeys[i]));
fileWrite.writeString(filename, "");
}
System.out.println("TOTAL SEARCH TIME = " + time + " Microseconds");
return;
}
}