com.arangodb.velocypack.VPackBuilder Java Examples

The following examples show how to use com.arangodb.velocypack.VPackBuilder. 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: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObjectInObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", ValueType.OBJECT); // add object in field "foo"
	builder.add("bar", 2); // add field "bar" with value 2 to object "foo"
	builder.close();// object "foo" end
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));

	final VPackSlice foo = slice.get("foo");
	assertThat(foo.isObject(), is(true));

	final VPackSlice bar = foo.get("bar"); // get field "bar" from "foo"
	assertThat(bar.isInteger(), is(true));
}
 
Example #2
Source File: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", 1); // add field "foo" with value 1
	builder.add("bar", 2); // add field "bar" with value 2
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));
	assertThat(slice.size(), is(2)); // number of fields

	final VPackSlice foo = slice.get("foo"); // get field "foo"
	assertThat(foo.isInteger(), is(true));
	assertThat(foo.getAsInt(), is(1));

	final VPackSlice bar = slice.get("bar"); // get field "bar"
	assertThat(bar.isInteger(), is(true));
	assertThat(bar.getAsInt(), is(2));

	// iterate over the fields
	for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext(); ) {
		final Entry<String, VPackSlice> field = iterator.next();
		assertThat(field.getValue().isInteger(), is(true));
	}
}
 
Example #3
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
private void writeMap(
	final String attribute,
	final Map<? extends Object, ? extends Object> source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	sink.add(attribute, ValueType.OBJECT);

	for (final Entry<? extends Object, ? extends Object> entry : source.entrySet()) {
		final Object key = entry.getKey();
		final Object value = entry.getValue();

		writeInternal(convertId(key), value, sink, getNonNullMapValueType(definedType));
	}

	sink.close();
}
 
Example #4
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
private void writeArray(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	if (byte[].class.equals(source.getClass())) {
		sink.add(attribute, Base64Utils.encodeToString((byte[]) source));
	}

	else {
		sink.add(attribute, ValueType.ARRAY);
		for (int i = 0; i < Array.getLength(source); ++i) {
			final Object element = Array.get(source, i);
			writeInternal(null, element, sink, getNonNullComponentType(definedType));
		}
		sink.close();
	}
}
 
Example #5
Source File: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObjectInObject() throws VPackException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT);// object start
    builder.add("foo", ValueType.OBJECT); // add object in field "foo"
    builder.add("bar", 2); // add field "bar" with value 2 to object "foo"
    builder.close();// object "foo" end
    builder.close();// object end

    final VPackSlice slice = builder.slice(); // create slice
    assertThat(slice.isObject(), is(true));

    final VPackSlice foo = slice.get("foo");
    assertThat(foo.isObject(), is(true));

    final VPackSlice bar = foo.get("bar"); // get field "bar" from "foo"
    assertThat(bar.isInteger(), is(true));
}
 
Example #6
Source File: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObject() throws VPackException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT);// object start
    builder.add("foo", 1); // add field "foo" with value 1
    builder.add("bar", 2); // add field "bar" with value 2
    builder.close();// object end

    final VPackSlice slice = builder.slice(); // create slice
    assertThat(slice.isObject(), is(true));
    assertThat(slice.size(), is(2)); // number of fields

    final VPackSlice foo = slice.get("foo"); // get field "foo"
    assertThat(foo.isInteger(), is(true));
    assertThat(foo.getAsInt(), is(1));

    final VPackSlice bar = slice.get("bar"); // get field "bar"
    assertThat(bar.isInteger(), is(true));
    assertThat(bar.getAsInt(), is(2));

    // iterate over the fields
    for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext(); ) {
        final Entry<String, VPackSlice> field = iterator.next();
        assertThat(field.getValue().isInteger(), is(true));
    }
}
 
Example #7
Source File: VPackExample.java    From arangodb-java-driver-async with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObjectInObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", ValueType.OBJECT); // add object in field "foo"
	builder.add("bar", 2); // add field "bar" with value 2 to object "foo"
	builder.close();// object "foo" end
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));

	final VPackSlice foo = slice.get("foo");
	assertThat(foo.isObject(), is(true));

	final VPackSlice bar = foo.get("bar"); // get field "bar" from "foo"
	assertThat(bar.isInteger(), is(true));
}
 
Example #8
Source File: VPackExample.java    From arangodb-java-driver-async with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", 1); // add field "foo" with value 1
	builder.add("bar", 2); // add field "bar" with value 2
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));
	assertThat(slice.size(), is(2)); // number of fields

	final VPackSlice foo = slice.get("foo"); // get field "foo"
	assertThat(foo.isInteger(), is(true));
	assertThat(foo.getAsInt(), is(1));

	final VPackSlice bar = slice.get("bar"); // get field "bar"
	assertThat(bar.isInteger(), is(true));
	assertThat(bar.getAsInt(), is(2));

	// iterate over the fields
	for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext();) {
		final Entry<String, VPackSlice> field = iterator.next();
		assertThat(field.getValue().isInteger(), is(true));
	}
}
 
Example #9
Source File: BaseDocumentTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void deserialize() throws VPackException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT);
    builder.add("_id", "test/test");
    builder.add("_key", "test");
    builder.add("_rev", "test");
    builder.add("a", "a");
    builder.close();

    final VPack.Builder vbuilder = new VPack.Builder();
    vbuilder.registerModule(new VPackDriverModule());
    final VPack vpacker = vbuilder.build();

    final BaseDocument entity = vpacker.deserialize(builder.slice(), BaseDocument.class);
    assertThat(entity.getId(), is(notNullValue()));
    assertThat(entity.getId(), is("test/test"));
    assertThat(entity.getKey(), is(notNullValue()));
    assertThat(entity.getKey(), is("test"));
    assertThat(entity.getRevision(), is(notNullValue()));
    assertThat(entity.getRevision(), is("test"));
    assertThat(entity.getProperties().size(), is(1));
    assertThat(String.valueOf(entity.getAttribute("a")), is("a"));
}
 
Example #10
Source File: ArangoDatabaseTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionVPackArray() throws VPackException {
    final VPackSlice params = new VPackBuilder().add(ValueType.ARRAY).add("hello").add("world").close().slice();
    final TransactionOptions options = new TransactionOptions().params(params);
    final String result = db
            .transaction("function (params) { return params[0] + ' ' + params[1];}", String.class, options);
    assertThat(result, is("hello world"));
}
 
Example #11
Source File: InsertDocumentExample.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void insertVPack() {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT).add("foo", "bar").close();
	final DocumentCreateEntity<VPackSlice> doc = collection.insertDocument(builder.slice());
	assertThat(doc.getKey(), is(notNullValue()));
}
 
Example #12
Source File: VPackSerializers.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
private static void serializeFieldLinks(final VPackBuilder builder, final Collection<FieldLink> links) {
    if (!links.isEmpty()) {
        builder.add("fields", ValueType.OBJECT);
        for (final FieldLink fieldLink : links) {
            builder.add(fieldLink.getName(), ValueType.OBJECT);
            final Collection<String> analyzers = fieldLink.getAnalyzers();
            if (!analyzers.isEmpty()) {
                builder.add("analyzers", ValueType.ARRAY);
                for (final String analyzer : analyzers) {
                    builder.add(analyzer);
                }
                builder.close();
            }
            final Boolean includeAllFields = fieldLink.getIncludeAllFields();
            if (includeAllFields != null) {
                builder.add("includeAllFields", includeAllFields);
            }
            final Boolean trackListPositions = fieldLink.getTrackListPositions();
            if (trackListPositions != null) {
                builder.add("trackListPositions", trackListPositions);
            }
            final StoreValuesType storeValues = fieldLink.getStoreValues();
            if (storeValues != null) {
                builder.add("storeValues", storeValues.name().toLowerCase());
            }
            serializeFieldLinks(builder, fieldLink.getFields());
            builder.close();
        }
        builder.close();
    }
}
 
Example #13
Source File: ArangoDatabaseTest.java    From arangodb-java-driver-async with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionVPack() throws VPackException, InterruptedException, ExecutionException {
    final TransactionOptions options = new TransactionOptions().params(new VPackBuilder().add("test").slice());
    db.transaction("function (params) {return params;}", VPackSlice.class, options)
            .whenComplete((result, ex) -> {
                assertThat(result.isString(), is(true));
                assertThat(result.getAsString(), is("test"));
            })
            .get();
}
 
Example #14
Source File: InsertDocumentExample.java    From arangodb-java-driver-async with Apache License 2.0 5 votes vote down vote up
@Test
public void insertVPack() throws ExecutionException, InterruptedException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT).add("foo", "bar").close();
    collection.insertDocument(builder.slice())
            .whenComplete((doc, ex) -> assertThat(doc.getKey(), is(notNullValue())))
            .get();
}
 
Example #15
Source File: ArangoDatabaseTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionVPack() throws VPackException {
    final TransactionOptions options = new TransactionOptions().params(new VPackBuilder().add("test").slice());
    final VPackSlice result = db.transaction("function (params) {return params;}", VPackSlice.class, options);
    assertThat(result.isString(), is(true));
    assertThat(result.getAsString(), is("test"));
}
 
Example #16
Source File: ArangoDatabaseTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionVPackObject() throws VPackException {
    final VPackSlice params = new VPackBuilder().add(ValueType.OBJECT).add("foo", "hello").add("bar", "world")
            .close().slice();
    final TransactionOptions options = new TransactionOptions().params(params);
    final String result = db
            .transaction("function (params) { return params['foo'] + ' ' + params['bar'];}", String.class, options);
    assertThat(result, is("hello world"));
}
 
Example #17
Source File: DefaultArangoTypeMapper.java    From spring-data with Apache License 2.0 5 votes vote down vote up
public void writeType(final TypeInformation<?> info, final VPackBuilder sink) {
	Assert.notNull(info, "TypeInformation must not be null!");

	final Alias alias = getAliasFor(info);
	if (alias.isPresent()) {
		accessor.writeTypeTo(sink, alias.getValue());
	}
}
 
Example #18
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void addTypeKeyIfNecessary(
	final TypeInformation<?> definedType,
	final Object value,
	final VPackBuilder sink) {

	final Class<?> referenceType = definedType != null ? definedType.getType() : Object.class;
	final Class<?> valueType = ClassUtils.getUserClass(value.getClass());
	if (!valueType.equals(referenceType)) {
		typeMapper.writeType(valueType, sink);
	}
}
 
Example #19
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void writeBaseEdgeDocument(
	final String attribute,
	final BaseEdgeDocument source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	final VPackBuilder builder = new VPackBuilder();
	writeMap(attribute, source.getProperties(), builder, definedType);
	builder.add(_ID, source.getId());
	builder.add(_KEY, source.getKey());
	builder.add(_REV, source.getRevision());
	builder.add(_FROM, source.getFrom());
	builder.add(_TO, source.getTo());
	sink.add(attribute, builder.slice());
}
 
Example #20
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void writeBaseDocument(
	final String attribute,
	final BaseDocument source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	final VPackBuilder builder = new VPackBuilder();
	writeMap(attribute, source.getProperties(), builder, definedType);
	builder.add(_ID, source.getId());
	builder.add(_KEY, source.getKey());
	builder.add(_REV, source.getRevision());
	sink.add(attribute, builder.slice());
}
 
Example #21
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void writeCollection(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	sink.add(attribute, ValueType.ARRAY);

	for (final Object entry : asCollection(source)) {
		writeInternal(null, entry, sink, getNonNullComponentType(definedType));
	}

	sink.close();
}
 
Example #22
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void writeProperty(final Object source, final VPackBuilder sink, final ArangoPersistentProperty property) {
	if (source == null) {
		return;
	}

	final TypeInformation<?> sourceType = ClassTypeInformation.from(source.getClass());
	final String fieldName = property.getFieldName();

	if (property.getRef().isPresent()) {
		if (sourceType.isCollectionLike()) {
			writeReferences(fieldName, source, sink);
		} else {
			writeReference(fieldName, source, sink);
		}
	}

	else if (property.getRelations().isPresent()) {
		// nothing to store
	}

	else if (property.getFrom().isPresent() || property.getTo().isPresent()) {
		if (!sourceType.isCollectionLike()) {
			writeReference(fieldName, source, sink);
		}
	}

	else {
		final Object entity = source instanceof LazyLoadingProxy ? ((LazyLoadingProxy) source).getEntity() : source;
		writeInternal(fieldName, entity, sink, property.getTypeInformation());
	}
}
 
Example #23
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void addKeyIfNecessary(
	final ArangoPersistentEntity<?> entity,
	final Object source,
	final VPackBuilder sink) {
	if (!entity.hasIdProperty() || entity.getIdentifierAccessor(source).getIdentifier() == null) {
		final Object id = entity.getArangoIdAccessor(source).getIdentifier();
		if (id != null) {
			sink.add(_KEY, MetadataUtils.determineDocumentKeyFromId((String) id));
		}
	}
}
 
Example #24
Source File: InsertDocumentExample.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void insertVPack() throws ExecutionException, InterruptedException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT).add("foo", "bar").close();
    collection.insertDocument(builder.slice())
            .whenComplete((doc, ex) -> assertThat(doc.getKey(), is(notNullValue())))
            .get();
}
 
Example #25
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final Object source, final VPackBuilder sink) {
	if (source == null) {
		writeSimple(null, null, sink);
		return;
	}

	final Object entity = source instanceof LazyLoadingProxy ? ((LazyLoadingProxy) source).getEntity() : source;

	writeInternal(null, entity, sink, ClassTypeInformation.OBJECT);
}
 
Example #26
Source File: ArangoDatabaseTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionVPack() throws VPackException, InterruptedException, ExecutionException {
    final TransactionOptions options = new TransactionOptions().params(new VPackBuilder().add("test").slice());
    db.transaction("function (params) {return params;}", VPackSlice.class, options)
            .whenComplete((result, ex) -> {
                assertThat(result.isString(), is(true));
                assertThat(result.getAsString(), is("test"));
            })
            .get();
}
 
Example #27
Source File: ArangoEntityWriter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
default VPackSlice write(final Object source) {
	final VPackBuilder builder = new VPackBuilder();
	write(source, builder);
	return builder.slice();
}
 
Example #28
Source File: ArangoSerializationTest.java    From arangodb-java-driver with Apache License 2.0 4 votes vote down vote up
@Test
public void deseriarlize() {
    final VPackBuilder builder = new VPackBuilder().add(ValueType.OBJECT).add("foo", "bar").close();
    final BaseDocument doc = util.deserialize(builder.slice(), BaseDocument.class);
    assertThat(doc.getAttribute("foo").toString(), is("bar"));
}
 
Example #29
Source File: CustomMappingTest.java    From spring-data with Apache License 2.0 4 votes vote down vote up
@Override
public VPackSlice convert(final CustomVPackTestEntity source) {
	return new VPackBuilder().add(ValueType.OBJECT).add(FIELD, source.getValue()).close().slice();
}
 
Example #30
Source File: DefaultArangoTypeMapper.java    From spring-data with Apache License 2.0 4 votes vote down vote up
public void writeTypeTo(final VPackBuilder sink, final Object alias) {
	if (this.typeKey != null) {
		sink.add(this.typeKey, alias.toString());
	}
}