org.apache.hugegraph.serializer.JsonSerializer Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.
*/
package org.apache.hugegraph.serializer;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.CloseableIterator;
import org.apache.hugegraph.HugeException;
import org.apache.hugegraph.api.API;
import org.apache.hugegraph.auth.SchemaDefine.AuthElement;
import org.apache.hugegraph.backend.id.Id;
import org.apache.hugegraph.backend.page.PageInfo;
import org.apache.hugegraph.iterator.Metadatable;
import org.apache.hugegraph.schema.EdgeLabel;
import org.apache.hugegraph.schema.IndexLabel;
import org.apache.hugegraph.schema.PropertyKey;
import org.apache.hugegraph.schema.SchemaElement;
import org.apache.hugegraph.schema.VertexLabel;
import org.apache.hugegraph.traversal.algorithm.CustomizedCrosspointsTraverser.CrosspointsPaths;
import org.apache.hugegraph.traversal.algorithm.FusiformSimilarityTraverser.SimilarsMap;
import org.apache.hugegraph.traversal.algorithm.HugeTraverser;
import org.apache.hugegraph.traversal.algorithm.SingleSourceShortestPathTraverser.NodeWithWeight;
import org.apache.hugegraph.traversal.algorithm.SingleSourceShortestPathTraverser.WeightedPaths;
import org.apache.hugegraph.traversal.optimize.TraversalUtil;
import org.apache.hugegraph.util.JsonUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
public class JsonSerializer implements Serializer {
private static final int LBUF_SIZE = 1024;
private static JsonSerializer INSTANCE = new JsonSerializer();
private JsonSerializer() {
}
public static JsonSerializer instance() {
return INSTANCE;
}
@Override
public String writeMap(Map, ?> map) {
return JsonUtil.toJson(map);
}
@Override
public String writeList(String label, Collection> list) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream(LBUF_SIZE)) {
out.write(String.format("{\"%s\": ", label).getBytes(API.CHARSET));
out.write(JsonUtil.toJson(list).getBytes(API.CHARSET));
out.write("}".getBytes(API.CHARSET));
return out.toString(API.CHARSET);
} catch (Exception e) {
throw new HugeException("Failed to serialize %s", e, label);
}
}
private String writeIterator(String label, Iterator> iter,
boolean paging) {
// Early throw if needed
iter.hasNext();
// Serialize Iterator
try (ByteArrayOutputStream out = new ByteArrayOutputStream(LBUF_SIZE)) {
out.write("{".getBytes(API.CHARSET));
out.write(String.format("\"%s\":[", label).getBytes(API.CHARSET));
// Write data
boolean first = true;
while (iter.hasNext()) {
if (!first) {
out.write(",".getBytes(API.CHARSET));
} else {
first = false;
}
out.write(JsonUtil.toJson(iter.next()).getBytes(API.CHARSET));
}
out.write("]".getBytes(API.CHARSET));
// Write page
if (paging) {
String page;
if (iter instanceof GraphTraversal, ?>) {
page = TraversalUtil.page((GraphTraversal, ?>) iter);
} else if (iter instanceof Metadatable) {
page = PageInfo.pageInfo(iter);
} else {
throw new HugeException("Invalid paging iterator: %s",
iter.getClass());
}
if (page != null) {
page = String.format(",\"page\": \"%s\"", page);
} else {
page = ",\"page\": null";
}
out.write(page.getBytes(API.CHARSET));
}
out.write("}".getBytes(API.CHARSET));
return out.toString(API.CHARSET);
} catch (HugeException e) {
throw e;
} catch (Exception e) {
throw new HugeException("Failed to serialize %s", e, label);
} finally {
try {
CloseableIterator.closeIterator(iter);
} catch (Exception e) {
throw new HugeException("Failed to close for %s", e, label);
}
}
}
@Override
public String writePropertyKey(PropertyKey propertyKey) {
return JsonUtil.toJson(propertyKey);
}
@Override
public String writeTaskWithSchema(
SchemaElement.TaskWithSchema taskWithSchema) {
StringBuilder builder = new StringBuilder();
long id = taskWithSchema.task() == null ?
0L : taskWithSchema.task().asLong();
SchemaElement schemaElement = taskWithSchema.schemaElement();
String type;
String schema;
if (schemaElement instanceof PropertyKey) {
type = "property_key";
schema = this.writePropertyKey((PropertyKey) schemaElement);
} else if (schemaElement instanceof IndexLabel) {
type = "index_label";
schema = this.writeIndexlabel((IndexLabel) schemaElement);
} else {
throw new HugeException("Invalid schema element '%s' in " +
"TaskWithSchema, only support " +
"[PropertyKey, IndexLabel]", schemaElement);
}
return builder.append("{\"").append(type).append("\": ")
.append(schema)
.append(", \"task_id\": ").append(id).append("}")
.toString();
}
@Override
public String writePropertyKeys(List propertyKeys) {
return writeList("propertykeys", propertyKeys);
}
@Override
public String writeVertexLabel(VertexLabel vertexLabel) {
return JsonUtil.toJson(vertexLabel);
}
@Override
public String writeVertexLabels(List vertexLabels) {
return writeList("vertexlabels", vertexLabels);
}
@Override
public String writeEdgeLabel(EdgeLabel edgeLabel) {
return JsonUtil.toJson(edgeLabel);
}
@Override
public String writeEdgeLabels(List edgeLabels) {
return writeList("edgelabels", edgeLabels);
}
@Override
public String writeIndexlabel(IndexLabel indexLabel) {
return JsonUtil.toJson(indexLabel);
}
@Override
public String writeIndexlabels(List indexLabels) {
return writeList("indexlabels", indexLabels);
}
@Override
public String writeVertex(Vertex vertex) {
return JsonUtil.toJson(vertex);
}
@Override
public String writeVertices(Iterator vertices, boolean paging) {
return this.writeIterator("vertices", vertices, paging);
}
@Override
public String writeEdge(Edge edge) {
return JsonUtil.toJson(edge);
}
@Override
public String writeEdges(Iterator edges, boolean paging) {
return this.writeIterator("edges", edges, paging);
}
@Override
public String writeIds(List ids) {
return JsonUtil.toJson(ids);
}
@Override
public String writeAuthElement(AuthElement elem) {
return this.writeMap(elem.asMap());
}
@Override
public String writeAuthElements(String label,
List elems) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy