/** * Currently doesn't allow to exist schema with the same name * @param type the query schema type * @param name the query schema name * @param <T> SubClass of SchemaElement * @return the queried schema object */ protected <T extends SchemaElement> T getSchema(HugeType type, String name) { LOG.debug("SchemaTransaction get {} by name '{}'", type, name); this.beforeRead(); ConditionQuery query = new ConditionQuery(type); query.eq(HugeKeys.NAME, name); Iterator<BackendEntry> itor = this.indexTx.query(query); this.afterRead(); if (itor.hasNext()) { T schema = this.deserialize(itor.next(), type); E.checkState(!itor.hasNext(), "Should not exist schema with same name '%s'", name); return schema; } return null; }
query.eq(HugeKeys.LABEL, label.id()); int pass = 0; int counter = 0;
private Iterator<Vertex> queryAllVariableVertices() { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); query.eq(HugeKeys.LABEL, vl.id()); query.showHidden(true); return this.graph.vertices(query); }
@Test public void testQueryEdgesByDirection() { HugeGraph graph = graph(); init18Edges(); // Query vertex by condition (filter by Direction) ConditionQuery q = new ConditionQuery(HugeType.EDGE); q.eq(HugeKeys.DIRECTION, Direction.OUT); Assert.assertThrows(BackendException.class, () -> { graph.edges(q); }); }
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(); }
indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id()); indexQuery.eq(HugeKeys.FIELD_VALUES, fieldValue); break; case SECONDARY: indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id()); indexQuery.eq(HugeKeys.FIELD_VALUES, joinedValues); break; case RANGE: indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id()); for (Condition condition : query.userpropConditions()) { assert condition instanceof Condition.Relation;
private <V> Iterator<HugeTask<V>> queryTask(Map<String, Object> conditions, long limit) { return this.call(() -> { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(TaskTransaction.TASK); query.eq(HugeKeys.LABEL, vl.id()); for (Map.Entry<String, Object> entry : conditions.entrySet()) { PropertyKey pk = this.graph.propertyKey(entry.getKey()); query.query(Condition.eq(pk.id(), entry.getValue())); } query.showHidden(true); if (limit != NO_LIMIT) { query.limit(limit); } Iterator<Vertex> vertices = this.tx().queryVertices(query); Iterator<HugeTask<V>> tasks = new MapperIterator<>(vertices, HugeTask::fromVertex); // Convert iterator to list to avoid across thread tx accessed return IteratorUtils.list(tasks); }).iterator(); }
@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); } }
@Test public void testQueryFilterByPropName() { HugeGraph graph = graph(); BackendFeatures features = graph.graphTransaction().store().features(); Assume.assumeTrue("Not support CONTAINS_KEY query", features.supportsQueryWithContainsKey()); init10Vertices(); VertexLabel language = graph.vertexLabel("language"); PropertyKey dynamic = graph.propertyKey("dynamic"); // Query vertex by condition (does contain the property name?) ConditionQuery q = new ConditionQuery(HugeType.VERTEX); q.eq(HugeKeys.LABEL, language.id()); q.key(HugeKeys.PROPERTIES, dynamic.id()); List<Vertex> vertexes = ImmutableList.copyOf(graph.vertices(q)); Assert.assertEquals(1, vertexes.size()); assertContains(vertexes, T.label, "language", "name", "python", "dynamic", true); }
@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); }
query.eq(HugeKeys.SORT_VALUES, query.userpropValuesString(keys));
/** * Currently doesn't allow to exist schema with the same name * @param type the query schema type * @param name the query schema name * @param <T> SubClass of SchemaElement * @return the queried schema object */ protected <T extends SchemaElement> T getSchema(HugeType type, String name) { LOG.debug("SchemaTransaction get {} by name '{}'", type, name); this.beforeRead(); ConditionQuery query = new ConditionQuery(type); query.eq(HugeKeys.NAME, name); Iterator<BackendEntry> itor = this.indexTx.query(query); this.afterRead(); if (itor.hasNext()) { T schema = this.deserialize(itor.next(), type); E.checkState(!itor.hasNext(), "Should not exist schema with same name '%s'", name); return schema; } return null; }
private Iterator<Vertex> queryAllVariableVertices() { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); query.eq(HugeKeys.LABEL, vl.id()); query.showHidden(true); return this.graph.vertices(query); }
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(); }
@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 ImmutableList.<BackendEntry>of().iterator(); } assert idQuery.ids().size() == 1 : idQuery.ids(); return super.query(idQuery); } }
private <V> Iterator<HugeTask<V>> queryTask(Map<String, Object> conditions, long limit) { return this.call(() -> { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(TaskTransaction.TASK); query.eq(HugeKeys.LABEL, vl.id()); for (Map.Entry<String, Object> entry : conditions.entrySet()) { PropertyKey pk = this.graph.propertyKey(entry.getKey()); query.query(Condition.eq(pk.id(), entry.getValue())); } query.showHidden(true); if (limit != NO_LIMIT) { query.limit(limit); } Iterator<Vertex> vertices = this.tx().queryVertices(query); return new MapperIterator<>(vertices, HugeTask::fromVertex); }); }
@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); }