9
9
10
10
public class DataBase {
11
11
private static final int NO_RECORDS = 100000 ;
12
+ private static final int NO_RECORDS_TEST = 10 ;
12
13
public static final String DATABASE_DIR = "./tmp/user_db" ;
13
14
public static final String PRIMARY_TABLE = "./tmp/user_db/primary_table_file1" ;
14
15
@@ -33,7 +34,7 @@ protected DataBase(){
33
34
}
34
35
35
36
}
36
-
37
+ //TODO ensure that DataBase.getInstance() does not open create database when unwanted
37
38
public static DataBase getInstance (){
38
39
if (db == null ){
39
40
db = new DataBase ();
@@ -84,15 +85,46 @@ private final boolean createBase(){
84
85
return false ;
85
86
}
86
87
System .out .println (PRIMARY_TABLE + " has been created of type: " + dbConfig .getType ());
87
- populateTable (this .database , NO_RECORDS );
88
-
88
+ //TODO change to populateTable after testing
89
+ int count = populateTable (this .database , NO_RECORDS );
90
+ //int count = populateTestTable(this.database, NO_RECORDS_TEST);
91
+ System .out .println (PRIMARY_TABLE + " has been inserted with: " + count + " records" );
89
92
return true ;
90
93
}
94
+
95
+ static int populateTestTable (Database my_table , int nrecs ){
96
+ DatabaseEntry kdbt , ddbt ;
97
+ int count = 0 ;
98
+ ddbt = new DatabaseEntry (new String ("test" ).getBytes ());
99
+ ddbt .setSize (4 );
100
+
101
+
102
+ try {
103
+ for (int i = 0 ; i < nrecs ; i ++){
104
+ kdbt = new DatabaseEntry (new Character ((char )(i + 97 )).toString ().getBytes ());
105
+ kdbt .setSize (1 );
106
+ OperationStatus result ;
107
+ result = my_table .exists (null , kdbt );
108
+ if (!result .toString ().equals ("OperationStatus.NOTFOUND" ))
109
+ throw new RuntimeException ("Key is already in the database!" );
91
110
92
- static void populateTable (Database my_table , int nrecs ) {
111
+ /* to insert the key/data pair into the database */
112
+ my_table .putNoOverwrite (null , kdbt , ddbt );
113
+ count ++;
114
+ }
115
+ }catch (DatabaseException dbe ) {
116
+ System .err .println ("Populate the table: " +dbe .toString ());
117
+ System .exit (1 );
118
+ }
119
+ return count ;
120
+ }
121
+
122
+ static int populateTable (Database my_table , int nrecs ) {
93
123
int range ;
94
124
DatabaseEntry kdbt , ddbt ;
95
- String s , key , data ;
125
+
126
+ String s ;
127
+
96
128
/*
97
129
* generate a random string with the length between 64 and 127,
98
130
* inclusive.
@@ -102,8 +134,8 @@ static void populateTable(Database my_table, int nrecs ) {
102
134
Random random = new Random (1000000 );
103
135
104
136
try {
105
- for ( int i = 0 ; i < nrecs ; i ++) {
106
-
137
+
138
+ for ( int i = 0 ; i < nrecs ; i ++) {
107
139
/* to generate a key string */
108
140
range = 64 + random .nextInt ( 64 );
109
141
s = "" ;
@@ -114,7 +146,7 @@ static void populateTable(Database my_table, int nrecs ) {
114
146
kdbt = new DatabaseEntry (s .getBytes ());
115
147
kdbt .setSize (s .length ());
116
148
117
-
149
+
118
150
119
151
/* to generate a data string */
120
152
range = 64 + random .nextInt ( 64 );
@@ -123,11 +155,7 @@ static void populateTable(Database my_table, int nrecs ) {
123
155
s +=(new Character ((char )(97 +random .nextInt (26 )))).toString ();
124
156
data = s ;
125
157
126
- if (i <= 10 ){
127
- System .out .println (key );
128
- System .out .println (data );
129
- System .out .println ();
130
- }
158
+
131
159
/* to create a DBT for data */
132
160
ddbt = new DatabaseEntry (s .getBytes ());
133
161
ddbt .setSize (s .length ());
@@ -138,14 +166,16 @@ static void populateTable(Database my_table, int nrecs ) {
138
166
if (!result .toString ().equals ("OperationStatus.NOTFOUND" ))
139
167
throw new RuntimeException ("Key is already in the database!" );
140
168
141
- /* to insert the key/data pair into the database */
142
- my_table .putNoOverwrite (null , kdbt , ddbt );
169
+ /* to insert the key/data pair into the database */
170
+ my_table .putNoOverwrite (null , kdbt , ddbt );
171
+ count ++;
143
172
}
144
173
}
145
174
catch (DatabaseException dbe ) {
146
175
System .err .println ("Populate the table: " +dbe .toString ());
147
- System .exit (1 );
176
+ System .exit (1 );
148
177
}
178
+ return count ;
149
179
}
150
180
151
181
public void close (){
@@ -162,6 +192,7 @@ public void close(){
162
192
fnfe .printStackTrace ();
163
193
}
164
194
database = null ;
195
+ db = null ;
165
196
}
166
197
167
198
}
0 commit comments