com.arangodb.internal.velocypack.VPackSerializers 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.velocypack;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.BaseEdgeDocument;
import com.arangodb.entity.CollectionType;
import com.arangodb.entity.DocumentField;
import com.arangodb.entity.LogLevel;
import com.arangodb.entity.Permissions;
import com.arangodb.entity.ReplicationFactor;
import com.arangodb.entity.ViewType;
import com.arangodb.entity.arangosearch.ArangoSearchProperties;
import com.arangodb.entity.arangosearch.CollectionLink;
import com.arangodb.entity.arangosearch.ConsolidateThreshold;
import com.arangodb.entity.arangosearch.FieldLink;
import com.arangodb.entity.arangosearch.StoreValuesType;
import com.arangodb.internal.velocystream.internal.AuthenticationRequest;
import com.arangodb.model.TraversalOptions;
import com.arangodb.model.TraversalOptions.Order;
import com.arangodb.velocypack.VPackBuilder;
import com.arangodb.velocypack.VPackSerializationContext;
import com.arangodb.velocypack.VPackSerializer;
import com.arangodb.velocypack.ValueType;
import com.arangodb.velocypack.exception.VPackException;
import com.arangodb.velocystream.Request;
/**
* @author Mark Vollmary
*
*/
public class VPackSerializers {
public static final VPackSerializer REQUEST = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final Request value,
final VPackSerializationContext context) throws VPackException {
builder.add(attribute, ValueType.ARRAY);
builder.add(value.getVersion());
builder.add(value.getType());
builder.add(value.getDatabase());
builder.add(value.getRequestType().getType());
builder.add(value.getRequest());
builder.add(ValueType.OBJECT);
for (final Entry entry : value.getQueryParam().entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
builder.close();
builder.add(ValueType.OBJECT);
for (final Entry entry : value.getHeaderParam().entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
builder.close();
builder.close();
}
};
public static final VPackSerializer AUTH_REQUEST = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final AuthenticationRequest value,
final VPackSerializationContext context) throws VPackException {
builder.add(attribute, ValueType.ARRAY);
builder.add(value.getVersion());
builder.add(value.getType());
builder.add(value.getEncryption());
builder.add(value.getUser());
builder.add(value.getPassword());
builder.close();
}
};
public static final VPackSerializer COLLECTION_TYPE = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final CollectionType value,
final VPackSerializationContext context) throws VPackException {
builder.add(attribute, value.getType());
}
};
public static final VPackSerializer BASE_DOCUMENT = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final BaseDocument value,
final VPackSerializationContext context) throws VPackException {
final Map doc = new HashMap();
doc.putAll(value.getProperties());
doc.put(DocumentField.Type.ID.getSerializeName(), value.getId());
doc.put(DocumentField.Type.KEY.getSerializeName(), value.getKey());
doc.put(DocumentField.Type.REV.getSerializeName(), value.getRevision());
context.serialize(builder, attribute, doc);
}
};
public static final VPackSerializer BASE_EDGE_DOCUMENT = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final BaseEdgeDocument value,
final VPackSerializationContext context) throws VPackException {
final Map doc = new HashMap();
doc.putAll(value.getProperties());
doc.put(DocumentField.Type.ID.getSerializeName(), value.getId());
doc.put(DocumentField.Type.KEY.getSerializeName(), value.getKey());
doc.put(DocumentField.Type.REV.getSerializeName(), value.getRevision());
doc.put(DocumentField.Type.FROM.getSerializeName(), value.getFrom());
doc.put(DocumentField.Type.TO.getSerializeName(), value.getTo());
context.serialize(builder, attribute, doc);
}
};
public static final VPackSerializer TRAVERSAL_ORDER = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final Order value,
final VPackSerializationContext context) throws VPackException {
if (TraversalOptions.Order.preorder_expander == value) {
builder.add(attribute, "preorder-expander");
} else {
builder.add(attribute, value.name());
}
}
};
public static final VPackSerializer LOG_LEVEL = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final LogLevel value,
final VPackSerializationContext context) throws VPackException {
builder.add(attribute, value.getLevel());
}
};
public static final VPackSerializer PERMISSIONS = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final Permissions value,
final VPackSerializationContext context) throws VPackException {
builder.add(attribute, value.toString().toLowerCase());
}
};
public static final VPackSerializer REPLICATION_FACTOR = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final ReplicationFactor value,
final VPackSerializationContext context) throws VPackException {
final Boolean satellite = value.getSatellite();
if (Boolean.TRUE == satellite) {
builder.add(attribute, "satellite");
} else if (value.getReplicationFactor() != null) {
builder.add(attribute, value.getReplicationFactor());
}
}
};
public static final VPackSerializer VIEW_TYPE = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final ViewType value,
final VPackSerializationContext context) throws VPackException {
final String type = value == ViewType.ARANGO_SEARCH ? "arangosearch" : value.name().toLowerCase();
builder.add(attribute, type);
}
};
public static final VPackSerializer ARANGO_SEARCH_PROPERTIES = new VPackSerializer() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final ArangoSearchProperties value,
final VPackSerializationContext context) throws VPackException {
final boolean wrap = !attribute.startsWith("_");
if (wrap) {
builder.add("properties", ValueType.OBJECT);
}
final String locale = value.getLocale();
if (locale != null) {
builder.add("locale", locale);
}
final Long commitIntervalMsec = value.getCommitIntervalMsec();
final Long cleanupIntervalStep = value.getCleanupIntervalStep();
final Collection thresholds = value.getThresholds();
if (commitIntervalMsec != null || cleanupIntervalStep != null || !thresholds.isEmpty()) {
builder.add("commit", ValueType.OBJECT);
if (commitIntervalMsec != null) {
builder.add("commitIntervalMsec", commitIntervalMsec);
}
if (cleanupIntervalStep != null) {
builder.add("cleanupIntervalStep", cleanupIntervalStep);
}
if (!thresholds.isEmpty()) {
builder.add("consolidate", ValueType.OBJECT);
for (final ConsolidateThreshold consolidateThreshold : thresholds) {
builder.add(consolidateThreshold.getType().name().toLowerCase(), ValueType.OBJECT);
final Double threshold = consolidateThreshold.getThreshold();
if (threshold != null) {
builder.add("threshold", threshold);
}
final Long segmentThreshold = consolidateThreshold.getSegmentThreshold();
if (segmentThreshold != null) {
builder.add("segmentThreshold", segmentThreshold);
}
builder.close();
}
builder.close();
}
builder.close();
}
final Collection links = value.getLinks();
if (!links.isEmpty()) {
builder.add("links", ValueType.OBJECT);
for (final CollectionLink collectionLink : links) {
builder.add(collectionLink.getName(), ValueType.OBJECT);
final Collection analyzers = collectionLink.getAnalyzers();
if (!analyzers.isEmpty()) {
builder.add("analyzers", ValueType.ARRAY);
for (final String analyzer : analyzers) {
builder.add(analyzer);
}
builder.close();
}
final Boolean includeAllFields = collectionLink.getIncludeAllFields();
if (includeAllFields != null) {
builder.add("includeAllFields", includeAllFields);
}
final Boolean trackListPositions = collectionLink.getTrackListPositions();
if (trackListPositions != null) {
builder.add("trackListPositions", trackListPositions);
}
final StoreValuesType storeValues = collectionLink.getStoreValues();
if (storeValues != null) {
builder.add("storeValues", storeValues.name().toLowerCase());
}
serializeFieldLinks(builder, collectionLink.getFields());
builder.close();
}
builder.close();
}
if (wrap) {
builder.close();
}
}
};
private static void serializeFieldLinks(final VPackBuilder builder, final Collection links) {
if (!links.isEmpty()) {
builder.add("fields", ValueType.OBJECT);
for (final FieldLink fieldLink : links) {
builder.add(fieldLink.getName(), ValueType.OBJECT);
final Collection 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();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy