use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class AzureCosmosClient 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.
*
* @param table The name of the table
* @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, Map<String, ByteIterator> values) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Insert key: {} into table: {}", key, table);
}
try {
CosmosContainer container = AzureCosmosClient.containerCache.get(table);
if (container == null) {
container = AzureCosmosClient.database.getContainer(table);
AzureCosmosClient.containerCache.put(table, container);
}
PartitionKey pk = new PartitionKey(key);
ObjectNode node = OBJECT_MAPPER.createObjectNode();
node.put("id", key);
for (Map.Entry<String, ByteIterator> pair : values.entrySet()) {
node.put(pair.getKey(), pair.getValue().toString());
}
if (AzureCosmosClient.useUpsert) {
container.upsertItem(node, pk, new CosmosItemRequestOptions());
} else {
container.createItem(node, pk, new CosmosItemRequestOptions());
}
return Status.OK;
} catch (CosmosException e) {
if (!AzureCosmosClient.includeExceptionStackInLog) {
e = null;
}
LOGGER.error("Failed to insert key {} to collection {} in database {}", key, table, AzureCosmosClient.databaseName, e);
}
return Status.ERROR;
}
use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class AzureClient method readEntity.
private Status readEntity(String key, Map<String, ByteIterator> result) {
try {
// firstly, retrieve the entity to be deleted
TableOperation retrieveOp = TableOperation.retrieve(partitionKey, key, DynamicTableEntity.class);
DynamicTableEntity entity = cloudTable.execute(retrieveOp).getResultAsType();
HashMap<String, EntityProperty> properties = entity.getProperties();
for (Entry<String, EntityProperty> entry : properties.entrySet()) {
String fieldName = entry.getKey();
ByteIterator fieldVal = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
result.put(fieldName, fieldVal);
}
return Status.OK;
} catch (Exception e) {
return Status.ERROR;
}
}
use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class CassandraCQLClient 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.
*
* Cassandra CQL uses "token" method for range scan which doesn't always yield
* intuitive results.
*
* @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 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) {
try {
PreparedStatement stmt = (fields == null) ? scanAllStmt.get() : scanStmts.get(fields);
// Prepare statement on demand
if (stmt == null) {
Select.Builder selectBuilder;
if (fields == null) {
selectBuilder = QueryBuilder.select().all();
} else {
selectBuilder = QueryBuilder.select();
for (String col : fields) {
((Select.Selection) selectBuilder).column(col);
}
}
Select selectStmt = selectBuilder.from(table);
// The statement builder is not setup right for tokens.
// So, we need to build it manually.
String initialStmt = selectStmt.toString();
StringBuilder scanStmt = new StringBuilder();
scanStmt.append(initialStmt.substring(0, initialStmt.length() - 1));
scanStmt.append(" WHERE ");
scanStmt.append(QueryBuilder.token(YCSB_KEY));
scanStmt.append(" >= ");
scanStmt.append("token(");
scanStmt.append(QueryBuilder.bindMarker());
scanStmt.append(")");
scanStmt.append(" LIMIT ");
scanStmt.append(QueryBuilder.bindMarker());
stmt = session.prepare(scanStmt.toString());
stmt.setConsistencyLevel(readConsistencyLevel);
if (trace) {
stmt.enableTracing();
}
PreparedStatement prevStmt = (fields == null) ? scanAllStmt.getAndSet(stmt) : scanStmts.putIfAbsent(new HashSet(fields), stmt);
if (prevStmt != null) {
stmt = prevStmt;
}
}
logger.debug(stmt.getQueryString());
logger.debug("startKey = {}, recordcount = {}", startkey, recordcount);
ResultSet rs = session.execute(stmt.bind(startkey, Integer.valueOf(recordcount)));
HashMap<String, ByteIterator> tuple;
while (!rs.isExhausted()) {
Row row = rs.one();
tuple = new HashMap<String, ByteIterator>();
ColumnDefinitions cd = row.getColumnDefinitions();
for (ColumnDefinitions.Definition def : cd) {
ByteBuffer val = row.getBytesUnsafe(def.getName());
if (val != null) {
tuple.put(def.getName(), new ByteArrayByteIterator(val.array()));
} else {
tuple.put(def.getName(), null);
}
}
result.add(tuple);
}
return Status.OK;
} catch (Exception e) {
logger.error(MessageFormatter.format("Error scanning with startkey: {}", startkey).getMessage(), e);
return Status.ERROR;
}
}
use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class CassandraCQLClient 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
* @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, Map<String, ByteIterator> values) {
try {
Set<String> fields = values.keySet();
PreparedStatement stmt = updateStmts.get(fields);
// Prepare statement on demand
if (stmt == null) {
Update updateStmt = QueryBuilder.update(table);
// Add fields
for (String field : fields) {
updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker()));
}
// Add key
updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker()));
stmt = session.prepare(updateStmt);
stmt.setConsistencyLevel(writeConsistencyLevel);
if (trace) {
stmt.enableTracing();
}
PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt);
if (prevStmt != null) {
stmt = prevStmt;
}
}
if (logger.isDebugEnabled()) {
logger.debug(stmt.getQueryString());
logger.debug("key = {}", key);
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
logger.debug("{} = {}", entry.getKey(), entry.getValue());
}
}
// Add fields
ColumnDefinitions vars = stmt.getVariables();
BoundStatement boundStmt = stmt.bind();
for (int i = 0; i < vars.size() - 1; i++) {
boundStmt.setString(i, values.get(vars.getName(i)).toString());
}
// Add key
boundStmt.setString(vars.size() - 1, key);
session.execute(boundStmt);
return Status.OK;
} catch (Exception e) {
logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e);
}
return Status.ERROR;
}
use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class CassandraCQLClientTest method testReadSingleColumn.
@Test
public void testReadSingleColumn() throws Exception {
insertRow();
final HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>();
final Set<String> fields = Sets.newHashSet("field1");
final Status status = client.read(TABLE, DEFAULT_ROW_KEY, fields, result);
assertThat(status, is(Status.OK));
assertThat(result.entrySet(), hasSize(1));
final Map<String, String> strResult = StringByteIterator.getStringMap(result);
assertThat(strResult, hasEntry("field1", "value2"));
}
Aggregations