jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1135556 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/store/ test/java/org/apache/jackrabbit/mk/
Date Tue, 14 Jun 2011 13:18:38 GMT
Author: thomasm
Date: Tue Jun 14 13:18:38 2011
New Revision: 1135556

URL: http://svn.apache.org/viewvc?rev=1135556&view=rev
Log:
Simple persistent data store implementation.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/ObjectStore.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
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java

Modified: 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=1135556&r1=1135555&r2=1135556&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2RevisionStore.java
Tue Jun 14 13:18:38 2011
@@ -16,12 +16,14 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import org.apache.jackrabbit.mk.api.MicroKernelException;
 import org.apache.jackrabbit.mk.util.StringUtils;
 import org.h2.jdbcx.JdbcConnectionPool;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.InputStream;
+import java.security.DigestInputStream;
 import java.security.DigestOutputStream;
 import java.security.MessageDigest;
 import java.sql.Connection;
@@ -62,6 +64,8 @@ public class H2RevisionStore extends Sim
             Statement stmt = con.createStatement();
             stmt.execute("create table if not exists REVS (ID binary primary key, DATA binary)");
             stmt.execute("create table if not exists head(id varchar) as select ''");
+            stmt.execute("create sequence if not exists datastore_id");
+            stmt.execute("create table if not exists datastore(id varchar primary key, data
blob)");
         } finally {
             con.close();
         }
@@ -178,4 +182,104 @@ public class H2RevisionStore extends Sim
         return id;
     }
 
+    @Override
+    public String writeBlob(InputStream in) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+        Connection con = cp.getConnection();
+        String id;
+        try {
+            String tempId;
+            PreparedStatement stmt = con.prepareStatement(
+                    "insert into datastore(id, data) values(datastore_id.nextval, ?)");
+            try {
+                MessageDigest digest = MessageDigest.getInstance("SHA-1");
+                DigestInputStream din = new DigestInputStream(in, digest);
+                stmt.setBinaryStream(1, din);
+                stmt.executeUpdate();
+                ResultSet rs = stmt.getGeneratedKeys();
+                rs.next();
+                tempId = rs.getString(1);
+                id = "ds:" + StringUtils.convertBytesToHex(digest.digest());
+            } finally {
+                stmt.close();
+            }
+            stmt = con.prepareStatement(
+                    "update datastore set id = ? where id = ? and not exists(select * from
datastore where id = ?)");
+            try {
+                stmt.setString(1, id);
+                stmt.setString(2, tempId);
+                stmt.setString(3, id);
+                int count = stmt.executeUpdate();
+                if (count == 0) {
+                    stmt.close();
+                    stmt = con.prepareStatement(
+                            "delete from datastore where id = ?");
+                    stmt.setString(1, tempId);
+                    stmt.executeUpdate();
+                }
+            } finally {
+                stmt.close();
+            }
+        } finally {
+            con.close();
+        }
+        return id;
+    }
+
+    @Override
+    public int readBlob(String blobId, long pos, byte[] buff, int off, int length) throws
Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+        Connection con = cp.getConnection();
+        try {
+            final PreparedStatement stmt = con.prepareStatement(
+                    "select data from datastore where id = ?");
+            try {
+                stmt.setString(1, blobId);
+                ResultSet rs = stmt.executeQuery();
+                if (!rs.next()) {
+                    throw new MicroKernelException("Blob not found: " + blobId);
+                }
+                InputStream in = rs.getBinaryStream(1);
+                in.skip(pos);
+                int len = in.read(buff, off, length);
+                rs.close();
+                return len;
+            } finally {
+                stmt.close();
+            }
+        } finally {
+            con.close();
+        }
+    }
+
+    @Override
+    public long getBlobLength(String blobId) throws Exception {
+        if (!initialized) {
+            throw new IllegalStateException("not initialized");
+        }
+        Connection con = cp.getConnection();
+        try {
+            final PreparedStatement stmt = con.prepareStatement(
+                    "select length(data) from datastore where id = ?");
+            try {
+                stmt.setString(1, blobId);
+                ResultSet rs = stmt.executeQuery();
+                if (!rs.next()) {
+                    throw new MicroKernelException("Blob not found: " + blobId);
+                }
+                long length = rs.getLong(1);
+                rs.close();
+                return length;
+            } finally {
+                stmt.close();
+            }
+        } finally {
+            con.close();
+        }
+    }
+
 }

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=1135556&r1=1135555&r2=1135556&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
Tue Jun 14 13:18:38 2011
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.mk.store;
 
 import java.io.File;
+import java.io.InputStream;
 import java.util.concurrent.locks.ReentrantLock;
 
 /**
@@ -114,4 +115,17 @@ public class ObjectStore implements Cons
     public void unlockHead() {
         headLock.unlock();
     }
+
+    public String writeBlob(InputStream in) throws Exception {
+        return store.writeBlob(in);
+    }
+
+    public int readBlob(String blobId, long pos, byte[] buff, int off, int length) throws
Exception {
+        return store.readBlob(blobId, pos, buff, off, length);
+    }
+
+    public long getBlobLength(String blobId) throws Exception {
+        return store.getBlobLength(blobId);
+    }
+
 }

Modified: 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=1135556&r1=1135555&r2=1135556&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/RevisionStore.java
Tue Jun 14 13:18:38 2011
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import java.io.InputStream;
+
 /**
  *
  */
@@ -24,4 +26,11 @@ public interface RevisionStore extends D
     String /* id */ getHeadRevision() throws Exception;
 
     void setHeadRevision(String revId) throws Exception;
+
+    String writeBlob(InputStream in) throws Exception;
+
+    int readBlob(String blobId, long pos, byte[] buff, int off, int length) throws Exception;
+
+    long getBlobLength(String blobId) throws Exception;
+
 }

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=1135556&r1=1135555&r2=1135556&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
Tue Jun 14 13:18:38 2011
@@ -164,4 +164,16 @@ public class SimpleRevisionStore impleme
         return new File(dataDir, buf.toString());
     }
 
+    public String writeBlob(InputStream in) throws Exception {
+        throw new UnsupportedOperationException();
+    }
+
+    public int readBlob(String blobId, long pos, byte[] buff, int off, int length) throws
Exception {
+        throw new UnsupportedOperationException();
+    }
+
+    public long getBlobLength(String blobId) throws Exception {
+        throw new UnsupportedOperationException();
+    }
+
 }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java?rev=1135556&r1=1135555&r2=1135556&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java
Tue Jun 14 13:18:38 2011
@@ -44,6 +44,7 @@ public class DataStoreTest extends TestC
         String[] s = new String[len];
         for (int i = 0; i < len; i++) {
             s[i] = mk.write(new ByteArrayInputStream(new byte[i]));
+            s[i] = mk.write(new ByteArrayInputStream(new byte[i]));
         }
         for (int i = 0; i < len; i++) {
             assertEquals(i, mk.getLength(s[i]));



Mime
View raw message