use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.
the class SqlgNotStepBarrier method processNextStart.
@Override
protected Traverser.Admin<S> processNextStart() {
if (this.first) {
this.first = false;
Multimap<String, Traverser.Admin<S>> startRecordIds = LinkedListMultimap.create();
while (this.starts.hasNext()) {
Traverser.Admin<S> start = this.starts.next();
if (start instanceof SqlgTraverser) {
((SqlgTraverser) start).setRequiresOneBulk(true);
}
List<Object> startObjects = start.path().objects();
StringBuilder recordIdConcatenated = new StringBuilder();
for (Object startObject : startObjects) {
if (startObject instanceof Element) {
Element e = (Element) startObject;
recordIdConcatenated.append(e.id().toString());
} else {
recordIdConcatenated.append(startObject.toString());
}
}
startRecordIds.put(recordIdConcatenated.toString(), start);
this.notTraversal.addStart(start);
}
while (this.notTraversal.hasNext()) {
Traverser.Admin<?> orTraverser = this.notTraversal.nextTraverser();
List<Object> filterTraverserObjects = orTraverser.path().objects();
String startId = "";
for (Object filteredTraverserObject : filterTraverserObjects) {
if (filteredTraverserObject instanceof Element) {
Element e = (Element) filteredTraverserObject;
startId += e.id().toString();
} else {
startId += filteredTraverserObject.toString();
}
startRecordIds.removeAll(startId);
if (startRecordIds.isEmpty()) {
break;
}
}
}
this.results.addAll(startRecordIds.values());
this.results.sort((o1, o2) -> {
SqlgTraverser x = (SqlgTraverser) o1;
SqlgTraverser y = (SqlgTraverser) o2;
return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
});
this.resultIterator = this.results.iterator();
}
if (this.resultIterator.hasNext()) {
Traverser.Admin<S> traverser = this.resultIterator.next();
return traverser;
} else {
// The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
reset();
throw FastNoSuchElementException.instance();
}
}
use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.
the class SqlgOrStepBarrier method processNextStart.
@Override
protected Traverser.Admin<S> processNextStart() {
if (this.first) {
this.first = false;
Multimap<String, Traverser.Admin<S>> startRecordIds = LinkedListMultimap.create();
while (this.starts.hasNext()) {
Traverser.Admin<S> start = this.starts.next();
if (start instanceof SqlgTraverser) {
((SqlgTraverser) start).setRequiresOneBulk(true);
}
List<Object> startObjects = start.path().objects();
StringBuilder recordIdConcatenated = new StringBuilder();
for (Object startObject : startObjects) {
if (startObject instanceof Element) {
Element e = (Element) startObject;
recordIdConcatenated.append(e.id().toString());
} else {
recordIdConcatenated.append(startObject.toString());
}
}
startRecordIds.put(recordIdConcatenated.toString(), start);
}
for (Traversal.Admin<S, ?> orTraversal : this.orTraversals) {
for (Traverser.Admin<S> start : startRecordIds.values()) {
orTraversal.addStart(start);
}
while (orTraversal.hasNext()) {
Traverser.Admin<?> orTraverser = orTraversal.nextTraverser();
List<Object> filterTraverserObjects = orTraverser.path().objects();
String startId = "";
for (Object filteredTraverserObject : filterTraverserObjects) {
if (filteredTraverserObject instanceof Element) {
Element e = (Element) filteredTraverserObject;
startId += e.id().toString();
} else {
startId += filteredTraverserObject.toString();
}
if (startRecordIds.containsKey(startId)) {
this.results.addAll(startRecordIds.get(startId));
startRecordIds.removeAll(startId);
}
if (startRecordIds.isEmpty()) {
break;
}
}
}
}
this.results.sort((o1, o2) -> {
SqlgTraverser x = (SqlgTraverser) o1;
SqlgTraverser y = (SqlgTraverser) o2;
return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
});
this.resultIterator = this.results.iterator();
}
if (this.resultIterator.hasNext()) {
Traverser.Admin<S> traverser = this.resultIterator.next();
return traverser;
} else {
// The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
reset();
throw FastNoSuchElementException.instance();
}
}
use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.
the class TestLocalVertexStepOptionalWithOrder method testUmlgRequirement.
/**
* optionals wrapped in local steps.
* The idea is that each traversed out/in edge needs to be sorted
*/
@Test
public void testUmlgRequirement() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3");
Vertex bb1 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb1");
Vertex bb2 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb2");
Vertex bb3 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb3");
a1.addEdge("ab", b1, "order", 3);
a1.addEdge("ab", b2, "order", 2);
a1.addEdge("ab", b3, "order", 1);
a1.addEdge("abb", bb1, "order", 3);
a1.addEdge("abb", bb2, "order", 2);
a1.addEdge("abb", bb3, "order", 1);
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2");
Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "c3");
b1.addEdge("bc", c1, "order", 1);
b1.addEdge("bc", c2, "order", 2);
b1.addEdge("bc", c3, "order", 3);
Vertex cc1 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc1");
Vertex cc2 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc2");
Vertex cc3 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc3");
b1.addEdge("bcc", cc1, "order", 3);
b1.addEdge("bcc", cc2, "order", 2);
b1.addEdge("bcc", cc3, "order", 1);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) sqlgGraph.traversal().V().hasLabel("A").as("a").order().by("name", Order.decr).local(__.optional(__.outE().as("e1").inV().as("b").order().by(T.label).by(__.select("e1").by("order")).local(__.optional(__.outE().as("e2").inV().order().by(T.label).by(__.select("e2").by("order")))))).path();
Assert.assertEquals(5, traversal.getSteps().size());
List<Path> paths = traversal.toList();
Assert.assertEquals(4, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
SqlgLocalStepBarrier sqlgLocalStepBarrier = (SqlgLocalStepBarrier) traversal.getSteps().get(2);
DefaultGraphTraversal<Vertex, Vertex> t = (DefaultGraphTraversal<Vertex, Vertex>) sqlgLocalStepBarrier.getLocalChildren().get(0);
// First optional is a non optimized, optimized SqlgOptionalBarrierStep
Assert.assertEquals(1, t.getSteps().size());
Assert.assertTrue(t.getSteps().get(0) instanceof SqlgOptionalStepBarrier);
SqlgOptionalStepBarrier sqlgOptionalStepBarrier = (SqlgOptionalStepBarrier) t.getSteps().get(0);
Traversal.Admin<?, ?> optionalTraversal = (Traversal.Admin<?, ?>) sqlgOptionalStepBarrier.getLocalChildren().get(0);
Assert.assertEquals(2, optionalTraversal.getSteps().size());
Assert.assertTrue(optionalTraversal.getSteps().get(0) instanceof SqlgVertexStep);
Assert.assertTrue(optionalTraversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
SqlgLocalStepBarrier sqlgLocalStepBarrier1 = (SqlgLocalStepBarrier) optionalTraversal.getSteps().get(1);
t = (DefaultGraphTraversal<Vertex, Vertex>) sqlgLocalStepBarrier.getLocalChildren().get(0);
// Second optional is a optimized
Assert.assertEquals(1, t.getSteps().size());
Assert.assertEquals(12, paths.size());
Path pathX = paths.get(0);
Assert.assertEquals(1, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a2));
pathX = paths.get(1);
Assert.assertEquals(3, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b3));
pathX = paths.get(2);
Assert.assertEquals(3, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b2));
pathX = paths.get(3);
Assert.assertEquals(5, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b1));
Assert.assertTrue(pathX.get(4).equals(c1));
pathX = paths.get(4);
Assert.assertEquals(5, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b1));
Assert.assertTrue(pathX.get(4).equals(c2));
pathX = paths.get(5);
Assert.assertEquals(5, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b1));
Assert.assertTrue(pathX.get(4).equals(c3));
pathX = paths.get(6);
Assert.assertEquals(5, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b1));
Assert.assertTrue(pathX.get(4).equals(cc3));
pathX = paths.get(7);
Assert.assertEquals(5, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b1));
Assert.assertTrue(pathX.get(4).equals(cc2));
pathX = paths.get(8);
Assert.assertEquals(5, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
Assert.assertTrue(pathX.get(2).equals(b1));
Assert.assertTrue(pathX.get(4).equals(cc1));
pathX = paths.get(9);
Assert.assertEquals(3, pathX.size());
Assert.assertTrue(pathX.get(0).equals(a1));
for (Path path : paths) {
for (Object v : path.objects()) {
Element vertex = (Element) v;
if (vertex instanceof Vertex) {
System.out.print(vertex.<String>value("name"));
System.out.print(" - ");
}
}
}
}
use of org.apache.tinkerpop.gremlin.structure.Element in project sqlg by pietermartin.
the class TestGremlinCompileWithAs method testHasLabelOutWithAs_assert.
private void testHasLabelOutWithAs_assert(SqlgGraph sqlgGraph, Vertex a1, Vertex b1, Vertex b2, Vertex b3, Vertex b4, Edge e1, Edge e2, Edge e3, Edge e4) {
DefaultGraphTraversal<Vertex, Map<String, Element>> traversal = (DefaultGraphTraversal) sqlgGraph.traversal().V(a1).outE("outB").as("e").inV().as("B").select("e", "B");
Assert.assertEquals(4, traversal.getSteps().size());
List<Map<String, Element>> result = traversal.toList();
Assert.assertEquals(2, traversal.getSteps().size());
Collections.sort(result, Comparator.comparing(o -> o.get("e").<String>value("edgeName")));
Assert.assertEquals(4, result.size());
Edge queryE0 = (Edge) result.get(0).get("e");
Vertex queryB0 = (Vertex) result.get(0).get("B");
Assert.assertEquals(e1, queryE0);
Assert.assertEquals("b1", queryE0.inVertex().value("name"));
Assert.assertEquals("a1", queryE0.outVertex().value("name"));
Assert.assertEquals("edge1", queryE0.value("edgeName"));
Assert.assertEquals(b1, queryB0);
Assert.assertEquals("b1", queryB0.value("name"));
Element queryE2 = result.get(1).get("e");
Element queryB2 = result.get(1).get("B");
Assert.assertEquals(e2, queryE2);
Assert.assertEquals("edge2", queryE2.value("edgeName"));
Assert.assertEquals(b2, queryB2);
Assert.assertEquals("b2", queryB2.value("name"));
Element queryE3 = result.get(2).get("e");
Element queryB3 = result.get(2).get("B");
Assert.assertEquals(e3, queryE3);
Assert.assertEquals("edge3", queryE3.value("edgeName"));
Assert.assertEquals(b3, queryB3);
Assert.assertEquals("b3", queryB3.value("name"));
Element queryE4 = result.get(3).get("e");
Element queryB4 = result.get(3).get("B");
Assert.assertEquals(e4, queryE4);
Assert.assertEquals("edge4", queryE4.value("edgeName"));
Assert.assertEquals(b4, queryB4);
Assert.assertEquals("b4", queryB4.value("name"));
DefaultGraphTraversal<Vertex, Edge> traversal1 = (DefaultGraphTraversal<Vertex, Edge>) sqlgGraph.traversal().V(a1.id()).bothE();
Assert.assertEquals(2, traversal1.getSteps().size());
final List<Edge> a1Edges = traversal1.toList();
Assert.assertEquals(1, traversal1.getSteps().size());
Assert.assertEquals(4, a1Edges.size());
List<String> names = new ArrayList<>(Arrays.asList("b1", "b2", "b3", "b4"));
for (Edge a1Edge : a1Edges) {
names.remove(a1Edge.inVertex().<String>value("name"));
Assert.assertEquals("a1", a1Edge.outVertex().<String>value("name"));
}
Assert.assertTrue(names.isEmpty());
}
use of org.apache.tinkerpop.gremlin.structure.Element in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method testCollection.
private void testCollection(Cardinality cardinality, String property, Object value1, Object value2) {
clopen();
Vertex v1 = graph.addVertex();
// Adding properties one at a time
v1.property(property, value1);
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
v1 = getV(graph, v1.id());
v1.property(property, value2);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Remove the properties
v1 = getV(graph, v1.id());
v1.properties(property).forEachRemaining(p -> {
if (p.value().equals(value1)) {
p.remove();
}
});
assertFalse(graph.query().has(property, value1).vertices().iterator().hasNext());
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
assertFalse(graph.query().has(property, value1).vertices().iterator().hasNext());
// Re add the properties
v1 = getV(graph, v1.id());
v1.property(property, value1);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Add a duplicate property
v1 = getV(graph, v1.id());
v1.property(property, value1);
assertEquals(Cardinality.SET.equals(cardinality) ? 2 : 3, Iterators.size(getOnlyVertex(graph.query().has(property, value1)).properties(property)));
// Flush the index
clopen();
assertEquals(Cardinality.SET.equals(cardinality) ? 2 : 3, Iterators.size(getOnlyVertex(graph.query().has(property, value1)).properties(property)));
// Add two properties at once to a fresh vertex
graph.vertices().forEachRemaining(Element::remove);
v1 = graph.addVertex();
v1.property(property, value1);
v1.property(property, value2);
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// Flush the index
clopen();
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices()));
// If this is a geo test then try a within query
if (value1 instanceof Geoshape && supportsGeoCollections()) {
assertEquals(v1, getOnlyElement(graph.query().has(property, Geo.WITHIN, Geoshape.circle(1.0, 1.0, 0.1)).vertices()));
assertEquals(v1, getOnlyElement(graph.query().has(property, Geo.WITHIN, Geoshape.circle(2.0, 2.0, 0.1)).vertices()));
}
// Test traversal property drop Issue #408
GraphTraversalSource g = graph.traversal();
g.V().drop().iterate();
// Flush the index
clopen();
g = graph.traversal();
v1 = g.addV().property(property, value1).property(property, value2).next();
g.addV().property(property, value1).property(property, value2).next();
// Flush the index
clopen();
g = graph.traversal();
assertEquals(2, g.V().has(property, value1).toList().size());
g.V().properties().drop().iterate();
// Flush the index
clopen();
g = graph.traversal();
assertFalse(g.V().has(property, value1).hasNext());
assertFalse(g.V().has(property, value2).hasNext());
}
Aggregations