static List<HasContainer> splitAndP(final List<HasContainer> hasContainers, final Iterable<HasContainer> has) { has.forEach(hasContainer -> { if (hasContainer.getPredicate() instanceof AndP) { for (final P<?> predicate : ((AndP<?>) hasContainer.getPredicate()).getPredicates()) { hasContainers.add(new HasContainer(hasContainer.getKey(), predicate)); } } else hasContainers.add(hasContainer); }); return hasContainers; }
public static HasContainer convert(final HasContainer container){ if (!(container.getPredicate() instanceof ConnectiveP)) { return container; } final ConnectiveJanusPredicate connectivePredicate = instanceConnectiveJanusPredicate(container.getPredicate()); return new HasContainer(container.getKey(), new ConnectiveJanusGraphP(connectivePredicate, convert(((ConnectiveP<?>) container.getPredicate()), connectivePredicate))); }
static boolean validJanusGraphHas(HasContainer has) { if (has.getPredicate() instanceof ConnectiveP) { final List<? extends P<?>> predicates = ((ConnectiveP<?>) has.getPredicate()).getPredicates(); return predicates.stream().allMatch(p-> validJanusGraphHas(new HasContainer(has.getKey(), p))); } else { return JanusGraphPredicate.Converter.supports(has.getBiPredicate()); } }
@Override public void addHasContainer(final HasContainer hasContainer) { if (hasContainer.getPredicate() instanceof AndP) { for (final P<?> predicate : ((AndP<?>) hasContainer.getPredicate()).getPredicates()) { this.addHasContainer(new HasContainer(hasContainer.getKey(), predicate)); } } else this.hasContainers.add(hasContainer); }
@Override public void addHasContainer(final HasContainer hasContainer) { if (hasContainer.getPredicate() instanceof AndP) { for (final P<?> predicate : ((AndP<?>) hasContainer.getPredicate()).getPredicates()) { this.addHasContainer(new HasContainer(hasContainer.getKey(), predicate)); } } else this.hasContainers.add(hasContainer); }
private HasContainer getIndexKey(final Class<? extends Element> indexedClass) { final Set<String> indexedKeys = ((TinkerGraph) this.getTraversal().getGraph().get()).getIndexedKeys(indexedClass); final Iterator<HasContainer> itty = IteratorUtils.filter(hasContainers.iterator(), c -> c.getPredicate().getBiPredicate() == Compare.eq && indexedKeys.contains(c.getKey())); return itty.hasNext() ? itty.next() : null; }
private Iterator<? extends Edge> edges() { final TinkerGraph graph = (TinkerGraph) this.getTraversal().getGraph().get(); final HasContainer indexedContainer = getIndexKey(Edge.class); // ids are present, filter on them first if (null == this.ids) return Collections.emptyIterator(); else if (this.ids.length > 0) return this.iteratorList(graph.edges(this.ids)); else return null == indexedContainer ? this.iteratorList(graph.edges()) : TinkerHelper.queryEdgeIndex(graph, indexedContainer.getKey(), indexedContainer.getPredicate().getValue()).stream() .filter(edge -> HasContainer.testAll(edge, this.hasContainers)) .collect(Collectors.<Edge>toList()).iterator(); }
private static void convPredicateValue(HugeGraph graph, HasContainer has) { // No need to convert if key is sysprop if (isSysProp(has.getKey())) { return; } PropertyKey pkey = graph.propertyKey(has.getKey()); List<P<Object>> leafPredicates = new ArrayList<>(); collectPredicates(leafPredicates, ImmutableList.of(has.getPredicate())); for (P<Object> predicate : leafPredicates) { Object value = validPredicateValue(predicate.getValue(), pkey); predicate.setValue(value); } }
public static Condition convAnd(HugeGraph graph, HugeType type, HasContainer has) { P<?> p = has.getPredicate(); assert p instanceof AndP; @SuppressWarnings("unchecked") List<P<Object>> predicates = ((AndP<Object>) p).getPredicates(); if (predicates.size() < 2) { throw newUnsupportedPredicate(p); } Condition cond = null; for (P<Object> predicate : predicates) { HasContainer newHas = new HasContainer(has.getKey(), predicate); Condition newCond = convHas2Condition(newHas, type, graph); if (cond == null) { cond = newCond; } else { cond = Condition.and(newCond, cond); } } return cond; }
public static Condition convOr(HugeGraph graph, HugeType type, HasContainer has) { P<?> p = has.getPredicate(); assert p instanceof OrP; @SuppressWarnings("unchecked") List<P<Object>> predicates = ((OrP<Object>) p).getPredicates(); if (predicates.size() < 2) { throw newUnsupportedPredicate(p); } Condition cond = null; for (P<Object> predicate : predicates) { HasContainer newHas = new HasContainer(has.getKey(), predicate); Condition newCond = convHas2Condition(newHas, type, graph); if (cond == null) { cond = newCond; } else { cond = Condition.or(newCond, cond); } } return cond; }
private Iterator<? extends Vertex> vertices() { final TinkerGraph graph = (TinkerGraph) this.getTraversal().getGraph().get(); final HasContainer indexedContainer = getIndexKey(Vertex.class); // ids are present, filter on them first if (null == this.ids) return Collections.emptyIterator(); else if (this.ids.length > 0) return this.iteratorList(graph.vertices(this.ids)); else return null == indexedContainer ? this.iteratorList(graph.vertices()) : IteratorUtils.filter(TinkerHelper.queryVertexIndex(graph, indexedContainer.getKey(), indexedContainer.getPredicate().getValue()).iterator(), vertex -> HasContainer.testAll(vertex, this.hasContainers)); }
label = hasContainers.stream() .filter(hasContainer -> hasContainer.getKey().equals(T.label.getAccessor())) .filter(hasContainer -> hasContainer.getPredicate() instanceof LabelP) .map(hasContainer -> (String) hasContainer.getValue()) .findAny();
private static Relation convCompare2Relation(HugeGraph graph, HugeType type, HasContainer has) { assert type.isGraph(); BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate(); assert bp instanceof Compare; boolean isSyspropKey = true; try { string2HugeKey(has.getKey()); } catch (IllegalArgumentException ignored) { isSyspropKey = false; } return isSyspropKey ? convCompare2SyspropRelation(graph, type, has) : convCompare2UserpropRelation(graph, type, has); }
public static Condition convIn2Relation(HugeGraph graph, HugeType type, HasContainer has) { BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate(); assert bp instanceof Contains; List<?> values = (List<?>) has.getValue(); throw newUnsupportedPredicate(has.getPredicate());
private static Condition convRelationType2Relation(HugeGraph graph, HugeType type, HasContainer has) { assert type.isGraph(); BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate(); assert bp instanceof RelationType; String key = has.getKey(); PropertyKey pkey = graph.propertyKey(key); Id pkeyId = pkey.id(); Object value = validPredicateValue(has.getValue(), pkey); return new Condition.UserpropRelation(pkeyId, (RelationType) bp, value); }
private static Relation convCompare2SyspropRelation(HugeGraph graph, HugeType type, HasContainer has) { BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate(); assert bp instanceof Compare; HugeKeys key = string2HugeKey(has.getKey()); Object value = convSysValueIfNeeded(graph, type, key, has.getValue()); switch ((Compare) bp) { case eq: return Condition.eq(key, value); case gt: return Condition.gt(key, value); case gte: return Condition.gte(key, value); case lt: return Condition.lt(key, value); case lte: return Condition.lte(key, value); case neq: return Condition.neq(key, value); } throw newUnsupportedPredicate(has.getPredicate()); }
hasContainer.getPredicate() : p.and(hasContainer.getPredicate());
private static Relation convCompare2UserpropRelation(HugeGraph graph, HugeType type, HasContainer has) { BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate(); assert bp instanceof Compare; String key = has.getKey(); PropertyKey pkey = graph.propertyKey(key); Id pkeyId = pkey.id(); Object value = validPredicateValue(has.getValue(), pkey); switch ((Compare) bp) { case eq: return Condition.eq(pkeyId, value); case gt: return Condition.gt(pkeyId, value); case gte: return Condition.gte(pkeyId, value); case lt: return Condition.lt(pkeyId, value); case lte: return Condition.lte(pkeyId, value); case neq: return Condition.neq(pkeyId, value); } throw newUnsupportedPredicate(has.getPredicate()); }
public static Condition convHas2Condition(HasContainer has, HugeType type, HugeGraph graph) { P<?> p = has.getPredicate(); E.checkArgument(p != null, "The predicate of has(%s) is null", has); BiPredicate<?, ?> bp = p.getBiPredicate(); Condition condition; if (keyForContainsKeyOrValue(has.getKey())) { condition = convContains2Relation(graph, has); } else if (bp instanceof Compare) { condition = convCompare2Relation(graph, type, has); } else if (bp instanceof RelationType) { condition = convRelationType2Relation(graph, type, has); } else if (bp instanceof Contains) { condition = convIn2Relation(graph, type, has); } else if (p instanceof AndP) { condition = convAnd(graph, type, has); } else if (p instanceof OrP) { condition = convOr(graph, type, has); } else { // TODO: deal with other Predicate throw newUnsupportedPredicate(p); } return condition; }
public static Condition convContains2Relation(HugeGraph graph, HasContainer has) { // Convert contains-key or contains-value BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate(); E.checkArgument(bp == Compare.eq, "CONTAINS query with relation '%s' is not supported", bp); HugeKeys key = string2HugeKey(has.getKey()); Object value = has.getValue(); if (keyForContainsKey(has.getKey())) { if (value instanceof String) { value = graph.propertyKey((String) value).id(); } return Condition.containsKey(key, value); } else { assert keyForContainsValue(has.getKey()); return Condition.contains(key, value); } }