@Override public boolean exists(String path) { String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath+"%"; boolean rc[] = new boolean[]{false}; withTransaction(dbi -> { rc[0] = countJsonRecords(dbi, like) > 0; }); return rc[0]; }
private static String toArrayIndexPath(int idx) { // todo: encode the idx using something like http://www.zanopha.com/docs/elen.pdf // so we get lexicographic ordering. return toLexSortableString(idx); }
@Override public void set(String path, InputStream body) { withTransaction(dbi -> { BatchManager mb = new BatchManager(dbi); String baseDBPath = JsonRecordSupport.convertToDBPath(path); mb.deleteRecordsForSet(baseDBPath); try { JsonRecordSupport.jsonStreamToRecords(indexPaths, baseDBPath, body, mb.createSetConsumer()); } catch (IOException e) { throw new JsonDBException(e); } mb.flush(); }); if( bus!=null ) { bus.broadcast("jsondb-updated", prefix(trimSuffix(path, "/"), "/")); } }
@Override public boolean delete(String path) { String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath+"%"; boolean rc[] = new boolean[]{false}; withTransaction(dbi -> { rc[0] = deleteJsonRecords(dbi, baseDBPath, like) > 0; }); if( bus!=null && rc[0] ) { bus.broadcast("jsondb-deleted", prefix(trimSuffix(path, "/"), "/")); } return rc[0]; }
static int toArrayIndex(String value) { return fromLexSortableStringToInt(value); }
private String validate(String value) { if( value == null ) { return null; } return validateKey(value); }
public void dropTables() { withTransaction(dbi -> { dbi.update("DROP TABLE jsondb"); }); }
public static JsonRecord of(String path, String value, int kind, String index) { return new JsonRecord(path, value, kind, index); }
@Override public JsonRecord map(int index, ResultSet r, StatementContext ctx) throws SQLException { return JsonRecord.of(r.getString("path"), r.getString("value"), r.getString("ovalue"), null); } }
static Filter child(String field, Op op, Object value) { return new ChildFilter(field, op, value); }
@Override public void set(String path, InputStream body) { withTransaction(dbi -> { BatchManager mb = new BatchManager(dbi); String baseDBPath = JsonRecordSupport.convertToDBPath(path); mb.deleteRecordsForSet(baseDBPath); try { JsonRecordSupport.jsonStreamToRecords(indexes, baseDBPath, body, mb.createSetConsumer()); } catch (IOException e) { throw new JsonDBException(e); } mb.flush(); }); if( bus!=null ) { bus.broadcast("jsondb-updated", prefix(trimSuffix(path, "/"), "/")); } }
@Override public boolean delete(String path) { String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath+"%"; boolean rc[] = new boolean[]{false}; withTransaction(dbi -> { rc[0] = deleteJsonRecords(dbi, baseDBPath, like) > 0; }); if( bus!=null && rc[0] ) { bus.broadcast("jsondb-deleted", prefix(trimSuffix(path, "/"), "/")); } return rc[0]; }
@Override public boolean exists(String path) { String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath+"%"; boolean rc[] = new boolean[]{false}; withTransaction(dbi -> { rc[0] = countJsonRecords(dbi, like) > 0; }); return rc[0]; }
private static String toArrayIndexPath(int idx) { // todo: encode the idx using something like http://www.zanopha.com/docs/elen.pdf // so we get lexicographic ordering. return toLexSortableString(idx, '['); }
protected static int toArrayIndex(String value) { return fromLexSortableStringToInt(value, '['); }
public void dropTables() { withTransaction(dbi -> { dbi.update("DROP TABLE jsondb"); }); }
public static JsonRecord of(String path, String value, String ovalue, String index) { return new JsonRecord(path, value, ovalue, index); }
public void createTables() { withTransaction(dbi -> { if(databaseKind == DatabaseKind.PostgreSQL) { dbi.update("CREATE TABLE jsondb (path VARCHAR COLLATE \"C\" PRIMARY KEY, value VARCHAR, kind INT, idx VARCHAR COLLATE \"C\")"); dbi.update("CREATE INDEX jsondb_idx ON jsondb (idx, value) WHERE idx IS NOT NULL"); } else { dbi.update("CREATE TABLE jsondb (path VARCHAR PRIMARY KEY, value VARCHAR, kind INT, idx VARCHAR)"); } }); }