Search in sources :

Example 1 with N1qlQuery

use of com.couchbase.client.java.query.N1qlQuery 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 2 with N1qlQuery

use of com.couchbase.client.java.query.N1qlQuery in project sling by apache.

the class CouchbaseNoSqlAdapter method deleteRecursive.

@Override
public boolean deleteRecursive(String path) {
    Bucket bucket = couchbaseClient.getBucket();
    // fetch all descendants and self for deletion
    Pattern descendantsAndSelf = Pattern.compile("^" + path + "(/.+)?$");
    N1qlQuery query = N1qlQuery.simple(select("*").from(couchbaseClient.getBucketName()).where("REGEXP_LIKE(`" + PN_PATH + "`, '" + descendantsAndSelf.pattern() + "')"), N1QL_PARAMS);
    N1qlQueryResult queryResult = bucket.query(query);
    handleQueryError(queryResult);
    final Iterator<N1qlQueryRow> results = queryResult.iterator();
    boolean deletedAny = false;
    while (results.hasNext()) {
        JsonObject item = results.next().value();
        JsonObject envelope = item.getObject(couchbaseClient.getBucketName());
        String itemPath = envelope.getString(PN_PATH);
        String itemCacheKey = CouchbaseKey.build(itemPath, cacheKeyPrefix);
        bucket.remove(itemCacheKey);
        deletedAny = true;
    }
    return deletedAny;
}
Also used : N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) Pattern(java.util.regex.Pattern) N1qlQuery(com.couchbase.client.java.query.N1qlQuery) Bucket(com.couchbase.client.java.Bucket) JsonObject(com.couchbase.client.java.document.json.JsonObject) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult)

Aggregations

Bucket (com.couchbase.client.java.Bucket)2 JsonObject (com.couchbase.client.java.document.json.JsonObject)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 Iterator (java.util.Iterator)1 Pattern (java.util.regex.Pattern)1 NoSqlData (org.apache.sling.nosql.generic.adapter.NoSqlData)1