use of com.basho.riak.client.core.query.RiakObject 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;
}
use of com.basho.riak.client.core.query.RiakObject in project YCSB by brianfrankcooper.
the class RiakKVClient method insert.
/**
* Insert 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. Also creates a secondary index (2i) for each record consisting of the key
* converted to long to be used for the scan operation.
*
* @param table The name of the table (Riak bucket)
* @param key The record key of the record to insert.
* @param values A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error
*/
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
Location location = new Location(new Namespace(bucketType, table), key);
RiakObject object = new RiakObject();
// riak.bucket_type property.
if (strongConsistency && performStrongConsistentScans) {
// Create a fake object to store in the default bucket-type just to keep track of the 2i indices.
Location fakeLocation = new Location(new Namespace(strongConsistentScansBucketType, table), key);
// Obviously, we want the fake object to contain as less data as possible. We can't create a void object, so
// we have to choose the minimum data size allowed: it is one byte.
RiakObject fakeObject = new RiakObject();
fakeObject.setValue(BinaryValue.create(new byte[] { 0x00 }));
fakeObject.getIndexes().getIndex(LongIntIndex.named("key_int")).add(getKeyAsLong(key));
StoreValue fakeStore = new StoreValue.Builder(fakeObject).withLocation(fakeLocation).build();
// We don't mind whether the operation is finished or not, because waiting for it to complete would slow down the
// client and make our solution too heavy to be seen as a valid compromise. This will obviously mean that under
// heavy load conditions a scan operation could fail due to an unfinished "fakeStore".
riakClient.executeAsync(fakeStore);
} else if (!strongConsistency) {
// The next operation is useless when using strong consistency model, so it's ok to perform it only when using
// eventual consistency.
object.getIndexes().getIndex(LongIntIndex.named("key_int")).add(getKeyAsLong(key));
}
// Store proper values into the object.
object.setValue(BinaryValue.create(serializeTable(values)));
StoreValue store = new StoreValue.Builder(object).withOption(StoreValue.Option.W, wvalue).withLocation(location).build();
RiakFuture<StoreValue.Response, Location> future = riakClient.executeAsync(store);
try {
future.get(transactionTimeLimit, TimeUnit.SECONDS);
} catch (TimeoutException e) {
if (debug) {
System.err.println("Unable to insert key " + key + ". Reason: TIME OUT");
}
return TIME_OUT;
} catch (Exception e) {
if (debug) {
System.err.println("Unable to insert key " + key + ". Reason: " + e.toString());
}
return Status.ERROR;
}
return Status.OK;
}
Aggregations