@Test public void testFlattenWithOr() { Condition c1 = Condition.eq(IdGenerator.of("c1"), "1"); Condition c2 = Condition.eq(IdGenerator.of("c2"), "2"); ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.query(c1.or(c2)); Assert.assertEquals(1, query.conditions().size()); List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query); Assert.assertEquals(2, queries.size()); Set<Set<Condition>> expect; expect = ImmutableSet.of(ImmutableSet.of(c1), ImmutableSet.of(c2)); Set<Set<Condition>> actual = new HashSet<>(); for (ConditionQuery q : queries) { actual.add(q.conditions()); } Assert.assertEquals(expect, actual); }
public static void verifyEdgesConditionQuery(ConditionQuery query) { assert query.resultType().isEdge(); int total = query.conditions().size(); if (total == 1) { // Supported: 1.query just by edge label, 2.query with scan if (query.containsCondition(HugeKeys.LABEL) || query.containsScanCondition()) { return; } } int matched = 0; for (HugeKeys key : EdgeId.KEYS) { Object value = query.condition(key); if (value == null) { break; } matched++; } if (matched != total) { throw new BackendException( "Not supported querying edges by %s, expect %s", query.conditions(), EdgeId.KEYS[matched]); } }
private HugeVertex queryVariableVertex(String key) { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); query.eq(HugeKeys.LABEL, vl.id()); PropertyKey pkey = this.graph.propertyKey(Hidden.hide(VARIABLE_KEY)); query.query(Condition.eq(pkey.id(), key)); query.showHidden(true); Iterator<Vertex> vertices = this.graph.vertices(query); if (!vertices.hasNext()) { return null; } return (HugeVertex) vertices.next(); }
private static ConditionQuery newQueryFromRelations(ConditionQuery query, Relations relations) { ConditionQuery cq = query.copy(); cq.resetConditions(); for (Relation relation : relations) { cq.query(relation); } return cq; }
public Iterator<Edge> queryVertexEdge(Object id, Directions direction) { ConditionQuery q = new ConditionQuery(HugeType.EDGE); q.eq(HugeKeys.OWNER_VERTEX, id); q.eq(HugeKeys.DIRECTION, direction); return this.hugegraph.edges(q); } }
public static ConditionQuery fillConditionQuery( List<HasContainer> hasContainers, ConditionQuery query, HugeGraph graph) { HugeType resultType = query.resultType(); for (HasContainer has : hasContainers) { Condition condition = convHas2Condition(has, resultType, graph); query.query(condition); } return query; }
public Query query(CassandraSessionPool.Session session, Query query) { Set<Condition> conditions = query.conditions(); if (!(query instanceof ConditionQuery) || conditions.isEmpty()) { return query; } ConditionQuery q = ((ConditionQuery) query).copy(); String name = (String) q.condition(HugeKeys.NAME); if (name != null && q.allSysprop() && conditions.size() == 1 && q.containsCondition(HugeKeys.NAME, Condition.RelationType.EQ)) { Set<Integer> ids = queryByNameIndex(session, this.table, name); if (ids.isEmpty()) { // Not found data with the specified label return null; } q.resetConditions(); for (Integer id : ids) { q.query(IdGenerator.of(id)); } } return q; }
protected Query optimizeQuery(ConditionQuery query) { Id label = (Id) query.condition(HugeKeys.LABEL); if (label != null && query.resultType().isVertex()) { VertexLabel vertexLabel = this.graph().vertexLabel(label); if (vertexLabel.idStrategy() == IdStrategy.PRIMARY_KEY) { "'%s' id strategy for vertex label '%s'", IdStrategy.PRIMARY_KEY, vertexLabel.name()); if (query.matchUserpropKeys(keys)) { query.optimized(OptimizedType.PRIMARY_KEY.ordinal()); String primaryValues = query.userpropValuesString(keys); LOG.debug("Query vertices by primaryKeys: {}", query); if (label != null && query.resultType().isEdge()) { List<Id> keys = this.graph().edgeLabel(label).sortKeys(); if (query.condition(HugeKeys.OWNER_VERTEX) != null && query.condition(HugeKeys.DIRECTION) != null && !keys.isEmpty() && query.matchUserpropKeys(keys)) { query.optimized(OptimizedType.SORT_KEYS.ordinal()); query = query.copy(); for (Condition.Relation r : query.userpropRelations()) { if (!keys.contains(r.key())) { continue; if (query.hasRangeCondition()) { for (Condition condition : query.userpropConditions()) {
protected Query optimizeQuery(ConditionQuery query) { Id label = (Id) query.condition(HugeKeys.LABEL); if (label != null && query.resultType().isVertex()) { VertexLabel vertexLabel = this.graph().vertexLabel(label); if (vertexLabel.idStrategy() == IdStrategy.PRIMARY_KEY) { "'%s' id strategy for vertex label '%s'", IdStrategy.PRIMARY_KEY, vertexLabel.name()); if (query.matchUserpropKeys(keys)) { query.optimized(OptimizedType.PRIMARY_KEY.ordinal()); String primaryValues = query.userpropValuesString(keys); LOG.debug("Query vertices by primaryKeys: {}", query); if (label != null && query.resultType().isEdge()) { List<Id> keys = this.graph().edgeLabel(label).sortKeys(); if (query.condition(HugeKeys.OWNER_VERTEX) != null && query.condition(HugeKeys.DIRECTION) != null && !keys.isEmpty() && query.matchUserpropKeys(keys)) { query.optimized(OptimizedType.SORT_KEY.ordinal()); query = query.copy(); query.eq(HugeKeys.SORT_VALUES, query.userpropValuesString(keys)); query.resetUserpropConditions(); LOG.debug("Query edges by sortKeys: {}", query); return query; if (query.allSysprop()) {
private static ConditionQuery matchIndexLabel(ConditionQuery query, IndexLabel indexLabel) { IndexType indexType = indexLabel.indexType(); boolean requireRange = query.hasRangeCondition(); boolean isRange = indexType == IndexType.RANGE; if (requireRange && !isRange) { Set<Id> queryKeys = query.userpropKeys(); List<Id> indexFields = indexLabel.indexFields(); if (!matchIndexFields(queryKeys, indexFields)) { "Invalid index fields size for %s: %s", indexType, indexFields); Object fieldValue = query.userpropValue(indexFields.get(0)); assert fieldValue instanceof String; indexQuery = new ConditionQuery(indexType.type(), query); indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id()); indexQuery.eq(HugeKeys.FIELD_VALUES, fieldValue); break; case SECONDARY: List<Id> joinedKeys = indexFields.subList(0, queryKeys.size()); String joinedValues = query.userpropValuesString(joinedKeys); indexQuery = new ConditionQuery(indexType.type(), query); indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id()); indexQuery.eq(HugeKeys.FIELD_VALUES, joinedValues); break; case RANGE: if (query.userpropConditions().size() > 2) {
private Query writeRangeIndexQuery(ConditionQuery query) { Id index = (Id) query.condition(HugeKeys.INDEX_LABEL_ID); E.checkArgument(index != null, "Please specify the index label"); List<Condition> fields = query.syspropConditions(HugeKeys.FIELD_VALUES); E.checkArgument(!fields.isEmpty(), "Please specify the index field values"); HugeType type = query.resultType(); if (keyEq != null) { Id id = formatIndexId(type, index, keyEq); IdQuery idQuery = new IdQuery(query, id); idQuery.limit(query.limit()); idQuery.offset(query.offset()); return idQuery; query = query.copy(); query.resetConditions(); query.gte(HugeKeys.ID, min); } else { query.gt(HugeKeys.ID, min); query.prefix(HugeKeys.ID, prefix); } else { Id max = formatIndexId(type, index, keyMax); if (keyMaxEq) { query.lte(HugeKeys.ID, max); } else {
private Query writeStringIndexQuery(ConditionQuery query) { E.checkArgument(query.allSysprop() && query.conditions().size() == 2, "There should be two conditions: " + "INDEX_LABEL_ID and FIELD_VALUES" + "in secondary index query"); Id index = (Id) query.condition(HugeKeys.INDEX_LABEL_ID); Object key = query.condition(HugeKeys.FIELD_VALUES); E.checkArgument(index != null, "Please specify the index label"); E.checkArgument(key != null, "Please specify the index key"); Id id = formatIndexId(query.resultType(), index, key); IdQuery idQuery = new IdQuery(query, id); idQuery.limit(query.limit()); idQuery.offset(query.offset()); return idQuery; }
@Watched(prefix = "index") private Set<Id> queryByLabel(ConditionQuery query) { HugeType queryType = query.resultType(); IndexLabel il = IndexLabel.label(queryType); Id label = (Id) query.condition(HugeKeys.LABEL); assert label != null; SchemaLabel schemaLabel; if (queryType.isVertex()) { schemaLabel = this.graph().vertexLabel(label); } else if (queryType.isEdge()) { schemaLabel = this.graph().edgeLabel(label); } else { throw new BackendException("Can't query %s by label", queryType); } if (!this.store().features().supportsQueryByLabel() && !schemaLabel.enableLabelIndex()) { throw new NoIndexException("Don't accept query by label '%s', " + "it disables label index", schemaLabel); } ConditionQuery indexQuery; indexQuery = new ConditionQuery(HugeType.SECONDARY_INDEX, query); indexQuery.eq(HugeKeys.INDEX_LABEL_ID, il.id()); indexQuery.eq(HugeKeys.FIELD_VALUES, label); // Set offset and limit to avoid redundant element ids indexQuery.limit(query.limit()); indexQuery.offset(query.offset()); indexQuery.capacity(query.capacity()); return this.doIndexQuery(il, indexQuery); }
String fieldValue = (String) query.userpropValue(indexField); Set<String> words = this.segmentWords(fieldValue); indexFields.add(indexField); query = query.copy(); query.unsetCondition(indexField); query.query(Condition.textContainsAny(indexField, words)); query.registerResultsFilter(elem -> { for (Condition cond : originQuery.conditions()) { Object key = cond.isRelation() ? ((Relation) cond).key() : null; if (key instanceof Id && indexFields.contains(key)) { String fvalue = (String) originQuery.userpropValue(field); if (this.matchSearchIndexWords(propValue, fvalue)) { continue;
@Watched(prefix = "index") private Iterator<BackendEntry> queryByName(ConditionQuery query) { if (!this.needIndexForName()) { return super.query(query); } IndexLabel il = IndexLabel.label(query.resultType()); String name = (String) query.condition(HugeKeys.NAME); E.checkState(name != null, "The name in condition can't be null " + "when querying schema by name"); ConditionQuery indexQuery; indexQuery = new ConditionQuery(HugeType.SECONDARY_INDEX, query); indexQuery.eq(HugeKeys.FIELD_VALUES, name); indexQuery.eq(HugeKeys.INDEX_LABEL_ID, il.id()); Iterator<BackendEntry> entries = super.query(indexQuery); IdQuery idQuery = new IdQuery(query.resultType(), query); while (entries.hasNext()) { HugeIndex index = this.serializer.readIndex(graph(), indexQuery, entries.next()); idQuery.query(index.elementIds()); } if (idQuery.ids().isEmpty()) { return Collections.emptyIterator(); } assert idQuery.ids().size() == 1 : idQuery.ids(); return super.query(idQuery); } }
ConditionQuery q = new ConditionQuery(HugeType.VERTEX); q.query(IdGenerator.of(authorId)); PropertyKey age = graph.propertyKey("age"); q.key(HugeKeys.PROPERTIES, age.id()); if (graph.graphTransaction().store().features() .supportsQueryWithContainsKey()) { q = new ConditionQuery(HugeType.EDGE); q.eq(HugeKeys.OWNER_VERTEX, IdGenerator.of(authorId)); q.eq(HugeKeys.DIRECTION, Directions.OUT); q.eq(HugeKeys.LABEL, authored.id()); q.eq(HugeKeys.SORT_VALUES, ""); q.eq(HugeKeys.OTHER_VERTEX, IdGenerator.of(book1Id));
public Query indexQuery(ConditionQuery query) { query.checkFlattened(); List<Condition> conds = query.syspropConditions(); if (conds.size() > 1 || (conds.size() == 1 && !query.containsCondition(HugeKeys.LABEL))) { throw new BackendException("Can't do index query with %s", conds); query.optimized(OptimizedType.INDEX.ordinal()); Set<Id> ids; if (query.allSysprop() && conds.size() == 1 && query.containsCondition(HugeKeys.LABEL)) {
HugeType type = label.type() == HugeType.VERTEX_LABEL ? HugeType.VERTEX : HugeType.EDGE; ConditionQuery query = new ConditionQuery(type); query.showHidden(true); query.capacity(Query.NO_CAPACITY); Iterator<T> itor = fetcher.apply(query); while (itor.hasNext()) { query.limit(Query.DEFAULT_CAPACITY); query.capacity(Query.NO_CAPACITY); query.eq(HugeKeys.LABEL, label.id()); int pass = 0; int counter = 0; do { query.offset(pass++ * Query.DEFAULT_CAPACITY);
"The edge query must contain direction"); ConditionQuery query = new ConditionQuery(HugeType.EDGE); query.eq(HugeKeys.OWNER_VERTEX, sourceVertex); query.query(Condition.or( Condition.eq(HugeKeys.DIRECTION, Directions.OUT), Condition.eq(HugeKeys.DIRECTION, Directions.IN))); } else { assert direction == Directions.OUT || direction == Directions.IN; query.eq(HugeKeys.DIRECTION, direction); query.eq(HugeKeys.LABEL, edgeLabels[0]); } else if (edgeLabels.length > 1) { query.query(Condition.in(HugeKeys.LABEL, Arrays.asList(edgeLabels))); } else {
@Override protected RowIterator queryByCond(Session session, ConditionQuery query) { assert !query.conditions().isEmpty(); List<Condition> conds = query.syspropConditions(HugeKeys.ID); E.checkArgument(!conds.isEmpty(), "Please specify the index conditions");