jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1186703 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index: BTree.java BTreeLeaf.java BTreeNode.java BTreePage.java IndexNode.java Indexer.java
Date Thu, 20 Oct 2011 09:56:51 GMT
Author: thomasm
Date: Thu Oct 20 09:56:50 2011
New Revision: 1186703

URL: http://svn.apache.org/viewvc?rev=1186703&view=rev
Log:
Index: cache write operations

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java
      - copied, changed from r1178722, jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexNode.java
Removed:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexNode.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTree.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreePage.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTree.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTree.java?rev=1186703&r1=1186702&r2=1186703&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTree.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTree.java
Thu Oct 20 09:56:50 2011
@@ -92,7 +92,11 @@ public class BTree {
         return -(min + 1);
     }
 
-    BTreePage getPage(IndexNode parent, String name) {
+    BTreePage getPageIfCached(BTreeNode parent, String name) {
+        return indexer.getPageIfCached(this, parent, name);
+    }
+
+    BTreePage getPage(BTreeNode parent, String name) {
         return indexer.getPage(this, parent, name);
     }
 
@@ -116,7 +120,7 @@ public class BTree {
             } else {
                 pos++;
             }
-            node = ((IndexNode) node).getChild(pos);
+            node = ((BTreeNode) node).getChild(pos);
         }
         return c;
     }
@@ -156,13 +160,21 @@ public class BTree {
         indexer.buffer(jsop);
     }
 
+    void modified(BTreePage page) {
+        indexer.modified(this, page, false);
+    }
+
+    public void moveCache(String oldPath) {
+        indexer.moveCache(this, oldPath);
+    }
+
     public boolean remove(String key, String value) {
-        IndexNode parent = null;
+        BTreeNode parent = null;
         int parentPos = 0;
         BTreePage n = getPage(null, "");
         while (true) {
-            if (n instanceof IndexNode) {
-                IndexNode page = (IndexNode) n;
+            if (n instanceof BTreeNode) {
+                BTreeNode page = (BTreeNode) n;
                 int pos = page.find(key, value);
                 if (pos < 0) {
                     pos = -pos - 1;
@@ -181,9 +193,15 @@ public class BTree {
                 page.delete(pos);
                 if (n.size() == 0 && parent != null) {
                     bufferDelete(page.getPath());
+                    indexer.modified(this, page, true);
                     // empty leaf with a parent
                     parent.delete(parentPos);
                     parent.writeData();
+                    if (parent.isEmpty()) {
+                        // empty node becomes a empty leaf
+                        BTreeLeaf p = new BTreeLeaf(this, parent.parent, parent.name, new
String[0], new String[0]);
+                        modified(p);
+                    }
                 } else {
                     page.writeData();
                 }
@@ -195,7 +213,7 @@ public class BTree {
     }
 
     public void add(String key, String value) {
-        IndexNode parent = null;
+        BTreeNode parent = null;
         int parentPos = 0;
         BTreePage n = getPage(null, "");
         while (true) {
@@ -204,10 +222,11 @@ public class BTree {
                 int split = getMinSize();
                 if (parent == null) {
                     // new root
-                    IndexNode root = new IndexNode(this, null, "",
+                    BTreeNode root = new BTreeNode(this, null, "",
                             new String[] { n.keys[split] },
                             new String[] { n.values[split] },
                             new String[] { "0", "1" });
+                    modified(root);
                     n.split(root, "0", split, "1");
                     n = root;
                 } else {
@@ -224,8 +243,8 @@ public class BTree {
                 // subsequent operations are based on the new structure
                 commit();
             }
-            if (n instanceof IndexNode) {
-                IndexNode page = (IndexNode) n;
+            if (n instanceof BTreeNode) {
+                BTreeNode page = (BTreeNode) n;
                 int pos = page.find(key, value);
                 if (pos < 0) {
                     pos = -pos - 1;

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java?rev=1186703&r1=1186702&r2=1186703&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeLeaf.java
Thu Oct 20 09:56:50 2011
@@ -26,8 +26,8 @@ import org.apache.jackrabbit.mk.util.Str
  */
 class BTreeLeaf extends BTreePage {
 
-    BTreeLeaf(BTree tree, IndexNode parent, String path, String[] data, String[] paths) {
-        super(tree, parent, path, data, paths);
+    BTreeLeaf(BTree tree, BTreeNode parent, String name, String[] data, String[] paths) {
+        super(tree, parent, name, data, paths);
     }
 
     BTreeLeaf nextLeaf() {
@@ -38,8 +38,8 @@ class BTreeLeaf extends BTreePage {
         return this;
     }
 
-    void split(IndexNode newParent, String newName, int pos, String siblingName) {
-        setParent(newParent, newName);
+    void split(BTreeNode newParent, String newName, int pos, String siblingName) {
+        setParent(newParent, newName, true);
         String[] k2 = Arrays.copyOfRange(keys, pos, keys.length, String[].class);
         String[] v2 = Arrays.copyOfRange(values, pos, values.length, String[].class);
         BTreeLeaf n2 = new BTreeLeaf(tree, parent, siblingName, k2, v2);
@@ -50,21 +50,25 @@ class BTreeLeaf extends BTreePage {
     }
 
     void insert(int pos, String key, String value) {
+        tree.modified(this);
         keys = StringUtils.arrayInsert(keys, pos, key);
         values = StringUtils.arrayInsert(values, pos, value);
     }
 
     void delete(int pos) {
+        tree.modified(this);
         keys = StringUtils.arrayRemove(keys, pos);
         values = StringUtils.arrayRemove(values, pos);
     }
 
     void writeData() {
+        tree.modified(this);
         tree.bufferSetArray(getPath(), "keys", keys);
         tree.bufferSetArray(getPath(), "values", values);
     }
 
     void writeCreate() {
+        tree.modified(this);
         JsopBuilder jsop = new JsopBuilder();
         jsop.appendTag("+ ").key(PathUtils.concat(tree.getName(), getPath())).object();
         jsop.key("keys").array();
@@ -82,8 +86,4 @@ class BTreeLeaf extends BTreePage {
         tree.buffer(jsop.toString());
     }
 
-    String getValue(int pos) {
-        return values[pos];
-    }
-
 }
\ No newline at end of file

Copied: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java
(from r1178722, jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexNode.java)
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java?p2=jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java&p1=jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexNode.java&r1=1178722&r2=1186703&rev=1186703&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/IndexNode.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreeNode.java
Thu Oct 20 09:56:50 2011
@@ -24,12 +24,12 @@ import org.apache.jackrabbit.mk.util.Str
 /**
  * An index node page.
  */
-class IndexNode extends BTreePage {
+class BTreeNode extends BTreePage {
 
     private String[] children;
 
-    IndexNode(BTree tree, IndexNode parent, String path, String[] keys, String[] values,
String[] children) {
-        super(tree, parent, path, keys, values);
+    BTreeNode(BTree tree, BTreeNode parent, String name, String[] keys, String[] values,
String[] children) {
+        super(tree, parent, name, keys, values);
         this.children = children;
     }
 
@@ -48,12 +48,18 @@ class IndexNode extends BTreePage {
         return tree.getPage(this, children[0]).firstLeaf();
     }
 
-    void split(IndexNode newParent, String newName, int pos, String siblingName) {
-        setParent(newParent, newName);
+    void split(BTreeNode newParent, String newName, int pos, String siblingName) {
+        setParent(newParent, newName, true);
         String[] k2 = Arrays.copyOfRange(keys, pos + 1, keys.length, String[].class);
         String[] v2 = Arrays.copyOfRange(values, pos + 1, values.length, String[].class);
         String[] c2 = Arrays.copyOfRange(children, pos + 1, children.length, String[].class);
-        IndexNode n2 = new IndexNode(tree, parent, siblingName, k2, v2, c2);
+        BTreeNode n2 = new BTreeNode(tree, parent, siblingName, k2, v2, c2);
+        for (String c : c2) {
+            BTreePage cp = tree.getPageIfCached(this, c);
+            if (cp != null) {
+                cp.setParent(n2, c, false);
+            }
+        }
         keys = Arrays.copyOfRange(keys, 0, pos, String[].class);
         values = Arrays.copyOfRange(values, 0, pos, String[].class);
         children = Arrays.copyOfRange(children, 0, pos + 1, String[].class);
@@ -64,6 +70,7 @@ class IndexNode extends BTreePage {
                     PathUtils.concat(tree.getName(), getParentPath(), siblingName, c)
             );
         }
+        tree.moveCache(getPath());
         writeData();
     }
 
@@ -86,12 +93,14 @@ class IndexNode extends BTreePage {
     }
 
     void writeData() {
+        tree.modified(this);
         tree.bufferSetArray(getPath(), "keys", keys);
         tree.bufferSetArray(getPath(), "values", values);
         tree.bufferSetArray(getPath(), "children", children);
     }
 
     void writeCreate() {
+        tree.modified(this);
         JsopBuilder jsop = new JsopBuilder();
         jsop.appendTag("+ ").key(PathUtils.concat(tree.getName(), getPath())).object();
         jsop.key("keys").array();
@@ -119,6 +128,7 @@ class IndexNode extends BTreePage {
     }
 
     void delete(int pos) {
+        tree.modified(this);
         if (size() > 0) {
             // empty parent
             keys = StringUtils.arrayRemove(keys, Math.max(0, pos - 1));
@@ -128,9 +138,14 @@ class IndexNode extends BTreePage {
     }
 
     void insert(int pos, String key, String value, String child) {
+        tree.modified(this);
         keys = StringUtils.arrayInsert(keys, pos, key);
         values = StringUtils.arrayInsert(values, pos, value);
         children = StringUtils.arrayInsert(children, pos + 1, child);
     }
 
+    boolean isEmpty() {
+        return children.length == 0;
+    }
+
 }
\ No newline at end of file

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreePage.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreePage.java?rev=1186703&r1=1186702&r2=1186703&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreePage.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/BTreePage.java
Thu Oct 20 09:56:50 2011
@@ -24,12 +24,12 @@ import org.apache.jackrabbit.mk.util.Pat
 abstract class BTreePage {
 
     protected final BTree tree;
-    protected IndexNode parent;
+    protected BTreeNode parent;
     protected String name;
     protected String[] keys;
     protected String[] values;
 
-    BTreePage(BTree tree, IndexNode parent, String name, String[] keys, String[] values)
{
+    BTreePage(BTree tree, BTreeNode parent, String name, String[] keys, String[] values)
{
         this.tree = tree;
         this.parent = parent;
         this.name = name;
@@ -38,22 +38,25 @@ abstract class BTreePage {
     }
 
     abstract void writeCreate();
-    abstract void split(IndexNode newParent, String newPath, int pos, String siblingPath);
+    abstract void split(BTreeNode newParent, String newPath, int pos, String siblingPath);
     abstract BTreeLeaf firstLeaf();
 
-    void setParent(IndexNode newParent, String newName) {
+    void setParent(BTreeNode newParent, String newName, boolean parentIsNew) {
         if (newParent != null) {
+            String oldPath = getPath();
             tree.bufferMove(
                     PathUtils.concat(tree.getName(), getPath()),
                     "temp");
-            newParent.writeCreate();
+            if (parentIsNew) {
+                newParent.writeCreate();
+            }
             tree.bufferMove(
                     "temp",
                     PathUtils.concat(tree.getName(), getParentPath(), newName));
             parent = newParent;
             name = newName;
-
-            int todoRequiredForMicroKernelImpl;
+            tree.moveCache(oldPath);
+            tree.modified(this);
             tree.commit();
         }
     }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java?rev=1186703&r1=1186702&r2=1186703&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/index/Indexer.java
Thu Oct 20 09:56:50 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.mk.index;
 
+import org.apache.jackrabbit.mk.MicroKernelImpl;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
@@ -27,6 +28,7 @@ import org.apache.jackrabbit.mk.util.Pat
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map.Entry;
 
 /**
  * A index mechanism. An index is bound to a certain repository, and supports
@@ -41,6 +43,7 @@ public class Indexer {
     private String indexRootNode;
     private StringBuilder buffer;
     private HashMap<String, Index> indexes = new HashMap<String, Index>();
+    private HashMap<String, BTreePage> modified = new HashMap<String, BTreePage>();
     private String readRevision;
 
     public Indexer(MicroKernel mk, String indexRootNode) {
@@ -116,11 +119,25 @@ public class Indexer {
         revision = mk.commit(indexRootNode, jsop, revision, null);
     }
 
-    BTreePage getPage(BTree tree, IndexNode parent, String name) {
+    BTreePage getPageIfCached(BTree tree, BTreeNode parent, String name) {
+        String p = getPath(tree, parent, name);
+        return modified.get(p);
+    }
+
+    private String getPath(BTree tree, BTreeNode parent, String name) {
         String p = parent == null ? name : PathUtils.concat(parent.getPath(), name);
         String indexRoot = PathUtils.concat(indexRootNode, tree.getName());
-        String json = mk.getNodes(PathUtils.concat(indexRoot, p), revision, 0, 0, 0);
+        return PathUtils.concat(indexRoot, p);
+    }
+
+    BTreePage getPage(BTree tree, BTreeNode parent, String name) {
+        String p = getPath(tree, parent, name);
         BTreePage page;
+        page = modified.get(p);
+        if (page != null) {
+            return page;
+        }
+        String json = mk.getNodes(p, revision, 0, 0, 0);
         if (json == null) {
             page = new BTreeLeaf(tree, parent, name,
                     new String[0], new String[0]);
@@ -133,7 +150,7 @@ public class Indexer {
             String values = n.getProperty("values");
             String children = n.getProperty("children");
             if (children != null) {
-                IndexNode node = new IndexNode(tree, parent, name,
+                BTreeNode node = new BTreeNode(tree, parent, name,
                         readArray(keys), readArray(values), readArray(children));
                 page = node;
             } else {
@@ -171,11 +188,48 @@ public class Indexer {
     }
 
     void commit() {
+        // TODO remove this method once MicroKernelImpl supports
+        // move + add node
+        if (mk instanceof MicroKernelImpl) {
+            commitChanges();
+        }
+    }
+
+    void modified(BTree tree, BTreePage page, boolean deleted) {
+        String indexRoot = PathUtils.concat(indexRootNode, tree.getName());
+        String p = PathUtils.concat(indexRoot, page.getPath());
+        if (deleted) {
+            modified.remove(p);
+        } else {
+            modified.put(p, page);
+        }
+    }
+
+    public void moveCache(BTree tree, String oldPath) {
+        String indexRoot = PathUtils.concat(indexRootNode, tree.getName());
+        String o = PathUtils.concat(indexRoot, oldPath);
+        HashMap<String, BTreePage> moved = new HashMap<String, BTreePage>();
+        for (Entry<String, BTreePage> e : modified.entrySet()) {
+            if (e.getKey().startsWith(o)) {
+                moved.put(e.getKey(), e.getValue());
+            }
+        }
+        for (String s : moved.keySet()) {
+            modified.remove(s);
+        }
+        for (BTreePage p : moved.values()) {
+            String n = PathUtils.concat(indexRoot, p.getPath());
+            modified.put(n, p);
+        }
+    }
+
+    void commitChanges() {
         if (buffer != null) {
             String jsop = buffer.toString();
             // System.out.println(jsop);
             revision = mk.commit(indexRootNode, jsop, revision, null);
             buffer = null;
+            modified.clear();
         }
     }
 
@@ -220,7 +274,7 @@ public class Indexer {
         JsopBuilder jsop = new JsopBuilder();
         jsop.appendTag("^ ").key("rev").value(readRevision);
         buffer(jsop.toString());
-        commit();
+        commitChanges();
     }
 
     private void updateWith(String jsop, String lastRevision) {



Mime
View raw message