com.arangodb.internal.InternalArangoCollection Maven / Gradle / Ivy
/*
* DISCLAIMER
*
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright holder is ArangoDB GmbH, Cologne, Germany
*/
package com.arangodb.internal;
import com.arangodb.ArangoDBException;
import com.arangodb.entity.*;
import com.arangodb.internal.ArangoExecutor.ResponseDeserializer;
import com.arangodb.internal.util.ArangoSerializationFactory.Serializer;
import com.arangodb.internal.util.DocumentUtil;
import com.arangodb.internal.util.RequestUtils;
import com.arangodb.model.*;
import com.arangodb.util.ArangoSerializer;
import com.arangodb.velocypack.Type;
import com.arangodb.velocypack.VPackSlice;
import com.arangodb.velocystream.Request;
import com.arangodb.velocystream.RequestType;
import java.util.*;
/**
* @author Mark Vollmary
* @author Michele Rastelli
*/
public abstract class InternalArangoCollection, D extends InternalArangoDatabase, E extends ArangoExecutor>
extends ArangoExecuteable {
private static final String COLLECTION = "collection";
protected static final String PATH_API_COLLECTION = "/_api/collection";
private static final String PATH_API_DOCUMENT = "/_api/document";
private static final String PATH_API_INDEX = "/_api/index";
private static final String PATH_API_IMPORT = "/_api/import";
private static final String PATH_API_USER = "/_api/user";
private static final String MERGE_OBJECTS = "mergeObjects";
private static final String IGNORE_REVS = "ignoreRevs";
private static final String RETURN_NEW = "returnNew";
private static final String NEW = "new";
private static final String RETURN_OLD = "returnOld";
private static final String OVERWRITE = "overwrite";
private static final String OVERWRITE_MODE = "overwriteMode";
private static final String OLD = "old";
private static final String SILENT = "silent";
private static final String TRANSACTION_ID = "x-arango-trx-id";
private final D db;
protected volatile String name;
protected InternalArangoCollection(final D db, final String name) {
super(db.executor, db.util, db.context);
this.db = db;
this.name = name;
}
public D db() {
return db;
}
public String name() {
return name;
}
protected Request insertDocumentRequest(final T value, final DocumentCreateOptions options) {
final Request request = request(db.name(), RequestType.POST, PATH_API_DOCUMENT, name);
final DocumentCreateOptions params = (options != null ? options : new DocumentCreateOptions());
request.putQueryParam(ArangoRequestParam.WAIT_FOR_SYNC, params.getWaitForSync());
request.putQueryParam(RETURN_NEW, params.getReturnNew());
request.putQueryParam(RETURN_OLD, params.getReturnOld());
request.putQueryParam(SILENT, params.getSilent());
request.putQueryParam(OVERWRITE, params.getOverwrite());
request.putQueryParam(OVERWRITE_MODE, params.getOverwriteMode() != null ? params.getOverwriteMode().getValue() : null);
request.putQueryParam(MERGE_OBJECTS, params.getMergeObjects());
request.putHeaderParam(TRANSACTION_ID, params.getStreamTransactionId());
request.setBody(util(Serializer.CUSTOM).serialize(value));
return request;
}
protected ResponseDeserializer> insertDocumentResponseDeserializer(
final T value, final DocumentCreateOptions options) {
return response -> {
final VPackSlice body = response.getBody();
final DocumentCreateEntity 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 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;
};
}
protected Request insertDocumentsRequest(final Collection values, final DocumentCreateOptions params) {
final Request request = request(db.name(), RequestType.POST, PATH_API_DOCUMENT, name);
request.putQueryParam(ArangoRequestParam.WAIT_FOR_SYNC, params.getWaitForSync());
request.putQueryParam(RETURN_NEW, params.getReturnNew());
request.putQueryParam(RETURN_OLD, params.getReturnOld());
request.putQueryParam(SILENT, params.getSilent());
request.putQueryParam(OVERWRITE, params.getOverwrite());
request.putQueryParam(OVERWRITE_MODE, params.getOverwriteMode() != null ? params.getOverwriteMode().getValue() : null);
request.putQueryParam(MERGE_OBJECTS, params.getMergeObjects());
request.putHeaderParam(TRANSACTION_ID, params.getStreamTransactionId());
request.setBody(util(Serializer.CUSTOM)
.serialize(values, new ArangoSerializer.Options().serializeNullValues(false).stringAsJson(true)));
return request;
}
@SuppressWarnings("unchecked")
protected ResponseDeserializer>> insertDocumentsResponseDeserializer(
final Collection values, final DocumentCreateOptions params) {
return response -> {
Class type = null;
if (Boolean.TRUE == params.getReturnNew()) {
if (!values.isEmpty()) {
type = (Class) values.iterator().next().getClass();
}
}
final MultiDocumentEntity> multiDocument = new MultiDocumentEntity<>();
final Collection> docs = new ArrayList<>();
final Collection errors = new ArrayList<>();
final Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy