org.ojai.FieldPath Java Examples

The following examples show how to use org.ojai.FieldPath. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestFieldPath.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testFieldPathEscapedInvalidSequences() {
  String[] validSequences = {"\\`", "\\\"", "\\\\", "\\.", "\\[", "\\]"};
  String sequence = null;
  try {
    for (String validSequence : validSequences) {
      sequence = validSequence;
      FieldPath.parseFrom(validSequence);

      // quoted with double-quote
      sequence = "\"" + validSequence + "\"";
      FieldPath.parseFrom(sequence);

      // quoted with back-tick
      sequence = "`" + validSequence + "`";
      FieldPath.parseFrom(sequence);
    }
  } catch (IllegalArgumentException e) {
    fail("FieldPath parsing failed for sequence: " + hexDump(sequence));
  }
}
 
Example #2
Source File: JsonDocumentStream.java    From ojai with Apache License 2.0 6 votes vote down vote up
static DocumentStream newDocumentStream(FileSystem fs,
    Path path, Map<FieldPath, Type> map, Events.Delegate delegate)
        throws IllegalArgumentException, IOException {
  final InputStream in = fs.open(path);
  return new JsonDocumentStream(in, map, delegate) {
    @Override
    public void close() {
      try {
        super.close();
      } finally {
        try {
          in.close();
        } catch (IOException e) {
          throw new OjaiException(e);
        }
      }
    }
  };
}
 
Example #3
Source File: ChangelogListenerImpl.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
private void handleUpdate(ChangeDataRecord changeDataRecord) {

        String documentId = changeDataRecord.getId().getString();
        log.debug("Updated document with id = '{}'", documentId);

        if (this.onUpdate == null) {
            return;
        }

        ObjectNode changes = mapper.createObjectNode();
        for (Map.Entry<FieldPath, ChangeNode> changeNodeEntry : changeDataRecord) {

            ChangeNode changeNode = changeNodeEntry.getValue();
            String jsonString = changeNode.getValue().asJsonString();
            String fieldPathAsString = changeNodeEntry.getKey().asPathString();
            changes.set(fieldPathAsString, parseJsonString(jsonString));
        }

        this.onUpdate.callback(documentId, changes);
    }
 
Example #4
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimplePathSingleSegment() {
  FieldPath fp = FieldPath.parseFrom("a");
  assertTrue(fp.getRootSegment().isLeaf());
  assertEquals("\"a\"", fp.asPathString(true));
  assertEquals("a", fp.asPathString());
  assertEquals("\"a\"", fp.asJsonString());
}
 
Example #5
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapedPathDoubleSegment() {
  FieldPath fp = FieldPath.parseFrom("\"a.b\"");
  assertTrue(fp.getRootSegment().isLeaf());
  assertEquals("\"a.b\"", fp.asPathString());
  assertEquals("\"a.b\"", fp.asPathString(false));
  assertEquals("\"\\\"a.b\\\"\"", fp.asJsonString());
}
 
Example #6
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuotedEscapedPath() {
  FieldPath fp = FieldPath.parseFrom("\"the\\\"quick.brown\\\\fox\"");
  assertTrue(fp.getRootSegment().isLastPath());
  assertEquals("\"the\\\"quick.brown\\\\fox\"", fp.asPathString(true));
  assertEquals("\"the\\\"quick.brown\\\\fox\"", fp.asPathString(false));
  assertEquals("\"the\\\"quick.brown\\\\fox\"", fp.asPathString());
  assertEquals("\"\\\"the\\\\\"quick.brown\\\\fox\\\"\"", fp.asJsonString());
}
 
Example #7
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testPathWithSpace() {
  FieldPath fp = FieldPath.parseFrom("work phone.cell phone");
  assertFalse(fp.getRootSegment().isLeaf());
  assertTrue(fp.getRootSegment().getChild().isLeaf());
  assertEquals("\"work phone\".\"cell phone\"", fp.asPathString(true));
  assertEquals("work phone.cell phone", fp.asPathString());
  assertEquals("\"work phone.cell phone\"", fp.asJsonString());

  fp = FieldPath.parseFrom("a[ ]");
  assertEquals("\"a\"[]", fp.asPathString(true));
  assertEquals("a[]", fp.asPathString());
  assertEquals("\"a[]\"", fp.asJsonString());
}
 
Example #8
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloneWithNewParent() {
  FieldPath fp1 = FieldPath.parseFrom("a.b.c");
  FieldPath fp2 = fp1.cloneWithNewParent("v");
  assertEquals("v.a.b.c", fp2.asPathString());
  assertEquals(fp1.getRootSegment(), fp2.getRootSegment().getChild());
}
 
Example #9
Source File: JsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public double getDouble(FieldPath fieldPath) {
  checkForArrayNotation(fieldPath);
  JsonValue value = getKeyValueAt(fieldPath.iterator());
  testNoSuchElement(fieldPath, value);
  return value.getDouble();
}
 
Example #10
Source File: JsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
public static void checkForArrayNotation(FieldPath fp) {
  FieldSegment curSegment = fp.getRootSegment();
  while (curSegment != null) {
    if (curSegment.isIndexed() && !curSegment.getIndexSegment().hasIndex()) {
      throw new IllegalArgumentException(
          ELEMENTS_ALL + " cannot be used to get/set/delete" +
              " a value in a Document");
    }
    curSegment = curSegment.getChild();
  }
}
 
Example #11
Source File: JsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public OInterval getInterval(FieldPath fieldPath) {
  JsonValue v = getKeyValueAt(fieldPath.iterator());
  if (v != null) {
    return v.getInterval();
  }
  return null;
}
 
Example #12
Source File: BaseFieldProjector.java    From ojai with Apache License 2.0 5 votes vote down vote up
protected BaseFieldProjector(@NonNullable Collection<FieldPath> includedPaths) {
  rootSegment = new ProjectionTree(DOCUMENT_ROOT, null);
  for (FieldPath includedPath : Preconditions.checkNotNull(includedPaths)) {
    rootSegment.addOrGetChild(includedPath.getRootSegment());
  }
  finishConstruction();
}
 
Example #13
Source File: JsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public BigDecimal getDecimal(FieldPath fieldPath) {
  checkForArrayNotation(fieldPath);
  JsonValue v = getKeyValueAt(fieldPath.iterator());
  if (v != null) {
    return v.getDecimal();
  }
  return null;
}
 
Example #14
Source File: JsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public Float getFloatObj(FieldPath fieldPath) {
  checkForArrayNotation(fieldPath);
  JsonValue value = getKeyValueAt(fieldPath.iterator());
  if (value != null) {
    return value.getFloat();
  }
  return null;
}
 
Example #15
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document setArray(FieldPath fieldPath, String[] values) {
  return setCommonFromObjectArray(fieldPath, values);
}
 
Example #16
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
private void testNoSuchElement(FieldPath fieldPath, JsonValue value) {
  if (value == null) {
    throw new NoSuchElementException("Field '"
        + fieldPath.asPathString() + "' not found in the document.");
  }
}
 
Example #17
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, boolean value) {
  return setCommon(fieldPath, new JsonValue(value, Type.BOOLEAN));
}
 
Example #18
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, float value) {
  throw readOnly();
}
 
Example #19
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, int value) {

  return setCommon(fieldPath, new JsonValue(value, Type.INT));
}
 
Example #20
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, OInterval value) {
  throw readOnly();
}
 
Example #21
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(String fieldPath, byte value) {
  return set(FieldPath.parseFrom(fieldPath), value);
}
 
Example #22
Source File: JsonDocumentStream.java    From ojai with Apache License 2.0 4 votes vote down vote up
Map<FieldPath, Type> getFieldPathTypeMap() {
  return fieldPathTypeMap;
}
 
Example #23
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public short getShort(FieldPath fieldPath) {
  return wrapped.getShort(fieldPath);
}
 
Example #24
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Byte getByteObj(FieldPath fieldPath) {
  return wrapped.getByteObj(fieldPath);
}
 
Example #25
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public int getInt(String fieldPath) {
  return getInt(FieldPath.parseFrom(fieldPath));
}
 
Example #26
Source File: Documents.java    From ojai with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the value at the specified fieldPath as a {@link Value} or the
 * specified {@code defaultValue} if the specified {@code FieldPath} does not
 * exist in the document.
 */
public static Value getValue(@NonNullable Document document,
    @NonNullable FieldPath fieldPath, @Nullable Value defaultValue) {
  Value docValue = document.getValue(fieldPath);
  return docValue != null ? docValue : defaultValue;
}
 
Example #27
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document setArray(String fieldPath, byte[] values) {
  return setArray(FieldPath.parseFrom(fieldPath), values);
}
 
Example #28
Source File: DocumentBase.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public double getDouble(String fieldPath) {
  return getDouble(FieldPath.parseFrom(fieldPath));
}
 
Example #29
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document setArray(String fieldPath, float[] values) {
  return setArray(FieldPath.parseFrom(fieldPath), values);
}
 
Example #30
Source File: DocumentBase.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public OTimestamp getTimestamp(String fieldPath) {
  return getTimestamp(FieldPath.parseFrom(fieldPath));
}