use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class AbstractDBTestCases method testInsertReadUpdate.
/**
* Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#update} .
*/
@Test
public void testInsertReadUpdate() {
DB client = getDB();
final String table = getClass().getSimpleName();
final String id = "update";
HashMap<String, ByteIterator> inserted = new HashMap<String, ByteIterator>();
inserted.put("a", new ByteArrayByteIterator(new byte[] { 1, 2, 3, 4 }));
Status result = client.insert(table, id, inserted);
assertThat("Insert did not return success (0).", result, is(Status.OK));
HashMap<String, ByteIterator> read = new HashMap<String, ByteIterator>();
Set<String> keys = Collections.singleton("a");
result = client.read(table, id, keys, read);
assertThat("Read did not return success (0).", result, is(Status.OK));
for (String key : keys) {
ByteIterator iter = read.get(key);
assertThat("Did not read the inserted field: " + key, iter, notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 1)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 2)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 3)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 4)));
assertFalse(iter.hasNext());
}
HashMap<String, ByteIterator> updated = new HashMap<String, ByteIterator>();
updated.put("a", new ByteArrayByteIterator(new byte[] { 5, 6, 7, 8 }));
result = client.update(table, id, updated);
assertThat("Update did not return success (0).", result, is(Status.OK));
read.clear();
result = client.read(table, id, null, read);
assertThat("Read, after update, did not return success (0).", result, is(Status.OK));
for (String key : keys) {
ByteIterator iter = read.get(key);
assertThat("Did not read the inserted field: " + key, iter, notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 5)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 6)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 7)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 8)));
assertFalse(iter.hasNext());
}
}
use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class S3Client method readFromStorage.
/**
* Download an object from S3.
*
* @param bucket
* The name of the bucket
* @param key
* The file key of the object to upload/update.
* @param result
* The Hash map where data from the object are written
*
*/
protected Status readFromStorage(String bucket, String key, HashMap<String, ByteIterator> result, SSECustomerKey ssecLocal) {
try {
Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key, ssecLocal);
//consuming the stream
InputStream objectData = objectAndMetadata.getKey().getObjectContent();
// writing the stream to bytes and to results
int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
byte[] inputStreamToByte = new byte[sizeOfFile];
objectData.read(inputStreamToByte, 0, sizeOfFile);
result.put(key, new ByteArrayByteIterator(inputStreamToByte));
objectData.close();
objectAndMetadata.getKey().close();
} catch (Exception e) {
System.err.println("Not possible to get the object " + key);
e.printStackTrace();
return Status.ERROR;
}
return Status.OK;
}
use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class AzureClient method readEntity.
private Status readEntity(String key, HashMap<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 com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class AzureClient method readSubset.
/*
* Read subset of properties instead of full fields with projection.
*/
public Status readSubset(String key, Set<String> fields, HashMap<String, ByteIterator> result) {
String whereStr = String.format("RowKey eq '%s'", key);
TableQuery<TableServiceEntity> projectionQuery = TableQuery.from(TableServiceEntity.class).where(whereStr).select(fields.toArray(new String[0]));
EntityResolver<HashMap<String, ByteIterator>> resolver = new EntityResolver<HashMap<String, ByteIterator>>() {
public HashMap<String, ByteIterator> resolve(String partitionkey, String rowKey, Date timeStamp, HashMap<String, EntityProperty> properties, String etag) {
HashMap<String, ByteIterator> tmp = new HashMap<String, ByteIterator>();
for (Entry<String, EntityProperty> entry : properties.entrySet()) {
String key = entry.getKey();
ByteIterator val = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
tmp.put(key, val);
}
return tmp;
}
};
try {
for (HashMap<String, ByteIterator> tmp : cloudTable.execute(projectionQuery, resolver)) {
for (Entry<String, ByteIterator> entry : tmp.entrySet()) {
String fieldName = entry.getKey();
ByteIterator fieldVal = entry.getValue();
result.put(fieldName, fieldVal);
}
}
return Status.OK;
} catch (Exception e) {
return Status.ERROR;
}
}
use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class CassandraCQLClient 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
* @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) {
try {
Statement stmt;
Select.Builder selectBuilder;
if (fields == null) {
selectBuilder = QueryBuilder.select().all();
} else {
selectBuilder = QueryBuilder.select();
for (String col : fields) {
((Select.Selection) selectBuilder).column(col);
}
}
stmt = selectBuilder.from(table).where(QueryBuilder.eq(YCSB_KEY, key)).limit(1);
stmt.setConsistencyLevel(readConsistencyLevel);
if (debug) {
System.out.println(stmt.toString());
}
if (trace) {
stmt.enableTracing();
}
ResultSet rs = session.execute(stmt);
if (rs.isExhausted()) {
return Status.NOT_FOUND;
}
// Should be only 1 row
Row row = rs.one();
ColumnDefinitions cd = row.getColumnDefinitions();
for (ColumnDefinitions.Definition def : cd) {
ByteBuffer val = row.getBytesUnsafe(def.getName());
if (val != null) {
result.put(def.getName(), new ByteArrayByteIterator(val.array()));
} else {
result.put(def.getName(), null);
}
}
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error reading key: " + key);
return Status.ERROR;
}
}
Aggregations