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 java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.arangodb.ArangoDBException;
import com.arangodb.entity.DocumentCreateEntity;
import com.arangodb.entity.DocumentDeleteEntity;
import com.arangodb.entity.DocumentField;
import com.arangodb.entity.DocumentUpdateEntity;
import com.arangodb.entity.ErrorEntity;
import com.arangodb.entity.IndexEntity;
import com.arangodb.entity.MultiDocumentEntity;
import com.arangodb.entity.Permissions;
import com.arangodb.internal.ArangoExecutor.ResponseDeserializer;
import com.arangodb.internal.util.ArangoSerializationFactory.Serializer;
import com.arangodb.internal.util.DocumentUtil;
import com.arangodb.model.CollectionPropertiesOptions;
import com.arangodb.model.CollectionRenameOptions;
import com.arangodb.model.DocumentCreateOptions;
import com.arangodb.model.DocumentDeleteOptions;
import com.arangodb.model.DocumentExistsOptions;
import com.arangodb.model.DocumentImportOptions;
import com.arangodb.model.DocumentReadOptions;
import com.arangodb.model.DocumentReplaceOptions;
import com.arangodb.model.DocumentUpdateOptions;
import com.arangodb.model.FulltextIndexOptions;
import com.arangodb.model.GeoIndexOptions;
import com.arangodb.model.HashIndexOptions;
import com.arangodb.model.ImportType;
import com.arangodb.model.OptionsBuilder;
import com.arangodb.model.PersistentIndexOptions;
import com.arangodb.model.SkiplistIndexOptions;
import com.arangodb.model.UserAccessOptions;
import com.arangodb.util.ArangoSerializer;
import com.arangodb.velocypack.Type;
import com.arangodb.velocypack.VPackSlice;
import com.arangodb.velocypack.exception.VPackException;
import com.arangodb.velocystream.Request;
import com.arangodb.velocystream.RequestType;
import com.arangodb.velocystream.Response;
/**
* @author Mark Vollmary
*
*/
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 OLD = "old";
private static final String SILENT = "silent";
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.setBody(util(Serializer.CUSTOM).serialize(value));
return request;
}
protected ResponseDeserializer> insertDocumentResponseDeserializer(
final T value,
final DocumentCreateOptions options) {
return new ResponseDeserializer>() {
@SuppressWarnings("unchecked")
@Override
public DocumentCreateEntity deserialize(final Response response) throws VPackException {
final VPackSlice body = response.getBody();
final DocumentCreateEntity doc = util().deserialize(body, DocumentCreateEntity.class);
final VPackSlice newDoc = body.get(NEW);
if (newDoc.isObject()) {
doc.setNew((T) util(Serializer.CUSTOM).deserialize(newDoc, value.getClass()));
}
final VPackSlice oldDoc = body.get(OLD);
if (oldDoc.isObject()) {
doc.setOld((T) 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(SILENT, params.getSilent());
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 new ResponseDeserializer>>() {
@Override
public MultiDocumentEntity> deserialize(final Response response)
throws VPackException {
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