jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1127890 - in /jackrabbit/sandbox/microkernel: pom.xml src/main/java/org/apache/jackrabbit/mk/Repository.java src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java
Date Thu, 26 May 2011 12:38:30 GMT
Author: stefan
Date: Thu May 26 12:38:30 2011
New Revision: 1127890

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

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
Modified:
    jackrabbit/sandbox/microkernel/pom.xml
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.java

Modified: jackrabbit/sandbox/microkernel/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/pom.xml?rev=1127890&r1=1127889&r2=1127890&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/pom.xml (original)
+++ jackrabbit/sandbox/microkernel/pom.xml Thu May 26 12:38:30 2011
@@ -37,6 +37,12 @@
             <version>1.1</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+            <version>1.3.154</version>
+        </dependency>
+
         <!--Test Dependencies-->
         <dependency>
            <groupId>junit</groupId>

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java?rev=1127890&r1=1127889&r2=1127890&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java
Thu May 26 12:38:30 2011
@@ -123,8 +123,12 @@ public class Repository {
         Node parent = store.getNode(ids[0]);
         // traverse path and store id of each element
         for (int i = 0; i < names.length; i++) {
-            ids[i + 1] = parent.getChildNodeEntries().get(names[i]);
-            parent = store.getNode(ids[i + 1]);
+            String id = parent.getChildNodeEntries().get(names[i]);
+            if (id == null) {
+                throw new Exception("path not found: " + path);
+            }
+            ids[i + 1] = id;
+            parent = store.getNode(id);
         }
         return ids;
     }

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java?rev=1127890&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
Thu May 26 12:38:30 2011
@@ -0,0 +1,163 @@
+/*
+ * 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 org.h2.jdbcx.JdbcConnectionPool;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.security.DigestOutputStream;
+import java.security.MessageDigest;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ *
+ */
+public class H2RevisionStore extends SimpleRevisionStore {
+
+    JdbcConnectionPool cp;
+
+    public H2RevisionStore() throws Exception {
+        initialized = false;
+    }
+
+    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 PersistentId(new File(homeDir, "HEAD"));
+        dataDir = new File(homeDir, "db");
+        if (!dataDir.exists()) {
+            dataDir.mkdir();
+        }
+
+        Class.forName("org.h2.Driver");
+        cp = JdbcConnectionPool.create("jdbc:h2:" + dataDir.getCanonicalPath() + "/revs",
"sa", "");
+        Connection con = cp.getConnection();
+        try {
+            ResultSet rs = con.getMetaData().getTables(null, null, "REVS", null);
+            if (!rs.next()) {
+                Statement stmt = con.createStatement();
+                stmt.execute("create table REVS (ID binary primary key, DATA binary)");
+                stmt.close();
+            }
+        } finally {
+            con.close();
+        }
+
+        initialized = true;
+    }
+
+    public void close() {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+
+        cp.dispose();
+
+        initialized = false;
+    }
+
+    public InputStream get(String id) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+
+        Connection con = cp.getConnection();
+        try {
+            PreparedStatement stmt = con.prepareStatement("select DATA from REVS where ID
= ?");
+            stmt.setBytes(1, hexToRaw(id));
+            ResultSet rs = stmt.executeQuery();
+            if (rs.next()) {
+                return new ByteArrayInputStream(rs.getBytes(1));
+            } else {
+                throw new Exception("not found: " + id);
+            }
+        } finally {
+            con.close();
+        }
+    }
+
+    public String put(InputStream in) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+
+        MessageDigest digest = MessageDigest.getInstance("SHA-1");
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        DigestOutputStream dos = new DigestOutputStream(baos, 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();
+            //baos.close();
+            in.close();
+        }
+
+        byte[] rawId = digest.digest();
+        String id = toHex(rawId);
+        Connection con = cp.getConnection();
+        try {
+            PreparedStatement stmt = con.prepareStatement("insert into REVS (ID, DATA) values
(?, ?)");
+            stmt.setBytes(1, rawId);
+            stmt.setBytes(2, baos.toByteArray());
+            stmt.executeUpdate();
+        } catch (SQLException e) {
+            if (e.getSQLState().equals("23505")) {
+                // duplicate key, ignore
+                return id;
+            } else {
+                // re-throw
+                throw e;
+            }
+        } finally {
+            con.close();
+        }
+        return id;
+    }
+
+    static byte[] hexToRaw(String hex) {
+        char[] chars = hex.toCharArray();
+        int len = chars.length;
+
+        byte[] raw = new byte[len / 2];
+        for (int i = 0; i < raw.length; i++) {
+            int n = Character.digit(chars[i * 2], 16) << 4;
+            n = n | Character.digit(chars[(i * 2) + 1], 16);
+            raw[i] = (byte) (n & 0xFF);
+        }
+
+        return raw;
+    }
+}

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=1127890&r1=1127889&r2=1127890&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
Thu May 26 12:38:30 2011
@@ -37,7 +37,8 @@ public class ObjectStore implements Cons
             throw new IllegalStateException("already initialized");
         }
 
-        store = new SimpleRevisionStore();
+        //store = new SimpleRevisionStore();
+        store = new H2RevisionStore();
         store.initialize(homeDir);
 
         // make sure we've got a HEAD commit



Mime
View raw message