Search in sources :

Example 1 with StringByteIterator

use of com.yahoo.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 HashMap<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 {
        HashMap<String, String> converted = (HashMap<String, String>) source;
        for (Map.Entry<String, String> entry : converted.entrySet()) {
            dest.put(entry.getKey(), new StringByteIterator(entry.getValue()));
        }
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) DBException(com.yahoo.ycsb.DBException) StringByteIterator(com.yahoo.ycsb.StringByteIterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with StringByteIterator

use of com.yahoo.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class BackoffSelectStrategy method decode.

/**
   * Decode the String from server and pass it into the decoded destination.
   *
   * @param source the loaded object.
   * @param fields the fields to check.
   * @param dest the result passed back to YCSB.
   */
private void decode(final String source, final Set<String> fields, final HashMap<String, ByteIterator> dest) {
    try {
        JsonNode json = JacksonTransformers.MAPPER.readTree(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");
    }
}
Also used : StringByteIterator(com.yahoo.ycsb.StringByteIterator) JsonNode(com.couchbase.client.deps.com.fasterxml.jackson.databind.JsonNode) TemporaryFailureException(com.couchbase.client.java.error.TemporaryFailureException) DBException(com.yahoo.ycsb.DBException)

Example 3 with StringByteIterator

use of com.yahoo.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class BackoffSelectStrategy method readN1ql.

/**
   * Performs the {@link #read(String, String, Set, HashMap)} operation via N1QL ("SELECT").
   *
   * If this option should be used, the "-p couchbase.kv=false" property must be set.
   *
   * @param docId the document ID
   * @param fields the fields to be loaded
   * @param result the result map where the doc needs to be converted into
   * @return The result of the operation.
   */
private Status readN1ql(final String docId, Set<String> fields, final HashMap<String, ByteIterator> result) throws Exception {
    String readQuery = "SELECT " + joinFields(fields) + " FROM `" + bucketName + "` USE KEYS [$1]";
    N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized(readQuery, JsonArray.from(docId), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism)));
    if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) {
        throw new DBException("Error while parsing N1QL Result. Query: " + readQuery + ", Errors: " + queryResult.errors());
    }
    N1qlQueryRow row;
    try {
        row = queryResult.rows().next();
    } catch (NoSuchElementException ex) {
        return Status.NOT_FOUND;
    }
    JsonObject content = row.value();
    if (fields == null) {
        // n1ql result set scoped under *.bucketName
        content = content.getObject(bucketName);
        fields = content.getNames();
    }
    for (String field : fields) {
        Object value = content.get(field);
        result.put(field, new StringByteIterator(value != null ? value.toString() : ""));
    }
    return Status.OK;
}
Also used : DBException(com.yahoo.ycsb.DBException) StringByteIterator(com.yahoo.ycsb.StringByteIterator) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 4 with StringByteIterator

use of com.yahoo.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;
    }
}
Also used : StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) SearchHit(org.elasticsearch.search.SearchHit) StringByteIterator(com.yahoo.ycsb.StringByteIterator) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) DBException(com.yahoo.ycsb.DBException) UnknownHostException(java.net.UnknownHostException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with StringByteIterator

use of com.yahoo.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class ElasticsearchClientTest method testUpdate.

/**
     * Test of update method, of class ElasticsearchClient.
     */
@Test
public void testUpdate() {
    System.out.println("update");
    int i;
    HashMap<String, ByteIterator> newValues = new HashMap<String, ByteIterator>(10);
    for (i = 1; i <= 10; i++) {
        newValues.put("field" + i, new StringByteIterator("newvalue" + i));
    }
    Status result = instance.update(MOCK_TABLE, MOCK_KEY1, newValues);
    assertEquals(Status.OK, result);
    //validate that the values changed
    HashMap<String, ByteIterator> resultParam = new HashMap<String, ByteIterator>(10);
    instance.read(MOCK_TABLE, MOCK_KEY1, MOCK_DATA.keySet(), resultParam);
    for (i = 1; i <= 10; i++) {
        assertEquals("newvalue" + i, resultParam.get("field" + i).toString());
    }
}
Also used : Status(com.yahoo.ycsb.Status) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) StringByteIterator(com.yahoo.ycsb.StringByteIterator) Test(org.junit.Test)

Aggregations

StringByteIterator (com.yahoo.ycsb.StringByteIterator)33 ByteIterator (com.yahoo.ycsb.ByteIterator)21 HashMap (java.util.HashMap)17 Status (com.yahoo.ycsb.Status)13 Test (org.junit.Test)9 DBException (com.yahoo.ycsb.DBException)5 IOException (java.io.IOException)5 SolrQuery (org.apache.solr.client.solrj.SolrQuery)4 SolrServerException (org.apache.solr.client.solrj.SolrServerException)4 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)4 SolrDocumentList (org.apache.solr.common.SolrDocumentList)4 Map (java.util.Map)3 JsonObject (com.couchbase.client.java.document.json.JsonObject)2 SolrDocument (org.apache.solr.common.SolrDocument)2 ReadOp (com.ceph.rados.ReadOp)1 ReadResult (com.ceph.rados.ReadOp.ReadResult)1 RadosException (com.ceph.rados.exceptions.RadosException)1 RadosObjectInfo (com.ceph.rados.jna.RadosObjectInfo)1 JsonNode (com.couchbase.client.deps.com.fasterxml.jackson.databind.JsonNode)1 TemporaryFailureException (com.couchbase.client.java.error.TemporaryFailureException)1