static int toArrayIndex(String value) { return fromLexSortableStringToInt(value); }
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, '['); }
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, "/"), "/"); }
currentPath = path + toArrayIndexPath(arrayIndex) + "/"; jsonStreamToRecords(indexes, jp, currentPath + validateKey(jp.getCurrentName()) + "/", consumer); } else if (nextToken == VALUE_NULL) { if (inArray) { currentPath = path + toArrayIndexPath(arrayIndex) + "/"; consumer.accept(JsonRecord.of(currentPath, String.valueOf(NULL_VALUE_PREFIX), "null", indexFieldValue(indexes, currentPath))); if( inArray ) { arrayIndex++; currentPath = path + toArrayIndexPath(arrayIndex) + "/"; value = toLexSortableString(value); // encode it so we can lexically sort. } else if( nextToken == JsonToken.VALUE_TRUE ) { ovalue = value; consumer.accept(JsonRecord.of(currentPath, value, ovalue, indexFieldValue(indexes, currentPath))); if( inArray ) { arrayIndex++;
@Test public void testSmallValues() { for (int i = 0; i < 10; i++) { String r1 = JsonRecordSupport.toLexSortableString(i, '+'); int r2 = JsonRecordSupport.fromLexSortableStringToInt(r1, '+'); assertThat(r2).isEqualTo(i); } } }
@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, "/"), "/")); } }
String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath + "%"; GetOptions.Order order = o.order(); String startAfter = validateKey(o.startAfter()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path <= :startAfter"); String startAt = validateKey(o.startAt()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path < :startAt"); String endAt = validateKey(o.endAt()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path > :endAt"); String endBefore = validateKey(o.endBefore()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path >= :endBefore");
private String validate(String value) { if( value == null ) { return null; } return validateKey(value); }
@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]; }
jg.writeStartArray(); int idx = JsonRecordSupport.toArrayIndex(nextPart); while (idx > pathPart.getIdx()) { pathPart.incrementIdx();
public static void jsonStreamToRecords(Set<String> indexes, String dbPath, InputStream is, Consumer<JsonRecord> consumer) throws IOException { try (JsonParser jp = new JsonFactory().createParser(is)) { jsonStreamToRecords(indexes, jp, dbPath, consumer); JsonToken jsonToken = jp.nextToken(); if (jsonToken != null) { throw new JsonParseException(jp, "Document did not terminate as expected."); } } }
currentPath = path + toArrayIndexPath(arrayIndex) + "/"; jsonStreamToRecords(indexes, jp, currentPath + validateKey(jp.getCurrentName()) + "/", consumer); } else if (nextToken == VALUE_NULL) { if (inArray) { currentPath = path + toArrayIndexPath(arrayIndex) + "/"; consumer.accept(JsonRecord.of(currentPath, "", nextToken.id(), indexFieldValue(indexes, currentPath))); if( inArray ) { arrayIndex++; currentPath = path + toArrayIndexPath(arrayIndex) + "/"; consumer.accept(JsonRecord.of(currentPath, jp.getValueAsString(), nextToken.id(), indexFieldValue(indexes, currentPath))); if( inArray ) { arrayIndex++;
@Test public void testDataTypes() { // see: http://www.zanopha.com/docs/elen.pdf int intitialValue = 1234567890; String r1 = toLexSortableString(intitialValue); assertThat(r1).isEqualTo("[[[2101234567890"); int r2 = JsonRecordSupport.fromLexSortableStringToInt(r1); assertThat(r2).isEqualTo(intitialValue); }
String baseDBPath = JsonRecordSupport.convertToDBPath(key); mb.deleteRecordsForSet(baseDBPath); JsonRecordSupport.jsonStreamToRecords(indexes, jp, baseDBPath, mb.createSetConsumer()); } catch (IOException e) { throw new JsonDBException(e);
String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath + "%"; GetOptions.Order order = o.order(); String startAfter = validateKey(o.startAfter()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path <= :startAfter"); String startAt = validateKey(o.startAt()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path < :startAt"); String endAt = validateKey(o.endAt()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path > :endAt"); String endBefore = validateKey(o.endBefore()); if (o.order() == GetOptions.Order.DESC) { sql.append(" and path >= :endBefore");
@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]; }
jg.writeStartArray(); int idx = JsonRecordSupport.toArrayIndex(nextPart); while (idx > pathPart.getIdx()) { pathPart.incrementIdx();
public static void jsonStreamToRecords(HashSet<String> indexes, String dbPath, InputStream is, Consumer<JsonRecord> consumer) throws IOException { try (JsonParser jp = new JsonFactory().createParser(is)) { jsonStreamToRecords(indexes, jp, dbPath, consumer); JsonToken jsonToken = jp.nextToken(); if (jsonToken != null) { throw new JsonParseException(jp, "Document did not terminate as expected."); } } }
@Test public void testDataTypes() { // see: http://www.zanopha.com/docs/elen.pdf int intitialValue = 1234567890; String r1 = JsonRecordSupport.toLexSortableString(intitialValue, '+'); assertThat(r1).isEqualTo("+++2101234567890"); int r2 = JsonRecordSupport.fromLexSortableStringToInt(r1, '+'); assertThat(r2).isEqualTo(intitialValue); }
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); }