Author: reschke
Date: Tue Nov 17 13:30:18 2015
New Revision: 1714779

URL: http://svn.apache.org/viewvc?rev=1714779&view=rev
Log:
OAK-3635: DocumentStore: clarify which methods support conditional UpdateOps, 
and enforce this in implementations

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentStore.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateUtils.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BasicDocumentStoreTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentStore.java?rev=1714779&r1=1714778&r2=1714779&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentStore.java
 Tue Nov 17 13:30:18 2015
@@ -23,6 +23,7 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 
 import org.apache.jackrabbit.oak.cache.CacheStats;
+import org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition;
 import org.apache.jackrabbit.oak.plugins.document.cache.CacheInvalidationStats;
 
 /**
@@ -178,8 +179,9 @@ public interface DocumentStore {
      *
      * @param <T> the document type
      * @param collection the collection
-     * @param updateOps the list of documents to add
+     * @param updateOps the list of documents to add (where {@link Condition}s 
are not allowed)
      * @return true if this worked (if none of the documents already existed)
+     * @throws IllegalArgumentException when at least one of the {@linkplain 
UpdateOp}s is conditional
      */
     <T extends Document> boolean create(Collection<T> collection, 
List<UpdateOp> updateOps);
 
@@ -193,7 +195,9 @@ public interface DocumentStore {
      * @param <T> the document type.
      * @param collection the collection.
      * @param keys the keys of the documents to update.
-     * @param updateOp the update operation to apply to each of the documents.
+     * @param updateOp the update operation to apply to each of the documents
+     *        (where {@link Condition}s are not allowed)
+     * @throws IllegalArgumentException when the {@linkplain UpdateOp} is 
conditional
      */
     <T extends Document> void update(Collection<T> collection,
                                      List<String> keys,
@@ -205,8 +209,9 @@ public interface DocumentStore {
      *
      * @param <T> the document type
      * @param collection the collection
-     * @param update the update operation
+     * @param update the update operation (where {@link Condition}s are not 
allowed)
      * @return the old document or <code>null</code> if it didn't exist before.
+     * @throws IllegalArgumentException when the {@linkplain UpdateOp} is 
conditional
      */
     @CheckForNull
     <T extends Document> T createOrUpdate(Collection<T> collection, UpdateOp 
update);

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateUtils.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateUtils.java?rev=1714779&r1=1714778&r2=1714779&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateUtils.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateUtils.java
 Tue Nov 17 13:30:18 2015
@@ -154,4 +154,17 @@ public class UpdateUtils {
         }
         return true;
     }
+
+    /**
+     * Ensures that the given {@link UpdateOp} is unconditional
+     * @param up the update operation
+     * @throws IllegalArgumentException when the operations is conditional
+     */
+    public static void assertUnconditional(@Nonnull UpdateOp up) {
+        Map<Key, Condition> conditions = up.getConditions();
+        if (!conditions.isEmpty()) {
+            throw new IllegalArgumentException(
+                    "This DocumentStore method does not support conditional 
updates, but the UpdateOp contained: " + conditions);
+        }
+    }
 }

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java?rev=1714779&r1=1714778&r2=1714779&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
 Tue Nov 17 13:30:18 2015
@@ -48,6 +48,7 @@ import com.mongodb.ReadPreference;
 import com.mongodb.WriteConcern;
 import org.apache.jackrabbit.oak.plugins.document.cache.CacheInvalidationStats;
 
+import static 
org.apache.jackrabbit.oak.plugins.document.UpdateUtils.assertUnconditional;
 import static 
org.apache.jackrabbit.oak.plugins.document.UpdateUtils.checkConditions;
 
 /**
@@ -213,6 +214,7 @@ public class MemoryDocumentStore impleme
     @CheckForNull
     @Override
     public <T extends Document> T createOrUpdate(Collection<T> collection, 
UpdateOp update) {
+        assertUnconditional(update);
         return internalCreateOrUpdate(collection, update, false);
     }
 
@@ -316,6 +318,7 @@ public class MemoryDocumentStore impleme
                 }
             }
             for (UpdateOp op : updateOps) {
+                assertUnconditional(op);
                 internalCreateOrUpdate(collection, op, false);
             }
             return true;
@@ -328,6 +331,7 @@ public class MemoryDocumentStore impleme
     public <T extends Document> void update(Collection<T> collection,
                                             List<String> keys,
                                             UpdateOp updateOp) {
+        assertUnconditional(updateOp);
         Lock lock = rwLock.writeLock();
         lock.lock();
         try {

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java?rev=1714779&r1=1714778&r2=1714779&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
 Tue Nov 17 13:30:18 2015
@@ -836,6 +836,7 @@ public class MongoDocumentStore implemen
     public <T extends Document> T createOrUpdate(Collection<T> collection, 
UpdateOp update)
             throws DocumentStoreException {
         log("createOrUpdate", update);
+        UpdateUtils.assertUnconditional(update);
         T doc = findAndModify(collection, update, true, false);
         log("createOrUpdate returns ", doc);
         return doc;
@@ -859,6 +860,7 @@ public class MongoDocumentStore implemen
         for (int i = 0; i < updateOps.size(); i++) {
             inserts[i] = new BasicDBObject();
             UpdateOp update = updateOps.get(i);
+            UpdateUtils.assertUnconditional(update);
             T target = collection.newDocument(this);
             UpdateUtils.applyChanges(target, update, comparator);
             docs.add(target);
@@ -933,6 +935,7 @@ public class MongoDocumentStore implemen
                                             List<String> keys,
                                             UpdateOp updateOp) {
         log("update", keys, updateOp);
+        UpdateUtils.assertUnconditional(updateOp);
         DBCollection dbCollection = getDBCollection(collection);
         QueryBuilder query = QueryBuilder.start(Document.ID).in(keys);
         // make sure we don't modify the original updateOp

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1714779&r1=1714778&r2=1714779&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 Tue Nov 17 13:30:18 2015
@@ -272,11 +272,13 @@ public class RDBDocumentStore implements
 
     @Override
     public <T extends Document> void update(Collection<T> collection, 
List<String> keys, UpdateOp updateOp) {
+        UpdateUtils.assertUnconditional(updateOp);
         internalUpdate(collection, keys, updateOp);
     }
 
     @Override
     public <T extends Document> T createOrUpdate(Collection<T> collection, 
UpdateOp update) {
+        UpdateUtils.assertUnconditional(update);
         return internalCreateOrUpdate(collection, update, true, false);
     }
 
@@ -886,6 +888,7 @@ public class RDBDocumentStore implements
             for (List<UpdateOp> chunks : Lists.partition(updates, CHUNKSIZE)) {
                 List<T> docs = new ArrayList<T>();
                 for (UpdateOp update : chunks) {
+                    UpdateUtils.assertUnconditional(update);
                     T doc = collection.newDocument(this);
                     update.increment(MODCOUNT, 1);
                     if (hasChangesToCollisions(update)) {

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BasicDocumentStoreTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BasicDocumentStoreTest.java?rev=1714779&r1=1714778&r2=1714779&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BasicDocumentStoreTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BasicDocumentStoreTest.java
 Tue Nov 17 13:30:18 2015
@@ -181,6 +181,55 @@ public class BasicDocumentStoreTest exte
     }
 
     @Test
+    public void testConditionalupdateForbidden() {
+        String id = this.getClass().getName() + 
".testConditionalupdateForbidden";
+
+        // remove if present
+        NodeDocument nd = super.ds.find(Collection.NODES, id);
+        if (nd != null) {
+            super.ds.remove(Collection.NODES, id);
+        }
+
+        try {
+            UpdateOp up = new UpdateOp(id, true);
+            up.set("_id", id);
+            up.equals("foo", "bar");
+            super.ds.create(Collection.NODES, Collections.singletonList(up));
+            fail("conditional create should fail");
+        }
+        catch (IllegalStateException expected) {
+            // reported by UpdateOp
+        }
+
+        UpdateOp cup = new UpdateOp(id, true);
+        cup.set("_id", id);
+        assertTrue(super.ds.create(Collection.NODES, 
Collections.singletonList(cup)));
+        removeMe.add(id);
+
+        try {
+            UpdateOp up = new UpdateOp(id, false);
+            up.set("_id", id);
+            up.equals("foo", "bar");
+            super.ds.createOrUpdate(Collection.NODES, up);
+            fail("conditional createOrUpdate should fail");
+        }
+        catch (IllegalArgumentException expected) {
+            // reported by DocumentStore
+        }
+
+        try {
+            UpdateOp up = new UpdateOp(id, false);
+            up.set("_id", id);
+            up.equals("foo", "bar");
+            super.ds.update(Collection.NODES, Collections.singletonList(id), 
up);
+            fail("conditional update should fail");
+        }
+        catch (IllegalArgumentException expected) {
+            // reported by DocumentStore
+        }
+    }
+
+    @Test
     public void testMaxIdAscii() {
         int result = testMaxId(true);
         assertTrue("needs to support keys of 512 bytes length, but only 
supports " + result, result >= 512);


Reply via email to