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);
}
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);
}
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);
}
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();
}
};
}
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);
}
}
}
Aggregations