Skip to content

Commit

Permalink
Gh-3359: Reduce number of vertex lookups for edges (#3360)
Browse files Browse the repository at this point in the history
* gh-3359 Reduce number of vertex lookups for edges

Stop inV/outV performing a full vertex lookup
Add new methods for doing this when we want to traverse an Edge

* headers

* tidy and add tests

* javadoc
  • Loading branch information
p29876 authored Jan 30, 2025
1 parent 3ec4c9b commit 72aeb9b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2024 Crown Copyright
* Copyright 2016-2025 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -128,15 +128,17 @@ public <V> Property<V> propertyWithoutUpdate(final String key, final V value) {
return newProperty;
}

// This is the method TinkerPop uses when traversing edges
// lookup the 'full' vertices rather than returning the 'dummy' ID vertices
@Override
public Iterator<Vertex> vertices(final Direction direction) {
switch (direction) {
case OUT:
return IteratorUtils.of(outVertex());
return IteratorUtils.of(lookupVertex(outVertex));
case IN:
return IteratorUtils.of(inVertex());
return IteratorUtils.of(lookupVertex(inVertex));
default:
return IteratorUtils.of(outVertex(), inVertex());
return IteratorUtils.of(lookupVertex(outVertex), lookupVertex(inVertex));
}
}

Expand All @@ -156,14 +158,27 @@ public String toString() {
return StringFactory.edgeString(this);
}


/**
* Gets the outgoing vertex of the edge.
*
* Note: the returned vertex will not have any properties set - only the ID
* if you need the 'full' vertex use {@link #vertices(Direction)}
*/
@Override
public Vertex outVertex() {
return getVertex(outVertex);
return outVertex;
}

/**
* Gets the incoming vertex of the edge.
*
* Note: the returned vertex will not have any properties set - only the ID
* if you need the 'full' vertex use {@link #vertices(Direction)}
*/
@Override
public Vertex inVertex() {
return getVertex(inVertex);
return inVertex;
}

/**
Expand Down Expand Up @@ -209,7 +224,7 @@ private static GafferPopVertex getValidVertex(final Object vertex, final GafferP
* @param vertex The vertex object or ID
* @return A valid Vertex based on the supplied object or ID.
*/
private Vertex getVertex(final GafferPopVertex vertex) {
private Vertex lookupVertex(final GafferPopVertex vertex) {
OperationChain<Iterable<? extends Element>> findBasedOnID = new OperationChain.Builder()
.first(new GetElements.Builder()
.input(new EntitySeed(vertex.id()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static uk.gov.gchq.gaffer.tinkerpop.util.modern.GafferPopModernTestUtils.AGE;
import static uk.gov.gchq.gaffer.tinkerpop.util.modern.GafferPopModernTestUtils.CREATED;
import static uk.gov.gchq.gaffer.tinkerpop.util.modern.GafferPopModernTestUtils.JOSH;
import static uk.gov.gchq.gaffer.tinkerpop.util.modern.GafferPopModernTestUtils.KNOWS;
Expand Down Expand Up @@ -277,6 +278,38 @@ void shouldGetEdgeById(String graph, GraphTraversalSource g) {
});
}

@ParameterizedTest(name = TEST_NAME_FORMAT)
@MethodSource("provideTraversals")
void shouldLookupInVertex(String graph, GraphTraversalSource g) {
final List<Vertex> result = g.E("[1,knows,2]").inV().toList();

assertThat(result)
.hasSize(1);

// Check that properties are set on the returned vertex
// i.e. a vertex lookup has been performed
Vertex vadas = result.get(0);
assertThat(vadas.id()).isEqualTo(VADAS.getId());
assertThat(vadas.property(NAME).value()).isEqualTo(VADAS.getName());
assertThat(vadas.property(AGE).value()).isEqualTo(VADAS.getAge());
}

@ParameterizedTest(name = TEST_NAME_FORMAT)
@MethodSource("provideTraversals")
void shouldLookupOutVertex(String graph, GraphTraversalSource g) {
final List<Vertex> result = g.E("[1,knows,2]").outV().toList();

assertThat(result)
.hasSize(1);

// Check that properties are set on the returned vertex
// i.e. a vertex lookup has been performed
Vertex marko = result.get(0);
assertThat(marko.id()).isEqualTo(MARKO.getId());
assertThat(marko.property(NAME).value()).isEqualTo(MARKO.getName());
assertThat(marko.property(AGE).value()).isEqualTo(MARKO.getAge());
}

@ParameterizedTest(name = TEST_NAME_FORMAT)
@MethodSource("provideTraversals")
void shouldAddV(String graph, GraphTraversalSource g) {
Expand Down

0 comments on commit 72aeb9b

Please sign in to comment.