jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1173071 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem: MemoryKernelImpl.java NodeImpl.java NodeListLarge.java NodeListSmall.java NodeMap.java
Date Tue, 20 Sep 2011 10:30:17 GMT
Author: thomasm
Date: Tue Sep 20 10:30:16 2011
New Revision: 1173071

URL: http://svn.apache.org/viewvc?rev=1173071&view=rev
Log:
Support starting the server and some other prefixes for getNodes. Implement NodeImpl.equals
and hashcode.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java?rev=1173071&r1=1173070&r2=1173071&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
Tue Sep 20 10:30:16 2011
@@ -20,14 +20,17 @@ import org.apache.jackrabbit.mk.api.Micr
 import org.apache.jackrabbit.mk.blobs.AbstractBlobStore;
 import org.apache.jackrabbit.mk.blobs.FileBlobStore;
 import org.apache.jackrabbit.mk.blobs.MemoryBlobStore;
+import org.apache.jackrabbit.mk.fs.FileUtils;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
+import org.apache.jackrabbit.mk.server.Server;
 import org.apache.jackrabbit.mk.util.Cache;
 import org.apache.jackrabbit.mk.util.CommitGate;
 import org.apache.jackrabbit.mk.util.ExceptionFactory;
 import org.apache.jackrabbit.mk.util.NonDescendingClock;
 import org.apache.jackrabbit.mk.util.PathUtils;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -62,6 +65,7 @@ public class MemoryKernelImpl implements
     private static final int MAX_REVISIONS_PER_NODE = 100;
     private static final HashMap<String, MemoryKernelImpl> INSTANCES = new HashMap<String,
MemoryKernelImpl>();
 
+    private final String name;
     private final AbstractBlobStore ds;
     private final NonDescendingClock clock = new NonDescendingClock(System.currentTimeMillis());
     private final CommitGate gate = new CommitGate();
@@ -69,55 +73,91 @@ public class MemoryKernelImpl implements
     private volatile long headRevId;
     private NodeMap nodeMap;
     private String lastJournalRevRange, lastJournal;
+    private Server server;
 
     private MemoryKernelImpl(String name) {
         if (DEBUG) {
             log("open " + name);
         }
+        this.name = name;
+        boolean startServer = false;
+        if (name.startsWith("server:")) {
+            startServer = true;
+            name = name.substring("server:".length());
+        }
         nodeMap = new NodeMap();
         if (name.startsWith("fs:")) {
             String dir = name.substring("fs:".length());
-            ds = new FileBlobStore(dir);
+            try {
+                ds = new FileBlobStore(dir);
+            } catch (IOException e) {
+                throw ExceptionFactory.convert(e);
+            }
         } else {
             ds = new MemoryBlobStore();
         }
         if (nodeMap.getRootId() == 0) {
-            clear();
+            NodeImpl head = new NodeImpl(nodeMap, 0);
+            Revision revNode = new Revision(0, 0, "", "");
+            head = revNode.store(head, new NodeImpl(nodeMap, 0));
+            head.addChildNode("data", new NodeImpl(nodeMap, 0));
+            head.addChildNode("config", new NodeImpl(nodeMap, 0));
+            NodeImpl root = new NodeImpl(nodeMap, 0);
+            root.addChildNode("head", head);
+            root.addChildNode("old", new NodeImpl(nodeMap, 0));
+            nodeMap.commit(root);
         } else {
             NodeImpl head = getRoot().getNode("head");
             String rev = head.getProperty("rev");
             headRevId = Revision.parseId(JsopTokenizer.decodeQuoted(rev));
             applyConfig(head);
         }
+        if (startServer) {
+            server = new Server(this);
+            try {
+                server.start();
+            } catch (IOException e) {
+                throw ExceptionFactory.convert(e);
+            }
+        }
     }
 
-    public static synchronized MemoryKernelImpl get(String name) {
+    /**
+     * Get or open the object. The following prefixes are supported:
+     * <ul><li>fs: store the binaries in the file system
+     * </li><li>server: also start the server
+     * </li></ul>
+     *
+     * @param url the url
+     * @return the object
+     */
+    public static synchronized MemoryKernelImpl get(String url) {
+        boolean clean = false;
+        if (url.endsWith(";clean")) {
+            url = url.substring(0, url.length() - ";clean".length());
+            clean = true;
+        }
+        url = url.replaceAll("\\{homeDir\\}", System.getProperty("homeDir", "."));
+        if (clean) {
+            String dir = url.substring(url.lastIndexOf(':') + 1);
+            try {
+                FileUtils.deleteRecursive(dir, false);
+            } catch (Exception e) {
+                throw ExceptionFactory.convert(e);
+            }
+        }
+        String name = url.substring("mem:".length());
+        if (clean) {
+            INSTANCES.remove(name);
+        }
         MemoryKernelImpl instance = INSTANCES.get(name);
-        if (instance == null || instance.nodeMap.getRootId() == 0) {
+        if (instance == null) {
             instance = new MemoryKernelImpl(name);
             INSTANCES.put(name, instance);
         }
         return instance;
     }
 
-    public synchronized void clear() {
-        if (DEBUG) {
-            log("clear");
-        }
-        headRevId = 0;
-        ds.clear();
-        nodeMap.clear();
-        NodeImpl head = new NodeImpl(nodeMap, 0);
-        Revision revNode = new Revision(0, 0, "", "");
-        head = revNode.store(head, new NodeImpl(nodeMap, 0));
-        head.addChildNode("data", new NodeImpl(nodeMap, 0));
-        head.addChildNode("config", new NodeImpl(nodeMap, 0));
-        NodeImpl root = new NodeImpl(nodeMap, 0);
-        root.addChildNode("head", head);
-        root.addChildNode("old", new NodeImpl(nodeMap, 0));
-        nodeMap.commit(root);
-    }
-
     private void applyConfig(NodeImpl head) {
         NodeImpl config = head.getNode("config");
         for (int i = 0, size = config.getPropertyCount(); i < size; i++) {
@@ -391,6 +431,17 @@ public class MemoryKernelImpl implements
         return s.length() < 100 ? s : (s.substring(0, 100) + "...");
     }
 
+    /**
+     * Get the nodes. The following prefixes are supported:
+     * <ul><li>:pretty - beautify (format) the result
+     * </li><li>:root - get the root node (including all old revisions)
+     * </li><li>:info - get internal info such as the node count
+     * </li></ul>
+     *
+     * @param path the path
+     * @param revisionId the revision
+     * @return the json string
+     */
     public String getNodes(String path, String revisionId) {
         if (DEBUG) {
             log("getNodes " + path + " revision:" + revisionId);
@@ -421,8 +472,18 @@ public class MemoryKernelImpl implements
             throw ExceptionFactory.get("Not an absolute path: " + path);
         }
         NodeImpl n;
-        if (path.startsWith("/:root")) {
-            n = getRoot().getNode(path.substring(7));
+        if (path.startsWith("/:")) {
+            if (path.startsWith("/:pretty")) {
+                return JsopBuilder.prettyPrint(
+                        doGetNodes("/" + PathUtils.relativize("/:pretty", path),
+                                revisionId, depth, offset, count));
+            } else if (path.startsWith("/:root")) {
+                n = getRoot().getNode(PathUtils.relativize("/:root", path));
+            } else if (path.startsWith("/:info")) {
+                n = nodeMap.getInfo(PathUtils.relativize("/:info", path));
+            } else {
+                n = getRevision(revisionId).getNode(path.substring(1));
+            }
         } else {
             n = getRevision(revisionId).getNode(path.substring(1));
         }
@@ -502,6 +563,11 @@ public class MemoryKernelImpl implements
         }
         gate.commit("end");
         nodeMap.close();
+        INSTANCES.remove(name);
+        if (server != null) {
+            server.stop();
+            server = null;
+        }
     }
 
     private static void log(String s) {

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java?rev=1173071&r1=1173070&r2=1173071&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
Tue Sep 20 10:30:16 2011
@@ -17,14 +17,15 @@
 package org.apache.jackrabbit.mk.mem;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
 import org.apache.jackrabbit.mk.Constants;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
+import org.apache.jackrabbit.mk.util.Cache;
 import org.apache.jackrabbit.mk.util.ExceptionFactory;
 import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.mk.util.StringCache;
-import org.apache.jackrabbit.mk.util.Cache;
 
 /**
  * An in-memory node, including all child nodes.
@@ -502,4 +503,28 @@ public class NodeImpl implements Cache.V
         return memory;
     }
 
+    public int hashCode() {
+        int hash = Arrays.hashCode(propertyValuePairs);
+        if (childNodes != null) {
+            hash ^= childNodes.hashCode();
+        }
+        return hash;
+    }
+
+    public boolean equals(Object other) {
+        if (other == this) {
+            return true;
+        }
+        if (other instanceof NodeImpl) {
+            NodeImpl o = (NodeImpl) other;
+            if (Arrays.equals(propertyValuePairs, o.propertyValuePairs)) {
+                if (childNodes == null || o.childNodes == null) {
+                    return childNodes == o.childNodes;
+                }
+                return childNodes.equals(o.childNodes);
+            }
+        }
+        return false;
+    }
+
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java?rev=1173071&r1=1173070&r2=1173071&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListLarge.java
Tue Sep 20 10:30:16 2011
@@ -23,7 +23,7 @@ import org.apache.jackrabbit.mk.json.Jso
 import org.apache.jackrabbit.mk.mem.NodeImpl.ChildVisitor;
 import org.apache.jackrabbit.mk.util.ExceptionFactory;
 import org.apache.jackrabbit.mk.util.IOUtils;
-import org.h2.util.StringUtils;
+import org.apache.jackrabbit.mk.util.StringUtils;
 
 /**
  * A large list of nodes.

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java?rev=1173071&r1=1173070&r2=1173071&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeListSmall.java
Tue Sep 20 10:30:16 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.mk.mem;
 
+import java.util.Arrays;
 import java.util.Iterator;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.mem.NodeImpl.ChildVisitor;
@@ -190,4 +191,31 @@ public class NodeListSmall implements No
         return memory;
     }
 
+    public int hashCode() {
+        if (size == 0) {
+            return 0;
+        }
+        return Arrays.hashCode(names) ^
+                Arrays.hashCode(children) ^
+                Arrays.hashCode(sort);
+    }
+
+    public boolean equals(Object other) {
+        if (other == this) {
+            return true;
+        }
+        if (other instanceof NodeListSmall) {
+            NodeListSmall o = (NodeListSmall) other;
+            if (size == o.size) {
+                if (size == 0) {
+                    return true;
+                }
+                return Arrays.equals(sort, o.sort) &&
+                        Arrays.equals(children, o.children) &&
+                        Arrays.equals(names, o.names);
+            }
+        }
+        return false;
+    }
+
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java?rev=1173071&r1=1173070&r2=1173071&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeMap.java
Tue Sep 20 10:30:16 2011
@@ -46,11 +46,6 @@ public class NodeMap {
         return nodes.get(x);
     }
 
-    public void clear() {
-        nodes.clear();
-        nextId.set(0);
-    }
-
     public void setSetting(String key, String value) {
         if (key.equals(MAX_MEMORY_CHILDREN)) {
             maxMemoryChildren = Integer.parseInt(value);
@@ -97,4 +92,11 @@ public class NodeMap {
         return value.startsWith("n");
     }
 
+    public NodeImpl getInfo(String path) {
+        NodeImpl n = new NodeImpl(this, 0);
+        n.setProperty("nodes", "" + nodes.size());
+        n.setProperty("root", "" + rootId);
+        return n;
+    }
+
 }



Mime
View raw message