Search in sources :

Example 6 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 7 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)

Example 8 with JsonObject

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

Example 9 with JsonObject

use of com.couchbase.client.java.document.json.JsonObject in project cas by apereo.

the class CouchbasePersonAttributeDao method getPerson.

@Override
@SneakyThrows
public IPersonAttributes getPerson(final String uid) {
    final N1qlQueryResult result = couchbase.query(couchbaseProperties.getUsernameAttribute(), uid);
    final Map<String, ?> attributes;
    if (result.allRows().isEmpty()) {
        LOGGER.debug("Couchbase query did not return any results/rows.");
        attributes = new LinkedHashMap<>();
    } else {
        attributes = result.allRows().stream().filter(row -> row.value().containsKey(couchbase.getBucket().name())).filter(row -> {
            final JsonObject value = (JsonObject) row.value().get(couchbase.getBucket().name());
            return value.containsKey(couchbaseProperties.getUsernameAttribute());
        }).map(row -> (JsonObject) row.value().get(couchbase.getBucket().name())).map(entity -> couchbase.collectAttributesFromEntity(entity, s -> !s.equals(couchbaseProperties.getUsernameAttribute())).entrySet()).flatMap(Collection::stream).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }
    return new CaseInsensitiveNamedPersonImpl(uid, stuffAttributesIntoList(attributes));
}
Also used : IPersonAttributes(org.apereo.services.persondir.IPersonAttributes) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult) CouchbasePrincipalAttributesProperties(org.apereo.cas.configuration.model.core.authentication.CouchbasePrincipalAttributesProperties) SneakyThrows(lombok.SneakyThrows) Collection(java.util.Collection) RequiredArgsConstructor(lombok.RequiredArgsConstructor) BasePersonAttributeDao(org.apereo.services.persondir.support.BasePersonAttributeDao) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) JsonObject(com.couchbase.client.java.document.json.JsonObject) ArrayList(java.util.ArrayList) CouchbaseClientFactory(org.apereo.cas.couchbase.core.CouchbaseClientFactory) LinkedHashMap(java.util.LinkedHashMap) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SimpleUsernameAttributeProvider(org.apereo.services.persondir.support.SimpleUsernameAttributeProvider) IUsernameAttributeProvider(org.apereo.services.persondir.support.IUsernameAttributeProvider) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) CaseInsensitiveNamedPersonImpl(org.apereo.services.persondir.support.CaseInsensitiveNamedPersonImpl) LinkedHashSet(java.util.LinkedHashSet) JsonObject(com.couchbase.client.java.document.json.JsonObject) Collection(java.util.Collection) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult) CaseInsensitiveNamedPersonImpl(org.apereo.services.persondir.support.CaseInsensitiveNamedPersonImpl) SneakyThrows(lombok.SneakyThrows)

Aggregations

JsonObject (com.couchbase.client.java.document.json.JsonObject)9 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)5 Bucket (com.couchbase.client.java.Bucket)4 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)3 JsonDocument (com.couchbase.client.java.document.JsonDocument)2 N1qlQuery (com.couchbase.client.java.query.N1qlQuery)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 NoSqlData (org.apache.sling.nosql.generic.adapter.NoSqlData)2 DocumentAlreadyExistsException (com.couchbase.client.java.error.DocumentAlreadyExistsException)1 SimpleN1qlQuery (com.couchbase.client.java.query.SimpleN1qlQuery)1 Statement (com.couchbase.client.java.query.Statement)1 ByteIterator (com.yahoo.ycsb.ByteIterator)1 DBException (com.yahoo.ycsb.DBException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1