-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBInterface.java
More file actions
409 lines (344 loc) · 8.96 KB
/
Copy pathDBInterface.java
File metadata and controls
409 lines (344 loc) · 8.96 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import com.sleepycat.je.Database;
class Pair {
public String s0;
public String s1;
public Pair(String a0, String a1)
{
s0 = a0; s1 = a1;
}
}
class Cache<T> {
ArrayList<String> tables;
ArrayList<T> caches;
public Cache()
{
tables = new ArrayList<String>();
caches = new ArrayList<T>();
}
public T get(String tableName)
{
return null;
/*
int idx = tables.indexOf(tableName);
if (idx != -1)
return caches.get(idx);
return null;*/
}
public boolean set(String tableName, T data)
{
int idx = tables.indexOf(tableName);
if (idx != -1)
{
caches.set(idx, data);
return true;
}
tables.add(tableName);
caches.add(data);
return false;
}
}
public class DBInterface
{
public DBDevice device;
// cache data
Cache<ArrayList<String> > cacheTables;
Cache<ArrayList<String> > cacheColNames;
Cache<ArrayList<ColumnInfo> > cacheColumns;
Cache<ArrayList<String> > cachePrimarys;
Cache<ArrayList<ForeignKeyInfo> > cacheForeigns;
Cache<ArrayList<String> > cacheForeignSlaves;
public DBInterface(Database db)
{
device = new DBDevice(db);
}
public void openCursor()
{
device.openCursor();
// make cache
cacheTables = new Cache<>();
cacheColNames = new Cache<>();
cacheColumns = new Cache<>();
cachePrimarys = new Cache<>();
cacheForeigns = new Cache<>();
cacheForeignSlaves = new Cache<>();
}
public void closeCursor()
{
device.closeCursor();
}
public void openDupCursor()
{
device.openDupCursor();
}
public void closeDupCursor()
{
device.closeDupCursor();
}
public void skip()
{
device.skip();
}
@FunctionalInterface
interface DataLambda<T> {
T f(int idx, String name) throws MyError;
}
@FunctionalInterface
interface TransformLambda<T> {
T f(int idx, String name, T input) throws MyError;
}
@FunctionalInterface
interface RowLambda {
boolean f(RowInfo row) throws MyError;
}
// key name convention & value separation functions
static String make(String name0, String name1)
{
return name0 + "/" + name1;
}
static String make(String name0, String name1, String name2)
{
return name0 + "/" + name1 + "/" + name2;
}
static String make(ArrayList< String > al)
{
if (al.size() < 1) return "";
return String.join("/", al);
}
static List<String> split(String s)
{
return Arrays.asList(s.split("/"));
}
static String makeIdx(String made, int idx)
{
return made + "/" + Integer.toString(idx);
}
static String makeSubKey(String made)
{
return made + "/";
}
static String makeTables()
{
return make("", "tables");
}
static String makeColumns(String tableName)
{
return make(tableName, "column");
}
static String makeColumnType(String tableName, String idxs)
{
return make(tableName, "type", idxs);
}
static String makeColumnNotNull(String tableName, String idxs)
{
return make(tableName, "notnull", idxs);
}
static String makePrimary(String tableName)
{
return make(tableName, "primary");
}
static String makeSlave(String tableName)
{
return make(tableName, "slave");
}
static String makeMaster(String tableName)
{
return make(tableName, "master");
}
static String makeRow (String tableName)
{
return make(tableName, "row");
}
// parse (idx, data) pair and return idx-sorted collection
<T> ArrayList<T> getSorted(String key, DataLambda<T> l) throws MyError
{
ArrayList<T> result = new ArrayList<>();
device.get(key, (ks, data) -> {
List<String> pair = split(data);
int idx = Integer.parseInt(pair.get(0));
String name = pair.get(1);
while (result.size() <= idx) result.add(null);
result.set(idx, l.f(idx, name));
return false;
});
return result;
}
// get additionals
<T> ArrayList<T> getAdditional(ArrayList<T> list, String key, TransformLambda<T> l) throws MyError
{
device.getSub(key, (ks, data) -> {
int idx = Integer.parseInt(ks.substring(key.length()));
list.set(idx, l.f(idx, data, list.get(idx)));
return false;
});
return list;
}
public ArrayList<String> getTables() throws MyError
{
ArrayList<String> cache = cacheTables.get("all");
if (cache == null)
{
cache = device.get(makeTables(), null);
cacheTables.set("all", cache);
}
return cache;
}
public void putTable(String tableName)
{
device.put(makeTables(), tableName);
}
public ArrayList<String> getColumnNames(String tableName) throws MyError
{
ArrayList<String> data = cacheColNames.get(tableName);
if (data == null)
{
data = getSorted(makeColumns(tableName), (idx, name) -> (name));
cacheColNames.set(tableName, data);
}
return data;
}
public ArrayList<ColumnInfo> getColumns(String tableName) throws MyError
{
ArrayList<ColumnInfo> data= cacheColumns.get(tableName);
if (data == null)
{
data = getSorted(makeColumns(tableName), (idx, name) -> (new ColumnInfo(name, 0)) );
getAdditional(data, makeColumnType(tableName, ""), (idx, ds, input) -> {
input.type = Integer.parseInt(ds);
return input;
});
getAdditional(data, makeColumnNotNull(tableName, ""), (idx, ds, input) -> {
input.notnull = ds.equals("Y");
return input;
});
cacheColumns.set(tableName, data);
}
return data;
}
public void putColumns(String tableName, ArrayList<ColumnInfo> lc)
{
for (int i=0; i<lc.size(); i++)
{
ColumnInfo col = lc.get(i);
String idxs = Integer.toString(i);
device.put(makeColumns(tableName), make(idxs, col.name));
device.put(makeColumnType(tableName, idxs), Integer.toString(col.type));
device.put(makeColumnNotNull(tableName, idxs), col.notnull ? "Y" : "N");
}
}
public ArrayList<String> getPrimary(String tableName) throws MyError
{
ArrayList<String> primary = cachePrimarys.get(tableName);
if (primary == null)
{
primary = new ArrayList<String>();
primary = device.get(makePrimary(tableName), null);
cachePrimarys.set(tableName, primary);
}
return primary;
}
public void putPrimary(String tableName, ArrayList<String> primary)
{
for (String item : primary) {
device.put(makePrimary(tableName), item);
}
}
public ArrayList<String> getForeignSlaves(String tableName) throws MyError
{
ArrayList<String> slaves = cacheForeignSlaves.get(tableName);
if (slaves == null)
{
slaves = device.get(makeSlave(tableName), null);
cacheForeignSlaves.set(tableName, slaves);
}
return slaves;
}
public ArrayList<ForeignKeyInfo> getForeignInfo(String tableName) throws MyError
{
ArrayList<ForeignKeyInfo> cache = cacheForeigns.get(tableName);
if (cache == null) {
ArrayList<ForeignKeyInfo> foreigns =
getSorted(makeMaster(tableName), (idx, ds) -> new ForeignKeyInfo(ds));
getAdditional(foreigns, makeSubKey(makeMaster(tableName)), (idx, ds, input) -> {
List<String> pair = split(ds);
input.cols1.add(pair.get(0));
input.cols2.add(pair.get(1));
return input;
});
cache = foreigns;
cacheForeigns.set(tableName, cache);
}
return cache;
}
public void putForeignInfo(String tableName, ArrayList<ForeignKeyInfo> foreign)
{
ArrayList<String> mNames = new ArrayList<String>();
for (int i=0; i<foreign.size(); i++)
{
String idxs;
ForeignKeyInfo item = foreign.get(i);
// check mastername is already exist in foreign names
int mIdx = mNames.indexOf(item.tableName);
if (mIdx != -1)
{
idxs = Integer.toString(mIdx);
} else {
idxs = Integer.toString(mNames.size());
device.put(makeMaster(tableName), make(idxs, item.tableName));
mNames.add(item.tableName);
}
for (int j=0; j<item.cols1.size(); j++)
{
device.put(make(makeMaster(tableName), idxs),
make(item.cols1.get(j), item.cols2.get(j)));
}
}
// make slave info
for (String mName : mNames)
{
device.put(makeSlave(mName), tableName);
}
}
public void deleteTable(String tableName) throws MyError
{
// delete slave info
ArrayList<String> possible_masters = getTables();
for (String possible_master : possible_masters)
{
device.delete(makeSlave(possible_master), (key, data) -> {
return data.equals(tableName);
});
}
device.deleteSub(makeSubKey(tableName), null);
device.delete(makeTables(), (ks, ds) -> (ds.equals(tableName)) );
}
public static RowInfo parseRow (String data)
{
return new RowInfo(DBProcessor.map(split(data), (row) -> (new CellInfo(row, 0))));
}
public static String composeRow (RowInfo row)
{
return make(DBProcessor.map(row.cells, (cell) -> (cell.encode()) ) );
}
public ArrayList<RowInfo> selectRows(String tableName, RowLambda sel) throws MyError
{
return DBProcessor.map(
device.get(
makeRow(tableName), (post, data) -> (sel.f(parseRow(data)))
), (data) -> (parseRow(data))
);
}
public void insertRow(String tableName, RowInfo row)
{
device.put(makeRow(tableName), composeRow(row));
}
public void deleteRows(String tableName, RowLambda del) throws MyError
{
device.delete(makeRow(tableName), (key, data) -> {
return del.f(parseRow(data));
});
}
}