use of com.yahoo.ycsb.ByteIterator 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.ByteIterator 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.ByteIterator in project YCSB by brianfrankcooper.
the class AsyncMongoDbClient 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 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. See the {@link DB}
* class's description for a discussion of error codes.
*/
@Override
public final Status scan(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
try {
final MongoCollection collection = database.getCollection(table);
final Find.Builder find = Find.builder().query(where("_id").greaterThanOrEqualTo(startkey)).limit(recordcount).batchSize(recordcount).sort(Sort.asc("_id")).readPreference(readPreference);
if (fields != null) {
final DocumentBuilder fieldsDoc = BuilderFactory.start();
for (final String field : fields) {
fieldsDoc.add(field, INCLUDE);
}
find.projection(fieldsDoc);
}
result.ensureCapacity(recordcount);
final MongoIterator<Document> cursor = collection.find(find);
if (!cursor.hasNext()) {
System.err.println("Nothing found in scan for key " + startkey);
return Status.NOT_FOUND;
}
while (cursor.hasNext()) {
// toMap() returns a Map but result.add() expects a
// Map<String,String>. Hence, the suppress warnings.
final Document doc = cursor.next();
final HashMap<String, ByteIterator> docAsMap = new HashMap<String, ByteIterator>();
fillMap(docAsMap, doc);
result.add(docAsMap);
}
return Status.OK;
} catch (final Exception e) {
System.err.println(e.toString());
return Status.ERROR;
}
}
use of com.yahoo.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class AsyncMongoDbClient 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. See the {@link DB}
* class's description for a discussion of error codes.
*/
@Override
public final Status insert(final String table, final String key, final HashMap<String, ByteIterator> values) {
try {
final MongoCollection collection = database.getCollection(table);
final DocumentBuilder toInsert = DOCUMENT_BUILDER.get().reset().add("_id", key);
final Document query = toInsert.build();
for (final Map.Entry<String, ByteIterator> entry : values.entrySet()) {
toInsert.add(entry.getKey(), entry.getValue().toArray());
}
// Do an upsert.
if (batchSize <= 1) {
long result;
if (useUpsert) {
result = collection.update(query, toInsert, /* multi= */
false, /* upsert= */
true, writeConcern);
} else {
// Return is not stable pre-SERVER-4381. No exception is success.
collection.insert(writeConcern, toInsert);
result = 1;
}
return result == 1 ? Status.OK : Status.NOT_FOUND;
}
// Use a bulk insert.
try {
if (useUpsert) {
batchedWrite.update(query, toInsert, /* multi= */
false, /* upsert= */
true);
} else {
batchedWrite.insert(toInsert);
}
batchedWriteCount += 1;
if (batchedWriteCount < batchSize) {
return Status.BATCHED_OK;
}
long count = collection.write(batchedWrite);
if (count == batchedWriteCount) {
batchedWrite.reset().mode(BatchedWriteMode.REORDERED);
batchedWriteCount = 0;
return Status.OK;
}
System.err.println("Number of inserted documents doesn't match the " + "number sent, " + count + " inserted, sent " + batchedWriteCount);
batchedWrite.reset().mode(BatchedWriteMode.REORDERED);
batchedWriteCount = 0;
return Status.ERROR;
} catch (Exception e) {
System.err.println("Exception while trying bulk insert with " + batchedWriteCount);
e.printStackTrace();
return Status.ERROR;
}
} catch (final Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
use of com.yahoo.ycsb.ByteIterator in project YCSB by brianfrankcooper.
the class OrientDBClientTest method insertRow.
/*
Inserts a row of deterministic values for the given insertKey using the orientDBClient.
*/
private Map<String, ByteIterator> insertRow(String insertKey) {
HashMap<String, ByteIterator> insertMap = new HashMap<>();
for (int i = 0; i < 3; i++) {
insertMap.put(FIELD_PREFIX + i, new StringByteIterator(buildDeterministicValue(insertKey, FIELD_PREFIX + i)));
}
orientDBClient.insert(CLASS, insertKey, insertMap);
return insertMap;
}
Aggregations