Search in sources :

Example 1 with StoreValue

use of com.basho.riak.client.api.commands.kv.StoreValue 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;
}
Also used : RiakObject(com.basho.riak.client.core.query.RiakObject) StoreValue(com.basho.riak.client.api.commands.kv.StoreValue) 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

StoreValue (com.basho.riak.client.api.commands.kv.StoreValue)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