Skip to content

Commit

Permalink
Remove unsupported types in GraphSONv4 from core.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenhuuu committed Oct 30, 2024
1 parent 73b34b3 commit 0e96e3d
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 835 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,70 +158,15 @@ static final class GraphSONModuleV4 extends GraphSONModule {
put(Set.class, "Set");

// TinkerPop Graph objects
put(Lambda.class, "Lambda");
put(Vertex.class, "Vertex");
put(Edge.class, "Edge");
put(Property.class, "Property");
put(Path.class, "Path");
put(VertexProperty.class, "VertexProperty");
put(Metrics.class, "Metrics");
put(TraversalMetrics.class, "TraversalMetrics");
put(TraversalExplanation.class, "TraversalExplanation");
put(Tree.class, "Tree");
put(AndP.class, "P");
put(OrP.class, "P");
put(P.class, "P");
put(TextP.class, "TextP");
Stream.of(
VertexProperty.Cardinality.class,
Column.class,
Direction.class,
DT.class,
Merge.class,
Operator.class,
Order.class,
Pop.class,
SackFunctions.Barrier.class,
Pick.class,
Scope.class,
T.class).forEach(e -> put(e, e.getSimpleName()));
Arrays.asList(
ConnectiveStrategy.class,
ElementIdStrategy.class,
EventStrategy.class,
HaltedTraverserStrategy.class,
PartitionStrategy.class,
SubgraphStrategy.class,
SeedStrategy.class,
LazyBarrierStrategy.class,
MatchAlgorithmStrategy.class,
AdjacentToIncidentStrategy.class,
ByModulatorOptimizationStrategy.class,
ProductiveByStrategy.class,
CountStrategy.class,
FilterRankingStrategy.class,
IdentityRemovalStrategy.class,
IncidentToAdjacentStrategy.class,
InlineFilterStrategy.class,
MatchPredicateStrategy.class,
OrderLimitStrategy.class,
OptionsStrategy.class,
PathProcessorStrategy.class,
PathRetractionStrategy.class,
RepeatUnrollStrategy.class,
ComputerVerificationStrategy.class,
LambdaRestrictionStrategy.class,
ReadOnlyStrategy.class,
StandardVerificationStrategy.class,
EarlyLimitStrategy.class,
EdgeLabelVerificationStrategy.class,
ReservedKeysVerificationStrategy.class,
//
GraphFilterStrategy.class,
VertexProgramStrategy.class
).forEach(strategy -> put(strategy, strategy.getSimpleName()));

GraphSONModule.tryLoadSparqlStrategy().ifPresent(s -> put(s, s.getSimpleName()));
}});

/**
Expand All @@ -237,9 +182,6 @@ protected GraphSONModuleV4(final boolean normalize, final TypeInfo typeInfo) {
addSerializer(Vertex.class, new GraphSONSerializersV4.VertexJacksonSerializer(normalize, typeInfo));
addSerializer(VertexProperty.class, new GraphSONSerializersV4.VertexPropertyJacksonSerializer(normalize, true));
addSerializer(Property.class, new GraphSONSerializersV4.PropertyJacksonSerializer());
addSerializer(Metrics.class, new GraphSONSerializersV4.MetricsJacksonSerializer());
addSerializer(TraversalMetrics.class, new GraphSONSerializersV4.TraversalMetricsJacksonSerializer());
addSerializer(TraversalExplanation.class, new GraphSONSerializersV4.TraversalExplanationJacksonSerializer());
addSerializer(Path.class, new GraphSONSerializersV4.PathJacksonSerializer());
addSerializer(DirectionalStarGraph.class, new StarGraphGraphSONSerializerV4(normalize));
addSerializer(Tree.class, new GraphSONSerializersV4.TreeJacksonSerializer());
Expand All @@ -258,22 +200,9 @@ protected GraphSONModuleV4(final boolean normalize, final TypeInfo typeInfo) {

// traversal
addSerializer(BulkSet.class, new TraversalSerializersV4.BulkSetJacksonSerializer());
addSerializer(Traversal.class, new TraversalSerializersV4.TraversalJacksonSerializer());
Stream.of(VertexProperty.Cardinality.class,
Column.class,
Stream.of(
Direction.class,
DT.class,
Merge.class,
Operator.class,
Order.class,
Pop.class,
SackFunctions.Barrier.class,
Scope.class,
Pick.class,
T.class).forEach(e -> addSerializer(e, new TraversalSerializersV4.EnumJacksonSerializer()));
addSerializer(P.class, new TraversalSerializersV4.PJacksonSerializer());
addSerializer(Lambda.class, new TraversalSerializersV4.LambdaJacksonSerializer());
addSerializer(TraversalStrategy.class, new TraversalSerializersV4.TraversalStrategyJacksonSerializer());

/////////////////////// DESERIALIZERS ////////////////////////////

Expand All @@ -282,10 +211,7 @@ protected GraphSONModuleV4(final boolean normalize, final TypeInfo typeInfo) {
addDeserializer(Edge.class, new GraphSONSerializersV4.EdgeJacksonDeserializer());
addDeserializer(Property.class, new GraphSONSerializersV4.PropertyJacksonDeserializer());
addDeserializer(Path.class, new GraphSONSerializersV4.PathJacksonDeserializer());
addDeserializer(TraversalExplanation.class, new GraphSONSerializersV4.TraversalExplanationJacksonDeserializer());
addDeserializer(VertexProperty.class, new GraphSONSerializersV4.VertexPropertyJacksonDeserializer());
addDeserializer(Metrics.class, new GraphSONSerializersV4.MetricsJacksonDeserializer());
addDeserializer(TraversalMetrics.class, new GraphSONSerializersV4.TraversalMetricsJacksonDeserializer());
addDeserializer(Tree.class, new GraphSONSerializersV4.TreeJacksonDeserializer());

// java.util - use the standard jackson serializers for collections when types aren't embedded
Expand All @@ -300,58 +226,9 @@ protected GraphSONModuleV4(final boolean normalize, final TypeInfo typeInfo) {
addDeserializer(Double.class, new GraphSONSerializersV4.DoubleJacksonDeserializer());

// traversal
Stream.of(VertexProperty.Cardinality.values(),
Column.values(),
Stream.of(
Direction.values(),
DT.values(),
Merge.values(),
Operator.values(),
Order.values(),
Pop.values(),
SackFunctions.Barrier.values(),
Scope.values(),
Pick.values(),
T.values()).flatMap(Stream::of).forEach(e -> addDeserializer(e.getClass(), new TraversalSerializersV4.EnumJacksonDeserializer(e.getDeclaringClass())));
addDeserializer(P.class, new TraversalSerializersV4.PJacksonDeserializer());
addDeserializer(TextP.class, new TraversalSerializersV4.TextPJacksonDeserializer());
addDeserializer(Lambda.class, new TraversalSerializersV4.LambdaJacksonDeserializer());
Arrays.asList(
ConnectiveStrategy.class,
ElementIdStrategy.class,
EventStrategy.class,
HaltedTraverserStrategy.class,
PartitionStrategy.class,
SubgraphStrategy.class,
SeedStrategy.class,
LazyBarrierStrategy.class,
MatchAlgorithmStrategy.class,
AdjacentToIncidentStrategy.class,
ByModulatorOptimizationStrategy.class,
ProductiveByStrategy.class,
CountStrategy.class,
FilterRankingStrategy.class,
IdentityRemovalStrategy.class,
IncidentToAdjacentStrategy.class,
InlineFilterStrategy.class,
MatchPredicateStrategy.class,
OrderLimitStrategy.class,
OptionsStrategy.class,
PathProcessorStrategy.class,
PathRetractionStrategy.class,
RepeatUnrollStrategy.class,
ComputerVerificationStrategy.class,
LambdaRestrictionStrategy.class,
ReadOnlyStrategy.class,
StandardVerificationStrategy.class,
EarlyLimitStrategy.class,
EdgeLabelVerificationStrategy.class,
ReservedKeysVerificationStrategy.class,
//
GraphFilterStrategy.class,
VertexProgramStrategy.class
).forEach(strategy -> addDeserializer(strategy, new TraversalSerializersV4.TraversalStrategyProxyJacksonDeserializer(strategy)));

GraphSONModule.tryLoadSparqlStrategy().ifPresent(s -> addDeserializer(s, new TraversalSerializersV4.TraversalStrategyProxyJacksonDeserializer(s)));
}

public static Builder build() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,44 +326,6 @@ public void serialize(final Tree tree, final JsonGenerator jsonGenerator, final
}
}

final static class TraversalExplanationJacksonSerializer extends StdScalarSerializer<TraversalExplanation> {
public TraversalExplanationJacksonSerializer() {
super(TraversalExplanation.class);
}

@Override
public void serialize(final TraversalExplanation traversalExplanation, final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider) throws IOException {
final Map<String, Object> m = new HashMap<>();
m.put(GraphSONTokens.ORIGINAL, getStepsAsList(traversalExplanation.getOriginalTraversal()));

final List<Pair<TraversalStrategy, Traversal.Admin<?, ?>>> strategyTraversals = traversalExplanation.getStrategyTraversals();

final List<Map<String, Object>> intermediates = new ArrayList<>();
for (final Pair<TraversalStrategy, Traversal.Admin<?, ?>> pair : strategyTraversals) {
final Map<String, Object> intermediate = new HashMap<>();
intermediate.put(GraphSONTokens.STRATEGY, pair.getValue0().toString());
intermediate.put(GraphSONTokens.CATEGORY, pair.getValue0().getTraversalCategory().getSimpleName());
intermediate.put(GraphSONTokens.TRAVERSAL, getStepsAsList(pair.getValue1()));
intermediates.add(intermediate);
}
m.put(GraphSONTokens.INTERMEDIATE, intermediates);

if (strategyTraversals.isEmpty())
m.put(GraphSONTokens.FINAL, getStepsAsList(traversalExplanation.getOriginalTraversal()));
else
m.put(GraphSONTokens.FINAL, getStepsAsList(strategyTraversals.get(strategyTraversals.size() - 1).getValue1()));

jsonGenerator.writeObject(m);
}

private List<String> getStepsAsList(final Traversal.Admin<?, ?> t) {
final List<String> steps = new ArrayList<>();
t.getSteps().iterator().forEachRemaining(s -> steps.add(s.toString()));
return steps;
}
}

final static class IntegerGraphSONSerializer extends StdScalarSerializer<Integer> {
public IntegerGraphSONSerializer() {
super(Integer.class);
Expand All @@ -388,52 +350,6 @@ public void serialize(final Double doubleValue, final JsonGenerator jsonGenerato
}
}

final static class TraversalMetricsJacksonSerializer extends StdScalarSerializer<TraversalMetrics> {
public TraversalMetricsJacksonSerializer() {
super(TraversalMetrics.class);
}

@Override
public void serialize(final TraversalMetrics traversalMetrics, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
throws IOException {
// creation of the map enables all the fields to be properly written with their type if required
final Map<String, Object> m = new HashMap<>();
m.put(GraphSONTokens.DURATION, traversalMetrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d);
final List<Metrics> metrics = new ArrayList<>();
metrics.addAll(traversalMetrics.getMetrics());
m.put(GraphSONTokens.METRICS, metrics);

jsonGenerator.writeObject(m);
}
}

final static class MetricsJacksonSerializer extends StdScalarSerializer<Metrics> {
public MetricsJacksonSerializer() {
super(Metrics.class);
}

@Override
public void serialize(final Metrics metrics, final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider) throws IOException {
final Map<String, Object> m = new HashMap<>();
m.put(GraphSONTokens.ID, metrics.getId());
m.put(GraphSONTokens.NAME, metrics.getName());
m.put(GraphSONTokens.COUNTS, metrics.getCounts());
m.put(GraphSONTokens.DURATION, metrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d);

if (!metrics.getAnnotations().isEmpty()) {
m.put(GraphSONTokens.ANNOTATIONS, metrics.getAnnotations());
}
if (!metrics.getNested().isEmpty()) {
final List<Metrics> nested = new ArrayList<>();
metrics.getNested().forEach(it -> nested.add(it));
m.put(GraphSONTokens.METRICS, nested);
}
jsonGenerator.writeObject(m);
}
}


/**
* Maps in the JVM can have {@link Object} as a key, but in JSON they must be a {@link String}.
*/
Expand Down Expand Up @@ -651,83 +567,6 @@ public boolean isCachable() {
}
}

static class TraversalExplanationJacksonDeserializer extends StdDeserializer<TraversalExplanation> {
public TraversalExplanationJacksonDeserializer() {
super(TraversalExplanation.class);
}

@Override
public TraversalExplanation deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
final Map<String, Object> explainData = deserializationContext.readValue(jsonParser, Map.class);
final String originalTraversal = explainData.get(GraphSONTokens.ORIGINAL).toString();
final List<Triplet<String, String, String>> intermediates = new ArrayList<>();
final List<Map<String,Object>> listMap = (List<Map<String,Object>>) explainData.get(GraphSONTokens.INTERMEDIATE);
for (Map<String,Object> m : listMap) {
intermediates.add(Triplet.with(m.get(GraphSONTokens.STRATEGY).toString(),
m.get(GraphSONTokens.CATEGORY).toString(),
m.get(GraphSONTokens.TRAVERSAL).toString()));
}

return new ImmutableExplanation(originalTraversal, intermediates);
}

@Override
public boolean isCachable() {
return true;
}
}

static class MetricsJacksonDeserializer extends StdDeserializer<Metrics> {
public MetricsJacksonDeserializer() {
super(Metrics.class);
}

@Override
public Metrics deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
final Map<String, Object> metricsData = deserializationContext.readValue(jsonParser, Map.class);
final MutableMetrics m = new MutableMetrics((String)metricsData.get(GraphSONTokens.ID), (String)metricsData.get(GraphSONTokens.NAME));

m.setDuration(Math.round((Double) metricsData.get(GraphSONTokens.DURATION) * 1000000), TimeUnit.NANOSECONDS);
for (Map.Entry<String, Long> count : ((Map<String, Long>)metricsData.getOrDefault(GraphSONTokens.COUNTS, new LinkedHashMap<>(0))).entrySet()) {
m.setCount(count.getKey(), count.getValue());
}
for (Map.Entry<String, Long> count : ((Map<String, Long>) metricsData.getOrDefault(GraphSONTokens.ANNOTATIONS, new LinkedHashMap<>(0))).entrySet()) {
m.setAnnotation(count.getKey(), count.getValue());
}
for (MutableMetrics nested : (List<MutableMetrics>)metricsData.getOrDefault(GraphSONTokens.METRICS, new ArrayList<>(0))) {
m.addNested(nested);
}
return m;
}

@Override
public boolean isCachable() {
return true;
}
}

static class TraversalMetricsJacksonDeserializer extends StdDeserializer<TraversalMetrics> {

public TraversalMetricsJacksonDeserializer() {
super(TraversalMetrics.class);
}

@Override
public TraversalMetrics deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
final Map<String, Object> traversalMetricsData = deserializationContext.readValue(jsonParser, Map.class);

return new DefaultTraversalMetrics(
Math.round((Double) traversalMetricsData.get(GraphSONTokens.DURATION) * 1000000),
(List<MutableMetrics>) traversalMetricsData.get(GraphSONTokens.METRICS)
);
}

@Override
public boolean isCachable() {
return true;
}
}

static class TreeJacksonDeserializer extends StdDeserializer<Tree> {

public TreeJacksonDeserializer() {
Expand Down
Loading

0 comments on commit 0e96e3d

Please sign in to comment.