Java Code Examples for com.arangodb.velocypack.VPackSlice#isArray()

The following examples show how to use com.arangodb.velocypack.VPackSlice#isArray() . 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: DefaultArangoConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
private Object readCollection(final TypeInformation<?> type, final VPackSlice source) {
	if (!source.isArray()) {
		throw new MappingException(
				String.format("Can't read collection type %s from VPack type %s!", type, source.getType()));
	}

	final TypeInformation<?> componentType = getNonNullComponentType(type);
	final Class<?> collectionType = Iterable.class.equals(type.getType()) ? Collection.class : type.getType();
	final Collection<Object> collection = CollectionFactory.createCollection(collectionType,
		componentType.getType(), source.getLength());

	final Iterator<VPackSlice> iterator = source.arrayIterator();

	while (iterator.hasNext()) {
		final VPackSlice elem = iterator.next();
		collection.add(readInternal(componentType, elem));
	}

	return collection;
}
 
Example 2
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
private Object readArray(final TypeInformation<?> type, final VPackSlice source) {
	if (!source.isArray()) {
		throw new MappingException(
				String.format("Can't read array type %s from VPack type %s!", type, source.getType()));
	}

	final TypeInformation<?> componentType = getNonNullComponentType(type);
	final int length = source.getLength();
	final Object array = Array.newInstance(componentType.getType(), length);

	for (int i = 0; i < length; ++i) {
		Array.set(array, i, readInternal(componentType, source.get(i)));
	}

	return array;
}
 
Example 3
Source File: DefaultArangoTypeMapper.java    From spring-data with Apache License 2.0 5 votes vote down vote up
public Alias readAliasFrom(final VPackSlice source) {
	if (source.isArray()) {
		return Alias.NONE;
	}

	if (source.isObject()) {
		final VPackSlice typeKey = source.get(this.typeKey);
		return Alias.ofNullable(typeKey.isString() ? typeKey.getAsString() : null);
	}

	throw new IllegalArgumentException("Cannot read alias from VPack type " + source.getType());
}
 
Example 4
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentUpdateEntity<T>>> updateDocumentsResponseDeserializer(
        final Class<T> returnType) {
    return response -> {
        final MultiDocumentEntity<DocumentUpdateEntity<T>> multiDocument = new MultiDocumentEntity<>();
        final Collection<DocumentUpdateEntity<T>> docs = new ArrayList<>();
        final Collection<ErrorEntity> errors = new ArrayList<>();
        final Collection<Object> documentsAndErrors = new ArrayList<>();
        final VPackSlice body = response.getBody();
        if (body.isArray()) {
            for (final Iterator<VPackSlice> iterator = body.arrayIterator(); iterator.hasNext(); ) {
                final VPackSlice next = iterator.next();
                if (next.get(ArangoResponseField.ERROR).isTrue()) {
                    final ErrorEntity error = util().deserialize(next, ErrorEntity.class);
                    errors.add(error);
                    documentsAndErrors.add(error);
                } else {
                    final DocumentUpdateEntity<T> doc = util().deserialize(next, DocumentUpdateEntity.class);
                    final VPackSlice newDoc = next.get(NEW);
                    if (newDoc.isObject()) {
                        doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, returnType));
                    }
                    final VPackSlice oldDoc = next.get(OLD);
                    if (oldDoc.isObject()) {
                        doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, returnType));
                    }
                    docs.add(doc);
                    documentsAndErrors.add(doc);
                }
            }
        }
        multiDocument.setDocuments(docs);
        multiDocument.setErrors(errors);
        multiDocument.setDocumentsAndErrors(documentsAndErrors);
        return multiDocument;
    };
}
 
Example 5
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentDeleteEntity<T>>> deleteDocumentsResponseDeserializer(
        final Class<T> type) {
    return response -> {
        final MultiDocumentEntity<DocumentDeleteEntity<T>> multiDocument = new MultiDocumentEntity<>();
        final Collection<DocumentDeleteEntity<T>> docs = new ArrayList<>();
        final Collection<ErrorEntity> errors = new ArrayList<>();
        final Collection<Object> documentsAndErrors = new ArrayList<>();
        final VPackSlice body = response.getBody();
        if (body.isArray()) {
            for (final Iterator<VPackSlice> iterator = body.arrayIterator(); iterator.hasNext(); ) {
                final VPackSlice next = iterator.next();
                if (next.get(ArangoResponseField.ERROR).isTrue()) {
                    final ErrorEntity error = util().deserialize(next, ErrorEntity.class);
                    errors.add(error);
                    documentsAndErrors.add(error);
                } else {
                    final DocumentDeleteEntity<T> doc = util().deserialize(next, DocumentDeleteEntity.class);
                    final VPackSlice oldDoc = next.get(OLD);
                    if (oldDoc.isObject()) {
                        doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, type));
                    }
                    docs.add(doc);
                    documentsAndErrors.add(doc);
                }
            }
        }
        multiDocument.setDocuments(docs);
        multiDocument.setErrors(errors);
        multiDocument.setDocumentsAndErrors(documentsAndErrors);
        return multiDocument;
    };
}
 
Example 6
Source File: InternalArangoDatabase.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
protected ResponseDeserializer<Collection<AqlFunctionEntity>> getAqlFunctionsResponseDeserializer() {
    return response -> {
        final VPackSlice body = response.getBody();
        // compatibility with ArangoDB < 3.4
        // https://www.arangodb.com/docs/stable/release-notes-upgrading-changes34.html
        final VPackSlice result = body.isArray() ? body : body.get(ArangoResponseField.RESULT);
        return util().deserialize(result, new Type<Collection<AqlFunctionEntity>>() {
        }.getType());
    };
}
 
Example 7
Source File: VPackDeserializers.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
protected static FieldLink deserializeField(final Entry<String, VPackSlice> field) {
    final VPackSlice value = field.getValue();
    final FieldLink link = FieldLink.on(field.getKey());
    final VPackSlice analyzers = value.get("analyzers");
    if (analyzers.isArray()) {
        final Iterator<VPackSlice> analyzerIterator = analyzers.arrayIterator();
        for (; analyzerIterator.hasNext(); ) {
            link.analyzers(analyzerIterator.next().getAsString());
        }
    }
    final VPackSlice includeAllFields = value.get("includeAllFields");
    if (includeAllFields.isBoolean()) {
        link.includeAllFields(includeAllFields.getAsBoolean());
    }
    final VPackSlice trackListPositions = value.get("trackListPositions");
    if (trackListPositions.isBoolean()) {
        link.trackListPositions(trackListPositions.getAsBoolean());
    }
    final VPackSlice storeValues = value.get("storeValues");
    if (storeValues.isString()) {
        link.storeValues(StoreValuesType.valueOf(storeValues.getAsString().toUpperCase()));
    }
    final VPackSlice fields = value.get("fields");
    if (fields.isObject()) {
        final Iterator<Entry<String, VPackSlice>> fieldsIterator = fields.objectIterator();
        for (; fieldsIterator.hasNext(); ) {
            link.fields(deserializeField(fieldsIterator.next()));
        }
    }
    return link;
}
 
Example 8
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
private Object readInternal(final TypeInformation<?> type, final VPackSlice source) {
	if (source == null) {
		return null;
	}

	if (VPackSlice.class.isAssignableFrom(type.getType())) {
		return source;
	}

	final TypeInformation<?> typeToUse = (source.isArray() || source.isObject()) ? typeMapper.readType(source, type)
			: type;
	final Class<?> rawTypeToUse = typeToUse.getType();

	if (conversions.hasCustomReadTarget(VPackSlice.class, typeToUse.getType())) {
		return conversionService.convert(source, rawTypeToUse);
	}

	if (conversions.hasCustomReadTarget(DBDocumentEntity.class, typeToUse.getType())) {
		return conversionService.convert(readSimple(DBDocumentEntity.class, source), rawTypeToUse);
	}

	if (!source.isArray() && !source.isObject()) {
		return convertIfNecessary(readSimple(rawTypeToUse, source), rawTypeToUse);
	}

	if (DBDocumentEntity.class.isAssignableFrom(rawTypeToUse)) {
		return readSimple(rawTypeToUse, source);
	}

	if (BaseDocument.class.isAssignableFrom(rawTypeToUse)) {
		return readBaseDocument(rawTypeToUse, source);
	}

	if (typeToUse.isMap()) {
		return readMap(typeToUse, source);
	}

	if (!source.isArray() && ClassTypeInformation.OBJECT.equals(typeToUse)) {
		return readMap(ClassTypeInformation.MAP, source);
	}

	if (typeToUse.getType().isArray()) {
		return readArray(typeToUse, source);
	}

	if (typeToUse.isCollectionLike()) {
		return readCollection(typeToUse, source);
	}

	if (ClassTypeInformation.OBJECT.equals(typeToUse)) {
		return readCollection(ClassTypeInformation.COLLECTION, source);
	}

	final ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(rawTypeToUse);
	return readEntity(typeToUse, source, entity);
}
 
Example 9
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentCreateEntity<T>>> insertDocumentsResponseDeserializer(
        final Collection<T> values, final DocumentCreateOptions params) {
    return response -> {
        Class<T> type = null;
        if (Boolean.TRUE == params.getReturnNew()) {
            if (!values.isEmpty()) {
                type = (Class<T>) values.iterator().next().getClass();
            }
        }
        final MultiDocumentEntity<DocumentCreateEntity<T>> multiDocument = new MultiDocumentEntity<>();
        final Collection<DocumentCreateEntity<T>> docs = new ArrayList<>();
        final Collection<ErrorEntity> errors = new ArrayList<>();
        final Collection<Object> documentsAndErrors = new ArrayList<>();
        final VPackSlice body = response.getBody();
        if (body.isArray()) {
            for (final Iterator<VPackSlice> iterator = body.arrayIterator(); iterator.hasNext(); ) {
                final VPackSlice next = iterator.next();
                if (next.get(ArangoResponseField.ERROR).isTrue()) {
                    final ErrorEntity error = util().deserialize(next, ErrorEntity.class);
                    errors.add(error);
                    documentsAndErrors.add(error);
                } else {
                    final DocumentCreateEntity<T> doc = util().deserialize(next, DocumentCreateEntity.class);
                    final VPackSlice newDoc = next.get(NEW);
                    if (newDoc.isObject()) {
                        doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, type));
                    }
                    final VPackSlice oldDoc = next.get(OLD);
                    if (oldDoc.isObject()) {
                        doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, type));
                    }
                    docs.add(doc);
                    documentsAndErrors.add(doc);
                }
            }
        }
        multiDocument.setDocuments(docs);
        multiDocument.setErrors(errors);
        multiDocument.setDocumentsAndErrors(documentsAndErrors);
        return multiDocument;
    };
}
 
Example 10
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T> ResponseDeserializer<MultiDocumentEntity<DocumentUpdateEntity<T>>> replaceDocumentsResponseDeserializer(
        final Collection<T> values, final DocumentReplaceOptions params) {
    return response -> {
        Class<T> type = null;
        if (Boolean.TRUE == params.getReturnNew() || Boolean.TRUE == params.getReturnOld()) {
            if (!values.isEmpty()) {
                type = (Class<T>) values.iterator().next().getClass();
            }
        }
        final MultiDocumentEntity<DocumentUpdateEntity<T>> multiDocument = new MultiDocumentEntity<>();
        final Collection<DocumentUpdateEntity<T>> docs = new ArrayList<>();
        final Collection<ErrorEntity> errors = new ArrayList<>();
        final Collection<Object> documentsAndErrors = new ArrayList<>();
        final VPackSlice body = response.getBody();
        if (body.isArray()) {
            for (final Iterator<VPackSlice> iterator = body.arrayIterator(); iterator.hasNext(); ) {
                final VPackSlice next = iterator.next();
                if (next.get(ArangoResponseField.ERROR).isTrue()) {
                    final ErrorEntity error = util().deserialize(next, ErrorEntity.class);
                    errors.add(error);
                    documentsAndErrors.add(error);
                } else {
                    final DocumentUpdateEntity<T> doc = util().deserialize(next, DocumentUpdateEntity.class);
                    final VPackSlice newDoc = next.get(NEW);
                    if (newDoc.isObject()) {
                        doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, type));
                    }
                    final VPackSlice oldDoc = next.get(OLD);
                    if (oldDoc.isObject()) {
                        doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, type));
                    }
                    docs.add(doc);
                    documentsAndErrors.add(doc);
                }
            }
        }
        multiDocument.setDocuments(docs);
        multiDocument.setErrors(errors);
        multiDocument.setDocumentsAndErrors(documentsAndErrors);
        return multiDocument;
    };
}