use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class HypertableClient 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) {
if (debug) {
System.out.println("Doing read from Hypertable columnfamily " + columnFamily);
System.out.println("Doing read for key: " + key);
}
try {
if (null != fields) {
Vector<HashMap<String, ByteIterator>> resMap = new Vector<HashMap<String, ByteIterator>>();
if (!scan(table, key, 1, fields, resMap).equals(Status.OK)) {
return Status.ERROR;
}
if (!resMap.isEmpty()) {
result.putAll(resMap.firstElement());
}
} else {
SerializedCellsReader reader = new SerializedCellsReader(null);
reader.reset(connection.get_row_serialized(ns, table, key));
while (reader.next()) {
result.put(new String(reader.get_column_qualifier()), new ByteArrayByteIterator(reader.get_value()));
}
}
} catch (ClientException e) {
if (debug) {
System.err.println("Error doing read: " + e.message);
}
return Status.ERROR;
} catch (TException e) {
if (debug) {
System.err.println("Error doing read");
}
return Status.ERROR;
}
return Status.OK;
}
use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class AbstractDBTestCases method testInsertReadUpdateWithUpsert.
/**
* Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#update} .
*/
@Test
public void testInsertReadUpdateWithUpsert() {
Properties props = new Properties();
props.setProperty("mongodb.upsert", "true");
DB client = getDB(props);
final String table = getClass().getSimpleName();
final String id = "updateWithUpsert";
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 NoSqlDbClient method read.
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
Key kvKey = createKey(table, key);
SortedMap<Key, ValueVersion> kvResult;
try {
kvResult = store.multiGet(kvKey, null, null);
} catch (FaultException e) {
System.err.println(e);
return Status.ERROR;
}
for (Map.Entry<Key, ValueVersion> entry : kvResult.entrySet()) {
/* If fields is null, read all fields */
String field = getFieldFromKey(entry.getKey());
if (fields != null && !fields.contains(field)) {
continue;
}
result.put(field, new ByteArrayByteIterator(entry.getValue().getValue().getValue()));
}
return Status.OK;
}
use of com.yahoo.ycsb.ByteArrayByteIterator in project YCSB by brianfrankcooper.
the class HBaseClient 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 tableName
* @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
*/
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
//if this is a "new" tableName, init HTable object. Else, use existing one
if (!this.tableName.equals(table)) {
hTable = null;
try {
getHTable(table);
this.tableName = table;
} catch (IOException e) {
System.err.println("Error accessing HBase tableName: " + e);
return Status.ERROR;
}
}
Scan s = new Scan(Bytes.toBytes(startkey));
//HBase has no record limit. Here, assume recordcount is small enough to bring back in one call.
//We get back recordcount records
s.setCaching(recordcount);
if (this.usePageFilter) {
s.setFilter(new PageFilter(recordcount));
}
//add specified fields or else all fields
if (fields == null) {
s.addFamily(columnFamilyBytes);
} else {
for (String field : fields) {
s.addColumn(columnFamilyBytes, Bytes.toBytes(field));
}
}
//get results
try (ResultScanner scanner = hTable.getScanner(s)) {
int numResults = 0;
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
//get row key
String key = Bytes.toString(rr.getRow());
if (debug) {
System.out.println("Got scan result for key: " + key);
}
HashMap<String, ByteIterator> rowResult = new HashMap<>();
for (KeyValue kv : rr.raw()) {
rowResult.put(Bytes.toString(kv.getQualifier()), new ByteArrayByteIterator(kv.getValue()));
}
//add rowResult to result vector
result.add(rowResult);
numResults++;
//if hit recordcount, bail out
if (numResults >= recordcount) {
break;
}
}
//done with row
} catch (IOException e) {
if (debug) {
System.out.println("Error in getting/parsing scan result: " + e);
}
return Status.ERROR;
}
return Status.OK;
}
use of com.yahoo.ycsb.ByteArrayByteIterator in project voltdb by VoltDB.
the class VoltClient4 method unpackRowData.
private HashMap<String, ByteIterator> unpackRowData(byte[] rowData, ByteBuffer buf, int nFields, Set<String> fields, HashMap<String, ByteIterator> result) {
for (int i = 0; i < nFields; i++) {
int len = buf.getInt();
int off = buf.position();
String key = new String(rowData, off, len, UTF8);
buf.position(off + len);
len = buf.getInt();
off = buf.position();
if (fields == null || fields.contains(key)) {
result.put(key, new ByteArrayByteIterator(rowData, off, len));
}
buf.position(off + len);
}
return result;
}
Aggregations