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; }
public IdQuery query(Id id) { E.checkArgumentNotNull(id, "Query id can't be null"); this.ids.add(id); this.checkCapacity(this.ids.size()); return this; }
@Override public ConditionQuery copy() { ConditionQuery query = (ConditionQuery) super.copy(); query.originQuery(this); query.conditions = new LinkedHashSet<>(this.conditions); return query; }
private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) { IdQuery newQuery = new IdQuery(HugeType.VERTEX, query); List<HugeVertex> vertices = new ArrayList<>(query.ids().size()); for (Id vertexId : query.ids()) { Object vertex = this.verticesCache.get(vertexId); if (vertex != null) { vertices.add((HugeVertex) vertex); } else { newQuery.query(vertexId); } } if (vertices.isEmpty()) { // Just use the origin query if find none from the cache newQuery = query; } if (!newQuery.empty()) { Iterator<HugeVertex> rs = super.queryVerticesFromBackend(newQuery); while (rs.hasNext()) { HugeVertex vertex = rs.next(); vertices.add(vertex); this.verticesCache.update(vertex.id(), vertex); } } return vertices.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); } }
public IdQuery query(Set<Id> ids) { for (Id id : ids) { this.query(id); } return this; }
@Watched(prefix = "tx") public BackendEntry query(HugeType type, Id id) { IdQuery q = new IdQuery(type, id); Iterator<BackendEntry> results = this.query(q); if (results.hasNext()) { BackendEntry entry = results.next(); assert !results.hasNext(); return entry; } return null; }
@Override public boolean test(HugeElement element) { if (!this.ids().isEmpty() && !super.test(element)) { return false; } if (this.resultsFilter != null) { return this.resultsFilter.apply(element); } for (Condition cond : this.conditions()) { if (!cond.test(element)) { return false; } } return true; }
private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) { IdQuery newQuery = new IdQuery(HugeType.VERTEX, query); List<HugeVertex> vertices = new ArrayList<>(query.ids().size()); for (Id vertexId : query.ids()) { Object vertex = this.verticesCache.get(vertexId); if (vertex != null) { vertices.add((HugeVertex) vertex); } else { newQuery.query(vertexId); } } if (vertices.isEmpty()) { // Just use the origin query if find none from the cache newQuery = query; } if (!newQuery.empty()) { Iterator<HugeVertex> rs = super.queryVerticesFromBackend(newQuery); while (rs.hasNext()) { HugeVertex vertex = rs.next(); vertices.add(vertex); this.verticesCache.update(vertex.id(), vertex); } } return vertices.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 ImmutableList.<BackendEntry>of().iterator(); } assert idQuery.ids().size() == 1 : idQuery.ids(); return super.query(idQuery); } }
public IdQuery(HugeType resultType, Id id) { this(resultType); this.query(id); }
private void checkVertexExistIfCustomizedId(Map<Id, HugeVertex> vertices) { Set<Id> ids = new HashSet<>(); for (HugeVertex vertex : vertices.values()) { VertexLabel vl = vertex.schemaLabel(); if (!vl.hidden() && vl.idStrategy().isCustomized()) { ids.add(vertex.id()); } } if (ids.isEmpty()) { return; } IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids); Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery); if (results.hasNext()) { HugeVertex existedVertex = results.next(); HugeVertex newVertex = vertices.get(existedVertex.id()); if (!existedVertex.label().equals(newVertex.label())) { throw new HugeException( "The newly added vertex with id:'%s' label:'%s' " + "is not allowed to insert, because already exist " + "a vertex with same id and different label:'%s'", newVertex.id(), newVertex.label(), existedVertex.label()); } } }
@Override public boolean test(HugeElement element) { if (!this.ids().isEmpty() && !super.test(element)) { return false; } if (this.resultsFilter != null) { return this.resultsFilter.apply(element); } for (Condition cond : this.conditions()) { if (!cond.test(element)) { return false; } } return true; }
public IdQuery(HugeType resultType, Set<Id> ids) { this(resultType); this.query(ids); }