use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.
the class SqlgWhereStrategy method apply.
@SuppressWarnings("resource")
@Override
public void apply(Admin<?, ?> traversal) {
if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
return;
}
SqlgGraph sqlgGraph = (SqlgGraph) traversal.getGraph().get();
// The query will read from the cache if this is for a cached vertex
if (sqlgGraph.features().supportsBatchMode() && sqlgGraph.tx().isInNormalBatchMode()) {
sqlgGraph.tx().flush();
}
List<Step<?, ?>> steps = new ArrayList(traversal.asAdmin().getSteps());
ListIterator<Step<?, ?>> stepIterator = steps.listIterator();
// get all steps per label
Map<String, Object> stepsByLabel = new HashMap<>();
stepIterator = steps.listIterator();
Step<?, ?> previous = null;
int idx = 0;
while (stepIterator.hasNext()) {
Step<?, ?> step = stepIterator.next();
captureLabels(step, stepsByLabel);
if (step instanceof WherePredicateStep<?>) {
WherePredicateStep<?> wps = (WherePredicateStep<?>) step;
if (wps.getPredicate().isPresent() && wps.getPredicate().get().getBiPredicate() instanceof FullText) {
Object referTo = previous;
if (wps.getStartKey().isPresent()) {
referTo = stepsByLabel.get(wps.getStartKey().get());
}
if (referTo instanceof SqlgGraphStep<?, ?>) {
SqlgGraphStep<?, ?> sgs = (SqlgGraphStep<?, ?>) referTo;
if (sgs.getReplacedSteps().size() > 0) {
referTo = sgs.getReplacedSteps().get(sgs.getReplacedSteps().size() - 1);
}
}
if (referTo instanceof ReplacedStep<?, ?>) {
ReplacedStep<?, ?> rs = (ReplacedStep<?, ?>) referTo;
rs.addHasContainer(new HasContainer("__dummy__", wps.getPredicate().get()));
traversal.removeStep(idx);
}
}
}
previous = step;
idx++;
}
}
use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.
the class TestPathStep method g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_by.
@Test
public void g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_by() throws IOException {
Graph graph = this.sqlgGraph;
loadModern(this.sqlgGraph);
assertModernGraph(graph, true, false);
GraphTraversalSource g = graph.traversal();
DefaultGraphTraversal<Vertex, Map<String, Object>> traversal = (DefaultGraphTraversal<Vertex, Map<String, Object>>) g.V().has(T.label, "person").as("a").local(__.out("created").as("b")).select("a", "b").by("name").by(T.id);
Assert.assertEquals(4, traversal.getSteps().size());
printTraversalForm(traversal);
Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
Assert.assertEquals(4, traversal.getSteps().size());
int counter = 0;
while (traversal.hasNext()) {
final Map<String, Object> map = traversal.next();
counter++;
Assert.assertEquals(2, map.size());
if (map.get("a").equals("marko")) {
Assert.assertEquals(convertToVertexId("lop"), map.get("b"));
} else if (map.get("a").equals("josh")) {
Assert.assertTrue(convertToVertexId("lop").equals(map.get("b")) || convertToVertexId("ripple").equals(map.get("b")));
} else if (map.get("a").equals("peter")) {
Assert.assertEquals(convertToVertexId("lop"), map.get("b"));
} else {
Assert.fail("The following map should not have been returned: " + map);
}
}
Assert.assertEquals(4, counter);
}
use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.
the class TestPathStep method testSimplePath.
@Test
public void testSimplePath() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1", "age", 1);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().as("a").has("name", "a1").as("b").has("age", 1).as("c").path();
Assert.assertEquals(3, traversal.getSteps().size());
List<Path> paths = traversal.toList();
Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
Assert.assertEquals(3, traversal.getSteps().size());
Assert.assertEquals(1, paths.size());
Assert.assertEquals(1, paths.get(0).size());
}
use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.
the class TestRangeLimit method testRangeOnVertexLabels.
@Test
public void testRangeOnVertexLabels() {
for (int i = 0; i < 100; i++) {
this.sqlgGraph.addVertex(T.label, "A", "name", "a" + i);
}
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Object> g = (DefaultGraphTraversal<Vertex, Object>) this.sqlgGraph.traversal().V().hasLabel("A").order().by("name").range(1, 4).values("name");
Assert.assertEquals(5, g.getSteps().size());
ensureRangeGlobal(g);
int cnt = 0;
Set<String> names = new HashSet<>();
String previous = null;
if (g.hasNext()) {
Assert.assertEquals(2, g.getSteps().size());
Assert.assertTrue(g.getSteps().get(0) instanceof SqlgGraphStep);
SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) g.getSteps().get(0);
assertStep(sqlgGraphStep, true, false, false, false, true);
}
while (g.hasNext()) {
String n = (String) g.next();
names.add(n);
if (previous != null) {
Assert.assertTrue(previous.compareTo(n) < 0);
}
previous = n;
cnt++;
}
ensureNoRangeGlobal(g);
Assert.assertEquals(3, cnt);
Assert.assertEquals(names.toString(), 3, names.size());
Assert.assertTrue(names.toString(), names.contains("a1"));
Assert.assertTrue(names.toString(), names.contains("a10"));
Assert.assertTrue(names.toString(), names.contains("a11"));
}
use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.
the class TestRangeLimit method testLimitAfterNonOptimizedStep.
@Test
public void testLimitAfterNonOptimizedStep() {
for (int i = 0; i < 100; i++) {
Vertex column = this.sqlgGraph.addVertex(T.label, "BigData.Column");
Vertex tag = this.sqlgGraph.addVertex(T.label, "BigData.Tag", "name", "NonAnonymized");
tag.addEdge("tag", column);
}
for (int i = 0; i < 100; i++) {
Vertex column = this.sqlgGraph.addVertex(T.label, "BigData.Column");
Vertex tag = this.sqlgGraph.addVertex(T.label, "BigData.Tag", "name", "Anonymized");
tag.addEdge("tag", column);
}
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("BigData.Column").where(__.in("tag").hasLabel("BigData.Tag").has("name", "Anonymized")).limit(3);
Assert.assertEquals(4, traversal.getSteps().size());
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(3, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
assertStep(sqlgGraphStep, true, false, false, true, true);
Assert.assertEquals(3, vertices.size());
}
Aggregations