use of org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException in project incubator-rya by apache.
the class MongoPcjDocuments method getPcjMetadata.
/**
* Gets the {@link PcjMetadata} from a provided PCJ Id.
*
* @param pcjId - The Id of the PCJ to get from MongoDB. (not null)
* @return - The {@link PcjMetadata} of the Pcj specified.
* @throws PCJStorageException The PCJ metadata document does not exist.
*/
public PcjMetadata getPcjMetadata(final String pcjId) throws PCJStorageException {
requireNonNull(pcjId);
// since query by ID, there will only be one.
final Document result = pcjCollection.find(new Document(PCJ_METADATA_ID, makeMetadataID(pcjId))).first();
if (result == null) {
throw new PCJStorageException("The PCJ: " + pcjId + " does not exist.");
}
final String sparql = result.getString(SPARQL_FIELD);
final int cardinality = result.getInteger(CARDINALITY_FIELD, 0);
final List<List<String>> varOrders = (List<List<String>>) result.get(VAR_ORDER_FIELD);
final Set<VariableOrder> varOrder = new HashSet<>();
for (final List<String> vars : varOrders) {
varOrder.add(new VariableOrder(vars));
}
return new PcjMetadata(sparql, cardinality, varOrder);
}
use of org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException in project incubator-rya by apache.
the class PcjTables method dropPcjTable.
/**
* Drops a PCJ index from Accumulo.
*
* @param accumuloConn - Connects to the Accumulo that hosts the PCJ indices. (not null)
* @param pcjTableName - The name of the PCJ table that will be dropped. (not null)
* @throws PCJStorageException - The table could not be dropped because of
* a security exception or because it does not exist.
*/
public void dropPcjTable(final Connector accumuloConn, final String pcjTableName) throws PCJStorageException {
checkNotNull(accumuloConn);
checkNotNull(pcjTableName);
try {
accumuloConn.tableOperations().delete(pcjTableName);
} catch (AccumuloException | AccumuloSecurityException | TableNotFoundException e) {
throw new PCJStorageException("Could not delete PCJ table named: " + pcjTableName, e);
}
}
use of org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException in project incubator-rya by apache.
the class PcjTables method makeWriteResultMutations.
/**
* Create the {@link Mutations} required to write a new {@link BindingSet}
* to a PCJ table for each {@link VariableOrder} that is provided.
*
* @param varOrders - The variables orders the result will be written to. (not null)
* @param result - A new PCJ result. (not null)
* @return Mutation that will write the result to a PCJ table.
* @throws PCJStorageException The binding set could not be encoded.
*/
private static Set<Mutation> makeWriteResultMutations(final Set<VariableOrder> varOrders, final VisibilityBindingSet result) throws PCJStorageException {
checkNotNull(varOrders);
checkNotNull(result);
final Set<Mutation> mutations = new HashSet<>();
final AccumuloPcjSerializer converter = new AccumuloPcjSerializer();
VisibilityBindingSetSerDe bsSerDe = new VisibilityBindingSetSerDe();
for (final VariableOrder varOrder : varOrders) {
try {
// Serialize the result to the variable order.
final byte[] rowKey = converter.convert(result, varOrder);
// Row ID = binding set values, Column Family = variable order of the binding set.
final Mutation addResult = new Mutation(rowKey);
final String visibility = result.getVisibility();
addResult.put(varOrder.toString(), "", new ColumnVisibility(visibility), new Value(bsSerDe.serialize(result).toArray()));
mutations.add(addResult);
} catch (Exception e) {
throw new PCJStorageException("Could not serialize a result.", e);
}
}
return mutations;
}
use of org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException in project incubator-rya by apache.
the class PcjTables method updateMockCardinality.
/**
* Update the cardinality of a PCJ by a {@code delta}.
*
* This method updates the PCJ table cardinality using a BatchWriter in the event that
* the Accumulo Connector is for a MockInstance. In the event that the cardinality is
* being updated asynchronously, there are no guarantees that the resulting cardinality
* will be correct.
*
* @param accumuloConn - A connection to a Mock Accumulo Instance that hosts the PCJ table. (not null)
* @param pcjTableName - The name of the PCJ table that will have its cardinality updated. (not null)
* @param delta - How much the cardinality will change.
* @throws PCJStorageException The cardinality could not be updated.
*/
private void updateMockCardinality(final Connector accumuloConn, final String pcjTableName, final long delta) throws PCJStorageException {
checkNotNull(accumuloConn);
checkNotNull(pcjTableName);
BatchWriter batchWriter = null;
try {
batchWriter = accumuloConn.createBatchWriter(pcjTableName, new BatchWriterConfig());
final long cardinality = getPcjMetadata(accumuloConn, pcjTableName).getCardinality();
final Mutation mutation = new Mutation(PCJ_METADATA_ROW_ID);
final Value newCardinality = new Value(longLexicoder.encode(cardinality + delta));
mutation.put(PCJ_METADATA_FAMILY, PCJ_METADATA_CARDINALITY, newCardinality);
batchWriter.addMutation(mutation);
} catch (TableNotFoundException | MutationsRejectedException e) {
throw new PCJStorageException("Could not update the cardinality value of the PCJ Table named: " + pcjTableName, e);
} finally {
if (batchWriter != null) {
try {
batchWriter.close();
} catch (final MutationsRejectedException e) {
throw new PCJStorageException("Could not update the cardinality value of the PCJ Table named: " + pcjTableName, e);
}
}
}
}
use of org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException in project incubator-rya by apache.
the class PcjTables method purgePcjTable.
/**
* Deletes all of the rows that are in a PCJ index and sets its cardinality back to 0.
*
* @param accumuloConn - Connects to the Accumulo that hosts the PCJ indices. (not null)
* @param pcjTableName - The name of the PCJ table that will be purged. (not null)
* @throws PCJStorageException Either the rows could not be dropped from the
* PCJ table or the metadata could not be written back to the table.
*/
public void purgePcjTable(final Connector accumuloConn, final String pcjTableName) throws PCJStorageException {
checkNotNull(accumuloConn);
checkNotNull(pcjTableName);
// Fetch the metadaata from the PCJ table.
final PcjMetadata oldMetadata = getPcjMetadata(accumuloConn, pcjTableName);
// Delete all of the rows
try {
accumuloConn.tableOperations().deleteRows(pcjTableName, null, null);
} catch (AccumuloException | AccumuloSecurityException | TableNotFoundException e) {
throw new PCJStorageException("Could not delete the rows of data from PCJ table named: " + pcjTableName, e);
}
// Store the new metadata.
final PcjMetadata newMetadata = new PcjMetadata(oldMetadata.getSparql(), 0L, oldMetadata.getVarOrders());
final List<Mutation> mutations = makeWriteMetadataMutations(newMetadata);
BatchWriter writer = null;
try {
writer = accumuloConn.createBatchWriter(pcjTableName, new BatchWriterConfig());
writer.addMutations(mutations);
writer.flush();
} catch (final TableNotFoundException | MutationsRejectedException e) {
throw new PCJStorageException("Could not rewrite the PCJ cardinality for table named '" + pcjTableName + "'. This table will not work anymore.", e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final MutationsRejectedException e) {
throw new PCJStorageException("Could not close the batch writer.", e);
}
}
}
}
Aggregations