Java Code Examples for org.apache.accumulo.core.client.ConditionalWriter#write()

The following examples show how to use org.apache.accumulo.core.client.ConditionalWriter#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccumuloRyaInstanceDetailsRepository.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void update(final RyaDetails oldDetails, final RyaDetails newDetails)
        throws NotInitializedException, ConcurrentUpdateException, RyaDetailsRepositoryException {
    // Preconditions.
    requireNonNull(oldDetails);
    requireNonNull(newDetails);

    if(!newDetails.getRyaInstanceName().equals( instanceName )) {
        throw new RyaDetailsRepositoryException("The instance name that was in the provided 'newDetails' does not match " +
                "the instance name that this repository is connected to. Make sure you're connected to the" +
                "correct Rya instance.");
    }

    if(!isInitialized()) {
        throw new NotInitializedException("Could not update the details for the Rya instanced named '" +
                instanceName + "' because it has not been initialized yet.");
    }

    // Use a conditional writer so that we can detect when the old details
    // are no longer the currently stored ones.
    ConditionalWriter writer = null;
    try {
        // Setup the condition that ensures the details have not changed since the edits were made.
        final byte[] oldDetailsBytes = serializer.serialize(oldDetails);
        final Condition condition = new Condition(COL_FAMILY, COL_QUALIFIER);
        condition.setValue( oldDetailsBytes );

        // Create the mutation that only performs the update if the details haven't changed.
        final ConditionalMutation mutation = new ConditionalMutation(ROW_ID);
        mutation.addCondition( condition );
        final byte[] newDetailsBytes = serializer.serialize(newDetails);
        mutation.put(COL_FAMILY, COL_QUALIFIER, new Value(newDetailsBytes));

        // Do the write.
        writer = connector.createConditionalWriter(detailsTableName, new ConditionalWriterConfig());
        final Result result = writer.write(mutation);
        switch(result.getStatus()) {
            case REJECTED:
            case VIOLATED:
                throw new ConcurrentUpdateException("Could not update the details for the Rya instance named '" +
                        instanceName + "' because the old value is out of date.");
            case UNKNOWN:
            case INVISIBLE_VISIBILITY:
                throw new RyaDetailsRepositoryException("Could not update the details for the Rya instance named '" + instanceName + "'.");
        }
    } catch (final TableNotFoundException | AccumuloException | AccumuloSecurityException e) {
        throw new RyaDetailsRepositoryException("Could not update the details for the Rya instance named '" + instanceName + "'.");
    } finally {
        if(writer != null) {
            writer.close();
        }
    }
}
 
Example 2
Source File: PcjTables.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Update the cardinality of a PCJ by a {@code delta}.
 *
 * @param accumuloConn - A connection to the Accumulo 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 updateCardinality(final Connector accumuloConn, final String pcjTableName, final long delta) throws PCJStorageException {
    checkNotNull(accumuloConn);
    checkNotNull(pcjTableName);

    ConditionalWriter conditionalWriter = null;
    try {
        conditionalWriter = accumuloConn.createConditionalWriter(pcjTableName, new ConditionalWriterConfig());

        boolean updated = false;
        while (!updated) {
            // Write the conditional update request to Accumulo.
            final long cardinality = getPcjMetadata(accumuloConn,pcjTableName).getCardinality();
            final ConditionalMutation mutation = makeUpdateCardinalityMutation(cardinality, delta);
            final ConditionalWriter.Result result = conditionalWriter.write(mutation);

            // Interpret the result.
            switch (result.getStatus()) {
            case ACCEPTED:
                updated = true;
                break;
            case REJECTED:
                break;
            case UNKNOWN:
                // We do not know if the mutation succeeded. At best, we
                // can hope the metadata hasn't been updated
                // since we originally fetched it and try again.
                // Otherwise, continue forwards as if it worked. It's
                // okay if this number is slightly off.
                final long newCardinality = getPcjMetadata(accumuloConn,pcjTableName).getCardinality();
                if (newCardinality != cardinality) {
                    updated = true;
                }
                break;
            case VIOLATED:
                throw new PCJStorageException("The cardinality could not be updated because the commit violated a table constraint.");
            case INVISIBLE_VISIBILITY:
                throw new PCJStorageException("The condition contains a visibility the updater can not satisfy.");
            }
        }
    } catch (AccumuloException | AccumuloSecurityException | TableNotFoundException e) {
        throw new PCJStorageException("Could not update the cardinality value of the PCJ Table named: " + pcjTableName, e);
    } finally {
        if (conditionalWriter != null) {
            conditionalWriter.close();
        }
    }
}