jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1125586 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/store/ test/java/org/apache/jackrabbit/mk/
Date Fri, 20 May 2011 22:49:57 GMT
Author: stefan
Date: Fri May 20 22:49:56 2011
New Revision: 1125586

URL: http://svn.apache.org/viewvc?rev=1125586&view=rev
Log:
MicroKernel prototype (WIP)

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/LockableFileRevision.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionStore.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Commit.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/DataStore.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/GitStore.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableCommit.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableNode.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Node.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Commit.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Commit.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Commit.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Commit.java
Fri May 20 22:49:56 2011
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.mk.store;
 
 import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -57,16 +58,18 @@ public class Commit extends StoredObject
 
     //--------------------------------------------------------< serialization >
 
-    public static Commit fromBytes(String id, byte[] data) throws Exception {
-        ByteArrayInputStream bais = new ByteArrayInputStream(data);
-        DataInputStream in = new DataInputStream(bais);
-        if (in.read() != Constants.COMMIT) {
-            throw new Exception("unknown data format");
+    public static Commit fromBytes(String id, InputStream is) throws Exception {
+        DataInputStream in = new DataInputStream(is);
+        try {
+            if (in.read() != Constants.COMMIT) {
+                throw new Exception("unknown data format");
+            }
+            String rootNodeId = in.readUTF();
+            long commitTS = in.readLong();
+            String parentId = in.readUTF();
+            return new Commit(id, rootNodeId, commitTS, "".equals(parentId) ? null : parentId);
+        } finally {
+            in.close();
         }
-        String rootNodeId = in.readUTF();
-        long commitTS = in.readLong();
-        String parentId = in.readUTF();
-        in.close();
-        return new Commit(id, rootNodeId, commitTS, "".equals(parentId) ? null : parentId);
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/DataStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/DataStore.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/DataStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/DataStore.java
Fri May 20 22:49:56 2011
@@ -16,13 +16,22 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import java.io.File;
+import java.io.InputStream;
+
 /**
  * <code>DataStore</code> represents a simple, <i>content-addressable</i>
  * data store.
  */
 public interface DataStore {
 
-    byte[] get(String id) throws Exception;
+    void initialize(File homeDir) throws Exception;
+
+    void close();
+
+    InputStream get(String id) throws Exception;
+
+    String /* id */ put(InputStream in) throws Exception;
 
-    String /* id */ put(byte[] data) throws Exception;
+    String computeId(InputStream in) throws Exception;
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/GitStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/GitStore.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/GitStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/GitStore.java
Fri May 20 22:49:56 2011
@@ -24,12 +24,15 @@ import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.storage.file.FileRepository;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.InputStream;
 
 /**
  *
  */
-public class GitStore implements DataStore {
+public class GitStore implements RevisionStore {
 
     Repository rep;
     boolean initialized;
@@ -38,6 +41,8 @@ public class GitStore implements DataSto
         initialized = false;
     }
 
+    //--------------------------------------------------------< RevisionStore >
+
     public void initialize(File homeDir) throws Exception {
         if (initialized) {
             throw new IllegalStateException("already initialized");
@@ -61,7 +66,7 @@ public class GitStore implements DataSto
         initialized = false;
     }
 
-    public String getHeadCommitId() throws Exception {
+    public String getHeadRevision() throws Exception {
         if (!initialized) {
             throw new IllegalStateException("not initialized");
         }
@@ -69,32 +74,59 @@ public class GitStore implements DataSto
         return ref == null || ref.getObjectId() == null ? null : ref.getObjectId().name();
     }
 
-    public void setHeadCommitId(String commitId) throws Exception {
+    public void setHeadRevision(String revId) throws Exception {
         if (!initialized) {
             throw new IllegalStateException("not initialized");
         }
         RefUpdate refUpd = rep.getRefDatabase().newUpdate("HEAD", false);
-        refUpd.setNewObjectId(ObjectId.fromString(commitId));
+        refUpd.setNewObjectId(ObjectId.fromString(revId));
         RefUpdate.Result result = refUpd.forceUpdate();
     }
 
-    public String computeId(byte[] data) {
+    public String computeId(InputStream in) throws Exception {
+        // todo FIXME
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+        byte[] buf = new byte[8192];
+        int read;
+        try {
+            while ((read = in.read(buf, 0, buf.length)) != -1) {
+                baos.write(buf, 0, read);
+            }
+        } finally {
+            in.close();
+        }
+        byte[] data = baos.toByteArray();
+
         ObjectInserter inserter = rep.getObjectDatabase().newInserter();
         return inserter.idFor(Constants.OBJ_BLOB, data).name();
     }
 
-    //------------------------------------------------------------< DataStore >
-    public byte[] get(String id) throws Exception {
+    public InputStream get(String id) throws Exception {
         if (!initialized) {
             throw new IllegalStateException("not initialized");
         }
-        return rep.getObjectDatabase().newReader().open(ObjectId.fromString(id), Constants.OBJ_BLOB).getBytes();
+        return rep.getObjectDatabase().newReader().open(ObjectId.fromString(id), Constants.OBJ_BLOB).openStream();
     }
 
-    public String put(byte[] data) throws Exception {
+    public String put(InputStream in) throws Exception {
         if (!initialized) {
             throw new IllegalStateException("not initialized");
         }
+        // todo FIXME
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+        byte[] buf = new byte[8192];
+        int read;
+        try {
+            while ((read = in.read(buf, 0, buf.length)) != -1) {
+                baos.write(buf, 0, read);
+            }
+        } finally {
+            in.close();
+        }
+        byte[] data = baos.toByteArray();
+
         ObjectInserter inserter = rep.getObjectDatabase().newInserter();
         ObjectId id;
         try {

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/LockableFileRevision.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/LockableFileRevision.java?rev=1125586&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/LockableFileRevision.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/LockableFileRevision.java
Fri May 20 22:49:56 2011
@@ -0,0 +1,139 @@
+/*
+ * 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.mk.store;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileLock;
+
+/**
+ * Maintains a file-based revision counter with locking, assuring uniqueness.
+ *
+ * copied from http://svn.apache.org/repos/asf/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/journal/LockableFileRevision.java
+ * with some modifications
+ */
+class LockableFileRevision {
+
+    /**
+     * Underlying file.
+     */
+    private final File file;
+
+    /**
+     * Underlying random access file.
+     */
+    private RandomAccessFile raf;
+
+    /**
+     * File lock.
+     */
+    private FileLock lock;
+
+    /**
+     * Current lock count.
+     */
+    private int locks;
+
+    /**
+     * Creates a new file based revision counter.
+     *
+     * @param file holding global counter
+     */
+    public LockableFileRevision(File file) throws IOException {
+        this.file = file;
+
+        if (!file.exists()) {
+            file.createNewFile();
+        }
+    }
+
+    /**
+     * Lock underlying file.
+     *
+     * @param shared whether to allow other readers or not
+     */
+    public synchronized void lock(boolean shared) throws IOException {
+        if (lock == null) {
+            try {
+                raf = new RandomAccessFile(file, shared ? "r" : "rw");
+                lock = raf.getChannel().lock(0L, Long.MAX_VALUE, shared);
+            } finally {
+                if (lock == null && raf != null) {
+                    raf.close();
+                    raf = null;
+                }
+            }
+        }
+        locks++;
+    }
+
+    /**
+     * Unlock underlying file.
+     */
+    public synchronized void unlock() throws IOException {
+        if (lock != null && --locks == 0) {
+            lock.release();
+            lock = null;
+
+            if (raf != null) {
+                raf.close();
+            }
+            raf = null;
+        }
+    }
+
+    /**
+     * Return current counter value.
+     *
+     * @return counter value
+     * @throws IOException if some error occurs
+     */
+    public String get() throws IOException {
+        lock(true);
+
+        try {
+            String value = null;
+            if (raf.length() > 0) {
+                raf.seek(0L);
+                value = raf.readUTF();
+            }
+            return value;
+
+        } finally {
+            unlock();
+        }
+    }
+
+    /**
+     * Set current counter value.
+     *
+     * @param value new counter value
+     * @throws IOException if some error occurs
+     */
+    public void set(String value) throws IOException {
+        lock(false);
+
+        try {
+            raf.seek(0L);
+            raf.writeUTF(value);
+        } finally {
+            unlock();
+        }
+    }
+
+}

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableCommit.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableCommit.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableCommit.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableCommit.java
Fri May 20 22:49:56 2011
@@ -16,9 +16,11 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -65,7 +67,7 @@ public class MutableCommit {
 
     //--------------------------------------------------------< serialization >
 
-    public byte[] toBytes() {
+    public InputStream toBytes() {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         DataOutputStream out = new DataOutputStream(baos);
         try {
@@ -77,6 +79,6 @@ public class MutableCommit {
         } catch (IOException safeToIgnore) {
         }
 
-        return baos.toByteArray();
+        return new ByteArrayInputStream(baos.toByteArray());
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableNode.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableNode.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MutableNode.java
Fri May 20 22:49:56 2011
@@ -16,9 +16,11 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -59,7 +61,7 @@ public class MutableNode {
 
     //--------------------------------------------------------< serialization >
 
-    public byte[] toBytes() {
+    public InputStream toBytes() {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         DataOutputStream out = new DataOutputStream(baos);
         try {
@@ -78,6 +80,6 @@ public class MutableNode {
         } catch (IOException safeToIgnore) {
         }
 
-        return baos.toByteArray();
+        return new ByteArrayInputStream(baos.toByteArray());
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Node.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Node.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Node.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Node.java
Fri May 20 22:49:56 2011
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.mk.store;
 import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -66,27 +67,29 @@ public class Node extends StoredObject {
 
     //--------------------------------------------------------< serialization >
 
-    public static Node fromBytes(String id, byte[] data) throws Exception {
-        ByteArrayInputStream bais = new ByteArrayInputStream(data);
-        DataInputStream in = new DataInputStream(bais);
-        if (in.read() != Constants.NODE) {
-            throw new Exception("unknown data format");
+    public static Node fromBytes(String id, InputStream is) throws Exception {
+        DataInputStream in = new DataInputStream(is);
+        try {
+            if (in.read() != Constants.NODE) {
+                throw new Exception("unknown data format");
+            }
+            int count = in.readInt();
+            Map<String, String> props = new HashMap<String, String>(count);
+            while (count-- > 0) {
+                String name = in.readUTF();
+                String value = in.readUTF();
+                props.put(name, value);
+            }
+            count = in.readInt();
+            LinkedHashMap<String, String> childEntries = new LinkedHashMap<String,
String>(count);
+            while (count-- > 0) {
+                String name = in.readUTF();
+                String childId = in.readUTF();
+                childEntries.put(name, childId);
+            }
+            return new Node(id, props, childEntries);
+        } finally {
+            in.close();
         }
-        int count = in.readInt();
-        Map<String, String> props = new HashMap<String, String>(count);
-        while (count-- > 0) {
-            String name = in.readUTF();
-            String value = in.readUTF();
-            props.put(name, value);
-        }
-        count = in.readInt();
-        LinkedHashMap<String, String> childEntries = new LinkedHashMap<String, String>(count);
-        while (count-- > 0) {
-            String name = in.readUTF();
-            String childId = in.readUTF();
-            childEntries.put(name, childId);
-        }
-        in.close();
-        return new Node(id, props, childEntries);
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
Fri May 20 22:49:56 2011
@@ -26,7 +26,7 @@ public class ObjectStore implements Cons
 
     boolean initialized;
     ReentrantLock headLock = new ReentrantLock();
-    GitStore store;
+    RevisionStore store;
 
     public ObjectStore() {
         initialized = false;
@@ -38,16 +38,17 @@ public class ObjectStore implements Cons
         }
 
         store = new GitStore();
+        //store = new SimpleRevisionStore();
         store.initialize(homeDir);
 
         // make sure we've got a HEAD commit
-        String head = store.getHeadCommitId();
+        String head = store.getHeadRevision();
         if (head == null) {
             String rootNodeId = store.put(new MutableNode().toBytes());
             MutableCommit initialCommit = new MutableCommit();
             initialCommit.setCommitTS(System.currentTimeMillis());
             initialCommit.setRootNodeId(rootNodeId);
-            store.setHeadCommitId(store.put(initialCommit.toBytes()));
+            store.setHeadRevision(store.put(initialCommit.toBytes()));
         }
 
         initialized = true;
@@ -67,8 +68,7 @@ public class ObjectStore implements Cons
             throw new IllegalStateException("not initialized");
         }
 
-        byte[] data = store.get(id);
-        return Node.fromBytes(id, data);
+        return Node.fromBytes(id, store.get(id));
     }
 
     public String putNode(MutableNode node) throws Exception {
@@ -84,8 +84,7 @@ public class ObjectStore implements Cons
             throw new IllegalStateException("not initialized");
         }
 
-        byte[] data = store.get(id);
-        return Commit.fromBytes(id, data);
+        return Commit.fromBytes(id, store.get(id));
     }
 
     public String putCommit(MutableCommit commit) throws Exception {
@@ -101,7 +100,7 @@ public class ObjectStore implements Cons
             throw new IllegalStateException("not initialized");
         }
 
-        return getCommit(store.getHeadCommitId());
+        return getCommit(store.getHeadRevision());
     }
 
     public void setHeadCommitId(String commitId) throws Exception {
@@ -109,7 +108,7 @@ public class ObjectStore implements Cons
             throw new IllegalStateException("not initialized");
         }
 
-        store.setHeadCommitId(commitId);
+        store.setHeadRevision(commitId);
     }
 
     public void lockHead() {
@@ -119,12 +118,4 @@ public class ObjectStore implements Cons
     public void unlockHead() {
         headLock.unlock();
     }
-
-    public String computeId(byte[] data) {
-        if (!initialized) {
-            throw new IllegalStateException("not initialized");
-        }
-
-        return store.computeId(data);
-    }
 }

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionStore.java?rev=1125586&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionStore.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionStore.java
Fri May 20 22:49:56 2011
@@ -0,0 +1,27 @@
+/*
+ * 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.mk.store;
+
+/**
+ *
+ */
+public interface RevisionStore extends DataStore {
+
+    String /* id */ getHeadRevision() throws Exception;
+
+    void setHeadRevision(String revId) throws Exception;
+}

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java?rev=1125586&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java
Fri May 20 22:49:56 2011
@@ -0,0 +1,129 @@
+/*
+ * 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.mk.store;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.security.MessageDigest;
+
+/**
+ *
+ */
+public class SimpleRevisionStore implements RevisionStore {
+
+    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
+
+    boolean initialized;
+    File homeDir;
+    File dataDir;
+    LockableFileRevision headRev;
+    MessageDigest digest;
+
+    public SimpleRevisionStore() throws Exception {
+        initialized = false;
+        digest = MessageDigest.getInstance("SHA-1");
+    }
+
+    public void initialize(File homeDir) throws Exception {
+        if (initialized) {
+            throw new IllegalStateException("already initialized");
+        }
+
+        homeDir = new File(homeDir, ".mk");
+        if (! homeDir.exists()) {
+            homeDir.mkdirs();
+        }
+        headRev = new LockableFileRevision(new File(homeDir, "HEAD"));
+        dataDir = new File(homeDir, "data");
+        if (! dataDir.exists()) {
+            dataDir.mkdir();
+        }
+
+        initialized = true;
+    }
+
+    public void close() {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+
+        digest = null;
+        initialized = false;
+    }
+
+    public String getHeadRevision() throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+
+        return headRev.get();
+    }
+
+    public void setHeadRevision(String revId) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+
+        headRev.set(revId);
+    }
+
+    public InputStream get(String id) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+        StringBuffer buf = new StringBuffer(id.substring(0, 3));
+        buf.append('/');
+        buf.append(id.substring(2));
+        File f = new File(dataDir, buf.toString());
+        return new BufferedInputStream(new FileInputStream(f));
+    }
+
+    public String put(InputStream in) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+        // todo implement
+        return null;  //To change body of implemented methods use File | Settings | File
Templates.
+    }
+
+    public String computeId(InputStream in) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+
+        digest.reset();
+        int read;
+        byte[] data = new byte[8192];
+        try {
+            while ((read = in.read(data, 0, data.length)) != -1) {
+                digest.update(data, 0, read);
+            }
+        } finally {
+            in.close();
+        }
+        byte[] bytes = digest.digest();
+
+        char[] chars = new char[2 * bytes.length];
+        for (int i = 0; i < bytes.length; i++) {
+            chars[2 * i] = HEX_CHARS[(bytes[i] & 0xF0) >>> 4];
+            chars[2 * i + 1] = HEX_CHARS[bytes[i] & 0x0F];
+        }
+        return new String(chars);
+    }
+}

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java?rev=1125586&r1=1125585&r2=1125586&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/ConcurrentWriteTest.java
Fri May 20 22:49:56 2011
@@ -76,12 +76,12 @@ public class ConcurrentWriteTest extends
 
         String history = mk.getRevisions(t0, -1);
         System.out.println("History:");
-        //System.out.println(history);
+        System.out.println(history);
         System.out.println();
 
         String journal = mk.getJournal(oldHead, head);
         System.out.println("Journal:");
-        System.out.println(journal);
+        //System.out.println(journal);
         System.out.println();
 
     }



Mime
View raw message