use of org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition in project jackrabbit-oak by apache.
the class MongoDocumentStore method remove.
@Override
public <T extends Document> int remove(Collection<T> collection, Map<String, Map<Key, Condition>> toRemove) {
log("remove", toRemove);
int num = 0;
DBCollection dbCollection = getDBCollection(collection);
Stopwatch watch = startWatch();
try {
List<String> batchIds = Lists.newArrayList();
List<DBObject> batch = Lists.newArrayList();
Iterator<Entry<String, Map<Key, Condition>>> it = toRemove.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Map<Key, Condition>> entry = it.next();
QueryBuilder query = createQueryForUpdate(entry.getKey(), entry.getValue());
batchIds.add(entry.getKey());
batch.add(query.get());
if (!it.hasNext() || batch.size() == IN_CLAUSE_BATCH_SIZE) {
DBObject q = new BasicDBObject();
q.put(QueryOperators.OR, batch);
try {
num += dbCollection.remove(q).getN();
} catch (Exception e) {
throw DocumentStoreException.convert(e, "Remove failed for " + batch);
} finally {
if (collection == Collection.NODES) {
invalidateCache(batchIds);
}
}
batchIds.clear();
batch.clear();
}
}
} finally {
stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, num);
}
return num;
}
use of org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition in project jackrabbit-oak by apache.
the class RDBDocumentStore method delete.
private <T extends Document> int delete(Collection<T> collection, Map<String, Map<Key, Condition>> toRemove) {
int numDeleted = 0;
RDBTableMetaData tmd = getTable(collection);
Map<String, Map<Key, Condition>> subMap = Maps.newHashMap();
Iterator<Entry<String, Map<Key, Condition>>> it = toRemove.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Map<Key, Condition>> entry = it.next();
subMap.put(entry.getKey(), entry.getValue());
if (subMap.size() == 64 || !it.hasNext()) {
Connection connection = null;
int num = 0;
Stopwatch watch = startWatch();
try {
connection = this.ch.getRWConnection();
num = db.delete(connection, tmd, subMap);
numDeleted += num;
connection.commit();
} catch (Exception ex) {
Set<String> ids = subMap.keySet();
throw handleException("deleting " + ids, ex, collection, ids);
} finally {
this.ch.closeConnection(connection);
stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, num);
}
subMap.clear();
}
}
return numDeleted;
}
use of org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition in project jackrabbit-oak by apache.
the class RDBDocumentStoreJDBC method delete.
public int delete(Connection connection, RDBTableMetaData tmd, Map<String, Map<Key, Condition>> toDelete) throws SQLException, DocumentStoreException {
String or = "";
StringBuilder whereClause = new StringBuilder();
for (Entry<String, Map<Key, Condition>> entry : toDelete.entrySet()) {
whereClause.append(or);
or = " or ";
whereClause.append("ID=?");
for (Entry<Key, Condition> c : entry.getValue().entrySet()) {
if (!c.getKey().getName().equals(MODIFIED)) {
throw new DocumentStoreException("Unsupported condition: " + c);
}
whereClause.append(" and MODIFIED");
if (c.getValue().type == Condition.Type.EQUALS && c.getValue().value instanceof Long) {
whereClause.append("=?");
} else if (c.getValue().type == Condition.Type.EXISTS) {
whereClause.append(" is not null");
} else {
throw new DocumentStoreException("Unsupported condition: " + c);
}
}
}
PreparedStatement stmt = connection.prepareStatement("delete from " + tmd.getName() + " where " + whereClause);
try {
int i = 1;
for (Entry<String, Map<Key, Condition>> entry : toDelete.entrySet()) {
setIdInStatement(tmd, stmt, i++, entry.getKey());
for (Entry<Key, Condition> c : entry.getValue().entrySet()) {
if (c.getValue().type == Condition.Type.EQUALS) {
stmt.setLong(i++, (Long) c.getValue().value);
}
}
}
return stmt.executeUpdate();
} finally {
stmt.close();
}
}
use of org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition in project jackrabbit-oak by apache.
the class UpdateUtils method checkConditions.
public static boolean checkConditions(@Nonnull Document doc, @Nonnull Map<Key, Condition> conditions) {
for (Map.Entry<Key, Condition> entry : conditions.entrySet()) {
Condition c = entry.getValue();
Key k = entry.getKey();
Object value = doc.get(k.getName());
Revision r = k.getRevision();
if (c.type == Condition.Type.EXISTS) {
if (r == null) {
throw new IllegalStateException("EXISTS must not contain null revision");
}
if (value == null) {
if (Boolean.TRUE.equals(c.value)) {
return false;
}
} else {
if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
if (Boolean.TRUE.equals(c.value)) {
if (!map.containsKey(r)) {
return false;
}
} else {
if (map.containsKey(r)) {
return false;
}
}
} else {
return false;
}
}
} else if (c.type == Condition.Type.EQUALS || c.type == Condition.Type.NOTEQUALS) {
if (r != null) {
if (value instanceof Map) {
value = ((Map) value).get(r);
} else {
value = null;
}
}
boolean equal = Objects.equal(value, c.value);
if (c.type == Condition.Type.EQUALS && !equal) {
return false;
} else if (c.type == Condition.Type.NOTEQUALS && equal) {
return false;
}
} else {
throw new IllegalArgumentException("Unknown condition: " + c.type);
}
}
return true;
}
use of org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition in project jackrabbit-oak by apache.
the class MongoDocumentStore method createQueryForUpdate.
@Nonnull
private static QueryBuilder createQueryForUpdate(String key, Map<Key, Condition> conditions) {
QueryBuilder query = getByKeyQuery(key);
for (Entry<Key, Condition> entry : conditions.entrySet()) {
Key k = entry.getKey();
Condition c = entry.getValue();
switch(c.type) {
case EXISTS:
query.and(k.toString()).exists(c.value);
break;
case EQUALS:
query.and(k.toString()).is(c.value);
break;
case NOTEQUALS:
query.and(k.toString()).notEquals(c.value);
break;
}
}
return query;
}
Aggregations