Skip to content

Commit

Permalink
Merge branch 'develop' into gh-3276-seed-with-vertex-only-gremlin
Browse files Browse the repository at this point in the history
  • Loading branch information
wb36499 authored Sep 13, 2024
2 parents d8473bf + 16fd6a3 commit d3eaa1c
Show file tree
Hide file tree
Showing 16 changed files with 696 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import uk.gov.gchq.gaffer.tinkerpop.generator.GafferPopElementGenerator;
import uk.gov.gchq.gaffer.tinkerpop.process.traversal.strategy.optimisation.GafferPopGraphStepStrategy;
import uk.gov.gchq.gaffer.tinkerpop.process.traversal.strategy.optimisation.GafferPopHasStepStrategy;
import uk.gov.gchq.gaffer.tinkerpop.process.traversal.strategy.optimisation.GafferPopVertexStepStrategy;
import uk.gov.gchq.gaffer.tinkerpop.process.traversal.util.GafferCustomTypeFactory;
import uk.gov.gchq.gaffer.tinkerpop.process.traversal.util.GafferVertexUtils;
import uk.gov.gchq.gaffer.tinkerpop.service.GafferPopNamedOperationServiceFactory;
Expand Down Expand Up @@ -289,7 +290,8 @@ public GafferPopGraph(final Configuration configuration, final Graph graph) {
// Add and register custom traversals
TraversalStrategies traversalStrategies = GlobalCache.getStrategies(this.getClass()).addStrategies(
GafferPopGraphStepStrategy.instance(),
GafferPopHasStepStrategy.instance());
GafferPopHasStepStrategy.instance(),
GafferPopVertexStepStrategy.instance());
GlobalCache.registerStrategies(this.getClass(), traversalStrategies);
}

Expand Down Expand Up @@ -951,10 +953,13 @@ private List<ElementSeed> getElementSeeds(final Iterable<Object> ids) {
List<Object> edgeIdList = new LinkedList<>();
// Extract Vertex ID
if (id instanceof Vertex) {
seeds.add(new EntitySeed(((Vertex) id).id()));
Object parsedId = GafferCustomTypeFactory.parseAsCustomTypeIfValid(((Vertex) id).id());
seeds.add(new EntitySeed(parsedId));
// Extract Edge ID
} else if (id instanceof Edge) {
seeds.add(new EdgeSeed(((Edge) id).outVertex().id(), ((Edge) id).inVertex().id()));
Object src = GafferCustomTypeFactory.parseAsCustomTypeIfValid(((Edge) id).outVertex().id());
Object target = GafferCustomTypeFactory.parseAsCustomTypeIfValid(((Edge) id).inVertex().id());
seeds.add(new EdgeSeed(src, target));
// Extract source and destination from ID list
} else if (id instanceof Iterable) {
((Iterable<?>) id).forEach(edgeIdList::add);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright 2024 Crown Copyright
*
* 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.
*/

package uk.gov.gchq.gaffer.tinkerpop.process.traversal.step;

import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.step.Configuring;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.FlatMapStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters;
import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.gov.gchq.gaffer.data.elementdefinition.view.View;
import uk.gov.gchq.gaffer.tinkerpop.GafferPopGraph;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Custom GafferPop VertexStep.
* <p>
* Takes an Iterable of Vertices as input rather than a single Vertex.
* This is so we can perform a single Gaffer query for multiple vertices rather
* than one each.
* <p>
* There are some consequences to this optimisation, for queries where there are
* multiple of the same input seed.
* In Gremlin, you would expect to get multiple of the same result.
* However, in Gaffer, if you supply multiple of the same seed only one result
* is returned.
* <p>
* e.g. for the Tinkerpop Modern Graph:
*
* <pre>
* (Gremlin) g.V().out() = [v2, v3, v3, v3, v4, v5]
* (GafferPop) g.V().out() = [v2, v3, v4, v5]
* </pre>
*/
public class GafferPopVertexStep<E extends Element> extends FlatMapStep<Iterable<Vertex>, E>
implements AutoCloseable, Configuring {
private static final Logger LOGGER = LoggerFactory.getLogger(GafferPopVertexStep.class);

protected Parameters parameters = new Parameters();
private final String[] edgeLabels;
private Direction direction;
private final Class<E> returnClass;

public GafferPopVertexStep(final VertexStep<E> originalVertexStep) {
super(originalVertexStep.getTraversal());
this.direction = originalVertexStep.getDirection();
this.edgeLabels = originalVertexStep.getEdgeLabels();
this.returnClass = originalVertexStep.getReturnClass();
this.traversal = originalVertexStep.getTraversal();
}

@Override
public Parameters getParameters() {
return this.parameters;
}

@Override
public void configure(final Object... keyValues) {
this.parameters.set(null, keyValues);
}

@Override
protected Iterator<E> flatMap(final Traverser.Admin<Iterable<Vertex>> traverser) {
return Vertex.class.isAssignableFrom(returnClass) ?
(Iterator<E>) this.vertices(traverser.get()) :
(Iterator<E>) this.edges(traverser.get());
}

public Direction getDirection() {
return this.direction;
}

public String[] getEdgeLabels() {
return this.edgeLabels;
}

public Class<E> getReturnClass() {
return this.returnClass;
}

public void reverseDirection() {
this.direction = this.direction.opposite();
}

public boolean returnsVertex() {
return this.returnClass.equals(Vertex.class);
}

public boolean returnsEdge() {
return this.returnClass.equals(Edge.class);
}

@Override
public String toString() {
return StringFactory.stepString(this, this.direction, Arrays.asList(this.edgeLabels),
this.returnClass.getSimpleName().toLowerCase());
}

@Override
public int hashCode() {
int result = Objects.hash(super.hashCode(), direction, returnClass);
// edgeLabels' order does not matter because in("x", "y") and in("y", "x") must
// be considered equal.
if (edgeLabels != null && edgeLabels.length > 0) {
final List<String> sortedEdgeLabels = Arrays.stream(edgeLabels)
.sorted(Comparator.nullsLast(Comparator.naturalOrder())).collect(Collectors.toList());
for (final String edgeLabel : sortedEdgeLabels) {
result = 31 * result + Objects.hashCode(edgeLabel);
}
}
return result;
}

@Override
public boolean equals(final Object other) {
return other != null
&& other.getClass().equals(this.getClass())
&& this.hashCode() == other.hashCode();
}

@Override
public void close() throws Exception {
closeIterator();
}

@Override
public Set<TraverserRequirement> getRequirements() {
return Collections.singleton(TraverserRequirement.OBJECT);
}

private Iterator<Vertex> vertices(final Iterable<Vertex> vertices) {
GafferPopGraph graph = (GafferPopGraph) getTraversal().getGraph().get();

if (edgeLabels.length == 0) {
LOGGER.debug("Getting {} AdjVertices for vertices", direction);
return graph.adjVertices((Iterable) vertices, direction);
}

LOGGER.debug("Getting {} AdjVertices for edges {} for vertices", direction, edgeLabels);
View view = new View.Builder().edges(Arrays.asList(edgeLabels)).build();
return graph.adjVerticesWithView((Iterable) vertices, direction, view);
}

private Iterator<Edge> edges(final Iterable<Vertex> vertices) {
GafferPopGraph graph = (GafferPopGraph) getTraversal().getGraph().get();

if (edgeLabels.length == 0) {
LOGGER.debug("Getting {} edges for vertices", direction);
return graph.edges((Iterable) vertices, direction);
}

LOGGER.debug("Getting {} edges with labels {} for vertices", direction, edgeLabels);
View view = new View.Builder().edges(Arrays.asList(edgeLabels)).build();
return graph.edgesWithView((Iterable) vertices, direction, view);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 Crown Copyright
*
* 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.
*/

package uk.gov.gchq.gaffer.tinkerpop.process.traversal.step;

import org.apache.tinkerpop.gremlin.process.traversal.Step;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.Traverser.Admin;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep;
import org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException;

import java.util.Iterator;

/**
* A Lazy implementation of the FoldStep
* Folds to a lazy iterable
*/
public class LazyFoldStep<S> extends AbstractStep<S, Iterable<S>> {

private boolean done = false;

public LazyFoldStep(final Traversal.Admin<S, Iterable<S>> traversal) {
super(traversal);
}

@Override
public Admin<Iterable<S>> processNextStart() {
if (done) {
throw FastNoSuchElementException.instance();
}

// Do nothing on subsequent calls
this.done = true;

Iterable<S> lazyIterable = () -> new Iterator<S>() {
@Override
public boolean hasNext() {
return LazyFoldStep.this.starts.hasNext();
}

@Override
public S next() {
if (!hasNext()) {
throw FastNoSuchElementException.instance();
}

return LazyFoldStep.this.starts.next().get();
}
};

return this.getTraversal().getTraverserGenerator().generate(lazyIterable, (Step) this, 1L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 Crown Copyright
*
* 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.
*/

package uk.gov.gchq.gaffer.tinkerpop.process.traversal.strategy.optimisation;

import org.apache.tinkerpop.gremlin.process.traversal.Traversal.Admin;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.gov.gchq.gaffer.tinkerpop.process.traversal.step.GafferPopVertexStep;
import uk.gov.gchq.gaffer.tinkerpop.process.traversal.step.LazyFoldStep;

/**
* Optimisation strategy to reduce the number of Gaffer operations performed.
* Replaces the VertexStep so they operate on multiple vertices at once.
*/
public final class GafferPopVertexStepStrategy
extends AbstractTraversalStrategy<TraversalStrategy.ProviderOptimizationStrategy>
implements TraversalStrategy.ProviderOptimizationStrategy {

private static final Logger LOGGER = LoggerFactory.getLogger(GafferPopVertexStepStrategy.class);
private static final GafferPopVertexStepStrategy INSTANCE = new GafferPopVertexStepStrategy();

private GafferPopVertexStepStrategy() {
}

@Override
public void apply(final Admin<?, ?> traversal) {
TraversalHelper.getStepsOfClass(VertexStep.class, traversal).forEach(originalVertexStep -> {
LOGGER.debug("Inserting FoldStep and replacing VertexStep");

// Replace vertex step
final GafferPopVertexStep<? extends Element> listVertexStep = new GafferPopVertexStep<>(
originalVertexStep);
TraversalHelper.replaceStep(originalVertexStep, listVertexStep, traversal);

// Add in a fold step before the new VertexStep so that the input is the list of
// all vertices
LazyFoldStep<Vertex> lazyFoldStep = new LazyFoldStep<>(originalVertexStep.getTraversal());
TraversalHelper.insertBeforeStep(lazyFoldStep, listVertexStep, traversal);
});
}

public static GafferPopVertexStepStrategy instance() {
return INSTANCE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void shouldReturnFilteredVerticesFromSpecificGraph() {

// Then
assertThat(result)
.containsExactly(0.4, 0.4, 1.0);
.containsExactlyInAnyOrder(0.4, 0.4, 1.0);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void shouldFilterVerticesByPropertyNot() {

assertThat(result)
.extracting(r -> r.id())
.containsExactlyInAnyOrder(RIPPLE.getId(), LOP.getId(), LOP.getId(), LOP.getId());
.containsExactlyInAnyOrder(RIPPLE.getId(), LOP.getId());
}

@Test
Expand Down
Loading

0 comments on commit d3eaa1c

Please sign in to comment.