public static String convertToDBPath(String base) { String value = Arrays.stream(base.split("/")).filter(x -> !x.isEmpty()).map(x -> INTEGER_PATTERN.matcher(validateKey(x)).matches() ? toArrayIndexPath(Integer.parseInt(x)) : x ).collect(Collectors.joining("/")); return Strings.suffix(Strings.prefix(value, "/"), "/"); }
public static String convertToDBPath(String base) { String value = Arrays.stream(base.split("/")).filter(x -> !x.isEmpty()).map(x -> INTEGER_PATTERN.matcher(validateKey(x)).matches() ? toArrayIndexPath(Integer.parseInt(x)) : x ).collect(Collectors.joining("/")); return Strings.suffix(Strings.prefix(value, "/"), "/"); }
@Override public String push(String path, InputStream body) { String key = createKey(); set(suffix(prefix(path, "/"), "/") + key + "/", body); return key; }
@Override public String push(String path, InputStream body) { String key = createKey(); set(suffix(prefix(path, "/"), "/") + key + "/", body); return key; }
@Override public Set<String> fetchIdsByPropertyValue(final String collectionPath, final String property, final String value) { String path = prefix(trimSuffix(collectionPath, "/"), "/"); String idx = path+"/#"+property; if( !indexPaths.contains(idx) ) { String message = "Index not defined for: collectionPath: " + path + ", property: " + property; LOG.warn("fetchIdsByPropertyValue not optimzed !!!: {}", message); return fetchIdsByPropertyValueFullTableScan(collectionPath, property, value); } else { final AtomicReference<Set<String>> ret = new AtomicReference<>(); withTransaction(dbi -> { final String query = "SELECT path FROM jsondb WHERE idx = ? AND value = ?"; final List<String> paths = dbi.createQuery(query) .bind(0, idx) .bind(1, STRING_VALUE_PREFIX+value) .map(StringColumnMapper.INSTANCE).list(); String suffix = "/" + property + "/"; ret.set(paths.stream() .map(x -> trimSuffix(x, suffix)) .collect(Collectors.toCollection(HashSet::new))); }); return ret.get(); } }
@Override public Set<String> fetchIdsByPropertyValue(final String collectionPath, final String property, final String value) { String path = prefix(trimSuffix(collectionPath, "/"), "/"); String idx = path+"/"+property; if( !indexes.contains(idx) ) { String message = "Index not defined for: collectionPath: " + path + ", property: " + property; LOG.warn("fetchIdsByPropertyValue not optimzed !!!: {}", message); return fetchIdsByPropertyValueFullTableScan(collectionPath, property, value); } else { final AtomicReference<Set<String>> ret = new AtomicReference<>(); withTransaction(dbi -> { final String query = "SELECT path FROM jsondb WHERE idx = ? AND value = ?"; final List<String> paths = dbi.createQuery(query) .bind(0, idx) .bind(1, value) .map(StringColumnMapper.INSTANCE).list(); String suffix = "/" + property + "/"; ret.set(paths.stream() .map(x -> trimSuffix(x, suffix)) .collect(Collectors.toCollection(HashSet::new))); }); return ret.get(); } }
@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 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]; }
if( bus!=null ) { for (String updatePath : updatePaths) { bus.broadcast("jsondb-updated", prefix(trimSuffix(updatePath, "/"), "/"));
if( bus!=null ) { for (String updatePath : updatePaths) { bus.broadcast("jsondb-updated", prefix(trimSuffix(updatePath, "/"), "/"));
@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 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, "/"), "/")); } }