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

The following examples show how to use com.arangodb.velocypack.VPackSlice#isObject() . 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 readMap(final TypeInformation<?> type, final VPackSlice source) {
	if (!source.isObject()) {
		throw new MappingException(
				String.format("Can't read map type %s from VPack type %s!", type, source.getType()));
	}

	final Class<?> keyType = getNonNullComponentType(type).getType();
	final TypeInformation<?> valueType = getNonNullMapValueType(type);
	final Map<Object, Object> map = CollectionFactory.createMap(type.getType(), keyType, source.size());

	final Iterator<Entry<String, VPackSlice>> iterator = source.objectIterator();

	while (iterator.hasNext()) {
		final Entry<String, VPackSlice> entry = iterator.next();
		if (typeMapper.isTypeKey(entry.getKey())) {
			continue;
		}

		final Object key = convertIfNecessary(entry.getKey(), keyType);
		final VPackSlice value = entry.getValue();

		map.put(key, readInternal(valueType, value));
	}

	return map;
}
 
Example 2
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
protected <T> ResponseDeserializer<DocumentCreateEntity<T>> insertDocumentResponseDeserializer(
        final T value, final DocumentCreateOptions options) {
    return response -> {
        final VPackSlice body = response.getBody();
        final DocumentCreateEntity<T> doc = util().deserialize(body, DocumentCreateEntity.class);
        final VPackSlice newDoc = body.get(NEW);
        if (newDoc.isObject()) {
            doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, value.getClass()));
        }
        final VPackSlice oldDoc = body.get(OLD);
        if (oldDoc.isObject()) {
            doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, value.getClass()));
        }
        if (options == null || Boolean.TRUE != options.getSilent()) {
            final Map<DocumentField.Type, String> values = new HashMap<>();
            values.put(DocumentField.Type.ID, doc.getId());
            values.put(DocumentField.Type.KEY, doc.getKey());
            values.put(DocumentField.Type.REV, doc.getRev());
            executor.documentCache().setValues(value, values);
        }
        return doc;
    };
}
 
Example 3
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
protected <T> ResponseDeserializer<DocumentUpdateEntity<T>> replaceDocumentResponseDeserializer(
        final T value, final DocumentReplaceOptions options) {
    return response -> {
        final VPackSlice body = response.getBody();
        final DocumentUpdateEntity<T> doc = util().deserialize(body, DocumentUpdateEntity.class);
        final VPackSlice newDoc = body.get(NEW);
        if (newDoc.isObject()) {
            doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, value.getClass()));
        }
        final VPackSlice oldDoc = body.get(OLD);
        if (oldDoc.isObject()) {
            doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, value.getClass()));
        }
        if (options == null || Boolean.TRUE != options.getSilent()) {
            final Map<DocumentField.Type, String> values = new HashMap<>();
            values.put(DocumentField.Type.REV, doc.getRev());
            executor.documentCache().setValues(value, values);
        }
        return doc;
    };
}
 
Example 4
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
protected <T, U> ResponseDeserializer<DocumentUpdateEntity<U>> updateDocumentResponseDeserializer(
        final T value, final DocumentUpdateOptions options, final Class<U> returnType) {
    return response -> {
        final VPackSlice body = response.getBody();
        final DocumentUpdateEntity<U> doc = util().deserialize(body, DocumentUpdateEntity.class);
        final VPackSlice newDoc = body.get(NEW);
        if (newDoc.isObject()) {
            doc.setNew(util(Serializer.CUSTOM).deserialize(newDoc, returnType));
        }
        final VPackSlice oldDoc = body.get(OLD);
        if (oldDoc.isObject()) {
            doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, returnType));
        }
        if (options == null || Boolean.TRUE != options.getSilent()) {
            final Map<DocumentField.Type, String> values = new HashMap<>();
            values.put(DocumentField.Type.REV, doc.getRev());
            executor.documentCache().setValues(value, values);
        }
        return doc;
    };
}
 
Example 5
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 6
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 7
Source File: InternalArangoCollection.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
protected <T> ResponseDeserializer<DocumentDeleteEntity<T>> deleteDocumentResponseDeserializer(
        final Class<T> type) {
    return response -> {
        final VPackSlice body = response.getBody();
        final DocumentDeleteEntity<T> doc = util().deserialize(body, DocumentDeleteEntity.class);
        final VPackSlice oldDoc = body.get(OLD);
        if (oldDoc.isObject()) {
            doc.setOld(util(Serializer.CUSTOM).deserialize(oldDoc, type));
        }
        return doc;
    };
}
 
Example 8
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 9
Source File: InternalArangoDatabase.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
protected ResponseDeserializer<Integer> deleteAqlFunctionResponseDeserializer() {
    return response -> {
        // compatibility with ArangoDB < 3.4
        // https://www.arangodb.com/docs/stable/release-notes-upgrading-changes34.html
        Integer count = null;
        final VPackSlice body = response.getBody();
        if (body.isObject()) {
            final VPackSlice deletedCount = body.get("deletedCount");
            if (deletedCount.isInteger()) {
                count = deletedCount.getAsInt();
            }
        }
        return count;
    };
}
 
Example 10
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 11
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 12
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 13
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;
    };
}