com.yahoo.ycsb.db
Class SherpaClient

java.lang.Object
  extended by com.yahoo.ycsb.DB
      extended by com.yahoo.ycsb.db.SherpaClient

public class SherpaClient
extends DB

This is a Java Sherpa client to be used for the Benchmark app. Since Sherpa is a RESful service, this class will make use of Apache's HttpClient 4.x to make the web service calls.


Field Summary
protected static java.lang.String ADDITIONAL_FIELD
           
protected static java.lang.String CODE
           
protected static java.lang.String CONTINUATION
           
protected static java.lang.String DELETE
           
protected static java.lang.String END_HASH_KEY
           
protected static java.lang.String END_HASH_KEY_VALUE
           
protected static java.lang.String FIELDS
           
protected static java.lang.String FIRST_FIELD
           
protected static java.lang.String GET
           
protected static java.lang.String HASH_SCAN
           
protected static java.lang.String HOSTS
           
protected static java.lang.String HTTP
           
protected static int HTTP_ERROR
           
protected static int JSON_ERROR
           
protected static java.lang.String NAMESPACE
           
protected static int OK
           
protected static java.lang.String ORDERED_SCAN
           
protected static java.lang.String PREDICATE
           
protected static java.lang.String RECORD_LIMIT
           
protected static java.lang.String RECORDS
           
protected static java.lang.String SCAN_COMPLETED
           
protected static java.lang.String SCANTYPE
           
protected static java.lang.String SET
           
protected static java.lang.String START_HASH_KEY
           
protected static java.lang.String START_HASH_KEY_HEADER
           
protected static java.lang.String START_HASH_KEY_VALUE
           
protected static java.lang.String START_KEY_ADDITIONAL
           
protected static java.lang.String START_KEY_FIRST
           
protected static java.lang.String STATUS
           
protected static java.lang.String URI_PATH
           
protected static java.lang.String UTF8
           
protected static java.lang.String VALUE
           
protected static java.lang.String YDHT
           
 
Constructor Summary
SherpaClient()
           
 
Method Summary
 void cleanup()
          Cleanup any state for this DB.
static java.lang.String createRecordBody(java.util.HashMap<java.lang.String,java.lang.String> values)
          Method to create the Sherpa JSON record body from an input HashMap of key/values.
 int delete(java.lang.String table, java.lang.String key)
          Delete a record from the database.
static java.lang.String encodeFields(java.util.Set<java.lang.String> fields)
          Method to encode a set of fields to retrieve from a Sherpa Get or Scan request.
static java.lang.String encodeString(java.lang.String key)
          Method to encode a String to UTF8.
static java.lang.String getHashKey(java.lang.String key)
          Method to return the hex string representation of a 32-bit unsigned key value used for doing Sherpa scans.
static int getResponseCode(JSONObject jsonObject)
          Helper method to retrieve the response status code from a JSON Response to determine if it was successful or not.
 void init()
          Initialize any state for this DB.
 int insert(java.lang.String table, java.lang.String key, java.util.HashMap<java.lang.String,java.lang.String> values)
          Insert a record in the database.
static void populateHashScanRecords(JSONObject curResult, java.util.Vector<java.util.HashMap<java.lang.String,java.lang.String>> result)
          Method to parse and store a JSON response from a hash scan request.
static void populateOrderedScanRecords(JSONObject curResult, java.util.Vector<java.util.HashMap<java.lang.String,java.lang.String>> result)
          Method to parse and store a JSON response from an ordered scan request.
static void populateReadRecord(JSONObject response, java.util.HashMap<java.lang.String,java.lang.String> result)
          Method to parse and store a JSON response from reading a single Sherpa record into the corresponding HashMap key/value pairs.
 int read(java.lang.String table, java.lang.String key, java.util.Set<java.lang.String> fields, java.util.HashMap<java.lang.String,java.lang.String> result)
          Read a record from the database.
 int scan(java.lang.String table, java.lang.String startkey, int recordcount, java.util.Set<java.lang.String> fields, java.util.Vector<java.util.HashMap<java.lang.String,java.lang.String>> result)
          Perform a range scan for a set of records in the database.
 int update(java.lang.String table, java.lang.String key, java.util.HashMap<java.lang.String,java.lang.String> values)
          Update a record in the database.
 
Methods inherited from class com.yahoo.ycsb.DB
getProperties, setProperties
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET

protected static final java.lang.String GET
See Also:
Constant Field Values

SET

protected static final java.lang.String SET
See Also:
Constant Field Values

DELETE

protected static final java.lang.String DELETE
See Also:
Constant Field Values

HASH_SCAN

protected static final java.lang.String HASH_SCAN
See Also:
Constant Field Values

ORDERED_SCAN

protected static final java.lang.String ORDERED_SCAN
See Also:
Constant Field Values

FIRST_FIELD

protected static final java.lang.String FIRST_FIELD
See Also:
Constant Field Values

ADDITIONAL_FIELD

protected static final java.lang.String ADDITIONAL_FIELD
See Also:
Constant Field Values

START_KEY_FIRST

protected static final java.lang.String START_KEY_FIRST
See Also:
Constant Field Values

START_KEY_ADDITIONAL

protected static final java.lang.String START_KEY_ADDITIONAL
See Also:
Constant Field Values

UTF8

protected static final java.lang.String UTF8
See Also:
Constant Field Values

START_HASH_KEY_HEADER

protected static final java.lang.String START_HASH_KEY_HEADER
See Also:
Constant Field Values

CONTINUATION

protected static final java.lang.String CONTINUATION
See Also:
Constant Field Values

START_HASH_KEY

protected static final java.lang.String START_HASH_KEY
See Also:
Constant Field Values

END_HASH_KEY

protected static final java.lang.String END_HASH_KEY
See Also:
Constant Field Values

START_HASH_KEY_VALUE

protected static final java.lang.String START_HASH_KEY_VALUE
See Also:
Constant Field Values

END_HASH_KEY_VALUE

protected static final java.lang.String END_HASH_KEY_VALUE
See Also:
Constant Field Values

RECORD_LIMIT

protected static final java.lang.String RECORD_LIMIT
See Also:
Constant Field Values

PREDICATE

protected static final java.lang.String PREDICATE
See Also:
Constant Field Values

SCAN_COMPLETED

protected static final java.lang.String SCAN_COMPLETED
See Also:
Constant Field Values

RECORDS

protected static final java.lang.String RECORDS
See Also:
Constant Field Values

URI_PATH

protected static final java.lang.String URI_PATH
See Also:
Constant Field Values

YDHT

protected static final java.lang.String YDHT
See Also:
Constant Field Values

STATUS

protected static final java.lang.String STATUS
See Also:
Constant Field Values

CODE

protected static final java.lang.String CODE
See Also:
Constant Field Values

FIELDS

protected static final java.lang.String FIELDS
See Also:
Constant Field Values

VALUE

protected static final java.lang.String VALUE
See Also:
Constant Field Values

OK

protected static final int OK
See Also:
Constant Field Values

JSON_ERROR

protected static final int JSON_ERROR
See Also:
Constant Field Values

HTTP_ERROR

protected static final int HTTP_ERROR
See Also:
Constant Field Values

HOSTS

protected static final java.lang.String HOSTS
See Also:
Constant Field Values

HTTP

protected static final java.lang.String HTTP
See Also:
Constant Field Values

NAMESPACE

protected static final java.lang.String NAMESPACE
See Also:
Constant Field Values

SCANTYPE

protected static final java.lang.String SCANTYPE
See Also:
Constant Field Values
Constructor Detail

SherpaClient

public SherpaClient()
Method Detail

init

public void init()
          throws DBException
Initialize any state for this DB. Called once per DB instance; there is one DB instance per client thread.

Overrides:
init in class DB
Throws:
DBException

cleanup

public void cleanup()
             throws DBException
Cleanup any state for this DB. Called once per DB instance; there is one DB instance per client thread.

Overrides:
cleanup in class DB
Throws:
DBException

encodeString

public static java.lang.String encodeString(java.lang.String key)
Method to encode a String to UTF8. All Sherpa table/record/field and record body data must be UTF8 since access is done via HTTP REST calls.

Parameters:
key - Key string we want to encode to UTF8
Returns:
UTF8 encoding of the input string

encodeFields

public static java.lang.String encodeFields(java.util.Set<java.lang.String> fields)
Method to encode a set of fields to retrieve from a Sherpa Get or Scan request.

Parameters:
fields - Set of String fields to retrieve from a Sherpa Get or Scan request
Returns:
String encoding of the set of fields to retrieve from a Sherpa request. This has the format of ?field=&field=...

getHashKey

public static java.lang.String getHashKey(java.lang.String key)
Method to return the hex string representation of a 32-bit unsigned key value used for doing Sherpa scans. This needs to match how Sherpa in the backend is hashing the keys to determine which tablet it is stored in.

Parameters:
key - Key string to be hashed
Returns:
String hex representation of the hashing of the input key

getResponseCode

public static int getResponseCode(JSONObject jsonObject)
Helper method to retrieve the response status code from a JSON Response to determine if it was successful or not.

Parameters:
jsonObject - JSONObject response from Sherpa
Returns:
int Response status code indicating if the request was successful or not

createRecordBody

public static java.lang.String createRecordBody(java.util.HashMap<java.lang.String,java.lang.String> values)
Method to create the Sherpa JSON record body from an input HashMap of key/values.

Parameters:
values - HashMap of key/values to be stored in a Sherpa record
Returns:
Sherpa JSON record representation of the input key/values HashMap

populateReadRecord

public static void populateReadRecord(JSONObject response,
                                      java.util.HashMap<java.lang.String,java.lang.String> result)
                               throws JSONException
Method to parse and store a JSON response from reading a single Sherpa record into the corresponding HashMap key/value pairs.

Parameters:
response - JSONObject response from reading a single Sherpa record
result - HashMap key/value pairs for the record's field/values
Throws:
JSONException - If there is an error parsing the JSON response

populateHashScanRecords

public static void populateHashScanRecords(JSONObject curResult,
                                           java.util.Vector<java.util.HashMap<java.lang.String,java.lang.String>> result)
                                    throws JSONException
Method to parse and store a JSON response from a hash scan request. This consists of multiple records each of which will be stored in the input result Vector as a HashMap of key/value pairs.

Parameters:
curResult - JSONObject response from a Sherpa hash scan request
result - Vector of Sherpa records each stored as a HashMap of key/values for the record's field/values
Throws:
JSONException - If there is an error parsing the JSON response

populateOrderedScanRecords

public static void populateOrderedScanRecords(JSONObject curResult,
                                              java.util.Vector<java.util.HashMap<java.lang.String,java.lang.String>> result)
                                       throws JSONException
Method to parse and store a JSON response from an ordered scan request. This consists of multiple records each of which will be stored in the input result Vector as a HashMap of key/value pairs.

Parameters:
curResult - JSONObject response from a Sherpa ordered scan request
result - Vector of Sherpa records each stored as a HashMap of key/values for the record's field/values
Throws:
JSONException - If there is an error parsing the JSON response

delete

public int delete(java.lang.String table,
                  java.lang.String key)
Description copied from class: DB
Delete a record from the database.

Specified by:
delete in class DB
Parameters:
table - The name of the table
key - The record key of the record to delete.
Returns:
Zero on success, a non-zero error code on error

insert

public int insert(java.lang.String table,
                  java.lang.String key,
                  java.util.HashMap<java.lang.String,java.lang.String> values)
Description copied from class: DB
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.

Specified by:
insert in class DB
Parameters:
table - The name of the table
key - The record key of the record to insert.
values - A HashMap of field/value pairs to insert in the record
Returns:
Zero on success, a non-zero error code on error

read

public int read(java.lang.String table,
                java.lang.String key,
                java.util.Set<java.lang.String> fields,
                java.util.HashMap<java.lang.String,java.lang.String> result)
Description copied from class: DB
Read a record from the database. Each field/value pair from the result will be stored in a HashMap.

Specified by:
read in class DB
Parameters:
table - The name of the table
key - The record key of the record to read.
fields - The list of fields to read, or null for all of them
result - A HashMap of field/value pairs for the result
Returns:
Zero on success, a non-zero error code on error

scan

public int scan(java.lang.String table,
                java.lang.String startkey,
                int recordcount,
                java.util.Set<java.lang.String> fields,
                java.util.Vector<java.util.HashMap<java.lang.String,java.lang.String>> result)
Description copied from class: DB
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.

Specified by:
scan in class DB
Parameters:
table - The name of the table
startkey - The record key of the first record to read.
recordcount - The number of records to read
fields - The list of fields to read, or null for all of them
result - A Vector of HashMaps, where each HashMap is a set field/value pairs for one record
Returns:
Zero on success, a non-zero error code on error

update

public int update(java.lang.String table,
                  java.lang.String key,
                  java.util.HashMap<java.lang.String,java.lang.String> values)
Description copied from class: DB
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.

Specified by:
update in class DB
Parameters:
table - The name of the table
key - The record key of the record to write.
values - A HashMap of field/value pairs to update in the record
Returns:
Zero on success, a non-zero error code on error