Search in sources :

Example 51 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class AndOrHasContainer method setParameterOnStatement.

public void setParameterOnStatement(Multimap<String, Object> keyValueMap) {
    for (HasContainer hasContainer : this.hasContainers) {
        WhereClause whereClause = WhereClause.from(hasContainer.getPredicate());
        whereClause.putKeyValueMap(hasContainer, keyValueMap);
    }
    for (AndOrHasContainer andOrHasContainer : this.andOrHasContainers) {
        andOrHasContainer.setParameterOnStatement(keyValueMap);
    }
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 52 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class ReplacedStep method groupIdsBySchemaTable.

/**
 * Groups the idHasContainers by SchemaTable.
 * Each SchemaTable has a list representing the idHasContainers with the relevant BiPredicate and RecordId
 *
 * @return
 */
private Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> groupIdsBySchemaTable() {
    Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> result = new HashMap<>();
    for (HasContainer idHasContainer : this.idHasContainers) {
        Map<SchemaTable, Boolean> newHasContainerMap = new HashMap<>();
        P<Object> idPredicate = (P<Object>) idHasContainer.getPredicate();
        BiPredicate biPredicate = idHasContainer.getBiPredicate();
        // This is statement is for g.V().hasId(Collection) where the logic is actually P.within not P.eq
        if (biPredicate == Compare.eq && idPredicate.getValue() instanceof Collection && ((Collection) idPredicate.getValue()).size() > 1) {
            biPredicate = Contains.within;
        }
        Multimap<BiPredicate, RecordId> biPredicateRecordIdMultimap;
        if (idPredicate.getValue() instanceof Collection) {
            Collection<Object> ids = (Collection<Object>) idPredicate.getValue();
            for (Object id : ids) {
                RecordId recordId = RecordId.from(id);
                List<Multimap<BiPredicate, RecordId>> biPredicateRecordIdList = result.get(recordId.getSchemaTable());
                Boolean newHasContainer = newHasContainerMap.get(recordId.getSchemaTable());
                if (biPredicateRecordIdList == null) {
                    biPredicateRecordIdList = new ArrayList<>();
                    biPredicateRecordIdMultimap = LinkedListMultimap.create();
                    biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
                    result.put(recordId.getSchemaTable(), biPredicateRecordIdList);
                    newHasContainerMap.put(recordId.getSchemaTable(), false);
                } else if (newHasContainer == null) {
                    biPredicateRecordIdMultimap = LinkedListMultimap.create();
                    biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
                    newHasContainerMap.put(recordId.getSchemaTable(), false);
                }
                biPredicateRecordIdMultimap = biPredicateRecordIdList.get(biPredicateRecordIdList.size() - 1);
                biPredicateRecordIdMultimap.put(biPredicate, recordId);
            }
        } else {
            Object id = idPredicate.getValue();
            RecordId recordId = RecordId.from(id);
            List<Multimap<BiPredicate, RecordId>> biPredicateRecordIdList = result.computeIfAbsent(recordId.getSchemaTable(), k -> new ArrayList<>());
            biPredicateRecordIdMultimap = LinkedListMultimap.create();
            biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
            biPredicateRecordIdMultimap.put(biPredicate, recordId);
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) P(org.apache.tinkerpop.gremlin.process.traversal.P) Multimap(com.google.common.collect.Multimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) BiPredicate(java.util.function.BiPredicate)

Example 53 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class ReplacedStep method getRootSchemaTableTrees.

/**
 * Calculates the root labels from which to start the query construction.
 * <p>
 * The hasContainers at this stage contains the {@link TopologyStrategy} from or without hasContainer.
 * After doing the filtering it must be removed from the hasContainers as it must not partake in sql generation.
 *
 * @return A set of SchemaTableTree. A SchemaTableTree for each root label.
 */
Set<SchemaTableTree> getRootSchemaTableTrees(SqlgGraph sqlgGraph, int replacedStepDepth) {
    Preconditions.checkState(this.isGraphStep(), "ReplacedStep must be for a GraphStep!");
    Set<SchemaTableTree> result = new HashSet<>();
    final GraphStep graphStep = (GraphStep) this.step;
    final boolean isVertex = graphStep.getReturnClass().isAssignableFrom(Vertex.class);
    final boolean isEdge = !isVertex;
    // RecordIds grouped by SchemaTable
    Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> groupedIds = null;
    if (!this.idHasContainers.isEmpty()) {
        groupedIds = groupIdsBySchemaTable();
    }
    // All tables depending on the strategy, topology tables only or the rest.
    Map<String, Map<String, PropertyType>> filteredAllTables = SqlgUtil.filterSqlgSchemaHasContainers(this.topology, this.hasContainers, false);
    // Optimization for the simple case of only one label specified.
    if (isVertex && this.labelHasContainers.size() == 1 && this.labelHasContainers.get(0).getBiPredicate() == Compare.eq) {
        HasContainer labelHasContainer = this.labelHasContainers.get(0);
        String table = (String) labelHasContainer.getValue();
        SchemaTable schemaTableWithPrefix = SchemaTable.from(sqlgGraph, table).withPrefix(isVertex ? VERTEX_PREFIX : EDGE_PREFIX);
        if (filteredAllTables.containsKey(schemaTableWithPrefix.toString())) {
            collectSchemaTableTrees(sqlgGraph, replacedStepDepth, result, groupedIds, schemaTableWithPrefix.toString());
        }
    } else {
        for (String table : filteredAllTables.keySet()) {
            // if graphStep's return class is Vertex ignore all edges and vice versa.
            if ((isVertex && table.substring(table.indexOf(".") + 1).startsWith(VERTEX_PREFIX)) || (isEdge && table.substring(table.indexOf(".") + 1).startsWith(EDGE_PREFIX))) {
                if (passesLabelHasContainers(sqlgGraph, isVertex, table)) {
                    collectSchemaTableTrees(sqlgGraph, replacedStepDepth, result, groupedIds, table);
                }
            }
        }
    }
    return result;
}
Also used : GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) BiPredicate(java.util.function.BiPredicate)

Example 54 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer 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)

Example 55 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class SqlgHasStep method clone.

@Override
public SqlgHasStep<S> clone() {
    final SqlgHasStep<S> clone = (SqlgHasStep<S>) super.clone();
    clone.hasContainers = new ArrayList<>();
    for (final HasContainer hasContainer : this.hasContainers) {
        clone.addHasContainer(hasContainer.clone());
    }
    return clone;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Aggregations

HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)56 AndOrHasContainer (org.umlg.sqlg.sql.parse.AndOrHasContainer)13 P (org.apache.tinkerpop.gremlin.process.traversal.P)9 Collectors (java.util.stream.Collectors)7 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)6 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)6 FullText (org.umlg.sqlg.predicate.FullText)6 PredicatesHolder (org.unipop.query.predicates.PredicatesHolder)6 java.util (java.util)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 BiPredicate (java.util.function.BiPredicate)5 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)5 JSONObject (org.json.JSONObject)5 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)4 BasicVertexCentricQueryBuilder (org.janusgraph.graphdb.query.vertex.BasicVertexCentricQueryBuilder)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)3 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)3 ConnectiveP (org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP)3