use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.
the class ElasticsearchClient 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 this class's
* description for a discussion of error codes.
*/
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
try {
final RangeQueryBuilder rangeQuery = rangeQuery("_id").gte(startkey);
final SearchResponse response = client.prepareSearch(indexKey).setTypes(table).setQuery(rangeQuery).setSize(recordcount).execute().actionGet();
HashMap<String, ByteIterator> entry;
for (SearchHit hit : response.getHits()) {
entry = new HashMap<>(fields.size());
for (String field : fields) {
entry.put(field, new StringByteIterator((String) hit.getSource().get(field)));
}
result.add(entry);
}
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.
the class ElasticsearchClient method read.
@Override
public Status read(final String table, final String key, final Set<String> fields, final Map<String, ByteIterator> result) {
try {
final SearchResponse searchResponse = search(table, key);
if (searchResponse.getHits().totalHits == 0) {
return Status.NOT_FOUND;
}
final SearchHit hit = searchResponse.getHits().getAt(0);
if (fields != null) {
for (final String field : fields) {
result.put(field, new StringByteIterator((String) hit.getSource().get(field)));
}
} else {
for (final Map.Entry<String, Object> e : hit.getSource().entrySet()) {
if (KEY.equals(e.getKey())) {
continue;
}
result.put(e.getKey(), new StringByteIterator((String) e.getValue()));
}
}
return Status.OK;
} catch (final Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.
the class ElasticsearchClient method scan.
@Override
public Status scan(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
try {
refreshIfNeeded();
final RangeQueryBuilder query = new RangeQueryBuilder(KEY).gte(startkey);
final SearchResponse response = client.prepareSearch(indexKey).setQuery(query).setSize(recordcount).get();
for (final SearchHit hit : response.getHits()) {
final HashMap<String, ByteIterator> entry;
if (fields != null) {
entry = new HashMap<>(fields.size());
for (final String field : fields) {
entry.put(field, new StringByteIterator((String) hit.getSource().get(field)));
}
} else {
entry = new HashMap<>(hit.getSource().size());
for (final Map.Entry<String, Object> field : hit.getSource().entrySet()) {
if (KEY.equals(field.getKey())) {
continue;
}
entry.put(field.getKey(), new StringByteIterator((String) field.getValue()));
}
}
result.add(entry);
}
return Status.OK;
} catch (final Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.
the class ElasticsearchIntegTestBase method testUpdate.
/**
* Test of update method, of class ElasticsearchClient.
*/
@Test
public void testUpdate() {
final HashMap<String, ByteIterator> newValues = new HashMap<>(10);
for (int i = 1; i <= 10; i++) {
newValues.put(FIELD_PREFIX + i, new StringByteIterator("newvalue" + i));
}
final Status updateResult = db.update(MOCK_TABLE, "1", newValues);
assertEquals(Status.OK, updateResult);
// validate that the values changed
final HashMap<String, ByteIterator> resultParam = new HashMap<>(10);
final Status readResult = db.read(MOCK_TABLE, "1", MOCK_DATA.keySet(), resultParam);
assertEquals(Status.OK, readResult);
for (int i = 1; i <= 10; i++) {
assertEquals("newvalue" + i, resultParam.get(FIELD_PREFIX + i).toString());
}
}
use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.
the class CouchbaseClient method decode.
/**
* Decode the object from server into the storable result.
*
* @param source the loaded object.
* @param fields the fields to check.
* @param dest the result passed back to the ycsb core.
*/
private void decode(final Object source, final Set<String> fields, final Map<String, ByteIterator> dest) {
if (useJson) {
try {
JsonNode json = JSON_MAPPER.readTree((String) source);
boolean checkFields = fields != null && !fields.isEmpty();
for (Iterator<Map.Entry<String, JsonNode>> jsonFields = json.fields(); jsonFields.hasNext(); ) {
Map.Entry<String, JsonNode> jsonField = jsonFields.next();
String name = jsonField.getKey();
if (checkFields && fields.contains(name)) {
continue;
}
JsonNode jsonValue = jsonField.getValue();
if (jsonValue != null && !jsonValue.isNull()) {
dest.put(name, new StringByteIterator(jsonValue.asText()));
}
}
} catch (Exception e) {
throw new RuntimeException("Could not decode JSON");
}
} else {
Map<String, String> converted = (HashMap<String, String>) source;
for (Map.Entry<String, String> entry : converted.entrySet()) {
dest.put(entry.getKey(), new StringByteIterator(entry.getValue()));
}
}
}
Aggregations