Search in sources :

Example 21 with PCJStorageException

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);
}
Also used : VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) ArrayList(java.util.ArrayList) List(java.util.List) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) Document(org.bson.Document) HashSet(java.util.HashSet)

Example 22 with PCJStorageException

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);
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Example 23 with PCJStorageException

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;
}
Also used : Value(org.apache.accumulo.core.data.Value) Mutation(org.apache.accumulo.core.data.Mutation) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) RepositoryException(org.openrdf.repository.RepositoryException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) MalformedQueryException(org.openrdf.query.MalformedQueryException) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) HashSet(java.util.HashSet)

Example 24 with PCJStorageException

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);
            }
        }
    }
}
Also used : TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 25 with PCJStorageException

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);
            }
        }
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) Mutation(org.apache.accumulo.core.data.Mutation) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) BatchWriter(org.apache.accumulo.core.client.BatchWriter) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Aggregations

PCJStorageException (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)25 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)11 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)11 AccumuloException (org.apache.accumulo.core.client.AccumuloException)9 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)9 RyaClientException (org.apache.rya.api.client.RyaClientException)9 MalformedQueryException (org.openrdf.query.MalformedQueryException)9 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)7 ConditionalMutation (org.apache.accumulo.core.data.ConditionalMutation)6 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)6 HashSet (java.util.HashSet)5 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)5 Mutation (org.apache.accumulo.core.data.Mutation)5 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)5 RyaDetailsRepositoryException (org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException)5 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)5 RepositoryException (org.openrdf.repository.RepositoryException)5 BatchWriter (org.apache.accumulo.core.client.BatchWriter)4 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)4 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)4