use of com.baidu.hugegraph.backend.query.IdQuery in project incubator-hugegraph by apache.
the class CachedGraphTransaction method queryVerticesByIds.
@Watched(prefix = "graphcache")
private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) {
if (query.idsSize() == 1) {
Id vertexId = query.ids().iterator().next();
HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
if (vertex != null) {
if (!vertex.expired()) {
return QueryResults.iterator(vertex);
}
this.verticesCache.invalidate(vertexId);
}
Iterator<HugeVertex> rs = super.queryVerticesFromBackend(query);
vertex = QueryResults.one(rs);
if (vertex == null) {
return QueryResults.emptyIterator();
}
if (needCacheVertex(vertex)) {
this.verticesCache.update(vertex.id(), vertex);
}
return QueryResults.iterator(vertex);
}
IdQuery newQuery = new IdQuery(HugeType.VERTEX, query);
List<HugeVertex> vertices = new ArrayList<>();
for (Id vertexId : query.ids()) {
HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
if (vertex == null) {
newQuery.query(vertexId);
} else if (vertex.expired()) {
newQuery.query(vertexId);
this.verticesCache.invalidate(vertexId);
} else {
vertices.add(vertex);
}
}
// Join results from cache and backend
ExtendableIterator<HugeVertex> results = new ExtendableIterator<>();
if (!vertices.isEmpty()) {
results.extend(vertices.iterator());
} else {
// Just use the origin query if find none from the cache
newQuery = query;
}
if (!newQuery.empty()) {
Iterator<HugeVertex> rs = super.queryVerticesFromBackend(newQuery);
// Generally there are not too much data with id query
ListIterator<HugeVertex> listIterator = QueryResults.toList(rs);
for (HugeVertex vertex : listIterator.list()) {
// Skip large vertex
if (needCacheVertex(vertex)) {
this.verticesCache.update(vertex.id(), vertex);
}
}
results.extend(listIterator);
}
return results;
}
use of com.baidu.hugegraph.backend.query.IdQuery in project incubator-hugegraph by apache.
the class DeleteExpiredIndexJob method deleteExpiredIndex.
/*
* Delete expired element(if exist) of the index,
* otherwise just delete expired index only
*/
private void deleteExpiredIndex(HugeGraphParams graph, HugeIndex index) {
GraphTransaction tx = graph.graphTransaction();
HugeType type = index.indexLabel().queryType().isVertex() ? HugeType.VERTEX : HugeType.EDGE;
IdQuery query = new IdQuery(type);
query.query(index.elementId());
query.showExpired(true);
Iterator<?> elements = type.isVertex() ? tx.queryVertices(query) : tx.queryEdges(query);
if (elements.hasNext()) {
HugeElement element = (HugeElement) elements.next();
if (element.expiredTime() == index.expiredTime()) {
element.remove();
} else {
tx.removeIndex(index);
}
} else {
tx.removeIndex(index);
}
}
use of com.baidu.hugegraph.backend.query.IdQuery in project incubator-hugegraph by apache.
the class QueryTest method testToString.
@Test
public void testToString() {
Query query = new Query(HugeType.VERTEX);
Assert.assertEquals("`Query * from VERTEX`", query.toString());
query.page("p1");
Assert.assertEquals("`Query * from VERTEX page 'p1'`", query.toString());
query = new Query(HugeType.VERTEX);
query.limit(10L);
Assert.assertEquals("`Query * from VERTEX limit 10`", query.toString());
query = new Query(HugeType.VERTEX);
query.aggregate(AggregateFunc.COUNT, null);
query.limit(10L);
Assert.assertEquals("`Query count(*) from VERTEX limit 10`", query.toString());
query = new Query(HugeType.VERTEX);
query.aggregate(AggregateFunc.MAX, "age");
query.limit(10L);
Assert.assertEquals("`Query max(age) from VERTEX limit 10`", query.toString());
query = new Query(HugeType.VERTEX);
query.page("p2");
query.limit(10L);
Assert.assertEquals("`Query * from VERTEX page 'p2', limit 10`", query.toString());
query = new Query(HugeType.VERTEX);
query.page("p3");
query.offset(100L);
query.limit(10L);
Assert.assertEquals("`Query * from VERTEX page 'p3', offset 100, " + "limit 10`", query.toString());
query = new Query(HugeType.VERTEX);
query.page("");
query.offset(100L);
query.limit(10L);
query.order(HugeKeys.NAME, Order.ASC);
query.order(HugeKeys.FIELDS, Order.DESC);
Assert.assertEquals("`Query * from VERTEX page '', offset 100, " + "limit 10, order by {NAME=ASC, FIELDS=DESC}`", query.toString());
IdQuery query2 = new IdQuery(HugeType.VERTEX, IdGenerator.of(1));
query2.query(IdGenerator.of(3));
query2.limit(10L);
Assert.assertEquals("`Query * from VERTEX limit 10 where id in [1, 3]`", query2.toString());
ConditionQuery query3 = new ConditionQuery(HugeType.EDGE);
query3.eq(HugeKeys.LABEL, 3);
query3.gt(HugeKeys.PROPERTIES, 10);
query3.lt(HugeKeys.PROPERTIES, 18);
query3.limit(10L);
String qStr = query3.toString();
Assert.assertTrue(qStr.contains("`Query * from EDGE limit 10 where ["));
Assert.assertTrue(qStr.contains("LABEL == 3"));
Assert.assertTrue(qStr.contains("PROPERTIES > 10"));
Assert.assertTrue(qStr.contains("PROPERTIES < 18"));
ConditionQuery query4 = new ConditionQuery(HugeType.EDGE);
query4.query(ImmutableSet.of(IdGenerator.of(1), IdGenerator.of(3)));
query4.eq(HugeKeys.LABEL, 3);
query4.lt(HugeKeys.PROPERTIES, 18);
query4.limit(10L);
qStr = query4.toString();
Assert.assertTrue(qStr.contains("`Query * from EDGE limit 10 " + "where id in [1, 3] and ["));
Assert.assertTrue(qStr.contains("LABEL == 3"));
Assert.assertTrue(qStr.contains("PROPERTIES < 18"));
}
use of com.baidu.hugegraph.backend.query.IdQuery in project incubator-hugegraph by apache.
the class GraphTransaction method checkVertexExistIfCustomizedId.
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);
try {
if (!results.hasNext()) {
return;
}
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());
}
} finally {
CloseableIterator.closeIterator(results);
}
}
use of com.baidu.hugegraph.backend.query.IdQuery in project incubator-hugegraph by apache.
the class GraphTransaction method removeLeftIndexIfNeeded.
private void removeLeftIndexIfNeeded(Map<Id, HugeVertex> vertices) {
Set<Id> ids = vertices.keySet();
if (ids.isEmpty()) {
return;
}
IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids);
Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery);
try {
while (results.hasNext()) {
HugeVertex existedVertex = results.next();
this.indexTx.updateVertexIndex(existedVertex, true);
}
} finally {
CloseableIterator.closeIterator(results);
}
}
Aggregations