Search in sources :

Example 1 with UpdateValue

use of com.basho.riak.client.api.commands.kv.UpdateValue in project YCSB by brianfrankcooper.

the class RiakKVClient method update.

/**
   * Update a record in the database. Any field/value pairs in the specified values HashMap will be written into the
   * record with the specified record key, overwriting any existing values with the same field name.
   *
   * @param table  The name of the table (Riak bucket)
   * @param key    The record key of the record to write.
   * @param values A HashMap of field/value pairs to update in the record
   * @return Zero on success, a non-zero error code on error
   */
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
    // If eventual consistency model is in use, then an update operation is pratically equivalent to an insert one.
    if (!strongConsistency) {
        return insert(table, key, values);
    }
    Location location = new Location(new Namespace(bucketType, table), key);
    UpdateValue update = new UpdateValue.Builder(location).withUpdate(new UpdateEntity(new RiakObject().setValue(BinaryValue.create(serializeTable(values))))).build();
    RiakFuture<UpdateValue.Response, Location> future = riakClient.executeAsync(update);
    try {
        // For some reason, the update transaction doesn't throw any exception when no cluster has been started, so one
        // needs to check whether it was done or not. When calling the wasUpdated() function with no nodes available, a
        // NullPointerException is thrown.
        // Moreover, such exception could be thrown when more threads are trying to update the same key or, more
        // generally, when the system is being queried by many clients (i.e. overloaded). This is a known limitation of
        // Riak KV's strong consistency implementation.
        future.get(transactionTimeLimit, TimeUnit.SECONDS).wasUpdated();
    } catch (TimeoutException e) {
        if (debug) {
            System.err.println("Unable to update key " + key + ". Reason: TIME OUT");
        }
        return TIME_OUT;
    } catch (Exception e) {
        if (debug) {
            System.err.println("Unable to update key " + key + ". Reason: " + e.toString());
        }
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : RiakObject(com.basho.riak.client.core.query.RiakObject) UpdateValue(com.basho.riak.client.api.commands.kv.UpdateValue) Namespace(com.basho.riak.client.core.query.Namespace) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Location(com.basho.riak.client.core.query.Location) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

UpdateValue (com.basho.riak.client.api.commands.kv.UpdateValue)1 Location (com.basho.riak.client.core.query.Location)1 Namespace (com.basho.riak.client.core.query.Namespace)1 RiakObject (com.basho.riak.client.core.query.RiakObject)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1