use of com.baidu.hugegraph.iterator.ExtendableIterator 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);
}
use of com.baidu.hugegraph.iterator.ExtendableIterator in project incubator-hugegraph by apache.
the class MysqlTable method query.
protected <R> Iterator<R> query(Session session, Query query, BiFunction<Query, ResultSetWrapper, Iterator<R>> parser) {
ExtendableIterator<R> rs = new ExtendableIterator<>();
if (query.limit() == 0L && !query.noLimit()) {
LOG.debug("Return empty result(limit=0) for query {}", query);
return rs;
}
List<StringBuilder> selections = this.query2Select(this.table(), query);
try {
for (StringBuilder selection : selections) {
ResultSetWrapper results = session.select(selection.toString());
rs.extend(parser.apply(query, results));
}
} catch (SQLException e) {
throw new BackendException("Failed to query [%s]", e, query);
}
LOG.debug("Return {} for query {}", rs, query);
return rs;
}
Aggregations