Search in sources :

Example 6 with TripleRowRegex

use of org.apache.rya.api.resolver.triple.TripleRowRegex 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);
    }
}
Also used : BatchScanner(org.apache.accumulo.core.client.BatchScanner) Scanner(org.apache.accumulo.core.client.Scanner) ByteRange(org.apache.rya.api.query.strategy.ByteRange) BatchScanner(org.apache.accumulo.core.client.BatchScanner) RyaStatement(org.apache.rya.api.domain.RyaStatement) Function(com.google.common.base.Function) TripleRowRegex(org.apache.rya.api.resolver.triple.TripleRowRegex) HashSet(java.util.HashSet) TableLayoutStrategy(org.apache.rya.api.layout.TableLayoutStrategy) Authorizations(org.apache.accumulo.core.security.Authorizations) TriplePatternStrategy(org.apache.rya.api.query.strategy.TriplePatternStrategy) Text(org.apache.hadoop.io.Text) RyaRange(org.apache.rya.api.domain.RyaRange) Range(org.apache.accumulo.core.data.Range) ByteRange(org.apache.rya.api.query.strategy.ByteRange) IOException(java.io.IOException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) RyaURI(org.apache.rya.api.domain.RyaURI) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) RdfCloudTripleStoreConfiguration(org.apache.rya.api.RdfCloudTripleStoreConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RyaStatement (org.apache.rya.api.domain.RyaStatement)6 RyaURI (org.apache.rya.api.domain.RyaURI)6 TripleRowRegex (org.apache.rya.api.resolver.triple.TripleRowRegex)6 TriplePatternStrategy (org.apache.rya.api.query.strategy.TriplePatternStrategy)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 BatchScanner (org.apache.accumulo.core.client.BatchScanner)3 Scanner (org.apache.accumulo.core.client.Scanner)3 Range (org.apache.accumulo.core.data.Range)3 Authorizations (org.apache.accumulo.core.security.Authorizations)3 Text (org.apache.hadoop.io.Text)3 TABLE_LAYOUT (org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT)3 RyaRange (org.apache.rya.api.domain.RyaRange)3 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)3 ByteRange (org.apache.rya.api.query.strategy.ByteRange)3 TripleRow (org.apache.rya.api.resolver.triple.TripleRow)3 HashSet (java.util.HashSet)2