use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.
the class SqlgHasStepStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
// noinspection OptionalGetWithoutIsPresent
if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
return;
}
List<HasStep> hasSteps = TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal);
for (HasStep<?> hasStep : hasSteps) {
SqlgHasStep sqlgHasStep = new SqlgHasStep(hasStep.getTraversal(), hasStep.getHasContainers().toArray(new HasContainer[] {}));
for (String label : hasStep.getLabels()) {
sqlgHasStep.addLabel(label);
}
TraversalHelper.replaceStep(hasStep, sqlgHasStep, hasStep.getTraversal());
}
}
use of org.umlg.sqlg.structure.SqlgGraph 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.structure.SqlgGraph in project sqlg by pietermartin.
the class SqlgOptionalStepStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
// noinspection OptionalGetWithoutIsPresent
if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
return;
}
List<OptionalStep> optionalSteps = TraversalHelper.getStepsOfAssignableClass(OptionalStep.class, traversal);
for (OptionalStep<S> optionalStep : optionalSteps) {
// The predicate branch step is a local traversal.
// As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work.
Traversal.Admin<S, S> optionalTraversal = optionalStep.getLocalChildren().get(0);
List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, optionalTraversal);
if (!reducingBarrierSteps.isEmpty()) {
continue;
}
SqlgOptionalStepBarrier sqlgOptionalStepBarrier = new SqlgOptionalStepBarrier<>(traversal, optionalTraversal);
for (String label : optionalStep.getLabels()) {
sqlgOptionalStepBarrier.addLabel(label);
}
TraversalHelper.replaceStep(optionalStep, sqlgOptionalStepBarrier, optionalStep.getTraversal());
}
}
use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.
the class SqlgTraversalFilterStepStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
// noinspection OptionalGetWithoutIsPresent
if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
return;
}
List<TraversalFilterStep> traversalFilterSteps = TraversalHelper.getStepsOfAssignableClass(TraversalFilterStep.class, traversal);
for (TraversalFilterStep<S> traversalFilterStep : traversalFilterSteps) {
List<Traversal.Admin<S, ?>> filterTraversals = traversalFilterStep.getLocalChildren();
Preconditions.checkState(filterTraversals.size() == 1);
Traversal.Admin<S, ?> filterTraversal = filterTraversals.get(0);
// reducing barrier steps like count does not work with Sqlg's barrier optimizations
List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, filterTraversal);
if (!reducingBarrierSteps.isEmpty()) {
continue;
}
SqlgTraversalFilterStepBarrier sqlgTraversalFilterStepBarrier = new SqlgTraversalFilterStepBarrier<>(traversal, filterTraversal);
for (String label : traversalFilterStep.getLabels()) {
sqlgTraversalFilterStepBarrier.addLabel(label);
}
TraversalHelper.replaceStep(traversalFilterStep, sqlgTraversalFilterStepBarrier, traversalFilterStep.getTraversal());
}
}
use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.
the class TestMultiThread method testMultipleGraphs.
/**
* test when each graph is created in its own thread, in distributed mode
*
* @throws Exception
*/
@Test
public void testMultipleGraphs() throws Exception {
URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
try {
configuration = new PropertiesConfiguration(sqlProperties);
Assume.assumeTrue(isPostgres());
configuration.addProperty("distributed", true);
if (!configuration.containsKey("jdbc.url"))
throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
} catch (ConfigurationException e) {
throw new RuntimeException(e);
}
ExecutorService executorService = newFixedThreadPool(50);
int loop = 400;
for (int i = 0; i < loop; i++) {
String n = "person" + i;
executorService.submit(() -> {
try {
try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
final Random random = new Random();
if (random.nextBoolean()) {
Vertex person = sqlgGraph1.addVertex(T.label, "Person_True", "name", n);
Vertex address = sqlgGraph1.addVertex(T.label, "Address_True", "name", n);
person.addEdge("address_True", address, "name", n);
} else {
Vertex person = sqlgGraph1.addVertex(T.label, "Person", "name", n);
Vertex address = sqlgGraph1.addVertex(T.label, "Address", "name", n);
person.addEdge("address", address, "name", n);
}
sqlgGraph1.tx().commit();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
fail(e.getMessage());
}
});
}
executorService.shutdown();
executorService.awaitTermination(100, TimeUnit.SECONDS);
try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
Assert.assertEquals(400, sqlgGraph1.traversal().V().hasLabel("Person_True").count().next().longValue() + sqlgGraph1.traversal().V().hasLabel("Person").count().next().longValue());
Assert.assertEquals(400, sqlgGraph1.traversal().V().hasLabel("Address_True").count().next().longValue() + sqlgGraph1.traversal().V().hasLabel("Address").count().next().longValue());
Assert.assertEquals(400, sqlgGraph1.traversal().E().hasLabel("address_True").count().next().longValue() + sqlgGraph1.traversal().E().hasLabel("address").count().next().longValue());
}
}
Aggregations