Search in sources :

Example 6 with ExtendableIterator

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);
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId)

Example 7 with ExtendableIterator

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;
}
Also used : SQLException(java.sql.SQLException) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

ExtendableIterator (com.baidu.hugegraph.iterator.ExtendableIterator)7 Id (com.baidu.hugegraph.backend.id.Id)4 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)3 BackendException (com.baidu.hugegraph.backend.BackendException)2 QueryId (com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId)2 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)2 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)1 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)1 RamTable (com.baidu.hugegraph.backend.store.ram.RamTable)1 FilterIterator (com.baidu.hugegraph.iterator.FilterIterator)1 LimitIterator (com.baidu.hugegraph.iterator.LimitIterator)1 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)1 ResultSet (com.datastax.driver.core.ResultSet)1 DriverException (com.datastax.driver.core.exceptions.DriverException)1 Select (com.datastax.driver.core.querybuilder.Select)1 ClassPath (com.google.common.reflect.ClassPath)1 ClassInfo (com.google.common.reflect.ClassPath.ClassInfo)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1