-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
J7FileStatsStorage.java
667 lines (569 loc) · 25.8 KB
/
J7FileStatsStorage.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package org.deeplearning4j.ui.storage.sqlite;
import lombok.NonNull;
import org.deeplearning4j.api.storage.*;
import org.eclipse.collections.impl.list.mutable.primitive.LongArrayList;
import org.nd4j.linalg.primitives.Pair;
import java.io.*;
import java.sql.*;
import java.util.*;
/**
* A Java 7 compatible file-based {@link StatsStorage} implementation, based on SQLite.
* Note: Where possible, use {@link org.deeplearning4j.ui.storage.FileStatsStorage} which should be faster (is based
* on MapDB).
* Obviously, the storage formats for J7FileStatsStorage and {@link org.deeplearning4j.ui.storage.FileStatsStorage} are
* incompatible.
*
* @author Alex Black
*/
public class J7FileStatsStorage implements StatsStorage {
private static final String TABLE_NAME_METADATA = "StorageMetaData";
private static final String TABLE_NAME_STATIC_INFO = "StaticInfo";
private static final String TABLE_NAME_UPDATES = "Updates";
private static final String INSERT_META_SQL = "INSERT OR REPLACE INTO " + TABLE_NAME_METADATA
+ " (SessionID, TypeID, ObjectClass, ObjectBytes) VALUES ( ?, ?, ?, ? );";
private static final String INSERT_STATIC_SQL = "INSERT OR REPLACE INTO " + TABLE_NAME_STATIC_INFO
+ " (SessionID, TypeID, WorkerID, ObjectClass, ObjectBytes) VALUES ( ?, ?, ?, ?, ? );";
private static final String INSERT_UPDATE_SQL = "INSERT OR REPLACE INTO " + TABLE_NAME_UPDATES
+ " (SessionID, TypeID, WorkerID, Timestamp, ObjectClass, ObjectBytes) VALUES ( ?, ?, ?, ?, ?, ? );";
private final File file;
private final Connection connection;
private List<StatsStorageListener> listeners = new ArrayList<>();
/**
* @param file Storage location for the stats
*/
public J7FileStatsStorage(@NonNull File file) {
this.file = file;
try {
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException("Error ninializing J7FileStatsStorage instance", e);
}
try {
initializeTables();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void initializeTables() throws SQLException {
//Need tables for:
//(a) Metadata -> session ID and type ID; class; StorageMetaData as a binary BLOB
//(b) Static info -> session ID, type ID, worker ID, persistable class, persistable bytes
//(c) Update info -> session ID, type ID, worker ID, timestamp, update class, update bytes
//First: check if tables exist
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = meta.getTables(null, null, "%", null);
boolean hasStorageMetaDataTable = false;
boolean hasStaticInfoTable = false;
boolean hasUpdatesTable = false;
while (rs.next()) {
//3rd value: table name - http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getTables%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String[]%29
String name = rs.getString(3);
if (TABLE_NAME_METADATA.equals(name))
hasStorageMetaDataTable = true;
else if (TABLE_NAME_STATIC_INFO.equals(name))
hasStaticInfoTable = true;
else if (TABLE_NAME_UPDATES.equals(name))
hasUpdatesTable = true;
}
Statement statement = connection.createStatement();
if (!hasStorageMetaDataTable) {
statement.executeUpdate("CREATE TABLE " + TABLE_NAME_METADATA + " (" + "SessionID TEXT NOT NULL, "
+ "TypeID TEXT NOT NULL, " + "ObjectClass TEXT NOT NULL, " + "ObjectBytes BLOB NOT NULL, "
+ "PRIMARY KEY ( SessionID, TypeID )" + ");");
}
if (!hasStaticInfoTable) {
statement.executeUpdate("CREATE TABLE " + TABLE_NAME_STATIC_INFO + " (" + "SessionID TEXT NOT NULL, "
+ "TypeID TEXT NOT NULL, " + "WorkerID TEXT NOT NULL, " + "ObjectClass TEXT NOT NULL, "
+ "ObjectBytes BLOB NOT NULL, " + "PRIMARY KEY ( SessionID, TypeID, WorkerID )" + ");");
}
if (!hasUpdatesTable) {
statement.executeUpdate("CREATE TABLE " + TABLE_NAME_UPDATES + " (" + "SessionID TEXT NOT NULL, "
+ "TypeID TEXT NOT NULL, " + "WorkerID TEXT NOT NULL, " + "Timestamp INTEGER NOT NULL, "
+ "ObjectClass TEXT NOT NULL, " + "ObjectBytes BLOB NOT NULL, "
+ "PRIMARY KEY ( SessionID, TypeID, WorkerID, Timestamp )" + ");");
}
statement.close();
}
private static Pair<String, byte[]> serializeForDB(Object object) {
String classStr = object.getClass().getName();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(object);
oos.close();
byte[] bytes = baos.toByteArray();
return new Pair<>(classStr, bytes);
} catch (IOException e) {
throw new RuntimeException("Error serializing object for storage", e);
}
}
private static <T> T deserialize(byte[] bytes) {
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
return (T) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private <T> T queryAndGet(String sql, int columnIndex) {
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(sql);
if (!rs.next())
return null;
byte[] bytes = rs.getBytes(columnIndex);
return deserialize(bytes);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private List<String> selectDistinct(String columnName, boolean queryMeta, boolean queryStatic, boolean queryUpdates,
String conditionColumn, String conditionValue) {
Set<String> unique = new HashSet<>();
try (Statement statement = connection.createStatement()) {
if (queryMeta) {
queryHelper(statement, querySqlHelper(columnName, TABLE_NAME_METADATA, conditionColumn, conditionValue),
unique);
}
if (queryStatic) {
queryHelper(statement,
querySqlHelper(columnName, TABLE_NAME_STATIC_INFO, conditionColumn, conditionValue),
unique);
}
if (queryUpdates) {
queryHelper(statement, querySqlHelper(columnName, TABLE_NAME_UPDATES, conditionColumn, conditionValue),
unique);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return new ArrayList<>(unique);
}
private String querySqlHelper(String columnName, String table, String conditionColumn, String conditionValue) {
String unique = "SELECT DISTINCT " + columnName + " FROM " + table;
if (conditionColumn != null) {
unique += " WHERE " + conditionColumn + " = '" + conditionValue + "'";
}
unique += ";";
return unique;
}
private void queryHelper(Statement statement, String q, Set<String> unique) throws SQLException {
ResultSet rs = statement.executeQuery(q);
while (rs.next()) {
String str = rs.getString(1);
unique.add(str);
}
}
protected List<StatsStorageEvent> checkStorageEvents(Persistable p) {
if (listeners.size() == 0)
return null;
StatsStorageEvent newSID = null;
StatsStorageEvent newTID = null;
StatsStorageEvent newWID = null;
String sid = p.getSessionID();
String tid = p.getTypeID();
String wid = p.getWorkerID();
//Is this a new session ID? type ID? worker ID?
//This is not the most efficient approach
boolean isNewSID = false;
boolean isNewTID = false;
boolean isNewWID = false;
if (!listSessionIDs().contains(sid)) {
isNewSID = true;
isNewTID = true;
isNewWID = true;
}
if (!isNewTID && !listTypeIDsForSession(sid).contains(tid)) {
isNewTID = true;
}
if (!isNewWID && !listWorkerIDsForSessionAndType(sid, tid).contains(wid)) {
isNewWID = true;
}
if (isNewSID) {
newSID = new StatsStorageEvent(this, StatsStorageListener.EventType.NewSessionID, p.getSessionID(),
p.getTypeID(), p.getWorkerID(), p.getTimeStamp());
}
if (isNewTID) {
newTID = new StatsStorageEvent(this, StatsStorageListener.EventType.NewTypeID, p.getSessionID(),
p.getTypeID(), p.getWorkerID(), p.getTimeStamp());
}
if (isNewWID) {
newWID = new StatsStorageEvent(this, StatsStorageListener.EventType.NewWorkerID, p.getSessionID(),
p.getTypeID(), p.getWorkerID(), p.getTimeStamp());
}
if (!isNewSID && !isNewTID && !isNewWID)
return null;
List<StatsStorageEvent> sses = new ArrayList<>(3);
if (newSID != null)
sses.add(newSID);
if (newTID != null)
sses.add(newTID);
if (newWID != null)
sses.add(newWID);
return sses;
}
@Override
public void putStorageMetaData(StorageMetaData storageMetaData) {
putStorageMetaData(Collections.singletonList(storageMetaData));
}
@Override
public void putStorageMetaData(Collection<? extends StorageMetaData> collection) {
List<StatsStorageEvent> sses = null;
try {
PreparedStatement ps = connection.prepareStatement(INSERT_META_SQL);
for (StorageMetaData storageMetaData : collection) {
List<StatsStorageEvent> ssesTemp = checkStorageEvents(storageMetaData);
if (ssesTemp != null) {
if (sses == null)
sses = ssesTemp;
else
sses.addAll(ssesTemp);
}
if (listeners.size() > 0) {
StatsStorageEvent sse = new StatsStorageEvent(this, StatsStorageListener.EventType.PostMetaData,
storageMetaData.getSessionID(), storageMetaData.getTypeID(),
storageMetaData.getWorkerID(), storageMetaData.getTimeStamp());
if (sses == null)
sses = new ArrayList<>();
sses.add(sse);
}
//Normally we'd batch these... sqlite has an autocommit feature that messes up batching with .addBatch() and .executeUpdate()
Pair<String, byte[]> p = serializeForDB(storageMetaData);
ps.setString(1, storageMetaData.getSessionID());
ps.setString(2, storageMetaData.getTypeID());
ps.setString(3, p.getFirst());
ps.setObject(4, p.getSecond());
ps.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
notifyListeners(sses);
}
@Override
public void putStaticInfo(Persistable staticInfo) {
putStaticInfo(Collections.singletonList(staticInfo));
}
@Override
public void putStaticInfo(Collection<? extends Persistable> collection) {
List<StatsStorageEvent> sses = null;
try {
PreparedStatement ps = connection.prepareStatement(INSERT_STATIC_SQL);
for (Persistable p : collection) {
List<StatsStorageEvent> ssesTemp = checkStorageEvents(p);
if (ssesTemp != null) {
if (sses == null)
sses = ssesTemp;
else
sses.addAll(ssesTemp);
}
if (listeners.size() > 0) {
StatsStorageEvent sse = new StatsStorageEvent(this, StatsStorageListener.EventType.PostStaticInfo,
p.getSessionID(), p.getTypeID(), p.getWorkerID(), p.getTimeStamp());
if (sses == null)
sses = new ArrayList<>();
sses.add(sse);
}
//Normally we'd batch these... sqlite has an autocommit feature that messes up batching with .addBatch() and .executeUpdate()
Pair<String, byte[]> pair = serializeForDB(p);
ps.setString(1, p.getSessionID());
ps.setString(2, p.getTypeID());
ps.setString(3, p.getWorkerID());
ps.setString(4, pair.getFirst());
ps.setBytes(5, pair.getSecond());
ps.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
notifyListeners(sses);
}
@Override
public void putUpdate(Persistable update) {
putUpdate(Collections.singletonList(update));
}
@Override
public void putUpdate(Collection<? extends Persistable> collection) {
List<StatsStorageEvent> sses = null;
try {
PreparedStatement ps = connection.prepareStatement(INSERT_UPDATE_SQL);
for (Persistable p : collection) {
List<StatsStorageEvent> ssesTemp = checkStorageEvents(p);
if (ssesTemp != null) {
if (sses == null)
sses = ssesTemp;
else
sses.addAll(ssesTemp);
}
if (listeners.size() > 0) {
StatsStorageEvent sse = new StatsStorageEvent(this, StatsStorageListener.EventType.PostUpdate,
p.getSessionID(), p.getTypeID(), p.getWorkerID(), p.getTimeStamp());
if (sses == null)
sses = new ArrayList<>();
sses.add(sse);
}
//Normally we'd batch these... sqlite has an autocommit feature that messes up batching with .addBatch() and .executeUpdate()
Pair<String, byte[]> pair = serializeForDB(p);
ps.setString(1, p.getSessionID());
ps.setString(2, p.getTypeID());
ps.setString(3, p.getWorkerID());
ps.setLong(4, p.getTimeStamp());
ps.setString(5, pair.getFirst());
ps.setObject(6, pair.getSecond());
ps.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
notifyListeners(sses);
}
@Override
public void close() throws IOException {
try {
connection.close();
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public boolean isClosed() {
try {
return connection.isClosed();
} catch (Exception e) {
return true;
}
}
@Override
public List<String> listSessionIDs() {
return selectDistinct("SessionID", true, true, false, null, null);
}
@Override
public boolean sessionExists(String sessionID) {
String existsMetaSQL = "SELECT 1 FROM " + TABLE_NAME_METADATA + " WHERE SessionID = '" + sessionID + "';";
String existsStaticSQL = "SELECT 1 FROM " + TABLE_NAME_STATIC_INFO + " WHERE SessionID = '" + sessionID + "';";
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(existsMetaSQL);
if (rs.next()) {
return true;
}
rs = statement.executeQuery(existsStaticSQL);
return rs.next();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Persistable getStaticInfo(String sessionID, String typeID, String workerID) {
String selectStaticSQL = "SELECT ObjectBytes FROM " + TABLE_NAME_STATIC_INFO + " WHERE SessionID = '"
+ sessionID + "' AND TypeID = '" + typeID + "' AND WorkerID = '" + workerID + "';";
return queryAndGet(selectStaticSQL, 1);
}
@Override
public List<Persistable> getAllStaticInfos(String sessionID, String typeID) {
String selectStaticSQL = "SELECT * FROM " + TABLE_NAME_STATIC_INFO + " WHERE SessionID = '" + sessionID
+ "' AND TypeID = '" + typeID + "';";
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(selectStaticSQL);
List<Persistable> out = new ArrayList<>();
while (rs.next()) {
byte[] bytes = rs.getBytes(5);
out.add((Persistable) deserialize(bytes));
}
return out;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<String> listTypeIDsForSession(String sessionID) {
return selectDistinct("TypeID", true, true, true, "SessionID", sessionID);
}
@Override
public List<String> listWorkerIDsForSession(String sessionID) {
return selectDistinct("WorkerID", false, true, true, "SessionID", sessionID);
}
@Override
public List<String> listWorkerIDsForSessionAndType(String sessionID, String typeID) {
String uniqueStatic = "SELECT DISTINCT WorkerID FROM " + TABLE_NAME_STATIC_INFO + " WHERE SessionID = '"
+ sessionID + "' AND TypeID = '" + typeID + "';";
String uniqueUpdates = "SELECT DISTINCT WorkerID FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '"
+ sessionID + "' AND TypeID = '" + typeID + "';";
Set<String> unique = new HashSet<>();
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(uniqueStatic);
while (rs.next()) {
String str = rs.getString(1);
unique.add(str);
}
rs = statement.executeQuery(uniqueUpdates);
while (rs.next()) {
String str = rs.getString(1);
unique.add(str);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return new ArrayList<>(unique);
}
@Override
public int getNumUpdateRecordsFor(String sessionID) {
String sql = "SELECT COUNT(*) FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '" + sessionID + "';";
try (Statement statement = connection.createStatement()) {
return statement.executeQuery(sql).getInt(1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public int getNumUpdateRecordsFor(String sessionID, String typeID, String workerID) {
String sql = "SELECT COUNT(*) FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '" + sessionID
+ "' AND TypeID = '" + typeID + "' AND WorkerID = '" + workerID + "';";
try (Statement statement = connection.createStatement()) {
return statement.executeQuery(sql).getInt(1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Persistable getLatestUpdate(String sessionID, String typeID, String workerID) {
String sql = "SELECT ObjectBytes FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '" + sessionID
+ "' AND TypeID = '" + typeID + "' AND WorkerID = '" + workerID
+ "' ORDER BY Timestamp DESC LIMIT 1;";
return queryAndGet(sql, 1);
}
@Override
public Persistable getUpdate(String sessionID, String typeId, String workerID, long timestamp) {
String sql = "SELECT ObjectBytes FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '" + sessionID
+ "' AND TypeID = '" + typeId + "' AND WorkerID = '" + workerID + "' AND Timestamp = '"
+ timestamp + "';";
return queryAndGet(sql, 1);
}
@Override
public List<Persistable> getLatestUpdateAllWorkers(String sessionID, String typeID) {
String sql = "SELECT workerId, MAX(Timestamp) FROM " + TABLE_NAME_UPDATES + " WHERE SessionID ='"
+ sessionID + "' AND " + "TypeID = '" + typeID + "' GROUP BY workerId";
Map<String,Long> m = new HashMap<>();
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
String wid = rs.getString(1);
long ts = rs.getLong(2);
m.put(wid, ts);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
List<Persistable> out = new ArrayList<>();
for(String s : m.keySet()){
out.add(getUpdate(sessionID, typeID, s, m.get(s)));
}
return out;
}
@Override
public List<Persistable> getAllUpdatesAfter(String sessionID, String typeID, String workerID, long timestamp) {
String sql = "SELECT * FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '" + sessionID + "' AND TypeID = '"
+ typeID + "' " + "AND Timestamp > " + timestamp + ";";
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(sql);
List<Persistable> out = new ArrayList<>();
while (rs.next()) {
byte[] bytes = rs.getBytes(6);
out.add((Persistable) deserialize(bytes));
}
return out;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<Persistable> getAllUpdatesAfter(String sessionID, String typeID, long timestamp) {
String sql = "SELECT ObjectBytes FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '" + sessionID + "' "
+ "AND Timestamp > " + timestamp + ";";
return queryUpdates(sql);
}
@Override
public long[] getAllUpdateTimes(String sessionID, String typeID, String workerID) {
/*
statement.executeUpdate("CREATE TABLE " + TABLE_NAME_UPDATES + " (" + "SessionID TEXT NOT NULL, "
+ "TypeID TEXT NOT NULL, " + "WorkerID TEXT NOT NULL, " + "Timestamp INTEGER NOT NULL, "
+ "ObjectClass TEXT NOT NULL, " + "ObjectBytes BLOB NOT NULL, "
+ "PRIMARY KEY ( SessionID, TypeID, WorkerID, Timestamp )" + ");");
*/
String sql = "SELECT Timestamp FROM " + TABLE_NAME_UPDATES + " WHERE SessionID = '" + sessionID + "' "
+ "AND TypeID = '" + typeID + "' AND workerID = '" + workerID + "';";
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(sql);
LongArrayList list = new LongArrayList();
while (rs.next()) {
list.add(rs.getLong(1));
}
return list.toArray();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<Persistable> getUpdates(String sessionID, String typeID, String workerID, long[] timestamps) {
if(timestamps == null || timestamps.length == 0){
return Collections.emptyList();
}
StringBuilder sb = new StringBuilder();
sb.append("SELECT ObjectBytes FROM ").append(TABLE_NAME_UPDATES).append(" WHERE SessionID = '").append(sessionID)
.append("' AND TypeID = '").append(typeID).append("' AND workerID='").append(workerID)
.append("' AND Timestamp IN (");
for( int i=0; i<timestamps.length; i++ ){
if(i > 0){
sb.append(",");
}
sb.append(timestamps[i]);
}
sb.append(");");
String sql = sb.toString();
return queryUpdates(sql);
}
private List<Persistable> queryUpdates(String sql){
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(sql);
List<Persistable> out = new ArrayList<>();
while (rs.next()) {
byte[] bytes = rs.getBytes(1);
out.add((Persistable) deserialize(bytes));
}
return out;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public StorageMetaData getStorageMetaData(String sessionID, String typeID) {
String sql = "SELECT ObjectBytes FROM " + TABLE_NAME_METADATA + " WHERE SessionID = '" + sessionID
+ "' AND TypeID = '" + typeID + "' LIMIT 1;";
return queryAndGet(sql, 1);
}
@Override
public void registerStatsStorageListener(StatsStorageListener listener) {
listeners.add(listener);
}
@Override
public void deregisterStatsStorageListener(StatsStorageListener listener) {
listeners.remove(listener);
}
@Override
public void removeAllListeners() {
listeners.clear();
}
@Override
public List<StatsStorageListener> getListeners() {
return new ArrayList<>(listeners);
}
@Override
public String toString() {
return "J7FileStatsStorage(file=" + file + ")";
}
protected void notifyListeners(List<StatsStorageEvent> sses) {
if (sses == null || sses.size() == 0 || listeners.size() == 0)
return;
for (StatsStorageListener l : listeners) {
for (StatsStorageEvent e : sses) {
l.notify(e);
}
}
}
}