Search in sources :

Example 1 with JsonObject

use of com.couchbase.client.java.document.json.JsonObject 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 2 with JsonObject

use of com.couchbase.client.java.document.json.JsonObject in project sling by apache.

the class CouchbaseNoSqlAdapter method store.

@Override
public boolean store(NoSqlData data) {
    Bucket bucket = couchbaseClient.getBucket();
    String cacheKey = CouchbaseKey.build(data.getPath(), cacheKeyPrefix);
    JsonObject envelope = JsonObject.create();
    envelope.put(PN_PATH, data.getPath());
    envelope.put(PN_DATA, JsonObject.from(data.getProperties(MultiValueMode.LISTS)));
    // for list-children query efficiency store parent path as well
    String parentPath = ResourceUtil.getParent(data.getPath());
    if (parentPath != null) {
        envelope.put(PN_PARENT_PATH, parentPath);
    }
    JsonDocument doc = JsonDocument.create(cacheKey, envelope);
    try {
        bucket.insert(doc);
        // created
        return true;
    } catch (DocumentAlreadyExistsException ex) {
        bucket.upsert(doc);
        // updated
        return false;
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) DocumentAlreadyExistsException(com.couchbase.client.java.error.DocumentAlreadyExistsException) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Example 3 with JsonObject

use of com.couchbase.client.java.document.json.JsonObject in project YCSB by brianfrankcooper.

the class BackoffSelectStrategy method scanSpecificFields.

/**
   * Performs the {@link #scan(String, String, int, Set, Vector)} operation N1Ql only for a subset of the fields.
   *
   * @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 The result of the operation.
   */
private Status scanSpecificFields(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
    String scanSpecQuery = "SELECT " + joinFields(fields) + " FROM `" + bucketName + "` WHERE meta().id >= '$1' LIMIT $2";
    N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized(scanSpecQuery, JsonArray.from(formatId(table, startkey), recordcount), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism)));
    if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) {
        throw new RuntimeException("Error while parsing N1QL Result. Query: " + scanSpecQuery + ", Errors: " + queryResult.errors());
    }
    boolean allFields = fields == null || fields.isEmpty();
    result.ensureCapacity(recordcount);
    for (N1qlQueryRow row : queryResult) {
        JsonObject value = row.value();
        if (fields == null) {
            value = value.getObject(bucketName);
        }
        Set<String> f = allFields ? value.getNames() : fields;
        HashMap<String, ByteIterator> tuple = new HashMap<String, ByteIterator>(f.size());
        for (String field : f) {
            tuple.put(field, new StringByteIterator(value.getString(field)));
        }
        result.add(tuple);
    }
    return Status.OK;
}
Also used : ByteIterator(com.yahoo.ycsb.ByteIterator) StringByteIterator(com.yahoo.ycsb.StringByteIterator) StringByteIterator(com.yahoo.ycsb.StringByteIterator) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 4 with JsonObject

use of com.couchbase.client.java.document.json.JsonObject in project sling by apache.

the class CouchbaseNoSqlAdapter method getChildren.

@Override
public Iterator<NoSqlData> getChildren(String parentPath) {
    Bucket bucket = couchbaseClient.getBucket();
    // fetch all direct children of this path
    N1qlQuery query = N1qlQuery.simple(select("*").from(couchbaseClient.getBucketName()).where(x(PN_PARENT_PATH).eq(s(parentPath))), N1QL_PARAMS);
    N1qlQueryResult queryResult = bucket.query(query);
    handleQueryError(queryResult);
    final Iterator<N1qlQueryRow> results = queryResult.iterator();
    return new Iterator<NoSqlData>() {

        @Override
        public boolean hasNext() {
            return results.hasNext();
        }

        @Override
        public NoSqlData next() {
            JsonObject item = results.next().value();
            JsonObject envelope = item.getObject(couchbaseClient.getBucketName());
            String path = envelope.getString(PN_PATH);
            JsonObject data = envelope.getObject(PN_DATA);
            return new NoSqlData(path, data.toMap(), MultiValueMode.LISTS);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) N1qlQuery(com.couchbase.client.java.query.N1qlQuery) Bucket(com.couchbase.client.java.Bucket) Iterator(java.util.Iterator) JsonObject(com.couchbase.client.java.document.json.JsonObject) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult)

Example 5 with JsonObject

use of com.couchbase.client.java.document.json.JsonObject in project sling by apache.

the class CouchbaseNoSqlAdapter method get.

@Override
public NoSqlData get(String path) {
    Bucket bucket = couchbaseClient.getBucket();
    String cacheKey = CouchbaseKey.build(path, cacheKeyPrefix);
    JsonDocument doc = bucket.get(cacheKey);
    if (doc == null) {
        return null;
    } else {
        JsonObject data = doc.content().getObject(PN_DATA);
        if (data == null) {
            return null;
        } else {
            return new NoSqlData(path, data.toMap(), MultiValueMode.LISTS);
        }
    }
}
Also used : NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) Bucket(com.couchbase.client.java.Bucket) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Aggregations

JsonObject (com.couchbase.client.java.document.json.JsonObject)6 Bucket (com.couchbase.client.java.Bucket)4 JsonDocument (com.couchbase.client.java.document.JsonDocument)2 N1qlQuery (com.couchbase.client.java.query.N1qlQuery)2 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)2 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 NoSqlData (org.apache.sling.nosql.generic.adapter.NoSqlData)2 DocumentAlreadyExistsException (com.couchbase.client.java.error.DocumentAlreadyExistsException)1 ByteIterator (com.yahoo.ycsb.ByteIterator)1 DBException (com.yahoo.ycsb.DBException)1 Iterator (java.util.Iterator)1 Pattern (java.util.regex.Pattern)1