Java源码示例:com.arangodb.velocypack.VPackBuilder

示例1
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();
}
 
示例2
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();
	}
}
 
示例3
@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));
	}
}
 
示例4
@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));
}
 
示例5
@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));
	}
}
 
示例6
@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));
}
 
示例7
@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"));
}
 
示例8
@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));
    }
}
 
示例9
@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));
}
 
示例10
@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);
}
 
示例11
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));
		}
	}
}
 
示例12
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());
	}
}
 
示例13
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();
}
 
示例14
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());
}
 
示例15
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());
}
 
示例16
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);
	}
}
 
示例17
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());
	}
}
 
示例18
@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();
}
 
示例19
@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();
}
 
示例20
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();
    }
}
 
示例21
@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()));
}
 
示例22
@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"));
}
 
示例23
@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"));
}
 
示例24
@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"));
}
 
示例25
@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();
}
 
示例26
@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();
}
 
示例27
default VPackSlice write(final Object source) {
	final VPackBuilder builder = new VPackBuilder();
	write(source, builder);
	return builder.slice();
}
 
示例28
@SuppressWarnings("unchecked")
private void writeInternal(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	final Class<?> rawType = source.getClass();
	final TypeInformation<?> type = ClassTypeInformation.from(rawType);

	if (conversions.isSimpleType(rawType)) {
		final Optional<Class<?>> customWriteTarget = conversions.getCustomWriteTarget(rawType);
		final Class<?> targetType = customWriteTarget.orElse(rawType);
		writeSimple(attribute, conversionService.convert(source, targetType), sink);
	}

	else if (BaseDocument.class.equals(rawType)) {
		writeBaseDocument(attribute, (BaseDocument) source, sink, definedType);
	}

	else if (BaseEdgeDocument.class.equals(rawType)) {
		writeBaseEdgeDocument(attribute, (BaseEdgeDocument) source, sink, definedType);
	}

	else if (type.isMap()) {
		writeMap(attribute, (Map<Object, Object>) source, sink, definedType);
	}

	else if (type.getType().isArray()) {
		writeArray(attribute, source, sink, definedType);
	}

	else if (type.isCollectionLike()) {
		writeCollection(attribute, source, sink, definedType);
	}

	else {
		final ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(source.getClass());
		writeEntity(attribute, source, sink, entity, definedType);
	}
}
 
示例29
private void writeReference(final String attribute, final Object source, final VPackBuilder sink) {
	getRefId(source).ifPresent(id -> sink.add(attribute, id));
}
 
示例30
@SuppressWarnings("unchecked")
private void writeSimple(final String attribute, final Object source, final VPackBuilder sink) {
	if (source == null) {
		sink.add(ValueType.NULL);
	}
	// com.arangodb.*
	else if (source instanceof VPackSlice) {
		sink.add(attribute, (VPackSlice) source);
	} //
	else if (source instanceof DBDocumentEntity) {
		writeMap(attribute, (Map<String, Object>) source, sink, ClassTypeInformation.MAP);
	}
	// java.lang.*
	else if (source instanceof Boolean) {
		sink.add(attribute, (Boolean) source);
	} //
	else if (source instanceof Byte) {
		sink.add(attribute, (Byte) source);
	} //
	else if (source instanceof Character) {
		sink.add(attribute, (Character) source);
	} //
	else if (source instanceof Short) {
		sink.add(attribute, (Short) source);
	} //
	else if (source instanceof Integer) {
		sink.add(attribute, (Integer) source);
	} //
	else if (source instanceof Long) {
		sink.add(attribute, (Long) source);
	} //
	else if (source instanceof Float) {
		sink.add(attribute, (Float) source);
	} //
	else if (source instanceof Double) {
		sink.add(attribute, (Double) source);
	} //
	else if (source instanceof String) {
		sink.add(attribute, (String) source);
	} //
	else if (source instanceof Class) {
		sink.add(attribute, ((Class<?>) source).getName());
	} //
	else if (source instanceof Enum) {
		sink.add(attribute, ((Enum<?>) source).name());
	}
	// primitive arrays
	else if (ClassUtils.isPrimitiveArray(source.getClass())) {
		writeArray(attribute, source, sink, ClassTypeInformation.OBJECT);
	}
	// java.util.Date / java.sql.Date / java.sql.Timestamp
	else if (source instanceof Date) {
		sink.add(attribute, DateUtil.format((Date) source));
	}
	// java.math.*
	else if (source instanceof BigInteger) {
		sink.add(attribute, (BigInteger) source);
	} //
	else if (source instanceof BigDecimal) {
		sink.add(attribute, (BigDecimal) source);
	}
	// java.time.*
	else if (source instanceof Instant) {
		sink.add(attribute, JavaTimeUtil.format((Instant) source));
	} //
	else if (source instanceof LocalDate) {
		sink.add(attribute, JavaTimeUtil.format((LocalDate) source));
	} //
	else if (source instanceof LocalDateTime) {
		sink.add(attribute, JavaTimeUtil.format((LocalDateTime) source));
	} //
	else if (source instanceof OffsetDateTime) {
		sink.add(attribute, JavaTimeUtil.format((OffsetDateTime) source));
	} //
	else if (source instanceof ZonedDateTime) {
		sink.add(attribute, JavaTimeUtil.format((ZonedDateTime) source));
	} //
	else {
		throw new MappingException(String.format("Type %s is not a simple type!", source.getClass()));
	}
}