jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1238711 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/
Date Tue, 31 Jan 2012 17:44:54 GMT
Author: stefan
Date: Tue Jan 31 17:44:54 2012
New Revision: 1238711

URL: http://svn.apache.org/viewvc?rev=1238711&view=rev
Log:
refactoring serialization code

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/AbstractPersistenceManager.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BDbPersistenceManager.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/FSPersistenceManager.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2PersistenceManager.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/InMemPersistenceManager.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MongoPersistenceManager.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/AbstractPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/AbstractPersistenceManager.java?rev=1238711&r1=1238710&r2=1238711&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/AbstractPersistenceManager.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/AbstractPersistenceManager.java
Tue Jan 31 17:44:54 2012
@@ -256,6 +256,6 @@ public abstract class AbstractPersistenc
     
     public byte[] toBytes(Node node, ByteArrayOutputStream out) throws Exception {
         node.serialize(new BinaryBinding(out));
-        return idFactory.createNodeId(node, out.toByteArray());
+        return idFactory.createContentId(out.toByteArray());
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BDbPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BDbPersistenceManager.java?rev=1238711&r1=1238710&r2=1238711&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BDbPersistenceManager.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BDbPersistenceManager.java
Tue Jan 31 17:44:54 2012
@@ -141,7 +141,7 @@ public class BDbPersistenceManager exten
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         node.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] rawId = idFactory.createNodeId(node, bytes);
+        byte[] rawId = idFactory.createContentId(bytes);
         return persist(bytes, rawId);
     }
 
@@ -163,7 +163,7 @@ public class BDbPersistenceManager exten
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         commit.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] rawId = idFactory.createRevisionId(commit, bytes);
+        byte[] rawId = idFactory.createCommitId(bytes);
         return persist(bytes, rawId);
     }
 
@@ -184,14 +184,24 @@ public class BDbPersistenceManager exten
 
     @Override
     protected ChildNodeEntriesBucket readCNEBucket(String id) throws NotFoundException, Exception
{
-        // todo implement
-        return null;
+        DatabaseEntry key = new DatabaseEntry(StringUtils.convertHexToBytes(id));
+        DatabaseEntry data = new DatabaseEntry();
+
+        if (db.get(null, key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
+            ByteArrayInputStream in = new ByteArrayInputStream(data.getData());
+            return ChildNodeEntriesBucket.deserialize(id, new BinaryBinding(in));
+        } else {
+            throw new NotFoundException(id);
+        }
     }
 
     @Override
     protected String writeCNEBucket(ChildNodeEntriesBucket bucket) throws Exception {
-        // todo implement
-        return null;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        bucket.serialize(new BinaryBinding(out));
+        byte[] bytes = out.toByteArray();
+        byte[] rawId = idFactory.createContentId(bytes);
+        return persist(bytes, rawId);
     }
 
     //-------------------------------------------------------< implementation >

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/FSPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/FSPersistenceManager.java?rev=1238711&r1=1238710&r2=1238711&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/FSPersistenceManager.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/FSPersistenceManager.java
Tue Jan 31 17:44:54 2012
@@ -102,7 +102,7 @@ public class FSPersistenceManager extend
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         node.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] rawId = idFactory.createNodeId(node, bytes);
+        byte[] rawId = idFactory.createContentId(bytes);
         return writeFile(bytes, rawId);
     }
 
@@ -126,7 +126,7 @@ public class FSPersistenceManager extend
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         commit.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] rawId = idFactory.createRevisionId(commit, bytes);
+        byte[] rawId = idFactory.createCommitId(bytes);
         return writeFile(bytes, rawId);
     }
 
@@ -147,14 +147,26 @@ public class FSPersistenceManager extend
 
     @Override
     protected ChildNodeEntriesBucket readCNEBucket(String id) throws NotFoundException, Exception
{
-        // todo implement
-        return null;
+        File f = getFile(id);
+        if (f.exists()) {
+            BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
+            try {
+                return ChildNodeEntriesBucket.deserialize(id, new BinaryBinding(in));
+            } finally {
+                in.close();
+            }
+        } else {
+            throw new NotFoundException(id);
+        }
     }
 
     @Override
     protected String writeCNEBucket(ChildNodeEntriesBucket bucket) throws Exception {
-        // todo implement
-        return null;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        bucket.serialize(new BinaryBinding(out));
+        byte[] bytes = out.toByteArray();
+        byte[] rawId = idFactory.createContentId(bytes);
+        return writeFile(bytes, rawId);
     }
 
     //-------------------------------------------------------< implementation >

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2PersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2PersistenceManager.java?rev=1238711&r1=1238710&r2=1238711&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2PersistenceManager.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/H2PersistenceManager.java
Tue Jan 31 17:44:54 2012
@@ -17,7 +17,6 @@
 package org.apache.jackrabbit.mk.store;
 
 import org.apache.jackrabbit.mk.blobs.BlobStore;
-import org.apache.jackrabbit.mk.blobs.DbBlobStore;
 import org.apache.jackrabbit.mk.blobs.FileBlobStore;
 import org.apache.jackrabbit.mk.model.ChildNodeEntriesBucket;
 import org.apache.jackrabbit.mk.model.Commit;
@@ -139,7 +138,7 @@ public class H2PersistenceManager extend
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         node.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] rawId = idFactory.createNodeId(node, bytes);
+        byte[] rawId = idFactory.createContentId(bytes);
         String id = StringUtils.convertBytesToHex(rawId);
 
         Connection con = cp.getConnection();
@@ -188,7 +187,7 @@ public class H2PersistenceManager extend
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         commit.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] rawId = idFactory.createRevisionId(commit, bytes);
+        byte[] rawId = idFactory.createCommitId(bytes);
         String id = StringUtils.convertBytesToHex(rawId);
 
         Connection con = cp.getConnection();
@@ -227,13 +226,50 @@ public class H2PersistenceManager extend
 
     @Override
     protected ChildNodeEntriesBucket readCNEBucket(String id) throws NotFoundException, Exception
{
-        // todo implement
-        return null;
+        Connection con = cp.getConnection();
+        try {
+            PreparedStatement stmt = con.prepareStatement("select DATA from REVS where ID
= ?");
+            try {
+                stmt.setBytes(1, StringUtils.convertHexToBytes(id));
+                ResultSet rs = stmt.executeQuery();
+                if (rs.next()) {
+                    ByteArrayInputStream in = new ByteArrayInputStream(rs.getBytes(1));
+                    return ChildNodeEntriesBucket.deserialize(id, new BinaryBinding(in));
+                } else {
+                    throw new NotFoundException(id);
+                }
+            } finally {
+                stmt.close();
+            }
+        } finally {
+            con.close();
+        }
     }
 
     @Override
     protected String writeCNEBucket(ChildNodeEntriesBucket bucket) throws Exception {
-        // todo implement
-        return null;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        bucket.serialize(new BinaryBinding(out));
+        byte[] bytes = out.toByteArray();
+        byte[] rawId = idFactory.createContentId(bytes);
+        String id = StringUtils.convertBytesToHex(rawId);
+
+        Connection con = cp.getConnection();
+        try {
+            PreparedStatement stmt = con
+                    .prepareStatement(
+                            "insert into REVS (ID, DATA) select ?, ? where not exists (select
1 from revs where ID = ?)");
+            try {
+                stmt.setBytes(1, rawId);
+                stmt.setBytes(2, bytes);
+                stmt.setBytes(3, rawId);
+                stmt.executeUpdate();
+            } finally {
+                stmt.close();
+            }
+        } finally {
+            con.close();
+        }
+        return id;
     }
 }
\ No newline at end of file

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java?rev=1238711&r1=1238710&r2=1238711&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
Tue Jan 31 17:44:54 2012
@@ -24,43 +24,33 @@ import org.apache.jackrabbit.mk.model.No
 import org.apache.jackrabbit.mk.util.AscendingClock;
 
 /**
- * Create new revision and node ids based on commit data.
+ * Create new commit and internal content object ids based on serialized data.
  */
 abstract class IdFactory {
 
     /**
-     * Create a new node id for some node.
-     * 
-     * @param node node
-     * @param serialized serialized data 
-     * @return raw node id as byte array
-     * @throws Exception if an error occurs
-     */
-    public byte[] createNodeId(Node node, byte[] serialized) throws Exception {
-        return digest(serialized);
-    }
-
-    /**
-     * Create a new bucket id for some bucket.
+     * Creates a new id based on the specified serialized data.
+     * <p/>
+     * The general contract of <code>createContentId</code> is:
+     * <p/>
+     * <code>createId(data1).equals(createId(data2)) == Arrays.equals(data1, data2)</code>
      *
-     * @param bucket bucket
      * @param serialized serialized data
-     * @return raw bucket id as byte array
+     * @return raw node id as byte array
      * @throws Exception if an error occurs
      */
-    public byte[] createBucketId(ChildNodeEntriesBucket bucket, byte[] serialized) throws
Exception {
+    public byte[] createContentId(byte[] serialized) throws Exception {
         return digest(serialized);
     }
 
     /**
-     * Create a new revision id for some commit.
+     * Create a new id for some commit.
      * 
-     * @param commit commit
-     * @param serialized serialized data 
+     * @param serialized serialized data
      * @return raw revision id as byte array
      * @throws Exception if an error occurs
      */
-    public abstract byte[] createRevisionId(Commit commit, byte[] serialized)
+    public abstract byte[] createCommitId(byte[] serialized)
             throws Exception;
     
     /**
@@ -98,7 +88,7 @@ abstract class IdFactory {
     public static IdFactory getDigestFactory() {
         return new IdFactory() {
             @Override
-            public byte[] createRevisionId(Commit commit, byte[] serialized)
+            public byte[] createCommitId(byte[] serialized)
                     throws Exception {
                 
                 return digest(serialized);
@@ -118,7 +108,7 @@ abstract class IdFactory {
             private final AscendingClock clock = new AscendingClock(System.currentTimeMillis());
             
             @Override
-            public byte[] createRevisionId(Commit commit, byte[] serialized)
+            public byte[] createCommitId(byte[] serialized)
                     throws Exception {
                 
                 return longToBytes(clock.time());

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/InMemPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/InMemPersistenceManager.java?rev=1238711&r1=1238710&r2=1238711&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/InMemPersistenceManager.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/InMemPersistenceManager.java
Tue Jan 31 17:44:54 2012
@@ -41,6 +41,7 @@ public class InMemPersistenceManager ext
 
     private final Map<String, StoredNode> nodes = Collections.synchronizedMap(new HashMap<String,
StoredNode>());
     private final Map<String, StoredCommit> commits = Collections.synchronizedMap(new
HashMap<String, StoredCommit>());
+    private final Map<String, ChildNodeEntriesBucket> buckets = Collections.synchronizedMap(new
HashMap<String, ChildNodeEntriesBucket>());
     private final BlobStore blobs = new MemoryBlobStore();
 
     private String head;
@@ -79,7 +80,7 @@ public class InMemPersistenceManager ext
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         node.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        String id = StringUtils.convertBytesToHex(idFactory.createNodeId(node, bytes));
+        String id = StringUtils.convertBytesToHex(idFactory.createContentId(bytes));
 
         if (!nodes.containsKey(id)) {
             nodes.put(id, StoredNode.deserialize(id, new BinaryBinding(new ByteArrayInputStream(bytes))));
@@ -103,7 +104,7 @@ public class InMemPersistenceManager ext
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         commit.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        String id = StringUtils.convertBytesToHex(idFactory.createRevisionId(commit, bytes));
+        String id = StringUtils.convertBytesToHex(idFactory.createCommitId(bytes));
 
         if (!commits.containsKey(id)) {
             commits.put(id, StoredCommit.deserialize(id, new BinaryBinding(new ByteArrayInputStream(bytes))));
@@ -129,13 +130,25 @@ public class InMemPersistenceManager ext
 
     @Override
     protected ChildNodeEntriesBucket readCNEBucket(String id) throws NotFoundException, Exception
{
-        // todo implement
-        return null;
+        ChildNodeEntriesBucket bucket = buckets.get(id);
+        if (bucket != null) {
+            return bucket;
+        } else {
+            throw new NotFoundException(id);
+        }
     }
 
     @Override
     protected String writeCNEBucket(ChildNodeEntriesBucket bucket) throws Exception {
-        // todo implement
-        return null;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        bucket.serialize(new BinaryBinding(out));
+        byte[] bytes = out.toByteArray();
+        String id = StringUtils.convertBytesToHex(idFactory.createContentId(bytes));
+
+        if (!buckets.containsKey(id)) {
+            buckets.put(id, ChildNodeEntriesBucket.deserialize(id, new BinaryBinding(new
ByteArrayInputStream(bytes))));
+        }
+
+        return id;
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MongoPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MongoPersistenceManager.java?rev=1238711&r1=1238710&r2=1238711&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MongoPersistenceManager.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/MongoPersistenceManager.java
Tue Jan 31 17:44:54 2012
@@ -166,7 +166,7 @@ public class MongoPersistenceManager ext
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         node.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] key = idFactory.createNodeId(node, bytes);
+        byte[] key = idFactory.createContentId(bytes);
         String id = StringUtils.convertBytesToHex(key);
 
         // todo support partitioned child node lists
@@ -223,7 +223,7 @@ public class MongoPersistenceManager ext
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         commit.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] key = idFactory.createRevisionId(commit, bytes);
+        byte[] key = idFactory.createCommitId(bytes);
         String id = StringUtils.convertBytesToHex(key);
 
         BasicDBObject commitObject;
@@ -313,7 +313,7 @@ public class MongoPersistenceManager ext
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         bucket.serialize(new BinaryBinding(out));
         byte[] bytes = out.toByteArray();
-        byte[] key = idFactory.createBucketId(bucket, bytes);
+        byte[] key = idFactory.createContentId(bytes);
         String id = StringUtils.convertBytesToHex(key);
 
         BasicDBObject bucketObject;



Mime
View raw message