Search in sources :

Example 1 with NoSqlData

use of org.apache.sling.nosql.generic.adapter.NoSqlData in project sling by apache.

the class NoSqlResourceProvider method delete.

public void delete(ResourceResolver resolver, String path) throws PersistenceException {
    if (ROOT_PATH.equals(path) || !adapter.validPath(path)) {
        throw new PersistenceException("Unable to delete resource at {}" + path, null, path, null);
    }
    Pattern pathsToDeletePattern = PathUtil.getSameOrDescendantPathPattern(path);
    // remove all existing path and probably descendant paths from list of deleted paths
    Iterator<String> deletedResourcesIterator = deletedResources.iterator();
    while (deletedResourcesIterator.hasNext()) {
        String deletedPath = deletedResourcesIterator.next();
        if (pathsToDeletePattern.matcher(deletedPath).matches()) {
            deletedResourcesIterator.remove();
        }
    }
    // remove all changed descendant items from changeset
    Iterator<Map.Entry<String, NoSqlData>> changeResourcesIterator = changedResources.entrySet().iterator();
    while (changeResourcesIterator.hasNext()) {
        Map.Entry<String, NoSqlData> entry = changeResourcesIterator.next();
        if (pathsToDeletePattern.matcher(entry.getKey()).matches()) {
            changeResourcesIterator.remove();
        }
    }
    // add path to delete
    deletedResources.add(path);
}
Also used : NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) Pattern(java.util.regex.Pattern) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 2 with NoSqlData

use of org.apache.sling.nosql.generic.adapter.NoSqlData in project sling by apache.

the class NoSqlResourceProvider method create.

// ### WRITE ACCESS ###
public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
    if (ROOT_PATH.equals(path) || !adapter.validPath(path)) {
        throw new PersistenceException("Illegal path - unable to create resource at " + path, null, path, null);
    }
    // check if already exists
    boolean deleted = this.deletedResources.remove(path);
    boolean exists = changedResources.containsKey(path) || this.adapter.get(path) != null;
    if (!deleted && exists) {
        throw new PersistenceException("Resource already exists at " + path, null, path, null);
    }
    // create new resource in changeset
    Map<String, Object> writableMap = properties != null ? new HashMap<String, Object>(properties) : new HashMap<String, Object>();
    NoSqlData data = new NoSqlData(path, NoSqlValueMap.convertForWriteAll(writableMap));
    changedResources.put(path, data);
    return new NoSqlResource(data, resolver, this);
}
Also used : NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 3 with NoSqlData

use of org.apache.sling.nosql.generic.adapter.NoSqlData in project sling by apache.

the class ValueMapConvertingNoSqlAdapter method deserializeUnsupportedTypes.

private NoSqlData deserializeUnsupportedTypes(NoSqlData data) {
    if (data == null) {
        return null;
    }
    Map<String, Object> deserializedMap = new HashMap<String, Object>();
    for (Map.Entry<String, Object> entry : data.getProperties().entrySet()) {
        Object deserializedValue = entry.getValue();
        if (entry.getValue() instanceof String) {
            String value = (String) entry.getValue();
            // Calendar.class
            if (value.indexOf(PREFIX_CALENDAR) == 0) {
                String calendarValue = value.substring(PREFIX_CALENDAR.length());
                try {
                    Date date = getISO8601Format().parse((String) calendarValue);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    deserializedValue = calendar;
                } catch (ParseException ex) {
                    log.warn("Unable to parse serialized calendar value: " + entry.getValue(), ex);
                }
            } else // byte[].class
            if (value.indexOf(PREFIX_BYTE_ARRAY) == 0) {
                String byteArrayValue = value.substring(PREFIX_BYTE_ARRAY.length());
                deserializedValue = DatatypeConverter.parseBase64Binary(byteArrayValue);
            }
        }
        deserializedMap.put(entry.getKey(), deserializedValue);
    }
    return new NoSqlData(data.getPath(), deserializedMap);
}
Also used : NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) HashMap(java.util.HashMap) Calendar(java.util.Calendar) ParseException(java.text.ParseException) HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date)

Example 4 with NoSqlData

use of org.apache.sling.nosql.generic.adapter.NoSqlData 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 NoSqlData

use of org.apache.sling.nosql.generic.adapter.NoSqlData 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

NoSqlData (org.apache.sling.nosql.generic.adapter.NoSqlData)9 Pattern (java.util.regex.Pattern)3 Bucket (com.couchbase.client.java.Bucket)2 JsonObject (com.couchbase.client.java.document.json.JsonObject)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 JsonDocument (com.couchbase.client.java.document.JsonDocument)1 N1qlQuery (com.couchbase.client.java.query.N1qlQuery)1 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)1 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 SortedMap (java.util.SortedMap)1 Resource (org.apache.sling.api.resource.Resource)1