Search in sources :

Example 1 with FullText

use of org.umlg.sqlg.predicate.FullText 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++;
    }
}
Also used : WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) FullText(org.umlg.sqlg.predicate.FullText)

Example 2 with FullText

use of org.umlg.sqlg.predicate.FullText in project sqlg by pietermartin.

the class TestGremlinCompileFullTextPredicate method testDefaultImplementation.

@Test
public void testDefaultImplementation() {
    FullText ft = new FullText("", null, true);
    assertTrue(ft.test("a fat cat sat on a mat and ate a fat rat", "cat"));
    assertTrue(ft.test("a fat cat sat on a mat and ate a fat rat", "cat rat"));
    assertFalse(ft.test("a fat cat sat on a mat and ate a fat rat", "cat cow"));
}
Also used : FullText(org.umlg.sqlg.predicate.FullText) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 3 with FullText

use of org.umlg.sqlg.predicate.FullText in project sqlg by pietermartin.

the class BaseStrategy method handleConnectiveStepInternal.

private Optional<AndOrHasContainer> handleConnectiveStepInternal(ConnectiveStep connectiveStep) {
    AndOrHasContainer.TYPE type = AndOrHasContainer.TYPE.from(connectiveStep);
    AndOrHasContainer outerAndOrHasContainer = new AndOrHasContainer(type);
    List<Traversal.Admin<?, ?>> localTraversals = connectiveStep.getLocalChildren();
    for (Traversal.Admin<?, ?> localTraversal : localTraversals) {
        if (!TraversalHelper.hasAllStepsOfClass(localTraversal, HasStep.class, ConnectiveStep.class)) {
            return Optional.empty();
        }
        AndOrHasContainer andOrHasContainer = new AndOrHasContainer(AndOrHasContainer.TYPE.NONE);
        outerAndOrHasContainer.addAndOrHasContainer(andOrHasContainer);
        for (Step<?, ?> step : localTraversal.getSteps()) {
            if (step instanceof HasStep) {
                HasStep<?> hasStep = (HasStep) step;
                for (HasContainer hasContainer : hasStep.getHasContainers()) {
                    if (hasContainerKeyNotIdOrLabel(hasContainer) && SUPPORTED_BI_PREDICATE.contains(hasContainer.getBiPredicate())) {
                        andOrHasContainer.addHasContainer(hasContainer);
                    } else if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getPredicate() instanceof AndP) {
                        AndP<?> andP = (AndP) hasContainer.getPredicate();
                        List<? extends P<?>> predicates = andP.getPredicates();
                        if (predicates.size() == 2) {
                            if (predicates.get(0).getBiPredicate() == Compare.gte && predicates.get(1).getBiPredicate() == Compare.lt) {
                                andOrHasContainer.addHasContainer(hasContainer);
                            } else if (predicates.get(0).getBiPredicate() == Compare.gt && predicates.get(1).getBiPredicate() == Compare.lt) {
                                andOrHasContainer.addHasContainer(hasContainer);
                            }
                        }
                    } else if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getPredicate() instanceof OrP) {
                        OrP<?> orP = (OrP) hasContainer.getPredicate();
                        List<? extends P<?>> predicates = orP.getPredicates();
                        if (predicates.size() == 2) {
                            if (predicates.get(0).getBiPredicate() == Compare.lt && predicates.get(1).getBiPredicate() == Compare.gt) {
                                andOrHasContainer.addHasContainer(hasContainer);
                            }
                        }
                    } else if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getBiPredicate() instanceof Text || hasContainer.getBiPredicate() instanceof FullText) {
                        andOrHasContainer.addHasContainer(hasContainer);
                    } else {
                        return Optional.empty();
                    }
                }
            } else {
                ConnectiveStep connectiveStepLocalChild = (ConnectiveStep) step;
                Optional<AndOrHasContainer> result = handleConnectiveStepInternal(connectiveStepLocalChild);
                if (result.isPresent()) {
                    andOrHasContainer.addAndOrHasContainer(result.get());
                } else {
                    return Optional.empty();
                }
            }
        }
    }
    return Optional.of(outerAndOrHasContainer);
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) LoopTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal) FullText(org.umlg.sqlg.predicate.FullText) Text(org.umlg.sqlg.predicate.Text) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) FullText(org.umlg.sqlg.predicate.FullText) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Example 4 with FullText

use of org.umlg.sqlg.predicate.FullText in project sqlg by pietermartin.

the class BaseStrategy method optimizeTextContains.

private List<HasContainer> optimizeTextContains(ReplacedStep<?, ?> replacedStep, List<HasContainer> hasContainers) {
    List<HasContainer> result = new ArrayList<>();
    for (HasContainer hasContainer : hasContainers) {
        if (hasContainerKeyNotIdOrLabel(hasContainer) && hasContainer.getBiPredicate() instanceof Text || hasContainer.getBiPredicate() instanceof FullText) {
            replacedStep.addHasContainer(hasContainer);
            result.add(hasContainer);
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer) FullText(org.umlg.sqlg.predicate.FullText) Text(org.umlg.sqlg.predicate.Text) FullText(org.umlg.sqlg.predicate.FullText)

Example 5 with FullText

use of org.umlg.sqlg.predicate.FullText in project sqlg by pietermartin.

the class SchemaTableTree method invalidateByHas.

/**
 * verify the "has" containers we have are valid with the schema table tree given
 *
 * @param schemaTableTree
 * @return true if any has container does NOT match, false if everything is fine
 */
private boolean invalidateByHas(SchemaTableTree schemaTableTree) {
    for (HasContainer hasContainer : schemaTableTree.hasContainers) {
        if (!hasContainer.getKey().equals(TopologyStrategy.TOPOLOGY_SELECTION_WITHOUT) && !hasContainer.getKey().equals(TopologyStrategy.TOPOLOGY_SELECTION_FROM)) {
            if (hasContainer.getKey().equals(label.getAccessor())) {
                Preconditions.checkState(false, "label hasContainers should have been removed by now.");
            // if (hasContainer.getValue() instanceof Collection) {
            // Collection<String> labels = (Collection<String>) hasContainer.getValue();
            // Set<SchemaTable> labelSchemaTables = labels.stream().map(l -> SchemaTable.from(this.sqlgGraph, l)).collect(Collectors.toSet());
            // BiPredicate<SchemaTable, Collection<SchemaTable>> biPredicate  = (BiPredicate<SchemaTable, Collection<SchemaTable>>) hasContainer.getBiPredicate();
            // boolean whatever = biPredicate.test(schemaTableTree.getSchemaTable().withOutPrefix(), labelSchemaTables);
            // if (!whatever) {
            // return true;
            // }
            // } else {
            // SchemaTable labelSchemaTable = SchemaTable.from(this.sqlgGraph, (String)hasContainer.getValue());
            // BiPredicate<SchemaTable, SchemaTable> biPredicate  = (BiPredicate<SchemaTable, SchemaTable>) hasContainer.getBiPredicate();
            // boolean whatever = biPredicate.test(schemaTableTree.getSchemaTable().withOutPrefix(), labelSchemaTable);
            // if (!whatever) {
            // return true;
            // }
            // }
            // //                    // we may have been given a type in a schema
            // //                    SchemaTable predicateSchemaTable = SchemaTable.from(this.sqlgGraph, hasContainer.getValue().toString());
            // //                    SchemaTable hasContainerLabelSchemaTable = getHasContainerSchemaTable(schemaTableTree, predicateSchemaTable);
            // //                    if (hasContainer.getBiPredicate().equals(Compare.eq) && !hasContainerLabelSchemaTable.toString().equals(schemaTableTree.getSchemaTable().toString())) {
            // //                        return true;
            // //                    }
            } else if (hasContainer.getKey().equals(T.id.getAccessor())) {
                if (hasContainer.getBiPredicate().equals(Compare.eq)) {
                    Object value = hasContainer.getValue();
                    SchemaTable hasContainerLabelSchemaTable = getIDContainerSchemaTable(schemaTableTree, value);
                    if (!hasContainerLabelSchemaTable.equals(schemaTableTree.getSchemaTable())) {
                        return true;
                    }
                } else if (hasContainer.getBiPredicate().equals(Contains.within)) {
                    Collection<?> c = (Collection<?>) hasContainer.getPredicate().getValue();
                    Iterator<?> it = c.iterator();
                    Collection<Object> ok = new LinkedList<>();
                    while (it.hasNext()) {
                        Object value = it.next();
                        SchemaTable hasContainerLabelSchemaTable = getIDContainerSchemaTable(schemaTableTree, value);
                        if (hasContainerLabelSchemaTable.equals(schemaTableTree.getSchemaTable())) {
                            ok.add(value);
                        }
                    }
                    if (ok.isEmpty()) {
                        return true;
                    }
                    ((P<Collection<Object>>) (hasContainer.getPredicate())).setValue(ok);
                }
            } else {
                if (hasContainer.getBiPredicate() instanceof FullText && ((FullText) hasContainer.getBiPredicate()).getQuery() != null) {
                    return false;
                }
                // check if the hasContainer is for a property that exists, if not remove this node from the query tree
                if (!this.getFilteredAllTables().get(schemaTableTree.getSchemaTable().toString()).containsKey(hasContainer.getKey())) {
                    if (!Existence.NULL.equals(hasContainer.getBiPredicate())) {
                        return true;
                    }
                }
                // Check if it is a Contains.within with a empty list of values
                if (hasEmptyWithin(hasContainer)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) FullText(org.umlg.sqlg.predicate.FullText)

Aggregations

FullText (org.umlg.sqlg.predicate.FullText)5 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)4 Text (org.umlg.sqlg.predicate.Text)2 AndOrHasContainer (org.umlg.sqlg.sql.parse.AndOrHasContainer)2 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 LoopTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.LoopTraversal)1 WherePredicateStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep)1 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)1 ConnectiveP (org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP)1 OrP (org.apache.tinkerpop.gremlin.process.traversal.util.OrP)1 Test (org.junit.Test)1 ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)1 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)1 SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)1 BaseTest (org.umlg.sqlg.test.BaseTest)1