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
/
DataBase.java
288 lines (245 loc) · 8.53 KB
/
DataBase.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
import com.sleepycat.db.*;
import java.io.*;
import java.util.*;
/*
* requires refactoring
* much cleaning required
*/
public class DataBase{
public static final int NO_RECORDS = 100000;
public static final int NO_RECORDS_TEST = 11;
public static final String DATABASE_DIR = "/tmp/slmyers_db";
public static final String PRIMARY_TABLE = "/tmp/slmyers_db/primary_table_file1";
private static final String DATA_SECONDARY_TABLE = "/tmp/slmyers_db/data_index";
public static final String TREE_TABLE = "/tmp/slmyers_db/search_file";
public static final String HASH_TABLE = "/tmp/slmyers_db/data_support_file";
public static boolean INITIALIZED = false;
private static DataBase db = null;
private Database database = null;
private Database databaseHash = null;
private Database databaseTree = null;
private SecondaryDatabase dataSecondary = null;
protected DataBase(){
}
public static DataBase getInstance(){
if(db == null){
db = new DataBase();
}
return db;
}
public void initDataBase(){
if(!createDirectory(DATABASE_DIR)){
System.err.println("Unable to create file for database");
System.exit(-1);
}
DatabaseType type;
int typeInt = Pref.getDbType();
if(typeInt == 1 ){
type = DatabaseType.BTREE;
}else if (typeInt == 2){
type = DatabaseType.HASH;
}else if (typeInt == 3){
initIndexFile(DatabaseType.BTREE);
configureDataSecondary();
INITIALIZED = true;
return;
}else{
throw new RuntimeException("unknown database attempting to be created in Database");
}
this.database = createDb(PRIMARY_TABLE, type);
System.out.println(PRIMARY_TABLE + " has been created of type: " + type);
int count = populateTable(this.database, NO_RECORDS);
System.out.println(PRIMARY_TABLE + " has been entered with " + count + " records.");
INITIALIZED = true;
}
public Database getPrimaryDb(){
return this.database;
}
public Database getIndexTree(){
return databaseTree;
}
public Database getIndexHash(){
return databaseHash;
}
public SecondaryDatabase getIndexSecondary(){
return this.dataSecondary;
}
public void initIndexFile(DatabaseType type){
if(type.equals(DatabaseType.BTREE)){
this.databaseTree = createDb(TREE_TABLE, type);
System.out.println(TREE_TABLE + " has been created of type: " + type);
int count = populateTable(this.databaseTree, NO_RECORDS);
System.out.println(TREE_TABLE + " has been entered with " + count + " records.");
}else if(type.equals(DatabaseType.HASH)){
this.databaseHash = createDb(HASH_TABLE, type);
System.out.println(HASH_TABLE + " has been created of type: " + type);
int count = populateTable(this.databaseHash, NO_RECORDS);
System.out.println(HASH_TABLE + " has been entered with " + count + " records.");
}
}
public void configureDataSecondary(){
SecondaryConfig secConfig = new SecondaryConfig();
secConfig.setKeyCreator(new DataKeyCreator());
secConfig.setAllowCreate(true);
secConfig.setType(DatabaseType.BTREE);
secConfig.setSortedDuplicates(true);
secConfig.setAllowPopulate(true);
try{
this.dataSecondary = new SecondaryDatabase(DATA_SECONDARY_TABLE, null, this.databaseTree, secConfig);
}catch(DatabaseException dbe){
System.err.println("Error while instantiating secondary 'data' database: " + dbe.toString());
this.close(true);
System.exit(-1);
}catch(FileNotFoundException fnfe){
System.err.println("Secondary database file not found: " + fnfe.toString());
}
System.out.println(DATA_SECONDARY_TABLE + " has been created of type: " + secConfig.getType());
}
public final boolean createDirectory(String file){
deleteFolder(new File(file));
File dbDirect = new File(file);
dbDirect.mkdirs();
return dbDirect.exists();
}
public Database createDb(String file, DatabaseType type){
Database db = null;
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setType(type);
dbConfig.setAllowCreate(true);
try{
db = new Database(file, null, dbConfig);
}catch (DatabaseException dbe){
System.err.println("unable to create database");
dbe.printStackTrace();
}catch (FileNotFoundException fnfe){
System.err.println("can not find file to create Database");
fnfe.printStackTrace();
}
return db;
}
private static int populateTable(Database my_table, int nrecs ) {
int range;
DatabaseEntry kdbt, ddbt;
int count = 0;
String s;
/*
* generate a random string with the length between 64 and 127,
* inclusive.
*
* Seed the random number once and once only.
*/
Random random = new Random(1000000);
try {
for (int i = 0; i < nrecs; i++) {
/* to generate a key string */
range = 64 + random.nextInt( 64 );
s = "";
for ( int j = 0; j < range; j++ )
s+=(new Character((char)(97+random.nextInt(26)))).toString();
/* to create a DBT for key */
kdbt = new DatabaseEntry(s.getBytes());
kdbt.setSize(s.length());
/* to generate a data string */
range = 64 + random.nextInt( 64 );
s = "";
for ( int j = 0; j < range; j++ )
s+=(new Character((char)(97+random.nextInt(26)))).toString();
/* to create a DBT for data */
ddbt = new DatabaseEntry(s.getBytes());
ddbt.setSize(s.length());
//TODO change so program recovers instead of exiting
OperationStatus result;
result = my_table.exists(null, kdbt);
if (!result.toString().equals("OperationStatus.NOTFOUND"))
throw new RuntimeException("Key is already in the database!");
/* to insert the key/data pair into the database */
my_table.putNoOverwrite(null, kdbt, ddbt);
count++;
}
}
catch (DatabaseException dbe) {
System.err.println("Populate the table: "+dbe.toString());
System.exit(1);
}
return count;
}
public void close(boolean clearFiles){
if(this.database != null){
try{
this.database.close();
this.database.remove(PRIMARY_TABLE,null,null);
System.out.println(PRIMARY_TABLE + " database is closed and removed");
}catch(DatabaseException dbe){
System.err.println("unable to close database");
dbe.printStackTrace();
}catch (FileNotFoundException fnfe){
System.err.println("can not find file to remove Database");
fnfe.printStackTrace();
}
database = null;
db = null;
}
if(this.databaseTree != null){
try{
this.dataSecondary.close();
System.out.println(DATA_SECONDARY_TABLE + " database is closed");
this.databaseTree.close();
this.database.remove(TREE_TABLE,null,null);
System.out.println(TREE_TABLE + " database is closed and removed");
}catch(DatabaseException dbe){
System.err.println("unable to close database(s)");
dbe.printStackTrace();
}catch (FileNotFoundException fnfe){
System.err.println("can not find file to remove Database(s)");
fnfe.printStackTrace();
}
this.dataSecondary = null;
this.databaseHash = null;
this.databaseTree = null;
}
INITIALIZED = false;
if(clearFiles){
deleteFolder(new File("/tmp/slmyers_db"));
WriteToFile.deleteAnswerFile();
System.out.println("/tmp/slmyers_db and ./answers deleted");
}
}
public static void deleteFolder(File folder) {
File[] files = folder.listFiles();
if(files!=null) { //some JVMs return null for empty dirs
for(File f: files) {
if(f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
}
}
folder.delete();
}
public void printSecondary() throws DatabaseException{
DatabaseEntry secKey = new DatabaseEntry();
DatabaseEntry pKey = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
SecondaryCursor cursor = this.dataSecondary.openSecondaryCursor(null, null);
String secondaryKey;
String primaryKey;
String dataString;
OperationStatus status;
secKey.setReuseBuffer(false);
pKey.setReuseBuffer(false);
data.setReuseBuffer(false);
while( (status = cursor.getNextNoDup(secKey, pKey, data, LockMode.DEFAULT)) == OperationStatus.SUCCESS){
System.out.println("secondary key: " + new String(secKey.getData()) + "\n");
System.out.println("primary key: " + new String(pKey.getData()) + "\n");
System.out.println("data: " + new String(data.getData()) + "\n");
while((status = cursor.getNextDup(secKey, pKey, data, LockMode.DEFAULT)) == OperationStatus.SUCCESS){
System.out.println("\tsecondary key: " + new String(secKey.getData()) + "\n");
System.out.println("\tprimary key: " + new String(pKey.getData()) + "\n");
System.out.println("\tdata: " + new String(data.getData()) + "\n");
}
System.out.println("=======================================");
}
cursor.close();
}
}