jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1126758 - in /jackrabbit/sandbox/microkernel: ./ src/main/java/org/apache/jackrabbit/mk/store/
Date Mon, 23 May 2011 21:24:24 GMT
Author: stefan
Date: Mon May 23 21:24:24 2011
New Revision: 1126758

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

Removed:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/GitStore.java
Modified:
    jackrabbit/sandbox/microkernel/pom.xml
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java

Modified: jackrabbit/sandbox/microkernel/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/pom.xml?rev=1126758&r1=1126757&r2=1126758&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/pom.xml (original)
+++ jackrabbit/sandbox/microkernel/pom.xml Mon May 23 21:24:24 2011
@@ -29,21 +29,7 @@
     <name>MicroKernel</name>
     <description>Hierarchical MVCC-based persistence store</description>
 
-    <repositories>
-        <repository>
-        <id>jgit-repository</id>
-        <url>http://download.eclipse.org/jgit/maven</url>
-        </repository>
-    </repositories>
-
     <dependencies>
-        <!-- core jgit library -->
-        <dependency>
-            <groupId>org.eclipse.jgit</groupId>
-            <artifactId>org.eclipse.jgit</artifactId>
-            <version>0.10.1</version>
-        </dependency>
-
         <!-- json-simple library -->
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>

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=1126758&r1=1126757&r2=1126758&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
Mon May 23 21:24:24 2011
@@ -37,8 +37,7 @@ public class ObjectStore implements Cons
             throw new IllegalStateException("already initialized");
         }
 
-        store = new GitStore();
-        //store = new SimpleRevisionStore();
+        store = new SimpleRevisionStore();
         store.initialize(homeDir);
 
         // make sure we've got a HEAD commit

Modified: 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=1126758&r1=1126757&r2=1126758&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/SimpleRevisionStore.java
Mon May 23 21:24:24 2011
@@ -19,7 +19,11 @@ package org.apache.jackrabbit.mk.store;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.security.DigestOutputStream;
 import java.security.MessageDigest;
 
 /**
@@ -30,14 +34,11 @@ public class SimpleRevisionStore impleme
     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 {
@@ -63,7 +64,6 @@ public class SimpleRevisionStore impleme
             throw new IllegalStateException("not initialized");
         }
 
-        digest = null;
         initialized = false;
     }
 
@@ -87,19 +87,58 @@ public class SimpleRevisionStore impleme
         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));
+        return new BufferedInputStream(new FileInputStream(getFile(id)));
     }
 
     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.
+
+        MessageDigest digest = MessageDigest.getInstance("SHA-1");
+
+        File tmp = File.createTempFile("tmp", null, dataDir);
+        try {
+            FileOutputStream fos = new FileOutputStream(tmp);
+
+            //OutputStream out = Channels.newOutputStream(fos.getChannel());
+            OutputStream out = fos;
+            DigestOutputStream dos = new DigestOutputStream(out, digest);
+
+            try {
+
+                int read;
+                byte[] buf = new byte[8192];
+                while ((read = in.read(buf, 0, buf.length)) != -1) {
+                    dos.write(buf, 0, read);
+                }
+            } finally {
+                dos.flush();
+                //fos.getChannel().force(true);
+                fos.close();
+                in.close();
+            }
+
+            String id = toHex(digest.digest());
+            File dst = getFile(id);
+            if (dst.exists()) {
+                // already exists
+                return id;
+            }
+            // move tmp file
+            tmp.setReadOnly();
+            if (tmp.renameTo(dst)) {
+                return id;
+            }
+            // make sure parent dir exists and try again
+            dst.getParentFile().mkdir();
+            if (tmp.renameTo(dst)) {
+                return id;
+            }
+            throw new Exception("failed to create " + dst);
+        } finally {
+            tmp.delete();
+        }
     }
 
     public String computeId(InputStream in) throws Exception {
@@ -107,18 +146,27 @@ public class SimpleRevisionStore impleme
             throw new IllegalStateException("not initialized");
         }
 
-        digest.reset();
+        MessageDigest digest = MessageDigest.getInstance("SHA-1");
         int read;
-        byte[] data = new byte[8192];
+        byte[] buf = new byte[8192];
         try {
-            while ((read = in.read(data, 0, data.length)) != -1) {
-                digest.update(data, 0, read);
+            while ((read = in.read(buf, 0, buf.length)) != -1) {
+                digest.update(buf, 0, read);
             }
         } finally {
             in.close();
         }
-        byte[] bytes = digest.digest();
+        return toHex(digest.digest());
+    }
+
+    private File getFile(String id) {
+        StringBuffer buf = new StringBuffer(id.substring(0, 2));
+        buf.append('/');
+        buf.append(id.substring(2));
+        return new File(dataDir, buf.toString());
+    }
 
+    private static String toHex(byte[] bytes) {
         char[] chars = new char[2 * bytes.length];
         for (int i = 0; i < bytes.length; i++) {
             chars[2 * i] = HEX_CHARS[(bytes[i] & 0xF0) >>> 4];



Mime
View raw message