Search in sources :

Example 1 with MapperIterator

use of com.baidu.hugegraph.iterator.MapperIterator in project incubator-hugegraph by apache.

the class ServerInfoManager method serverInfos.

private Iterator<HugeServerInfo> serverInfos(Map<String, Object> conditions, long limit, String page) {
    return this.call(() -> {
        ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
        if (page != null) {
            query.page(page);
        }
        HugeGraph graph = this.graph.graph();
        VertexLabel vl = graph.vertexLabel(HugeServerInfo.P.SERVER);
        query.eq(HugeKeys.LABEL, vl.id());
        for (Map.Entry<String, Object> entry : conditions.entrySet()) {
            PropertyKey pk = 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<HugeServerInfo> servers = new MapperIterator<>(vertices, HugeServerInfo::fromVertex);
        // Convert iterator to list to avoid across thread tx accessed
        return QueryResults.toList(servers);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 2 with MapperIterator

use of com.baidu.hugegraph.iterator.MapperIterator in project incubator-hugegraph by apache.

the class StandardTaskScheduler method queryTask.

private <V> Iterator<HugeTask<V>> queryTask(Map<String, Object> conditions, long limit, String page) {
    return this.call(() -> {
        ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
        if (page != null) {
            query.page(page);
        }
        VertexLabel vl = this.graph().vertexLabel(P.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 QueryResults.toList(tasks);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 3 with MapperIterator

use of com.baidu.hugegraph.iterator.MapperIterator in project hugegraph-common by hugegraph.

the class MapperIteratorTest method testMapper.

@Test
public void testMapper() {
    AtomicInteger keysCount = new AtomicInteger(0);
    Iterator<String> keys = DATA.keySet().iterator();
    Function<String, Integer> mapper = key -> {
        keysCount.incrementAndGet();
        return DATA.get(key);
    };
    Iterator<Integer> results = new MapperIterator<>(keys, mapper);
    List<Integer> actual = new ArrayList<>();
    while (results.hasNext()) {
        actual.add(results.next());
    }
    List<Integer> expected = ImmutableList.of(1, 2, 3, 4);
    Assert.assertEquals(4, keysCount.get());
    Assert.assertEquals(expected, actual);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) CloseableItor(com.baidu.hugegraph.unit.iterator.ExtendableIteratorTest.CloseableItor) Test(org.junit.Test) Function(java.util.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Assert(com.baidu.hugegraph.testutil.Assert) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 4 with MapperIterator

use of com.baidu.hugegraph.iterator.MapperIterator in project incubator-hugegraph by apache.

the class RelationshipManager method queryRelationship.

private Iterator<Edge> queryRelationship(Id source, Directions direction, String label, Map<String, Object> conditions, long limit) {
    ConditionQuery query = new ConditionQuery(HugeType.EDGE);
    EdgeLabel el = this.graph().edgeLabel(label);
    if (direction == null) {
        direction = Directions.OUT;
    }
    if (source != null) {
        query.eq(HugeKeys.OWNER_VERTEX, source);
        query.eq(HugeKeys.DIRECTION, direction);
    }
    if (label != null) {
        query.eq(HugeKeys.LABEL, el.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<Edge> edges = this.tx().queryEdges(query);
    if (limit == NO_LIMIT) {
        return edges;
    }
    long[] size = new long[1];
    return new MapperIterator<>(edges, edge -> {
        if (++size[0] > limit) {
            return null;
        }
        return edge;
    });
}
Also used : MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 5 with MapperIterator

use of com.baidu.hugegraph.iterator.MapperIterator in project incubator-hugegraph by apache.

the class GraphTransaction method queryVerticesByIds.

protected Iterator<Vertex> queryVerticesByIds(Object[] vertexIds, boolean adjacentVertex, boolean checkMustExist) {
    Query.checkForceCapacity(vertexIds.length);
    // NOTE: allowed duplicated vertices if query by duplicated ids
    List<Id> ids = InsertionOrderUtil.newList();
    Map<Id, HugeVertex> vertices = new HashMap<>(vertexIds.length);
    IdQuery query = new IdQuery(HugeType.VERTEX);
    for (Object vertexId : vertexIds) {
        HugeVertex vertex;
        Id id = HugeVertex.getIdValue(vertexId);
        if (id == null || this.removedVertices.containsKey(id)) {
            // The record has been deleted
            continue;
        } else if ((vertex = this.addedVertices.get(id)) != null || (vertex = this.updatedVertices.get(id)) != null) {
            if (vertex.expired()) {
                continue;
            }
            // Found from local tx
            vertices.put(vertex.id(), vertex);
        } else {
            // Prepare to query from backend store
            query.query(id);
        }
        ids.add(id);
    }
    if (!query.empty()) {
        // Query from backend store
        query.mustSortByInput(false);
        Iterator<HugeVertex> it = this.queryVerticesFromBackend(query);
        QueryResults.fillMap(it, vertices);
    }
    return new MapperIterator<>(ids.iterator(), id -> {
        HugeVertex vertex = vertices.get(id);
        if (vertex == null) {
            if (checkMustExist) {
                throw new NotFoundException("Vertex '%s' does not exist", id);
            } else if (adjacentVertex) {
                assert !checkMustExist;
                // Return undefined if adjacentVertex but !checkMustExist
                vertex = HugeVertex.undefined(this.graph(), id);
            } else {
                // Return null
                assert vertex == null;
            }
        }
        return vertex;
    });
}
Also used : MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) HashMap(java.util.HashMap) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) 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

MapperIterator (com.baidu.hugegraph.iterator.MapperIterator)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)3 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)3 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)2 Id (com.baidu.hugegraph.backend.id.Id)2 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)2 BatchMapperIterator (com.baidu.hugegraph.iterator.BatchMapperIterator)2 FlatMapperIterator (com.baidu.hugegraph.iterator.FlatMapperIterator)2 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)2 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 ExtendableIterator (com.baidu.hugegraph.iterator.ExtendableIterator)1