You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2016/10/24 11:54:29 UTC

svn commit: r1766390 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/document/ main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ test/java/org/apache/jackrabbit/oak/plugins/document/

Author: reschke
Date: Mon Oct 24 11:54:29 2016
New Revision: 1766390

URL: http://svn.apache.org/viewvc?rev=1766390&view=rev
Log:
OAK-4951: UpdateOp without set operation for _id: clarify, add unit tests, fix DocumentStore instances

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateOp.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/mongo/MongoDocumentStore.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/UpdateOp.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateOp.java?rev=1766390&r1=1766389&r2=1766390&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateOp.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UpdateOp.java Mon Oct 24 11:54:29 2016
@@ -193,6 +193,9 @@ public final class UpdateOp {
 
     /**
      * Set the property to the given String value.
+     * <p>
+     * Note that {@link Document#ID} does not need to be set using this method;
+     * it is sufficiently specified by the id parameter set in the constructor.
      *
      * @param property the property name
      * @param value the value

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=1766390&r1=1766389&r2=1766390&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 Mon Oct 24 11:54:29 2016
@@ -46,6 +46,7 @@ public class UpdateUtils {
      */
     public static void applyChanges(@Nonnull Document doc,
                                     @Nonnull UpdateOp update) {
+        doc.put(Document.ID, update.getId());
         for (Entry<Key, Operation> e : checkNotNull(update).getChanges().entrySet()) {
             Key k = e.getKey();
             Operation op = e.getValue();

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=1766390&r1=1766389&r2=1766390&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 Mon Oct 24 11:54:29 2016
@@ -1122,6 +1122,7 @@ public class MongoDocumentStore implemen
         for (int i = 0; i < updateOps.size(); i++) {
             inserts[i] = new BasicDBObject();
             UpdateOp update = updateOps.get(i);
+            inserts[i].put(Document.ID, update.getId());
             UpdateUtils.assertUnconditional(update);
             T target = collection.newDocument(this);
             UpdateUtils.applyChanges(target, update);
@@ -1515,11 +1516,15 @@ public class MongoDocumentStore implemen
 
         // always increment modCount
         updateOp.increment(Document.MOD_COUNT, 1);
+        if (includeId) {
+            setUpdates.append(Document.ID, updateOp.getId());
+        }
 
         // other updates
         for (Entry<Key, Operation> entry : updateOp.getChanges().entrySet()) {
             Key k = entry.getKey();
             if (!includeId && k.getName().equals(Document.ID)) {
+                // TODO: remove once set _id is prohibited (OAK-4952)
                 // avoid exception "Mod on _id not allowed"
                 continue;
             }

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=1766390&r1=1766389&r2=1766390&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 Mon Oct 24 11:54:29 2016
@@ -66,6 +66,81 @@ public class BasicDocumentStoreTest exte
         UpdateOp up = new UpdateOp(id, true);
         up.set("_id", id);
         assertTrue(super.ds.create(Collection.NODES, Collections.singletonList(up)));
+        super.ds.invalidateCache();
+        assertNotNull(super.ds.find(Collection.NODES, id));
+        removeMe.add(id);
+    }
+
+    @Test
+    public void testAddAndRemoveWithoutIdInUpdateOp() {
+        String id = this.getClass().getName() + ".testAddAndRemoveWithoutIdInUpdateOp";
+
+        // remove if present
+        NodeDocument nd = super.ds.find(Collection.NODES, id);
+        if (nd != null) {
+            super.ds.remove(Collection.NODES, id);
+        }
+
+        // add
+        UpdateOp up = new UpdateOp(id, true);
+        assertTrue(super.ds.create(Collection.NODES, Collections.singletonList(up)));
+        super.ds.invalidateCache();
+        assertNotNull(super.ds.find(Collection.NODES, id));
+        removeMe.add(id);
+    }
+
+    @Test
+    public void testConflictingId() {
+        String id = this.getClass().getName() + ".testConflictingId";
+
+        UpdateOp up = new UpdateOp(id, true);
+        try {
+            up.set("_id", id + "x");
+        } catch (IllegalArgumentException expected) {
+        } finally {
+            removeMe.add(id);
+        }
+    }
+
+    @Test
+    public void testCreateOrUpdate() {
+        String id = this.getClass().getName() + ".testCreateOrUpdate";
+
+        // remove if present
+        NodeDocument nd = super.ds.find(Collection.NODES, id);
+        if (nd != null) {
+            super.ds.remove(Collection.NODES, id);
+        }
+
+        // create
+        UpdateOp up = new UpdateOp(id, true);
+        up.set("_id", id);
+        assertNull(super.ds.createOrUpdate(Collection.NODES, up));
+
+        // update
+        up = new UpdateOp(id, true);
+        up.set("_id", id);
+        assertNotNull(super.ds.createOrUpdate(Collection.NODES, up));
+        removeMe.add(id);
+    }
+
+    @Test
+    public void testCreateOrUpdateWithoutIdInUpdateOp() {
+        String id = this.getClass().getName() + ".testCreateOrUpdateWithoutIdInUpdateOp";
+
+        // remove if present
+        NodeDocument nd = super.ds.find(Collection.NODES, id);
+        if (nd != null) {
+            super.ds.remove(Collection.NODES, id);
+        }
+
+        // create
+        UpdateOp up = new UpdateOp(id, true);
+        assertNull(super.ds.createOrUpdate(Collection.NODES, up));
+
+        // update
+        up = new UpdateOp(id, true);
+        assertNotNull(super.ds.createOrUpdate(Collection.NODES, up));
         removeMe.add(id);
     }