use of com.baidu.hugegraph.iterator.FilterIterator in project incubator-hugegraph by apache.
the class HugeTraverser method edgesOfVertex.
private Iterator<Edge> edgesOfVertex(Id source, EdgeStep edgeStep, boolean mustAllSK) {
Id[] edgeLabels = edgeStep.edgeLabels();
Query query = GraphTransaction.constructEdgesQuery(source, edgeStep.direction(), edgeLabels);
ConditionQuery filter = null;
if (mustAllSK) {
this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties());
} else {
filter = (ConditionQuery) query.copy();
this.fillFilterByProperties(filter, edgeStep.properties());
}
query.capacity(Query.NO_CAPACITY);
if (edgeStep.limit() != NO_LIMIT) {
query.limit(edgeStep.limit());
}
Iterator<Edge> edges = this.graph().edges(query);
if (filter != null) {
ConditionQuery finalFilter = filter;
edges = new FilterIterator<>(edges, (e) -> {
return finalFilter.test((HugeEdge) e);
});
}
return edgeStep.skipSuperNodeIfNeeded(edges);
}
use of com.baidu.hugegraph.iterator.FilterIterator in project incubator-hugegraph by apache.
the class GraphTransaction method joinTxEdges.
private Iterator<?> joinTxEdges(Query query, Iterator<HugeEdge> edges, Map<Id, HugeVertex> removingVertices) {
assert query.resultType().isEdge();
BiFunction<Query, HugeEdge, HugeEdge> matchTxFunc = (q, e) -> {
assert q.resultType() == HugeType.EDGE;
if (e.expired() && !q.showExpired()) {
// Filter expired edges with TTL
return null;
}
// Filter edges matched conditions
return q.test(e) ? e : q.test(e = e.switchOwner()) ? e : null;
};
edges = this.joinTxRecords(query, edges, matchTxFunc, this.addedEdges, this.removedEdges, this.updatedEdges);
if (removingVertices.isEmpty()) {
return edges;
}
// Filter edges that belong to deleted vertex
return new FilterIterator<HugeEdge>(edges, edge -> {
for (HugeVertex v : removingVertices.values()) {
if (edge.belongToVertex(v)) {
return false;
}
}
return true;
});
}
use of com.baidu.hugegraph.iterator.FilterIterator in project hugegraph-common by hugegraph.
the class FilterIteratorTest method testFilter.
@Test
public void testFilter() {
AtomicInteger callbackCount = new AtomicInteger(0);
Iterator<Integer> values = DATA.iterator();
Function<Integer, Boolean> filter = value -> {
callbackCount.incrementAndGet();
return (value % 2 == 0);
};
Iterator<Integer> results = new FilterIterator<>(values, filter);
List<Integer> actual = new ArrayList<>();
while (results.hasNext()) {
actual.add(results.next());
}
Assert.assertEquals(4, callbackCount.get());
Assert.assertEquals(ImmutableList.of(2, 4), actual);
}
use of com.baidu.hugegraph.iterator.FilterIterator in project hugegraph-common by hugegraph.
the class FilterIteratorTest method testNextWithOriginIteratorReturnNullElem.
@Test
public void testNextWithOriginIteratorReturnNullElem() {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(null);
list.add(3);
Iterator<Integer> vals = list.iterator();
AtomicInteger callbackCount = new AtomicInteger(0);
Iterator<Integer> results = new FilterIterator<>(vals, val -> {
callbackCount.incrementAndGet();
return true;
});
Assert.assertTrue(results.hasNext());
for (int i = 0; i < 2; i++) {
results.next();
}
Assert.assertFalse(results.hasNext());
Assert.assertEquals(2, callbackCount.get());
}
use of com.baidu.hugegraph.iterator.FilterIterator in project incubator-hugegraph by apache.
the class GraphTransaction method joinTxRecords.
private <V extends HugeElement> Iterator<V> joinTxRecords(Query query, Iterator<V> records, BiFunction<Query, V, V> matchFunc, Map<Id, V> addedTxRecords, Map<Id, V> removedTxRecords, Map<Id, V> updatedTxRecords) {
this.checkOwnerThread();
// Return the origin results if there is no change in tx
if (addedTxRecords.isEmpty() && removedTxRecords.isEmpty() && updatedTxRecords.isEmpty()) {
return records;
}
Set<V> txResults = InsertionOrderUtil.newSet();
/*
* Collect added/updated records
* Records in memory have higher priority than query from backend store
*/
for (V elem : addedTxRecords.values()) {
if (query.reachLimit(txResults.size())) {
break;
}
if ((elem = matchFunc.apply(query, elem)) != null) {
txResults.add(elem);
}
}
for (V elem : updatedTxRecords.values()) {
if (query.reachLimit(txResults.size())) {
break;
}
if ((elem = matchFunc.apply(query, elem)) != null) {
txResults.add(elem);
}
}
// Filter backend record if it's updated in memory
Iterator<V> backendResults = new FilterIterator<>(records, elem -> {
Id id = elem.id();
return !addedTxRecords.containsKey(id) && !updatedTxRecords.containsKey(id) && !removedTxRecords.containsKey(id);
});
return new ExtendableIterator<V>(txResults.iterator(), backendResults);
}
Aggregations