Search in sources :

Example 11 with UpdateOp

use of org.apache.jackrabbit.oak.plugins.document.UpdateOp in project jackrabbit-oak by apache.

the class RDBDocumentStoreTest method testRDBQueryConditions.

@Test
public void testRDBQueryConditions() {
    if (ds instanceof RDBDocumentStore) {
        RDBDocumentStore rds = (RDBDocumentStore) ds;
        // create ten documents
        long now = System.currentTimeMillis();
        String base = this.getClass().getName() + ".testRDBQuery-";
        for (int i = 0; i < 10; i++) {
            String id = base + i;
            UpdateOp up = new UpdateOp(id, true);
            up.set(NodeDocument.DELETED_ONCE, i % 2 == 1);
            up.set(NodeDocument.MODIFIED_IN_SECS, now++);
            boolean success = super.ds.create(Collection.NODES, Collections.singletonList(up));
            assertTrue("document with " + id + " not created", success);
            removeMe.add(id);
        }
        List<QueryCondition> conditions = new ArrayList<QueryCondition>();
        // matches every second
        conditions.add(new QueryCondition(NodeDocument.DELETED_ONCE, "=", 0));
        // matches first eight
        conditions.add(new QueryCondition(NodeDocument.MODIFIED_IN_SECS, "<", now - 2));
        List<NodeDocument> result = rds.query(Collection.NODES, base, base + "A", RDBDocumentStore.EMPTY_KEY_PATTERN, conditions, 10);
        assertEquals(4, result.size());
    }
}
Also used : UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) ArrayList(java.util.ArrayList) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Test(org.junit.Test) AbstractDocumentStoreTest(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)

Example 12 with UpdateOp

use of org.apache.jackrabbit.oak.plugins.document.UpdateOp in project jackrabbit-oak by apache.

the class RDBDocumentStoreTest method testRDBQueryKeyPatterns.

@Test
public void testRDBQueryKeyPatterns() {
    if (ds instanceof RDBDocumentStore) {
        int cnt = 10;
        RDBDocumentStore rds = (RDBDocumentStore) ds;
        // create ten documents
        String base = this.getClass().getName() + ".testRDBQuery-";
        for (int i = 0; i < cnt; i++) {
            // every second is a "regular" path
            String id = "1:" + (i % 2 == 1 ? "p" : "") + "/" + base + i;
            UpdateOp up = new UpdateOp(id, true);
            up.set("_test", base);
            boolean success = super.ds.create(Collection.NODES, Collections.singletonList(up));
            assertTrue("document with " + id + " not created", success);
            removeMe.add(id);
        }
        List<QueryCondition> conditions = new ArrayList<QueryCondition>();
        List<NodeDocument> result = rds.query(Collection.NODES, NodeDocument.MIN_ID_VALUE, NodeDocument.MAX_ID_VALUE, Arrays.asList("_:/%", "__:/%", "___:/%"), conditions, 10000);
        for (NodeDocument d : result) {
            if (base.equals(d.get("_test"))) {
                assertTrue(d.getId().startsWith("1:p"));
            }
        }
        Iterable<NodeDocument> it = rds.queryAsIterable(Collection.NODES, NodeDocument.MIN_ID_VALUE, NodeDocument.MAX_ID_VALUE, Arrays.asList("_:/%", "__:/%", "___:/%"), conditions, Integer.MAX_VALUE, null);
        assertTrue(it instanceof Closeable);
        int c1 = 0, c2 = 0;
        for (NodeDocument d : it) {
            if (base.equals(d.get("_test"))) {
                assertTrue(d.getId().startsWith("1:p"));
                c1 += 1;
            }
        }
        // check that getting the iterator twice works
        for (NodeDocument d : it) {
            if (base.equals(d.get("_test"))) {
                assertTrue(d.getId().startsWith("1:p"));
                c2 += 1;
            }
        }
        assertEquals(cnt / 2, c1);
        assertEquals(cnt / 2, c2);
        Utils.closeIfCloseable(it);
    }
}
Also used : UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Test(org.junit.Test) AbstractDocumentStoreTest(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)

Example 13 with UpdateOp

use of org.apache.jackrabbit.oak.plugins.document.UpdateOp in project jackrabbit-oak by apache.

the class RevisionsCommandTest method sweep.

@Test
public void sweep() throws Exception {
    int clusterId = ns.getClusterId();
    String output = captureSystemErr(new Sweep(clusterId));
    assertThat(output, containsString("cannot sweep revisions for active clusterId"));
    output = captureSystemErr(new Sweep(0));
    assertThat(output, containsString("clusterId option is required"));
    output = captureSystemErr(new Sweep(99));
    assertThat(output, containsString("store does not have changes with clusterId"));
    ns.dispose();
    output = captureSystemOut(new Sweep(clusterId));
    assertThat(output, containsString("Revision sweep not needed for clusterId"));
    // remove the sweep revision to force a sweep run
    MongoConnection c = connectionFactory.getConnection();
    DocumentMK.Builder builder = builderProvider.newBuilder().setMongoDB(c.getDB());
    DocumentStore store = builder.getDocumentStore();
    UpdateOp op = new UpdateOp(getIdFromPath("/"), false);
    op.removeMapEntry("_sweepRev", new Revision(0, 0, clusterId));
    assertNotNull(store.findAndUpdate(Collection.NODES, op));
    output = captureSystemOut(new Sweep(clusterId));
    assertThat(output, containsString("Updated sweep revision to"));
}
Also used : DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) Revision(org.apache.jackrabbit.oak.plugins.document.Revision) UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection) Test(org.junit.Test)

Example 14 with UpdateOp

use of org.apache.jackrabbit.oak.plugins.document.UpdateOp in project jackrabbit-oak by apache.

the class RDBDocumentStore method createOrUpdate.

@Override
public <T extends Document> List<T> createOrUpdate(Collection<T> collection, List<UpdateOp> updateOps) {
    if (!BATCHUPDATES) {
        List<T> results = new ArrayList<T>(updateOps.size());
        for (UpdateOp update : updateOps) {
            results.add(createOrUpdate(collection, update));
        }
        return results;
    }
    final Stopwatch watch = startWatch();
    Map<UpdateOp, T> results = new LinkedHashMap<UpdateOp, T>();
    Map<String, UpdateOp> operationsToCover = new LinkedHashMap<String, UpdateOp>();
    Set<UpdateOp> duplicates = new HashSet<UpdateOp>();
    for (UpdateOp updateOp : updateOps) {
        UpdateUtils.assertUnconditional(updateOp);
        if (operationsToCover.containsKey(updateOp.getId())) {
            duplicates.add(updateOp);
            results.put(updateOp, null);
        } else {
            UpdateOp clone = updateOp.copy();
            addUpdateCounters(clone);
            operationsToCover.put(clone.getId(), clone);
            results.put(clone, null);
        }
    }
    Map<String, T> oldDocs = new HashMap<String, T>();
    if (collection == Collection.NODES) {
        oldDocs.putAll(readDocumentCached(collection, operationsToCover.keySet()));
    }
    // iteration count
    int i = 0;
    // it's better to send them sequentially
    while (operationsToCover.size() > 2) {
        // We should try to insert documents only during the first
        // iteration. In the 2nd and 3rd iterations we only deal with
        // conflicting documents, so they already exist in the database
        // and there's no point in inserting them.
        boolean upsert = i == 0;
        if (i++ == 3) {
            // updates should be applied sequentially
            break;
        }
        for (List<UpdateOp> partition : partition(newArrayList(operationsToCover.values()), CHUNKSIZE)) {
            Map<UpdateOp, T> successfulUpdates = bulkUpdate(collection, partition, oldDocs, upsert);
            results.putAll(successfulUpdates);
            operationsToCover.values().removeAll(successfulUpdates.keySet());
        }
    }
    // if there are some changes left, we'll apply them one after another
    for (UpdateOp updateOp : updateOps) {
        UpdateOp conflictedOp = operationsToCover.remove(updateOp.getId());
        if (conflictedOp != null) {
            results.put(conflictedOp, createOrUpdate(collection, updateOp));
        } else if (duplicates.contains(updateOp)) {
            results.put(updateOp, createOrUpdate(collection, updateOp));
        }
    }
    stats.doneCreateOrUpdate(watch.elapsed(TimeUnit.NANOSECONDS), collection, Lists.transform(updateOps, new Function<UpdateOp, String>() {

        @Override
        public String apply(UpdateOp input) {
            return input.getId();
        }
    }));
    return new ArrayList<T>(results.values());
}
Also used : UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) LinkedHashMap(java.util.LinkedHashMap) Function(com.google.common.base.Function) HashSet(java.util.HashSet)

Example 15 with UpdateOp

use of org.apache.jackrabbit.oak.plugins.document.UpdateOp in project jackrabbit-oak by apache.

the class TimingDocumentStoreWrapper method createOrUpdate.

@Override
public <T extends Document> List<T> createOrUpdate(Collection<T> collection, List<UpdateOp> updateOps) {
    try {
        long start = now();
        List<T> result = base.createOrUpdate(collection, updateOps);
        updateAndLogTimes("createOrUpdate", start, 0, size(result));
        if (logCommonCall()) {
            List<String> ids = new ArrayList<String>();
            for (UpdateOp op : updateOps) {
                ids.add(op.getId());
            }
            logCommonCall(start, "createOrUpdate " + collection + " " + updateOps + " " + ids);
        }
        return result;
    } catch (Exception e) {
        throw convert(e);
    }
}
Also used : UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) ArrayList(java.util.ArrayList) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)

Aggregations

UpdateOp (org.apache.jackrabbit.oak.plugins.document.UpdateOp)21 ArrayList (java.util.ArrayList)12 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)8 Test (org.junit.Test)8 Stopwatch (com.google.common.base.Stopwatch)5 Connection (java.sql.Connection)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 AbstractDocumentStoreTest (org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)4 Revision (org.apache.jackrabbit.oak.plugins.document.Revision)4 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)3 HashSet (java.util.HashSet)3 CheckForNull (javax.annotation.CheckForNull)3 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)3 QueryCondition (org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition)3 Function (com.google.common.base.Function)2 BasicDBObject (com.mongodb.BasicDBObject)2 BulkWriteOperation (com.mongodb.BulkWriteOperation)2 DBCollection (com.mongodb.DBCollection)2 DBObject (com.mongodb.DBObject)2