Search in sources :

Example 1 with FetchValue

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

the class RiakKVClient method scan.

/**
   * Perform a range scan for a set of records in the database. Each field/value pair from the result will be stored in
   * a HashMap.
   * Note: The scan operation requires the use of secondary indexes (2i) and LevelDB.
   *
   * @param table       The name of the table (Riak bucket)
   * @param startkey    The record key of the first record to read.
   * @param recordcount The number of records to read
   * @param fields      The list of fields to read, or null for all of them
   * @param result      A Vector of HashMaps, where each HashMap is a set field/value pairs for one record
   * @return Zero on success, a non-zero error code on error
   */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    if (strongConsistency && !performStrongConsistentScans) {
        return Status.NOT_IMPLEMENTED;
    }
    // The strong consistent bucket-type is not capable of storing 2i indexes. So, we need to read them from the fake
    // one (which we use only to store indexes). This is why, when using such a consistency model, the bucketType2i
    // variable is set to FAKE_BUCKET_TYPE.
    IntIndexQuery iiq = new IntIndexQuery.Builder(new Namespace(bucketType2i, table), "key", getKeyAsLong(startkey), Long.MAX_VALUE).withMaxResults(recordcount).withPaginationSort(true).build();
    Location location;
    RiakFuture<IntIndexQuery.Response, IntIndexQuery> future = riakClient.executeAsync(iiq);
    try {
        IntIndexQuery.Response response = future.get(transactionTimeLimit, TimeUnit.SECONDS);
        List<IntIndexQuery.Response.Entry> entries = response.getEntries();
        // If no entries were retrieved, then something bad happened...
        if (entries.size() == 0) {
            if (debug) {
                System.err.println("Unable to scan any record starting from key " + startkey + ", aborting transaction. " + "Reason: NOT FOUND");
            }
            return Status.NOT_FOUND;
        }
        for (IntIndexQuery.Response.Entry entry : entries) {
            // fetching the key from the one retrieved with the 2i indexes search operation.
            if (strongConsistency) {
                location = new Location(new Namespace(bucketType, table), entry.getRiakObjectLocation().getKeyAsString());
            } else {
                location = entry.getRiakObjectLocation();
            }
            FetchValue fv = new FetchValue.Builder(location).withOption(FetchValue.Option.R, rvalue).build();
            FetchValue.Response keyResponse = fetch(fv);
            if (keyResponse.isNotFound()) {
                if (debug) {
                    System.err.println("Unable to scan all requested records starting from key " + startkey + ", aborting " + "transaction. Reason: NOT FOUND");
                }
                return Status.NOT_FOUND;
            }
            // Create the partial result to add to the result vector.
            HashMap<String, ByteIterator> partialResult = new HashMap<>();
            createResultHashMap(fields, keyResponse, partialResult);
            result.add(partialResult);
        }
    } catch (TimeoutException e) {
        if (debug) {
            System.err.println("Unable to scan all requested records starting from key " + startkey + ", aborting " + "transaction. Reason: TIME OUT");
        }
        return TIME_OUT;
    } catch (Exception e) {
        if (debug) {
            System.err.println("Unable to scan all records starting from key " + startkey + ", aborting transaction. " + "Reason: " + e.toString());
        }
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : RiakUtils.createResultHashMap(com.yahoo.ycsb.db.riak.RiakUtils.createResultHashMap) IntIndexQuery(com.basho.riak.client.api.commands.indexes.IntIndexQuery) Namespace(com.basho.riak.client.core.query.Namespace) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) FetchValue(com.basho.riak.client.api.commands.kv.FetchValue) Location(com.basho.riak.client.core.query.Location) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with FetchValue

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

the class RiakKVClient method read.

/**
   * Read a record from the database. Each field/value pair from the result will be stored in a HashMap.
   *
   * @param table  The name of the table (Riak bucket)
   * @param key    The record key of the record to read.
   * @param fields The list of fields to read, or null for all of them
   * @param result A HashMap of field/value pairs for the result
   * @return Zero on success, a non-zero error code on error
   */
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    Location location = new Location(new Namespace(bucketType, table), key);
    FetchValue fv = new FetchValue.Builder(location).withOption(FetchValue.Option.R, rvalue).build();
    FetchValue.Response response;
    try {
        response = fetch(fv);
        if (response.isNotFound()) {
            if (debug) {
                System.err.println("Unable to read key " + key + ". Reason: NOT FOUND");
            }
            return Status.NOT_FOUND;
        }
    } catch (TimeoutException e) {
        if (debug) {
            System.err.println("Unable to read key " + key + ". Reason: TIME OUT");
        }
        return TIME_OUT;
    } catch (Exception e) {
        if (debug) {
            System.err.println("Unable to read key " + key + ". Reason: " + e.toString());
        }
        return Status.ERROR;
    }
    // Create the result HashMap.
    createResultHashMap(fields, response, result);
    return Status.OK;
}
Also used : FetchValue(com.basho.riak.client.api.commands.kv.FetchValue) 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

FetchValue (com.basho.riak.client.api.commands.kv.FetchValue)2 Location (com.basho.riak.client.core.query.Location)2 Namespace (com.basho.riak.client.core.query.Namespace)2 IOException (java.io.IOException)2 TimeoutException (java.util.concurrent.TimeoutException)2 IntIndexQuery (com.basho.riak.client.api.commands.indexes.IntIndexQuery)1 RiakUtils.createResultHashMap (com.yahoo.ycsb.db.riak.RiakUtils.createResultHashMap)1