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);
});
}
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);
});
}
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);
}
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;
});
}
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;
});
}
Aggregations