use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.
the class PcjTablesIT method addResults.
/**
* Ensure when results have been written to the PCJ table that they are in Accumulo.
* <p>
* The method being tested is {@link PcjTables#addResults(Connector, String, java.util.Collection)}
*/
@Test
public void addResults() throws PcjException, TableNotFoundException, BindingSetConversionException, AccumuloException, AccumuloSecurityException {
final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
final Connector accumuloConn = cluster.getConnector();
// Create a PCJ table in the Mini Accumulo.
final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "testPcj");
final Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
final PcjTables pcjs = new PcjTables();
pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
// Add a few results to the PCJ table.
final MapBindingSet alice = new MapBindingSet();
alice.addBinding("name", new URIImpl("http://Alice"));
alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
final MapBindingSet bob = new MapBindingSet();
bob.addBinding("name", new URIImpl("http://Bob"));
bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
final MapBindingSet charlie = new MapBindingSet();
charlie.addBinding("name", new URIImpl("http://Charlie"));
charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
final Set<BindingSet> results = Sets.<BindingSet>newHashSet(alice, bob, charlie);
pcjs.addResults(accumuloConn, pcjTableName, Sets.<VisibilityBindingSet>newHashSet(new VisibilityBindingSet(alice), new VisibilityBindingSet(bob), new VisibilityBindingSet(charlie)));
// Make sure the cardinality was updated.
final PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
assertEquals(3, metadata.getCardinality());
// Scan Accumulo for the stored results.
final Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);
// Ensure the expected results match those that were stored.
final Multimap<String, BindingSet> expectedResults = HashMultimap.create();
expectedResults.putAll("name;age", results);
expectedResults.putAll("age;name", results);
assertEquals(expectedResults, fetchedResults);
}
use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.
the class PcjTablesIT method purge.
@Test
public void purge() throws PCJStorageException, AccumuloException, AccumuloSecurityException {
final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
final Connector accumuloConn = cluster.getConnector();
// Create a PCJ table in the Mini Accumulo.
final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "testPcj");
final Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
final PcjTables pcjs = new PcjTables();
pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
// Add a few results to the PCJ table.
final MapBindingSet alice = new MapBindingSet();
alice.addBinding("name", new URIImpl("http://Alice"));
alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
final MapBindingSet bob = new MapBindingSet();
bob.addBinding("name", new URIImpl("http://Bob"));
bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
final MapBindingSet charlie = new MapBindingSet();
charlie.addBinding("name", new URIImpl("http://Charlie"));
charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
pcjs.addResults(accumuloConn, pcjTableName, Sets.<VisibilityBindingSet>newHashSet(new VisibilityBindingSet(alice), new VisibilityBindingSet(bob), new VisibilityBindingSet(charlie)));
// Make sure the cardinality was updated.
PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
assertEquals(3, metadata.getCardinality());
// Purge the data.
pcjs.purgePcjTable(accumuloConn, pcjTableName);
// Make sure the cardinality was updated to 0.
metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
assertEquals(0, metadata.getCardinality());
}
use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.
the class PcjTablesIT method loadPcjResults.
/**
* Scan accumulo for the results that are stored in a PCJ table. The
* multimap stores a set of deserialized binding sets that were in the PCJ
* table for every variable order that is found in the PCJ metadata.
*/
private static Multimap<String, BindingSet> loadPcjResults(final Connector accumuloConn, final String pcjTableName) throws PcjException, TableNotFoundException, BindingSetConversionException {
final Multimap<String, BindingSet> fetchedResults = HashMultimap.create();
// Get the variable orders the data was written to.
final PcjTables pcjs = new PcjTables();
final PcjMetadata pcjMetadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
// Scan Accumulo for the stored results.
for (final VariableOrder varOrder : pcjMetadata.getVarOrders()) {
final Scanner scanner = accumuloConn.createScanner(pcjTableName, new Authorizations());
scanner.fetchColumnFamily(new Text(varOrder.toString()));
for (final Entry<Key, Value> entry : scanner) {
final byte[] serializedResult = entry.getKey().getRow().getBytes();
final BindingSet result = converter.convert(serializedResult, varOrder);
fetchedResults.put(varOrder.toString(), result);
}
}
return fetchedResults;
}
use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.
the class PcjTablesIT method populatePcj.
/**
* Ensure when results are already stored in Rya, that we are able to populate
* the PCJ table for a new SPARQL query using those results.
* <p>
* The method being tested is: {@link PcjTables#populatePcj(Connector, String, RepositoryConnection, String)}
*/
@Test
public void populatePcj() throws RepositoryException, PcjException, TableNotFoundException, BindingSetConversionException, AccumuloException, AccumuloSecurityException {
// Load some Triples into Rya.
final Set<Statement> triples = new HashSet<>();
triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
for (final Statement triple : triples) {
ryaConn.add(triple);
}
// Create a PCJ table that will include those triples in its results.
final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
final Connector accumuloConn = cluster.getConnector();
final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "testPcj");
final Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
final PcjTables pcjs = new PcjTables();
pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
// Populate the PCJ table using a Rya connection.
pcjs.populatePcj(accumuloConn, pcjTableName, ryaConn);
// Scan Accumulo for the stored results.
final Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);
// Make sure the cardinality was updated.
final PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
assertEquals(3, metadata.getCardinality());
// Ensure the expected results match those that were stored.
final MapBindingSet alice = new MapBindingSet();
alice.addBinding("name", new URIImpl("http://Alice"));
alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
final MapBindingSet bob = new MapBindingSet();
bob.addBinding("name", new URIImpl("http://Bob"));
bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
final MapBindingSet charlie = new MapBindingSet();
charlie.addBinding("name", new URIImpl("http://Charlie"));
charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
final Set<BindingSet> results = Sets.<BindingSet>newHashSet(alice, bob, charlie);
final Multimap<String, BindingSet> expectedResults = HashMultimap.create();
expectedResults.putAll("name;age", results);
expectedResults.putAll("age;name", results);
assertEquals(expectedResults, fetchedResults);
}
use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.
the class PcjTablesIT method dropPcj.
@Test
public void dropPcj() throws PCJStorageException, AccumuloException, AccumuloSecurityException {
final Connector accumuloConn = cluster.getConnector();
// Create a PCJ index.
final String tableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "thePcj");
final Set<VariableOrder> varOrders = Sets.<VariableOrder>newHashSet(new VariableOrder("x"));
final String sparql = "SELECT x WHERE ?x <http://isA> <http://Food>";
final PcjTables pcjs = new PcjTables();
pcjs.createPcjTable(accumuloConn, tableName, varOrders, sparql);
// Fetch its metadata to show that it has actually been created.
final PcjMetadata expectedMetadata = new PcjMetadata(sparql, 0L, varOrders);
PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, tableName);
assertEquals(expectedMetadata, metadata);
// Drop it.
pcjs.dropPcjTable(accumuloConn, tableName);
// Show the metadata is no longer present.
PCJStorageException tableDoesNotExistException = null;
try {
metadata = pcjs.getPcjMetadata(accumuloConn, tableName);
} catch (final PCJStorageException e) {
tableDoesNotExistException = e;
}
assertNotNull(tableDoesNotExistException);
}
Aggregations