Skip to content

Commit

Permalink
Let VertexAPI use simplified property serializer
Browse files Browse the repository at this point in the history
Implement #307

Change-Id: I7e47b75160ff240157aec30c1e9b4690a3412e4e
  • Loading branch information
Linary committed Jan 15, 2019
1 parent 28217e9 commit b278f28
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion hugegraph-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>0.32.0.0</Implementation-Version>
<Implementation-Version>0.34.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ public final class ApiVersion {
* [0.30] Issue-32: Change index create API to return indexLabel and task id
* [0.31] Issue-182: Support restore graph in restoring and merging mode
* [0.32] Issue-250: Keep depth and degree consistent for traverser api
*
* [0.34] Issue-307: Let VertexAPI use simplified property serializer
*/

// The second parameter of Version.of() is for IDE running without JAR
public static final Version VERSION = Version.of(ApiVersion.class, "0.32");
public static final Version VERSION = Version.of(ApiVersion.class, "0.34");

public static final void check() {
// Check version of hugegraph-core. Firstly do check from version 0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import org.apache.tinkerpop.shaded.jackson.core.JsonGenerator;
import org.apache.tinkerpop.shaded.jackson.core.JsonParser;
import org.apache.tinkerpop.shaded.jackson.databind.DeserializationContext;
import org.apache.tinkerpop.shaded.jackson.databind.JsonSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.SerializerProvider;
import org.apache.tinkerpop.shaded.jackson.databind.deser.std.StdDeserializer;
import org.apache.tinkerpop.shaded.jackson.databind.jsontype.TypeSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.ser.std.DateSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.ser.std.UUIDSerializer;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.backend.id.EdgeId;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.id.IdGenerator;
Expand All @@ -47,6 +49,7 @@
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.type.define.HugeKeys;

@SuppressWarnings("serial")
Expand Down Expand Up @@ -85,6 +88,8 @@ public class HugeGraphSONModule extends TinkerPopJacksonModule {

// HugeGraph shard serializer
TYPE_DEFINITIONS.put(Shard.class, "Shard");

TYPE_DEFINITIONS.put(HugeVertex.class, "HugeVertex");
}

public static void register(HugeGraphIoRegistry io) {
Expand Down Expand Up @@ -118,6 +123,54 @@ private HugeGraphSONModule() {
addSerializer(IndexLabel.class, new IndexLabelSerializer());

addSerializer(Shard.class, new ShardSerializer());

addSerializer(HugeVertex.class, new HugeVertexSerializer());
}

private static class HugeVertexSerializer extends StdSerializer<HugeVertex> {

public HugeVertexSerializer() {
super(HugeVertex.class);
}

@Override
public void serialize(HugeVertex vertex,
JsonGenerator jsonGenerator,
SerializerProvider provider)
throws IOException {
jsonGenerator.writeStartObject();

jsonGenerator.writeFieldName("id");
Id id = vertex.id();
if (id.number()) {
jsonGenerator.writeNumber(id.asLong());
} else {
jsonGenerator.writeString(id.asString());
}
jsonGenerator.writeStringField("label", vertex.label());
jsonGenerator.writeStringField("type", "vertex");
// Start write properties
jsonGenerator.writeFieldName("properties");
jsonGenerator.writeStartObject();
vertex.properties().forEachRemaining(vp -> {
String key = vp.key();
Object val = vp.value();
try {
jsonGenerator.writeFieldName(key);
JsonSerializer<Object> serializer =
provider.findValueSerializer(val.getClass());
serializer.serialize(val, jsonGenerator, provider);
} catch (IOException e) {
throw new HugeException(
"Failed to serialize property(%s: %s) " +
"for vertex '%s'", key, val, vertex);
}
});
// End wirte properties
jsonGenerator.writeEndObject();

jsonGenerator.writeEndObject();
}
}

@Override
Expand All @@ -141,7 +194,7 @@ public OptionalSerializer() {
@Override
public void serialize(Optional optional,
JsonGenerator jsonGenerator,
SerializerProvider serializer)
SerializerProvider provider)
throws IOException {
if (optional.isPresent()) {
jsonGenerator.writeObject(optional.get());
Expand All @@ -160,7 +213,7 @@ public IdSerializer(Class<T> clazz) {
@Override
public void serialize(T value,
JsonGenerator jsonGenerator,
SerializerProvider serializer)
SerializerProvider provider)
throws IOException {
if (value.number()) {
jsonGenerator.writeNumber(value.asLong());
Expand All @@ -172,11 +225,11 @@ public void serialize(T value,
@Override
public void serializeWithType(T value,
JsonGenerator jsonGenerator,
SerializerProvider serializers,
SerializerProvider provider,
TypeSerializer typeSer)
throws IOException {
typeSer.writeTypePrefixForScalar(value, jsonGenerator);
this.serialize(value, jsonGenerator, serializers);
this.serialize(value, jsonGenerator, provider);
typeSer.writeTypeSuffixForScalar(value, jsonGenerator);
}
}
Expand Down Expand Up @@ -229,7 +282,7 @@ public PropertyKeySerializer() {
@Override
public void serialize(PropertyKey pk,
JsonGenerator jsonGenerator,
SerializerProvider serializer)
SerializerProvider provider)
throws IOException {
writeEntry(jsonGenerator, schemaSerializer.writePropertyKey(pk));
}
Expand All @@ -245,7 +298,7 @@ public VertexLabelSerializer() {
@Override
public void serialize(VertexLabel vl,
JsonGenerator jsonGenerator,
SerializerProvider serializer)
SerializerProvider provider)
throws IOException {
writeEntry(jsonGenerator, schemaSerializer.writeVertexLabel(vl));
}
Expand All @@ -260,7 +313,7 @@ public EdgeLabelSerializer() {
@Override
public void serialize(EdgeLabel el,
JsonGenerator jsonGenerator,
SerializerProvider serializer)
SerializerProvider provider)
throws IOException {
writeEntry(jsonGenerator, schemaSerializer.writeEdgeLabel(el));
}
Expand All @@ -276,7 +329,7 @@ public IndexLabelSerializer() {
@Override
public void serialize(IndexLabel il,
JsonGenerator jsonGenerator,
SerializerProvider serializer)
SerializerProvider provider)
throws IOException {
writeEntry(jsonGenerator, schemaSerializer.writeIndexLabel(il));
}
Expand All @@ -290,7 +343,7 @@ public ShardSerializer() {

@Override
public void serialize(Shard shard, JsonGenerator jsonGenerator,
SerializerProvider serializer)
SerializerProvider provider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("start", shard.start());
Expand Down

0 comments on commit b278f28

Please sign in to comment.