use of site.ycsb.ByteIterator in project gora by apache.
the class GoraClientTest method testRead.
/**
* Test read performs a read record test from the database.
*/
@Test
public void testRead() {
insertData();
HashMap<String, ByteIterator> results = new HashMap<>();
// this could be null as well
Set<String> fields = new HashSet<>();
Status result = benchmarkClient.read(Constants.TEST_TABLE, Constants.TEST_KEY_1, fields, results);
assertEquals(Status.OK, result);
assertEquals(DATA_TO_INSERT.size(), results.size());
assertEquals(DATA_TO_INSERT.get(Constants.TEST_FIELD_0).toString(), results.get(Constants.TEST_FIELD_0).toString());
assertEquals(Constants.TEST_VALUE_0, results.get(Constants.TEST_FIELD_0).toString());
}
use of site.ycsb.ByteIterator in project gora by apache.
the class GoraBenchmarkClient 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.
*
* @param table The name of the table
* @param startKey the start key
* @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 The result of the operation.
*/
@Override
public Status scan(String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
try {
Query<String, User> goraQuery = dataStore.newQuery();
goraQuery.setStartKey(startKey);
goraQuery.setLimit(recordCount);
Result<String, User> resultSet = goraQuery.execute();
while (resultSet.next()) {
HashMap<String, ByteIterator> resultsMap = new HashMap<>();
for (int fieldCount = 0; fieldCount < totalFieldCount; fieldCount++) {
String field = FIELDS[fieldCount + 1];
int fieldIndex = fieldCount + 1;
String value = resultSet.get().get(fieldIndex).toString();
resultsMap.put(field, new StringByteIterator(value));
}
result.add(resultsMap);
}
} catch (Exception e) {
LOG.info("There is a problem in scanning data from the table \n {}", e.getMessage(), e);
return Status.ERROR;
}
return Status.OK;
}
use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class BackoffSelectStrategy method scanSpecificFields.
/**
* Performs the {@link #scan(String, String, int, Set, Vector)} operation N1Ql only for a subset of the fields.
*
* @param table The name of the table
* @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 The result of the operation.
*/
private Status scanSpecificFields(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
String scanSpecQuery = "SELECT " + joinFields(fields) + " FROM `" + bucketName + "` WHERE meta().id >= '$1' LIMIT $2";
N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized(scanSpecQuery, JsonArray.from(formatId(table, startkey), recordcount), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism)));
if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) {
throw new RuntimeException("Error while parsing N1QL Result. Query: " + scanSpecQuery + ", Errors: " + queryResult.errors());
}
boolean allFields = fields == null || fields.isEmpty();
result.ensureCapacity(recordcount);
for (N1qlQueryRow row : queryResult) {
JsonObject value = row.value();
if (fields == null) {
value = value.getObject(bucketName);
}
Set<String> f = allFields ? value.getNames() : fields;
HashMap<String, ByteIterator> tuple = new HashMap<String, ByteIterator>(f.size());
for (String field : f) {
tuple.put(field, new StringByteIterator(value.getString(field)));
}
result.add(tuple);
}
return Status.OK;
}
use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class GoogleDatastoreClient method doSingleItemMutation.
private Status doSingleItemMutation(String table, String key, @Nullable Map<String, ByteIterator> values, MutationType mutationType) {
// First build the key.
Key.Builder datastoreKey = buildPrimaryKey(table, key);
// Build a commit request in non-transactional mode.
// Single item mutation to google datastore
// is always atomic and strongly consistent. Transaction is only necessary
// for multi-item mutation, or Read-modify-write operation.
CommitRequest.Builder commitRequest = CommitRequest.newBuilder();
commitRequest.setMode(Mode.NON_TRANSACTIONAL);
if (mutationType == MutationType.DELETE) {
commitRequest.addMutationsBuilder().setDelete(datastoreKey);
} else {
// If this is not for delete, build the entity.
Entity.Builder entityBuilder = Entity.newBuilder();
entityBuilder.setKey(datastoreKey);
for (Entry<String, ByteIterator> val : values.entrySet()) {
entityBuilder.getMutableProperties().put(val.getKey(), Value.newBuilder().setStringValue(val.getValue().toString()).setExcludeFromIndexes(skipIndex).build());
}
Entity entity = entityBuilder.build();
logger.debug("entity built as: " + entity.toString());
if (mutationType == MutationType.UPSERT) {
commitRequest.addMutationsBuilder().setUpsert(entity);
} else if (mutationType == MutationType.UPDATE) {
commitRequest.addMutationsBuilder().setUpdate(entity);
} else {
throw new RuntimeException("Impossible MutationType, code bug.");
}
}
try {
datastore.commit(commitRequest.build());
logger.debug("successfully committed.");
} catch (DatastoreException exception) {
// Catch all Datastore rpc errors.
// Log the exception, the name of the method called and the error code.
logger.error(String.format("Datastore Exception when committing (%s): %s %s", exception.getMessage(), exception.getMethodName(), exception.getCode()));
// will bubble up to the user as part of the YCSB Status "name".
return new Status("ERROR-" + exception.getCode(), exception.getMessage());
}
return Status.OK;
}
use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class GridDBClientTest method testUpdate.
@Test
public void testUpdate() {
HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>();
HashMap<String, ByteIterator> values = new HashMap<String, ByteIterator>();
insertToDatabase();
String keyForUpdate = "field2";
Set<String> fields = Collections.singleton(keyForUpdate);
String strValueToUpdate = "new_value_2";
ByteIterator valForUpdate = new StringByteIterator(strValueToUpdate);
values.put(keyForUpdate, valForUpdate);
Status updateStatus = myClient.update(TEST_TABLE, DEFAULT_ROW_KEY, values);
assertEquals(updateStatus, Status.OK);
// After update, we read the update row for get new value
myClient.read(TEST_TABLE, DEFAULT_ROW_KEY, fields, result);
assertNotEquals(result.entrySet(), 0);
boolean found = false;
for (int i = 0; i < FIELD_COUNT; i++) {
ByteIterator iter = result.get("field" + i);
byte[] byteArray1 = iter.toArray();
String value = new String(byteArray1);
// check result has row value is new update value or not
if (value.equals(strValueToUpdate)) {
found = true;
}
}
assertEquals(found, true);
}
Aggregations