use of org.apache.rya.api.query.strategy.ByteRange in project incubator-rya by apache.
the class AccumuloRyaQueryEngine method query.
@Override
public CloseableIterable<RyaStatement> query(BatchRyaQuery ryaQuery) throws RyaDAOException {
Preconditions.checkNotNull(ryaQuery);
Iterable<RyaStatement> stmts = ryaQuery.getQueries();
Preconditions.checkNotNull(stmts);
// query configuration
String[] auths = ryaQuery.getAuths();
final Authorizations authorizations = auths != null ? new Authorizations(auths) : configuration.getAuthorizations();
final Long ttl = ryaQuery.getTtl();
Long currentTime = ryaQuery.getCurrentTime();
Long maxResults = ryaQuery.getMaxResults();
Integer batchSize = ryaQuery.getBatchSize();
Integer numQueryThreads = ryaQuery.getNumQueryThreads();
String regexSubject = ryaQuery.getRegexSubject();
String regexPredicate = ryaQuery.getRegexPredicate();
String regexObject = ryaQuery.getRegexObject();
TableLayoutStrategy tableLayoutStrategy = configuration.getTableLayoutStrategy();
int maxRanges = ryaQuery.getMaxRanges();
// TODO: cannot span multiple tables here
try {
Collection<Range> ranges = new HashSet<Range>();
TABLE_LAYOUT layout = null;
RyaURI context = null;
TriplePatternStrategy strategy = null;
for (RyaStatement stmt : stmts) {
// TODO: This will be overwritten
context = stmt.getContext();
strategy = ryaContext.retrieveStrategy(stmt);
if (strategy == null) {
throw new IllegalArgumentException("TriplePattern[" + stmt + "] not supported");
}
Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), stmt.getContext(), null);
// use range to set scanner
// populate scanner based on authorizations, ttl
layout = entry.getKey();
ByteRange byteRange = entry.getValue();
Range range = new Range(new Text(byteRange.getStart()), new Text(byteRange.getEnd()));
ranges.add(range);
}
// no ranges
if (layout == null || strategy == null)
throw new IllegalArgumentException("No table layout specified, or no statements.");
final TripleRowRegex tripleRowRegex = strategy.buildRegex(regexSubject, regexPredicate, regexObject, null, null);
final String table = layoutToTable(layout, tableLayoutStrategy);
boolean useBatchScanner = ranges.size() > maxRanges;
FluentCloseableIterable<RyaStatement> results = null;
if (useBatchScanner) {
BatchScanner scanner = connector.createBatchScanner(table, authorizations, numQueryThreads);
scanner.setRanges(ranges);
fillScanner(scanner, context, null, ttl, null, tripleRowRegex, ryaQuery.getConf());
results = FluentCloseableIterable.from(new ScannerBaseCloseableIterable(scanner)).transform(keyValueToRyaStatementFunctionMap.get(layout));
} else {
final RyaURI fcontext = context;
final RdfCloudTripleStoreConfiguration fconf = ryaQuery.getConf();
FluentIterable<RyaStatement> fluent = FluentIterable.from(ranges).transformAndConcat(new Function<Range, Iterable<Map.Entry<Key, Value>>>() {
@Override
public Iterable<Map.Entry<Key, Value>> apply(Range range) {
try {
Scanner scanner = connector.createScanner(table, authorizations);
scanner.setRange(range);
fillScanner(scanner, fcontext, null, ttl, null, tripleRowRegex, fconf);
return scanner;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).transform(keyValueToRyaStatementFunctionMap.get(layout));
results = FluentCloseableIterable.from(CloseableIterables.wrap(fluent));
}
if (maxResults != null) {
results = results.limit(maxResults.intValue());
}
return results;
} catch (Exception e) {
throw new RyaDAOException(e);
}
}
Aggregations