com.arangodb.internal.velocypack.VPackDeserializers 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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.arangodb.entity.ArangoDBVersion;
import com.arangodb.entity.ArangoDBVersion.License;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.BaseEdgeDocument;
import com.arangodb.entity.CollectionStatus;
import com.arangodb.entity.CollectionType;
import com.arangodb.entity.LogLevel;
import com.arangodb.entity.Permissions;
import com.arangodb.entity.QueryExecutionState;
import com.arangodb.entity.ReplicationFactor;
import com.arangodb.entity.ViewEntity;
import com.arangodb.entity.ViewType;
import com.arangodb.entity.arangosearch.ArangoSearchProperties;
import com.arangodb.entity.arangosearch.ArangoSearchPropertiesEntity;
import com.arangodb.entity.arangosearch.CollectionLink;
import com.arangodb.entity.arangosearch.ConsolidationType;
import com.arangodb.entity.arangosearch.ConsolidationPolicy;
import com.arangodb.entity.arangosearch.FieldLink;
import com.arangodb.entity.arangosearch.StoreValuesType;
import com.arangodb.velocypack.VPackDeserializationContext;
import com.arangodb.velocypack.VPackDeserializer;
import com.arangodb.velocypack.VPackSlice;
import com.arangodb.velocypack.exception.VPackException;
import com.arangodb.velocystream.Response;
/**
* @author Mark Vollmary
*
*/
public class VPackDeserializers {
private static final Logger LOGGER = LoggerFactory.getLogger(VPackDeserializers.class);
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
public static final VPackDeserializer RESPONSE = new VPackDeserializer() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Response deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
final Response response = new Response();
response.setVersion(vpack.get(0).getAsInt());
response.setType(vpack.get(1).getAsInt());
response.setResponseCode(vpack.get(2).getAsInt());
if (vpack.size() > 3) {
response.setMeta((Map) context.deserialize(vpack.get(3), Map.class));
}
return response;
}
};
public static final VPackDeserializer COLLECTION_TYPE = new VPackDeserializer() {
@Override
public CollectionType deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return CollectionType.fromType(vpack.getAsInt());
}
};
public static final VPackDeserializer COLLECTION_STATUS = new VPackDeserializer() {
@Override
public CollectionStatus deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return CollectionStatus.fromStatus(vpack.getAsInt());
}
};
@SuppressWarnings("unchecked")
public static final VPackDeserializer BASE_DOCUMENT = new VPackDeserializer() {
@SuppressWarnings("rawtypes")
@Override
public BaseDocument deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return new BaseDocument((Map) context.deserialize(vpack, Map.class));
}
};
@SuppressWarnings("unchecked")
public static final VPackDeserializer BASE_EDGE_DOCUMENT = new VPackDeserializer() {
@SuppressWarnings("rawtypes")
@Override
public BaseEdgeDocument deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return new BaseEdgeDocument((Map) context.deserialize(vpack, Map.class));
}
};
public static final VPackDeserializer DATE_STRING = new VPackDeserializer() {
@Override
public Date deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
try {
return new SimpleDateFormat(DATE_TIME_FORMAT).parse(vpack.getAsString());
} catch (final ParseException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("got ParseException for date string: " + vpack.getAsString());
}
}
return null;
}
};
public static final VPackDeserializer LOG_LEVEL = new VPackDeserializer() {
@Override
public LogLevel deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return LogLevel.fromLevel(vpack.getAsInt());
}
};
public static final VPackDeserializer LICENSE = new VPackDeserializer() {
@Override
public License deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return License.valueOf(vpack.getAsString().toUpperCase());
}
};
public static final VPackDeserializer PERMISSIONS = new VPackDeserializer() {
@Override
public Permissions deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return Permissions.valueOf(vpack.getAsString().toUpperCase());
}
};
public static final VPackDeserializer QUERY_EXECUTION_STATE = new VPackDeserializer() {
@Override
public QueryExecutionState deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return QueryExecutionState.valueOf(vpack.getAsString().toUpperCase().replaceAll(" ", "_"));
}
};
public static final VPackDeserializer REPLICATION_FACTOR = new VPackDeserializer() {
@Override
public ReplicationFactor deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
final ReplicationFactor replicationFactor = new ReplicationFactor();
if (vpack.isString() && vpack.getAsString().equals("satellite")) {
replicationFactor.setSatellite(true);
} else {
replicationFactor.setReplicationFactor(vpack.getAsInt());
}
return replicationFactor;
}
};
public static final VPackDeserializer VIEW_TYPE = new VPackDeserializer() {
@Override
public ViewType deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
return "arangosearch".equals(vpack.getAsString()) ? ViewType.ARANGO_SEARCH
: ViewType.valueOf(vpack.getAsString().toUpperCase());
}
};
public static final VPackDeserializer ARANGO_SEARCH_PROPERTIES = new VPackDeserializer() {
@Override
public ArangoSearchProperties deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
final ArangoSearchProperties properties = new ArangoSearchProperties();
final VPackSlice consolidationIntervalMsec = vpack.get("consolidationIntervalMsec");
if (consolidationIntervalMsec.isInteger()) {
properties.setConsolidationIntervalMsec(consolidationIntervalMsec.getAsLong());
}
final VPackSlice cleanupIntervalStep = vpack.get("cleanupIntervalStep");
if (cleanupIntervalStep.isInteger()) {
properties.setCleanupIntervalStep(cleanupIntervalStep.getAsLong());
}
final VPackSlice consolidationPolicy = vpack.get("consolidationPolicy");
if (consolidationPolicy.isObject()) {
properties.setConsolidationPolicy(
(ConsolidationPolicy) context.deserialize(consolidationPolicy, ConsolidationPolicy.class));
}
final VPackSlice links = vpack.get("links");
if (links.isObject()) {
final Iterator> collectionIterator = links.objectIterator();
for (; collectionIterator.hasNext();) {
final Entry entry = collectionIterator.next();
final VPackSlice value = entry.getValue();
final CollectionLink link = CollectionLink.on(entry.getKey());
final VPackSlice analyzers = value.get("analyzers");
if (analyzers.isArray()) {
final Iterator 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> fieldsIterator = fields.objectIterator();
for (; fieldsIterator.hasNext();) {
link.fields(deserializeField(fieldsIterator.next()));
}
}
properties.addLink(link);
}
}
return properties;
}
};
protected static FieldLink deserializeField(final Entry field) {
final VPackSlice value = field.getValue();
final FieldLink link = FieldLink.on(field.getKey());
final VPackSlice analyzers = value.get("analyzers");
if (analyzers.isArray()) {
final Iterator 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> fieldsIterator = fields.objectIterator();
for (; fieldsIterator.hasNext();) {
link.fields(deserializeField(fieldsIterator.next()));
}
}
return link;
}
public static final VPackDeserializer ARANGO_SEARCH_PROPERTIES_ENTITY = new VPackDeserializer() {
@Override
public ArangoSearchPropertiesEntity deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
final ViewEntity entity = context.deserialize(vpack, ViewEntity.class);
final ArangoSearchProperties properties = context.deserialize(vpack, ArangoSearchProperties.class);
final ArangoSearchPropertiesEntity result = new ArangoSearchPropertiesEntity(entity.getId(),
entity.getName(), entity.getType(), properties);
return result;
}
};
public static final VPackDeserializer CONSOLIDATE = new VPackDeserializer() {
@Override
public ConsolidationPolicy deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
final VPackSlice type = vpack.get("type");
if (type.isString()) {
final ConsolidationPolicy consolidate = ConsolidationPolicy
.of(ConsolidationType.valueOf(type.getAsString().toUpperCase()));
final VPackSlice threshold = vpack.get("threshold");
if (threshold.isNumber()) {
consolidate.threshold(threshold.getAsDouble());
}
final VPackSlice segmentThreshold = vpack.get("segmentThreshold");
if (segmentThreshold.isInteger()) {
consolidate.segmentThreshold(segmentThreshold.getAsLong());
}
return consolidate;
}
return null;
}
};
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy