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));
}
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);
}
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());
}
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());
}
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;
}
Aggregations