Search in sources :

Example 1 with Condition

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;
}
Also used : Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) BulkWriteException(com.mongodb.BulkWriteException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) JournalEntry(org.apache.jackrabbit.oak.plugins.document.JournalEntry) Entry(java.util.Map.Entry) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)

Example 2 with Condition

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;
}
Also used : Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) RDBJDBCTools.closeResultSet(org.apache.jackrabbit.oak.plugins.document.rdb.RDBJDBCTools.closeResultSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) Connection(java.sql.Connection) Stopwatch(com.google.common.base.Stopwatch) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Entry(java.util.Map.Entry) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)

Example 3 with Condition

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();
    }
}
Also used : QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)

Example 4 with Condition

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;
}
Also used : Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) TreeMap(java.util.TreeMap) Map(java.util.Map) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)

Example 5 with Condition

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;
}
Also used : Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) QueryBuilder(com.mongodb.QueryBuilder) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key) Nonnull(javax.annotation.Nonnull)

Aggregations

Condition (org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition)6 Key (org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)6 Map (java.util.Map)5 HashMap (java.util.HashMap)3 TreeMap (java.util.TreeMap)3 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)3 Stopwatch (com.google.common.base.Stopwatch)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 QueryBuilder (com.mongodb.QueryBuilder)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Entry (java.util.Map.Entry)2 ExecutionException (java.util.concurrent.ExecutionException)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BasicDBObject (com.mongodb.BasicDBObject)1 BulkWriteException (com.mongodb.BulkWriteException)1 DBCollection (com.mongodb.DBCollection)1 DBObject (com.mongodb.DBObject)1 MongoException (com.mongodb.MongoException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1