Java Code Examples for org.apache.accumulo.core.data.ConditionalMutation#put()
The following examples show how to use
org.apache.accumulo.core.data.ConditionalMutation#put() .
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: PcjTables.java From rya with Apache License 2.0 | 6 votes |
/** * Creates a {@link ConditionalMutation} that only updates the cardinality * of the PCJ table if the old value has not changed by the time this mutation * is committed to Accumulo. * * @param current - The current cardinality value. * @param delta - How much the cardinality will change. * @return The mutation that will perform the conditional update. */ private static ConditionalMutation makeUpdateCardinalityMutation(final long current, final long delta) { // Try to update the cardinality by the delta. final ConditionalMutation mutation = new ConditionalMutation(PCJ_METADATA_ROW_ID); final Condition lastCardinalityStillCurrent = new Condition( PCJ_METADATA_FAMILY, PCJ_METADATA_CARDINALITY); // Require the old cardinality to be the value we just read. final byte[] currentCardinalityBytes = longLexicoder.encode( current ); lastCardinalityStillCurrent.setValue( currentCardinalityBytes ); mutation.addCondition(lastCardinalityStillCurrent); // If that is the case, then update to the new value. final Value newCardinality = new Value( longLexicoder.encode(current + delta) ); mutation.put(PCJ_METADATA_FAMILY, PCJ_METADATA_CARDINALITY, newCardinality); return mutation; }
Example 2
Source File: ARS.java From accumulo-examples with Apache License 2.0 | 4 votes |
public void cancel(String what, String when, String who) throws Exception { String row = what + ":" + when; // Even though this method is only deleting a column, its important to use a conditional writer. // By updating the seq # when deleting a reservation, it // will cause any concurrent reservations to retry. If this delete were done using a batch // writer, then a concurrent reservation could report WAIT_LISTED // when it actually got the reservation. // its important to use an isolated scanner so that only whole mutations are seen try ( ConditionalWriter cwriter = client.createConditionalWriter(rTable, new ConditionalWriterConfig()); Scanner scanner = new IsolatedScanner(client.createScanner(rTable, Authorizations.EMPTY))) { while (true) { scanner.setRange(new Range(row)); int seq = -1; String reservation = null; for (Entry<Key,Value> entry : scanner) { String cf = entry.getKey().getColumnFamilyData().toString(); String cq = entry.getKey().getColumnQualifierData().toString(); String val = entry.getValue().toString(); // EXCERCISE avoid linear scan if (cf.equals("tx") && cq.equals("seq")) { seq = Integer.parseInt(val); } else if (cf.equals("res") && val.equals(who)) { reservation = cq; } } if (reservation != null) { ConditionalMutation update = new ConditionalMutation(row, new Condition("tx", "seq").setValue(seq + "")); update.putDelete("res", reservation); update.put("tx", "seq", (seq + 1) + ""); Status status = cwriter.write(update).getStatus(); switch (status) { case ACCEPTED: // successfully canceled reservation return; case REJECTED: case UNKNOWN: // retry // EXCERCISE exponential back-off could be used here break; default: throw new RuntimeException("Unexpected status " + status); } } else { // not reserved, nothing to do break; } } } }
Example 3
Source File: AccumuloRyaInstanceDetailsRepository.java From rya with Apache License 2.0 | 4 votes |
@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(); } } }