jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1452806 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/segment/ test/java/org/apache/jackrabbit/oak/plugins/segment/
Date Tue, 05 Mar 2013 14:16:13 GMT
Author: jukka
Date: Tue Mar  5 14:16:12 2013
New Revision: 1452806

URL: http://svn.apache.org/r1452806
Log:
OAK-593: SegmentMK: Hierarchy of journals

Add naive journal merge mechanism

Added:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/JournalTest.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Journal.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MemoryStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Journal.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Journal.java?rev=1452806&r1=1452805&r2=1452806&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Journal.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Journal.java
Tue Mar  5 14:16:12 2013
@@ -22,4 +22,6 @@ public interface Journal {
 
     boolean setHead(RecordId base, RecordId head);
 
+    void merge();
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MemoryStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MemoryStore.java?rev=1452806&r1=1452805&r2=1452806&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MemoryStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MemoryStore.java
Tue Mar  5 14:16:12 2013
@@ -16,27 +16,102 @@
  */
 package org.apache.jackrabbit.oak.plugins.segment;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import java.util.Collections;
+import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.RebaseDiff;
 
 import com.google.common.collect.Maps;
 
 public class MemoryStore implements SegmentStore {
 
-    private final ConcurrentMap<String, RecordId> journals =
-            Maps.newConcurrentMap();
+    private static final class MemoryJournal implements Journal {
+
+        private final SegmentStore store;
+
+        private final Journal parent;
+
+        private RecordId base;
+
+        private RecordId head;
+
+        MemoryJournal(SegmentStore store, NodeState root) {
+            this.store = checkNotNull(store);
+            this.parent = null;
+
+            SegmentWriter writer =
+                    new SegmentWriter(store, new SegmentReader(store));
+            RecordId id = writer.writeNode(root).getRecordId();
+            writer.flush();
+
+            this.base = id;
+            this.head = id;
+        }
+
+        MemoryJournal(SegmentStore store, String parent) {
+            this.store = checkNotNull(store);
+            this.parent = store.getJournal(checkNotNull(parent));
+            this.base = this.parent.getHead();
+            this.head = base;
+        }
+
+        @Override
+        public synchronized RecordId getHead() {
+            return head;
+        }
+
+        @Override
+        public synchronized boolean setHead(RecordId base, RecordId head) {
+            if (checkNotNull(base).equals(this.head)) {
+                this.head = checkNotNull(head);
+                return true;
+            } else {
+                return false;
+            }
+        }
+
+        @Override
+        public synchronized void merge() {
+            if (parent != null) {
+                SegmentReader reader = new SegmentReader(store);
+                NodeState before = new SegmentNodeState(reader, base);
+                NodeState after = new SegmentNodeState(reader, head);
+
+                SegmentWriter writer = new SegmentWriter(store, reader);
+                while (!parent.setHead(base, head)) {
+                    RecordId newBase = parent.getHead();
+                    NodeBuilder builder =
+                            new SegmentNodeState(reader, newBase).builder();
+                    after.compareAgainstBaseState(
+                            before, new RebaseDiff(builder));
+                    NodeState state = builder.getNodeState();
+                    RecordId newHead = writer.writeNode(state).getRecordId();
+                    writer.flush();
+
+                    base = newBase;
+                    head = newHead;
+                }
+
+                base = head;
+            }
+        }
+
+    }
+
+    private final Map<String, Journal> journals = Maps.newHashMap();
 
     private final ConcurrentMap<UUID, Segment> segments =
             Maps.newConcurrentMap();
 
     public MemoryStore(NodeState root) {
-        SegmentWriter writer = new SegmentWriter(this, new SegmentReader(this));
-        journals.put("root", writer.writeNode(root).getRecordId());
-        writer.flush();
+        journals.put("root", new MemoryJournal(this, root));
     }
 
     public MemoryStore() {
@@ -44,23 +119,13 @@ public class MemoryStore implements Segm
     }
 
     @Override
-    public Journal getJournal(final String name) {
-        return new Journal() {
-            @Override
-            public RecordId getHead() {
-                RecordId head = journals.get(name);
-                if (head != null) {
-                    return head;
-                } else {
-                    throw new IllegalArgumentException(
-                            "Journal not found: " + name);
-                }
-            }
-            @Override
-            public boolean setHead(RecordId base, RecordId head) {
-                return journals.replace(name, base, head);
-            }
-        };
+    public synchronized Journal getJournal(final String name) {
+        Journal journal = journals.get(name);
+        if (journal == null) {
+            journal = new MemoryJournal(this, "root");
+            journals.put(name, journal);
+        }
+        return journal;
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java?rev=1452806&r1=1452805&r2=1452806&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MongoStore.java
Tue Mar  5 14:16:12 2013
@@ -78,6 +78,10 @@ public class MongoStore implements Segme
                         ImmutableMap.of("_id", name, "head", head.toString()));
                 return journals.findAndModify(baseObject, headObject) != null;
             }
+            @Override
+            public void merge() {
+                throw new UnsupportedOperationException();
+            }
         };
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java?rev=1452806&r1=1452805&r2=1452806&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java
Tue Mar  5 14:16:12 2013
@@ -71,6 +71,10 @@ public class SegmentNodeStoreService ext
                     public boolean setHead(RecordId base, RecordId head) {
                         return store[0].getJournal(name).setHead(base, head);
                     }
+                    @Override
+                    public void merge() {
+                        store[0].getJournal(name).merge();
+                    }
                 };
             }
             @Override

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/JournalTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/JournalTest.java?rev=1452806&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/JournalTest.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/JournalTest.java
Tue Mar  5 14:16:12 2013
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.segment;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.spi.commit.EmptyHook;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStoreBranch;
+import org.junit.Test;
+
+public class JournalTest {
+
+    private final SegmentStore store = new MemoryStore();
+
+    private final SegmentNodeStore root = new SegmentNodeStore(store);
+
+    private final SegmentNodeStore left = new SegmentNodeStore(store, "left");
+
+    private final SegmentNodeStore right = new SegmentNodeStore(store, "right");
+
+    @Test
+    public void testChangesFromRoot() throws CommitFailedException {
+        NodeState oldState = root.getRoot();
+        assertEquals(oldState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        NodeBuilder builder = oldState.builder();
+        builder.setProperty("foo", "bar");
+        NodeState newState = builder.getNodeState();
+
+        NodeStoreBranch branch = root.branch();
+        branch.setRoot(newState);
+        branch.merge(EmptyHook.INSTANCE);
+
+        assertEquals(newState, root.getRoot());
+        assertEquals(oldState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        store.getJournal("left").merge();
+        assertEquals(newState, root.getRoot());
+        assertEquals(newState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        store.getJournal("right").merge();
+        assertEquals(newState, root.getRoot());
+        assertEquals(newState, left.getRoot());
+        assertEquals(newState, right.getRoot());
+    }
+
+    @Test
+    public void testChangesToRoot() throws CommitFailedException {
+        NodeState oldState = root.getRoot();
+        assertEquals(oldState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        NodeBuilder builder = oldState.builder();
+        builder.setProperty("foo", "bar");
+        NodeState newState = builder.getNodeState();
+
+        NodeStoreBranch branch = left.branch();
+        branch.setRoot(newState);
+        branch.merge(EmptyHook.INSTANCE);
+
+        assertEquals(oldState, root.getRoot());
+        assertEquals(newState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        store.getJournal("left").merge();
+        assertEquals(newState, root.getRoot());
+        assertEquals(newState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        store.getJournal("right").merge();
+        assertEquals(newState, root.getRoot());
+        assertEquals(newState, left.getRoot());
+        assertEquals(newState, right.getRoot());
+    }
+
+    @Test
+    public void testConcurrentChanges() throws CommitFailedException {
+        NodeState oldState = root.getRoot();
+        assertEquals(oldState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        NodeBuilder leftBuilder = oldState.builder();
+        leftBuilder.setProperty("foo", "bar");
+        NodeState leftState = leftBuilder.getNodeState();
+
+        NodeStoreBranch leftBranch = left.branch();
+        leftBranch.setRoot(leftState);
+        leftBranch.merge(EmptyHook.INSTANCE);
+
+        assertEquals(oldState, root.getRoot());
+        assertEquals(leftState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        store.getJournal("left").merge();
+        assertEquals(leftState, root.getRoot());
+        assertEquals(leftState, left.getRoot());
+        assertEquals(oldState, right.getRoot());
+
+        NodeBuilder rightBuilder = oldState.builder();
+        rightBuilder.setProperty("bar", "foo");
+        NodeState rightState = rightBuilder.getNodeState();
+
+        NodeStoreBranch rightBranch = right.branch();
+        rightBranch.setRoot(rightState);
+        rightBranch.merge(EmptyHook.INSTANCE);
+
+        store.getJournal("right").merge();
+        NodeState newState = root.getRoot();
+        assertEquals("bar", newState.getProperty("foo").getValue(Type.STRING));
+        assertEquals("foo", newState.getProperty("bar").getValue(Type.STRING));
+        assertEquals(leftState, left.getRoot());
+        assertEquals(newState, right.getRoot());
+
+        store.getJournal("left").merge();
+        assertEquals(newState, root.getRoot());
+        assertEquals(newState, left.getRoot());
+        assertEquals(newState, right.getRoot());
+}
+
+}



Mime
View raw message