use of com.baidu.hugegraph.backend.query.Aggregate in project incubator-hugegraph by apache.
the class CassandraTable method queryNumber.
@Override
public Number queryNumber(CassandraSessionPool.Session session, Query query) {
Aggregate aggregate = query.aggregateNotNull();
Iterator<Number> results = this.query(query, statement -> {
// Set request timeout to a large value
int timeout = session.aggregateTimeout();
statement.setReadTimeoutMillis(timeout * 1000);
return session.query(statement);
}, (q, rs) -> {
Row row = rs.one();
if (row == null) {
return IteratorUtils.of(aggregate.defaultValue());
}
return IteratorUtils.of(row.getLong(0));
});
return aggregate.reduce(results);
}
use of com.baidu.hugegraph.backend.query.Aggregate in project incubator-hugegraph by apache.
the class CassandraTable method query2Select.
protected List<Select> query2Select(String table, Query query) {
// Build query
Selection selection = QueryBuilder.select();
// Set aggregate
Aggregate aggregate = query.aggregate();
if (aggregate != null) {
if (aggregate.countAll()) {
selection.countAll();
} else {
selection.fcall(aggregate.func().string(), aggregate.column());
}
}
// Set table
Select select = selection.from(table);
// NOTE: Cassandra does not support query.offset()
if (query.offset() != 0) {
LOG.debug("Query offset is not supported on Cassandra store " + "currently, it will be replaced by [0, offset + limit)");
}
// Set order-by
for (Map.Entry<HugeKeys, Order> order : query.orders().entrySet()) {
String name = formatKey(order.getKey());
if (order.getValue() == Order.ASC) {
select.orderBy(QueryBuilder.asc(name));
} else {
assert order.getValue() == Order.DESC;
select.orderBy(QueryBuilder.desc(name));
}
}
// Is query by id?
List<Select> ids = this.queryId2Select(query, select);
if (query.conditionsSize() == 0) {
// Query only by id
this.setPageState(query, ids);
LOG.debug("Query only by id(s): {}", ids);
return ids;
} else {
List<Select> conds = new ArrayList<>(ids.size());
for (Select id : ids) {
// Query by condition
conds.addAll(this.queryCondition2Select(query, id));
}
this.setPageState(query, conds);
LOG.debug("Query by conditions: {}", conds);
return conds;
}
}
use of com.baidu.hugegraph.backend.query.Aggregate in project incubator-hugegraph by apache.
the class InMemoryDBTable method queryNumber.
@Override
public Number queryNumber(BackendSession session, Query query) {
Aggregate aggregate = query.aggregateNotNull();
if (aggregate.func() != AggregateFunc.COUNT) {
throw new NotSupportException(aggregate.toString());
}
assert aggregate.func() == AggregateFunc.COUNT;
Iterator<BackendEntry> results = this.query(session, query);
long total = 0L;
while (results.hasNext()) {
total += this.sizeOfBackendEntry(results.next());
}
return total;
}
use of com.baidu.hugegraph.backend.query.Aggregate in project incubator-hugegraph by apache.
the class GraphTransaction method queryNumber.
@Override
public Number queryNumber(Query query) {
E.checkArgument(!this.hasUpdate(), "It's not allowed to query number when " + "there are uncommitted records.");
if (!(query instanceof ConditionQuery)) {
return super.queryNumber(query);
}
Aggregate aggregate = query.aggregateNotNull();
QueryList<Number> queries = this.optimizeQueries(query, q -> {
boolean indexQuery = q.getClass() == IdQuery.class;
OptimizedType optimized = ((ConditionQuery) query).optimized();
Number result;
if (!indexQuery) {
result = super.queryNumber(q);
} else {
E.checkArgument(aggregate.func() == AggregateFunc.COUNT, "The %s operator on index is not supported now", aggregate.func().string());
if (this.optimizeAggrByIndex && optimized == OptimizedType.INDEX) {
// The ids size means results count (assume no left index)
result = q.idsSize();
} else {
assert optimized == OptimizedType.INDEX_FILTER || optimized == OptimizedType.INDEX;
assert q.resultType().isVertex() || q.resultType().isEdge();
result = IteratorUtils.count(q.resultType().isVertex() ? this.queryVertices(q) : this.queryEdges(q));
}
}
return new QueryResults<>(IteratorUtils.of(result), q);
});
QueryResults<Number> results = queries.empty() ? QueryResults.empty() : queries.fetch(this.pageSize);
return aggregate.reduce(results.iterator());
}
use of com.baidu.hugegraph.backend.query.Aggregate in project incubator-hugegraph by apache.
the class MysqlTable method queryNumber.
@Override
public Number queryNumber(Session session, Query query) {
Aggregate aggregate = query.aggregateNotNull();
Iterator<Number> results = this.query(session, query, (q, rs) -> {
try {
if (!rs.resultSet().next()) {
return IteratorUtils.of(aggregate.defaultValue());
}
return IteratorUtils.of(rs.resultSet().getLong(1));
} catch (SQLException e) {
throw new BackendException(e);
} finally {
rs.close();
}
});
return aggregate.reduce(results);
}
Aggregations