Search in sources :

Example 11 with JanusGraphElement

use of org.janusgraph.core.JanusGraphElement in project janusgraph by JanusGraph.

the class AdjacentVertexHasUniquePropertyOptimizerStrategy method isValidStep.

/**
 * Determines whether this HasStep can be answered by a unique index and thus, returns either 0 or 1 match
 */
@Override
protected boolean isValidStep(HasStep<?> step) {
    StandardJanusGraphTx tx = (StandardJanusGraphTx) JanusGraphTraversalUtil.getTx(step.getTraversal());
    List<String> givenKeys = step.getHasContainers().stream().map(HasContainer::getKey).collect(Collectors.toList());
    List<PredicateCondition<String, JanusGraphElement>> constraints = step.getHasContainers().stream().filter(hc -> hc.getBiPredicate() == Compare.eq).map(hc -> new PredicateCondition<>(hc.getKey(), JanusGraphPredicateUtils.convert(hc.getBiPredicate()), hc.getValue())).filter(pc -> tx.validDataType(pc.getValue().getClass())).collect(Collectors.toList());
    final MultiCondition<JanusGraphElement> conditions = QueryUtil.constraints2QNF(tx, constraints);
    // check all matching unique indexes
    return IndexSelectionUtil.existsMatchingIndex(conditions, indexType -> indexType.isCompositeIndex() && ((CompositeIndexType) indexType).getCardinality() == Cardinality.SINGLE && IndexSelectionUtil.isIndexSatisfiedByGivenKeys(indexType, givenKeys));
}
Also used : PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) MultiCondition(org.janusgraph.graphdb.query.condition.MultiCondition) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep) FilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.FilterStep) HashSet(java.util.HashSet) Cardinality(org.janusgraph.core.Cardinality) IndexSelectionUtil(org.janusgraph.graphdb.query.index.IndexSelectionUtil) P(org.apache.tinkerpop.gremlin.process.traversal.P) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Edge(org.apache.tinkerpop.gremlin.structure.Edge) JanusGraphElement(org.janusgraph.core.JanusGraphElement) QueryUtil(org.janusgraph.graphdb.query.QueryUtil) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) Set(java.util.Set) JanusGraphPredicateUtils(org.janusgraph.graphdb.query.JanusGraphPredicateUtils) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Collectors(java.util.stream.Collectors) JanusGraphTraversalUtil(org.janusgraph.graphdb.tinkerpop.optimize.JanusGraphTraversalUtil) NoneStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.NoneStep) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) List(java.util.List) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) IdStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.IdStep) ADJACENT_ID(org.janusgraph.graphdb.types.system.ImplicitKey.ADJACENT_ID) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) JanusGraphElement(org.janusgraph.core.JanusGraphElement) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType)

Example 12 with JanusGraphElement

use of org.janusgraph.core.JanusGraphElement in project janusgraph by JanusGraph.

the class IndexSerializer method getQuery.

public IndexQuery getQuery(final MixedIndexType index, final Condition condition, final OrderList orders) {
    final Condition newCondition = ConditionUtil.literalTransformation(condition, new Function<Condition<JanusGraphElement>, Condition<JanusGraphElement>>() {

        @Nullable
        @Override
        public Condition<JanusGraphElement> apply(final Condition<JanusGraphElement> condition) {
            Preconditions.checkArgument(condition instanceof PredicateCondition);
            final PredicateCondition pc = (PredicateCondition) condition;
            final PropertyKey key = (PropertyKey) pc.getKey();
            return new PredicateCondition<>(key2Field(index, key), pc.getPredicate(), pc.getValue());
        }
    });
    ImmutableList<IndexQuery.OrderEntry> newOrders = IndexQuery.NO_ORDER;
    if (!orders.isEmpty() && IndexSelectionUtil.indexCoversOrder(index, orders)) {
        final ImmutableList.Builder<IndexQuery.OrderEntry> lb = ImmutableList.builder();
        for (int i = 0; i < orders.size(); i++) {
            lb.add(new IndexQuery.OrderEntry(key2Field(index, orders.getKey(i)), orders.getOrder(i), orders.getKey(i).dataType()));
        }
        newOrders = lb.build();
    }
    return new IndexQuery(index.getStoreName(), newCondition, newOrders);
}
Also used : PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) Condition(org.janusgraph.graphdb.query.condition.Condition) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) JointIndexQuery(org.janusgraph.graphdb.query.graph.JointIndexQuery) ImmutableList(com.google.common.collect.ImmutableList) JanusGraphElement(org.janusgraph.core.JanusGraphElement) Nullable(javax.annotation.Nullable) PropertyKey(org.janusgraph.core.PropertyKey)

Example 13 with JanusGraphElement

use of org.janusgraph.core.JanusGraphElement in project janusgraph by JanusGraph.

the class IndexSerializerTest method testReindexElementAppliesToWithEntries.

@Test
public void testReindexElementAppliesToWithEntries() {
    Map<String, Map<String, List<IndexEntry>>> docStore = new HashMap<>();
    IndexSerializer mockSerializer = mockSerializer();
    MixedIndexType mit = mock(MixedIndexType.class);
    JanusGraphElement indexableElement = mockIndexAppliesTo(mit, true);
    assertTrue("re-index", mockSerializer.reindexElement(indexableElement, mit, docStore));
    assertEquals("doc store size", 1, docStore.size());
}
Also used : HashMap(java.util.HashMap) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphElement(org.janusgraph.core.JanusGraphElement) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 14 with JanusGraphElement

use of org.janusgraph.core.JanusGraphElement in project janusgraph by JanusGraph.

the class IndexSerializerTest method testReindexElementAppliesToNoEntries.

@Test
public void testReindexElementAppliesToNoEntries() {
    Map<String, Map<String, List<IndexEntry>>> docStore = new HashMap<>();
    IndexSerializer mockSerializer = mockSerializer();
    MixedIndexType mit = mock(MixedIndexType.class);
    JanusGraphElement indexableElement = mockIndexAppliesTo(mit, false);
    assertFalse("re-index", mockSerializer.reindexElement(indexableElement, mit, docStore));
    assertEquals("doc store size", 0, docStore.size());
}
Also used : HashMap(java.util.HashMap) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphElement(org.janusgraph.core.JanusGraphElement) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 15 with JanusGraphElement

use of org.janusgraph.core.JanusGraphElement in project janusgraph by JanusGraph.

the class IndexSerializerTest method mockIndexableElement.

private JanusGraphElement mockIndexableElement(String key, String value, boolean indexable) {
    StandardJanusGraphTx tx = mock(StandardJanusGraphTx.class);
    JanusGraphElement indexableElement = spy(new StandardVertex(tx, 1L, ElementLifeCycle.New));
    Property pk2 = indexableElement.property(key, value);
    Iterator it = Arrays.asList(pk2).iterator();
    doReturn(it).when(indexableElement).properties(key);
    if (indexable)
        doReturn(Arrays.asList(value).iterator()).when(indexableElement).values(key);
    else
        // skpping the values section!!
        doReturn(new ArrayList<>().iterator()).when(indexableElement).values(key);
    return indexableElement;
}
Also used : JanusGraphElement(org.janusgraph.core.JanusGraphElement) StandardVertex(org.janusgraph.graphdb.vertices.StandardVertex) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) Iterator(java.util.Iterator) Property(org.apache.tinkerpop.gremlin.structure.Property)

Aggregations

JanusGraphElement (org.janusgraph.core.JanusGraphElement)17 Map (java.util.Map)8 PropertyKey (org.janusgraph.core.PropertyKey)8 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 List (java.util.List)6 MixedIndexType (org.janusgraph.graphdb.types.MixedIndexType)6 Preconditions (com.google.common.base.Preconditions)5 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 Set (java.util.Set)5 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)5 RelationType (org.janusgraph.core.RelationType)5 IndexEntry (org.janusgraph.diskstorage.indexing.IndexEntry)5 ElementCategory (org.janusgraph.graphdb.internal.ElementCategory)5 OrderList (org.janusgraph.graphdb.internal.OrderList)5 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)5 Test (org.junit.jupiter.api.Test)5 Iterator (java.util.Iterator)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4