Search in sources :

Example 1 with IdQuery

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;
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) ArrayList(java.util.ArrayList) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 2 with IdQuery

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);
    }
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeType(com.baidu.hugegraph.type.HugeType) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 3 with IdQuery

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"));
}
Also used : IdPrefixQuery(com.baidu.hugegraph.backend.query.IdPrefixQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdRangeQuery(com.baidu.hugegraph.backend.query.IdRangeQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Test(org.junit.Test)

Example 4 with IdQuery

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);
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) HugeException(com.baidu.hugegraph.HugeException) HashSet(java.util.HashSet)

Example 5 with IdQuery

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);
    }
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Aggregations

IdQuery (com.baidu.hugegraph.backend.query.IdQuery)11 Id (com.baidu.hugegraph.backend.id.Id)7 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)5 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)4 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3 Query (com.baidu.hugegraph.backend.query.Query)3 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)3 HugeException (com.baidu.hugegraph.HugeException)2 BackendException (com.baidu.hugegraph.backend.BackendException)2 BatchMapperIterator (com.baidu.hugegraph.iterator.BatchMapperIterator)2 ExtendableIterator (com.baidu.hugegraph.iterator.ExtendableIterator)2 FlatMapperIterator (com.baidu.hugegraph.iterator.FlatMapperIterator)2 MapperIterator (com.baidu.hugegraph.iterator.MapperIterator)2 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)2 HugeType (com.baidu.hugegraph.type.HugeType)2 HashMap (java.util.HashMap)2 QueryId (com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId)1 Condition (com.baidu.hugegraph.backend.query.Condition)1 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)1 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)1