/
query.java
358 lines (325 loc) · 8.63 KB
/
query.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import java.util.*;
import java.io.*;
import java.lang.String;
import com.sleepycat.db.*;
import java.lang.Object;
public class query
{
private static Vector<String> text = new Vector<String>();
private static Vector<String> con = new Vector<String>();
private static Vector<String> title = new Vector<String>();
private static Set<String> resultSet = new HashSet<String>();
private static int searches = 0;
private static DatabaseConfig dbConfig = new DatabaseConfig();
private static DatabaseEntry key = new DatabaseEntry();
private static DatabaseEntry data = new DatabaseEntry();
private static boolean firstKeyword = true;
public static void main(String args[])
{
String input = readEntry("Enter Query:");
String[] tokens = input.split(":");
//fixes the problem of extra colons not related to a search
int len = tokens.length;
for (int i = 0; i < len - 1; i++)
{
boolean found = false;
if( (tokens[i].substring((tokens[i].length() - 2), tokens[i].length())).compareTo("tx") == 0 )
{
found =true;
}
else if( (tokens[i].substring((tokens[i].length() - 2), tokens[i].length())).compareTo("ti") == 0 )
{
found =true;
}
else if( (tokens[i].substring((tokens[i].length() - 2), tokens[i].length())).compareTo("co") == 0 )
{
found =true;
}
if (found == false)// then there was a split and : didn't belong to a search term
{
//join this sting with the next one, and shuffle down all the other strings
String temp = tokens[i] + ":" + tokens[i+1];
tokens[i] = temp;
for (int j = i+1; j < len-1; j++)
{
tokens[j] = tokens[j+1];
}
len--;
}
}
//split on :
//read the last 2 characters of the previous string. adds all but the last 2 characters of the next string
//unless its the last string, then the whole thing is added
searches += split("tx", text, tokens);
searches += split("ti", title, tokens);
searches += split("co", con, tokens);
//database searches
searchDB("tx.idx", text);
searchDB("ti.idx", title);
searchDB("co.idx", con);
try
{
//see results that have hits that matches searches
boolean found = false;
if (resultSet.size() > 0)
{
Iterator<String> iter = resultSet.iterator();
while (iter.hasNext())
{
findMatching(iter.next());
}
}
else
{
System.out.println("No results");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
/**
* Function:
* Print the title of the matching doc id from the searches.
*
* Param:
* match - the doc id of the search result.
*
* Return:
* None.
*/
static void findMatching(String match)
{
// find the text where the id is the same as match,
//then split the string on | and print the token[1] bit as title and ID
//search the title database for title base on match
System.out.println();
try
{
Database ti_db = new Database("doc.idx", null, dbConfig);
DatabaseEntry id = new DatabaseEntry();
DatabaseEntry title = new DatabaseEntry();
id.setData(match.getBytes());
id.setSize(match.length());
OperationStatus oprStatus;
if (ti_db.get(null, id, title, LockMode.DEFAULT) == OperationStatus.SUCCESS)
{
String b = new String (title.getData());
String[] maiden = b.split("|"); //iron
System.out.println("Id: "+match);
System.out.print("Title: ");
int i = 1;
while (maiden[i].equals("|") == false)
{
System.out.print(maiden[i]);
i++;
}
System.out.println();
}
ti_db.close();
}
catch (Exception ex)
{
ex.getMessage();
System.out.println(ex.getMessage());
}
}
/**
* Function:
* Read a line from stdin.
*
* Param:
* prompt - the prompt string to print.
*
* Return:
* The line read from user.
*/
static String readEntry(String prompt)
{
try
{
StringBuffer buffer = new StringBuffer();
System.out.println(prompt);
System.out.flush();
int c = System.in.read();
while (c!= '\n' && c != -1)
{
buffer.append((char) c);
c = System.in.read();
}
return buffer.toString().trim();
}
catch (IOException e)
{
return "";
}
}
/**
* Function:
* Search the tokens array for string compare. The next element after this token is the
* desired search term.
*
* Param:
* compare - the token that represents the type of keyword search (tx, ti, co)
* dest - the vector to put the keyword searches that belong to this type of compare.
* tokens - the input array of tokens. It is assumed that the next element following a token
* is a string of everything up to and including the next token identifier.
*
* Return:
* An integer that represents the number of search terms found.
*/
static int split(String compare, Vector<String> dest, String[] tokens)
{
int len = tokens.length;
int searches = 0;
for (int i = 0; i <(len-1); i++)
{
if( (tokens[i].substring((tokens[i].length() - 2), tokens[i].length())).compareTo(compare) == 0 )
{
String temp = "";
if((i+1) < (len -1))
{
temp = tokens[i+1].substring(0, (tokens[i+1]).length()-3);
dest.add(temp);
searches++;
}
if((i + 1) == (len -1))
{
temp = tokens[i+1];
dest.add(temp);
searches++;
}
}
}
return searches;
}
/**
* Function:
* Perform the search on a database.
*
* Param:
* DBfile - the filename of the database that the searches will be performed in.
* vec - the vector that holds the search terms.
*
* Return:
* None.
*/
static void searchDB(String DBfile, Vector<String> vec)
{
try
{
dbConfig.setErrorStream(System.err);
dbConfig.setErrorPrefix("MyDbs");
Database db = new Database(DBfile, null, dbConfig);
Cursor cur = db.openCursor(null, null);
DatabaseEntry key = null;
DatabaseEntry data = null;
OperationStatus oprStatus;
for(int size = (vec.size()-1); size >= 0; size--)
{
String temp = vec.get(size);
//wildcard case
if(temp.endsWith("%")){
if (temp.length() == 1)
{
System.out.println("Warning, just '%' entered");
db.close();
return;
}
Set<String> set = new HashSet<String>();
temp = temp.substring(0,(temp.length()-1));
key = new DatabaseEntry();
key.setData(temp.getBytes());
key.setSize(temp.length());
data = new DatabaseEntry();
oprStatus = cur.getFirst(key, data, LockMode.DEFAULT);
while(oprStatus == OperationStatus.SUCCESS)
{
String result = new String(data.getData());
String word = new String(key.getData());;
// Because we are traversing the db with a cursor, will change.
// We check if the next key starts with our keyword search.
if(word.startsWith(temp))
{
Vector<String> dreamTheater = new Vector<String>(Arrays.asList(result.split(",")));
set.addAll(dreamTheater);
}
key = new DatabaseEntry();
data = new DatabaseEntry();
oprStatus = cur.getNext(key, data, LockMode.DEFAULT);
}
if (firstKeyword)
{
resultSet = set;
firstKeyword = false;
}
else
{
resultSet = setIntersection(resultSet, set);
}
}
else
{
Set<String> set = new HashSet<String>();
//cursor search, works for multiple results;
key = new DatabaseEntry();
key.setData(temp.getBytes());
key.setSize(temp.length());
data = new DatabaseEntry();
oprStatus = cur.getSearchKey(key, data, LockMode.DEFAULT);
while(oprStatus == OperationStatus.SUCCESS)
{
String result = new String(data.getData());
Vector<String> dreamTheater = new Vector<String>(Arrays.asList(result.split(",")));
set = new HashSet<String>(dreamTheater);
if (firstKeyword)
{
resultSet = set;
firstKeyword = false;
}
else
{
resultSet = setIntersection(resultSet, set);
}
oprStatus = cur.getNextDup(key, data, LockMode.DEFAULT);
}
}
}
db.close();
}
catch (DatabaseException ex)
{
System.out.println(ex.getMessage());
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
}
/**
* Function:
* Get the intersection of two sets.
*
* Param:
* set1 - first set of strings. This is the bigger one that we are matching against.
* set2 - second set of strings.
*
* Return:
* A set of the elements that are in both sets.
*/
private static Set<String> setIntersection(Set<String> set1, Set<String> set2)
{
Set<String> resultSet = new HashSet<String>();
Iterator<String> iter = set2.iterator();
while(iter.hasNext())
{
String temp = iter.next();
if (set1.contains(temp))
{
resultSet.add(temp);
}
}
return resultSet;
}
}