use of org.apache.rya.api.layout.TableLayoutStrategy in project incubator-rya by apache.
the class AccumuloRyaQueryEngine method query.
@Override
public CloseableIterable<RyaStatement> query(RyaQuery ryaQuery) throws RyaDAOException {
Preconditions.checkNotNull(ryaQuery);
RyaStatement stmt = ryaQuery.getQuery();
Preconditions.checkNotNull(stmt);
// query configuration
String[] auths = ryaQuery.getAuths();
Authorizations authorizations = auths != null ? new Authorizations(auths) : configuration.getAuthorizations();
Long ttl = ryaQuery.getTtl();
Long currentTime = ryaQuery.getCurrentTime();
Long maxResults = ryaQuery.getMaxResults();
Integer batchSize = ryaQuery.getBatchSize();
String regexSubject = ryaQuery.getRegexSubject();
String regexPredicate = ryaQuery.getRegexPredicate();
String regexObject = ryaQuery.getRegexObject();
TableLayoutStrategy tableLayoutStrategy = configuration.getTableLayoutStrategy();
try {
// find triple pattern range
TriplePatternStrategy strategy = ryaContext.retrieveStrategy(stmt);
TABLE_LAYOUT layout;
Range range;
RyaURI subject = stmt.getSubject();
RyaURI predicate = stmt.getPredicate();
RyaType object = stmt.getObject();
RyaURI context = stmt.getContext();
String qualifier = stmt.getQualifer();
TripleRowRegex tripleRowRegex = null;
if (strategy != null) {
// otherwise, full table scan is supported
Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(subject, predicate, object, context, null);
layout = entry.getKey();
ByteRange byteRange = entry.getValue();
range = new Range(new Text(byteRange.getStart()), new Text(byteRange.getEnd()));
} else {
range = new Range();
layout = TABLE_LAYOUT.SPO;
strategy = ryaContext.retrieveStrategy(layout);
}
byte[] objectTypeInfo = null;
if (object != null) {
// TODO: Not good to serialize this twice
if (object instanceof RyaRange) {
objectTypeInfo = RyaContext.getInstance().serializeType(((RyaRange) object).getStart())[1];
} else {
objectTypeInfo = RyaContext.getInstance().serializeType(object)[1];
}
}
tripleRowRegex = strategy.buildRegex(regexSubject, regexPredicate, regexObject, null, objectTypeInfo);
// use range to set scanner
// populate scanner based on authorizations, ttl
String table = layoutToTable(layout, tableLayoutStrategy);
Scanner scanner = connector.createScanner(table, authorizations);
scanner.setRange(range);
if (batchSize != null) {
scanner.setBatchSize(batchSize);
}
fillScanner(scanner, context, qualifier, ttl, currentTime, tripleRowRegex, ryaQuery.getConf());
FluentCloseableIterable<RyaStatement> results = FluentCloseableIterable.from(new ScannerBaseCloseableIterable(scanner)).transform(keyValueToRyaStatementFunctionMap.get(layout));
if (maxResults != null) {
results = results.limit(maxResults.intValue());
}
return results;
} catch (Exception e) {
throw new RyaDAOException(e);
}
}
use of org.apache.rya.api.layout.TableLayoutStrategy in project incubator-rya by apache.
the class RyaTableNames method getTableNames.
/**
* Get the the Accumulo table names that are used by an instance of Rya.
*
* @param ryaInstanceName - The name of the Rya instance. (not null)
* @param conn - A connector to the host Accumulo instance. (not null)
* @return The Accumulo table names that are used by the Rya instance.
* @throws NotInitializedException The instance's Rya Details have not been initialized.
* @throws RyaDetailsRepositoryException General problem with the Rya Details repository.
* @throws PCJStorageException General problem with the PCJ storage.
*/
public List<String> getTableNames(final String ryaInstanceName, final Connector conn) throws NotInitializedException, RyaDetailsRepositoryException, PCJStorageException {
// Build the list of tables that may be present within the Rya instance.
final List<String> tables = new ArrayList<>();
// Core Rya tables.
final TableLayoutStrategy coreTableNames = new TablePrefixLayoutStrategy(ryaInstanceName);
tables.add(coreTableNames.getSpo());
tables.add(coreTableNames.getPo());
tables.add(coreTableNames.getOsp());
tables.add(coreTableNames.getEval());
tables.add(coreTableNames.getNs());
tables.add(coreTableNames.getProspects());
tables.add(coreTableNames.getSelectivity());
// Rya Details table.
tables.add(AccumuloRyaInstanceDetailsRepository.makeTableName(ryaInstanceName));
// Secondary Indexer Tables.
final RyaDetailsRepository detailsRepo = new AccumuloRyaInstanceDetailsRepository(conn, ryaInstanceName);
final RyaDetails details = detailsRepo.getRyaInstanceDetails();
if (details.getEntityCentricIndexDetails().isEnabled()) {
tables.add(EntityCentricIndex.makeTableName(ryaInstanceName));
}
if (details.getFreeTextIndexDetails().isEnabled()) {
tables.addAll(AccumuloFreeTextIndexer.makeTableNames(ryaInstanceName));
}
if (details.getTemporalIndexDetails().isEnabled()) {
tables.add(AccumuloTemporalIndexer.makeTableName(ryaInstanceName));
}
if (details.getPCJIndexDetails().isEnabled()) {
try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(conn, ryaInstanceName)) {
final List<String> pcjIds = pcjStorage.listPcjs();
final PcjTableNameFactory tableNameFactory = new PcjTableNameFactory();
for (final String pcjId : pcjIds) {
tables.add(tableNameFactory.makeTableName(ryaInstanceName, pcjId));
}
}
}
// Verify they actually exist. If any don't, remove them from the list.
final TableOperations tableOps = conn.tableOperations();
final Iterator<String> tablesIt = tables.iterator();
while (tablesIt.hasNext()) {
final String table = tablesIt.next();
if (!tableOps.exists(table)) {
tablesIt.remove();
}
}
return tables;
}
use of org.apache.rya.api.layout.TableLayoutStrategy 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