jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dpfis...@apache.org
Subject svn commit: r1212468 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/
Date Fri, 09 Dec 2011 15:05:38 GMT
Author: dpfister
Date: Fri Dec  9 15:05:38 2011
New Revision: 1212468

URL: http://svn.apache.org/viewvc?rev=1212468&view=rev
Log:
Make ID generation for commits and nodes overridable

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
  (with props)
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/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=1212468&r1=1212467&r2=1212468&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
Fri Dec  9 15:05:38 2011
@@ -42,6 +42,7 @@ public abstract class AbstractPersistenc
 
     private String headId;
     private final ReentrantReadWriteLock headLock = new ReentrantReadWriteLock();
+    protected IdFactory idFactory = IdFactory.getDigestFactory();
 
     Map<String, Object> cache;
 
@@ -111,7 +112,7 @@ public abstract class AbstractPersistenc
         String val = System.getProperty(CACHE_SIZE);
         return (val != null) ? Integer.parseInt(val) : DEFAULT_CACHE_SIZE;
     }
-
+    
     //--------------------------------------------------------< RevisionStore >
 
     public String putNode(Node node) throws Exception {

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=1212468&r1=1212467&r2=1212468&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
Fri Dec  9 15:05:38 2011
@@ -16,15 +16,9 @@
  */
 package org.apache.jackrabbit.mk.store;
 
-import com.sleepycat.je.Database;
-import com.sleepycat.je.DatabaseConfig;
-import com.sleepycat.je.DatabaseEntry;
-import com.sleepycat.je.Durability;
-import com.sleepycat.je.Environment;
-import com.sleepycat.je.EnvironmentConfig;
-import com.sleepycat.je.EnvironmentMutableConfig;
-import com.sleepycat.je.LockMode;
-import com.sleepycat.je.OperationStatus;
+import java.io.File;
+import java.io.InputStream;
+
 import org.apache.jackrabbit.mk.blobs.BlobStore;
 import org.apache.jackrabbit.mk.blobs.FileBlobStore;
 import org.apache.jackrabbit.mk.model.Commit;
@@ -34,9 +28,15 @@ import org.apache.jackrabbit.mk.model.St
 import org.apache.jackrabbit.mk.store.util.Serializer;
 import org.apache.jackrabbit.mk.util.StringUtils;
 
-import java.io.File;
-import java.io.InputStream;
-import java.security.MessageDigest;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseConfig;
+import com.sleepycat.je.DatabaseEntry;
+import com.sleepycat.je.Durability;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.EnvironmentConfig;
+import com.sleepycat.je.EnvironmentMutableConfig;
+import com.sleepycat.je.LockMode;
+import com.sleepycat.je.OperationStatus;
 
 /**
  *
@@ -135,7 +135,9 @@ public class BDbPersistenceManager exten
 
     @Override
     protected String writeNode(Node node) throws Exception {
-        return persist(Serializer.toBytes(node));
+        byte[] data = Serializer.toBytes(node);
+        byte[] rawId = idFactory.createNodeId(node, data);
+        return persist(data, rawId);
     }
 
     @Override
@@ -152,7 +154,9 @@ public class BDbPersistenceManager exten
 
     @Override
     protected String writeCommit(Commit commit) throws Exception {
-        return persist(Serializer.toBytes(commit));
+        byte[] data = Serializer.toBytes(commit);
+        byte[] rawId = idFactory.createRevisionId(commit, data);
+        return persist(data, rawId);
     }
 
     @Override
@@ -170,8 +174,7 @@ public class BDbPersistenceManager exten
         return blobStore.getBlobLength(blobId);
     }
 
-    protected String persist(byte[] bytes) throws Exception {
-        byte[] rawId = MessageDigest.getInstance("SHA-1").digest(bytes);
+    protected String persist(byte[] bytes, byte[] rawId) throws Exception {
         String id = StringUtils.convertBytesToHex(rawId);
 
         DatabaseEntry key = new DatabaseEntry(rawId);

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=1212468&r1=1212467&r2=1212468&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
Fri Dec  9 15:05:38 2011
@@ -16,6 +16,13 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
 import org.apache.jackrabbit.mk.blobs.BlobStore;
 import org.apache.jackrabbit.mk.blobs.FileBlobStore;
 import org.apache.jackrabbit.mk.model.Commit;
@@ -26,17 +33,6 @@ import org.apache.jackrabbit.mk.store.ut
 import org.apache.jackrabbit.mk.util.IOUtils;
 import org.apache.jackrabbit.mk.util.StringUtils;
 
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.security.DigestOutputStream;
-import java.security.MessageDigest;
-
 /**
  *
  */
@@ -105,7 +101,9 @@ public class FSPersistenceManager extend
 
     @Override
     protected String writeNode(Node node) throws Exception {
-        return writeFile(new ByteArrayInputStream(Serializer.toBytes(node)));
+        byte[] bytes = Serializer.toBytes(node);
+        byte[] rawId = idFactory.createNodeId(node, bytes);
+        return writeFile(bytes, rawId);
     }
 
     @Override
@@ -127,7 +125,9 @@ public class FSPersistenceManager extend
 
     @Override
     protected String writeCommit(Commit commit) throws Exception {
-        return writeFile(new ByteArrayInputStream(Serializer.toBytes(commit)));
+        byte[] bytes = Serializer.toBytes(commit);
+        byte[] rawId = idFactory.createRevisionId(commit, bytes);
+        return writeFile(bytes, rawId);
     }
 
     @Override
@@ -152,31 +152,20 @@ public class FSPersistenceManager extend
         return new File(dataDir, buf.toString());
     }
 
-    private String writeFile(InputStream in) throws Exception {
-        MessageDigest digest = MessageDigest.getInstance("SHA-1");
-
+    private String writeFile(byte[] data, byte[] rawId) throws Exception {
         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);
-                }
+                fos.write(data);
             } finally {
-                dos.flush();
                 //fos.getChannel().force(true);
                 fos.close();
-                in.close();
             }
 
-            String id = StringUtils.convertBytesToHex(digest.digest());
+            String id = StringUtils.convertBytesToHex(rawId);
             File dst = getFile(id);
             if (dst.exists()) {
                 // already exists

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=1212468&r1=1212467&r2=1212468&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
Fri Dec  9 15:05:38 2011
@@ -29,7 +29,6 @@ import org.h2.jdbcx.JdbcConnectionPool;
 
 import java.io.File;
 import java.io.InputStream;
-import java.security.MessageDigest;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -135,7 +134,7 @@ public class H2PersistenceManager extend
     @Override
     protected String writeNode(Node node) throws Exception {
         byte[] bytes = Serializer.toBytes(node);
-        byte[] rawId = MessageDigest.getInstance("SHA-1").digest(bytes);
+        byte[] rawId = idFactory.createNodeId(node, bytes);
         String id = StringUtils.convertBytesToHex(rawId);
 
         Connection con = cp.getConnection();
@@ -177,11 +176,11 @@ public class H2PersistenceManager extend
             con.close();
         }
     }
-
+    
     @Override
     protected String writeCommit(Commit commit) throws Exception {
         byte[] bytes = Serializer.toBytes(commit);
-        byte[] rawId = MessageDigest.getInstance("SHA-1").digest(bytes);
+        byte[] rawId = idFactory.createRevisionId(commit, bytes);
         String id = StringUtils.convertBytesToHex(rawId);
 
         Connection con = cp.getConnection();

Added: 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=1212468&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
Fri Dec  9 15:05:38 2011
@@ -0,0 +1,115 @@
+/*
+ * 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.security.MessageDigest;
+
+import org.apache.jackrabbit.mk.model.Commit;
+import org.apache.jackrabbit.mk.model.Node;
+import org.apache.jackrabbit.mk.util.AscendingClock;
+
+/**
+ * Create new revision and node ids based on commit data.
+ */
+abstract class IdFactory {
+
+    /**
+     * Create a new node id for some node.
+     * 
+     * @param commit commit
+     * @param serialized serialized data 
+     * @return raw revision 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 revision id for some commit.
+     * 
+     * @param commit commit
+     * @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)
+            throws Exception;
+    
+    /**
+     * Return a digest for some data.
+     * 
+     * @param data data
+     * @return digest
+     */
+    protected byte[] digest(byte[] data) throws Exception {
+        return MessageDigest.getInstance("SHA-1").digest(data);
+    }
+    
+    /**
+     * Convert a long value into a fixed-size byte array of size 16.
+     * 
+     * @param value value
+     * @return byte array
+     */
+    static byte[] longToBytes(long value) {
+        byte[] result = new byte[16];
+        
+        for (int i = result.length - 1; i >= 0 && value != 0; i--) {
+            result[i] = (byte) (value & 0xff);
+            value >>>= 8;
+        }
+        return result;
+    }
+    
+    /**
+     * Return the default factory that will create node and revision ids based
+     * on their content. 
+     * 
+     * @return factory
+     */
+    public static IdFactory getDigestFactory() {
+        return new IdFactory() {
+            @Override
+            public byte[] createRevisionId(Commit commit, byte[] serialized)
+                    throws Exception {
+                
+                return digest(serialized);
+            }
+        };
+    }
+    
+    /**
+     * Return a factory that will create node ids based on their content, while
+     * returning strictly ascending revision ids based on the current system time.
+     * 
+     * @return factory
+     */
+    public static IdFactory getAscendingClockFactory() {
+        return new IdFactory() {
+
+            private final AscendingClock clock = new AscendingClock(System.currentTimeMillis());
+            
+            @Override
+            public byte[] createRevisionId(Commit commit, byte[] serialized)
+                    throws Exception {
+                
+                return longToBytes(clock.time());
+            }
+        };
+    };
+}

Propchange: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/IdFactory.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

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=1212468&r1=1212467&r2=1212468&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
Fri Dec  9 15:05:38 2011
@@ -16,6 +16,12 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import java.io.File;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.jackrabbit.mk.blobs.BlobStore;
 import org.apache.jackrabbit.mk.blobs.MemoryBlobStore;
 import org.apache.jackrabbit.mk.model.Commit;
@@ -25,13 +31,6 @@ import org.apache.jackrabbit.mk.model.St
 import org.apache.jackrabbit.mk.store.util.Serializer;
 import org.apache.jackrabbit.mk.util.StringUtils;
 
-import java.io.File;
-import java.io.InputStream;
-import java.security.MessageDigest;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
 /**
  *
  */
@@ -76,7 +75,7 @@ public class InMemPersistenceManager ext
     @Override
     protected String writeNode(Node node) throws Exception {
         byte[] bytes = Serializer.toBytes(node);
-        String id = StringUtils.convertBytesToHex(MessageDigest.getInstance("SHA-1").digest(bytes));
+        String id = StringUtils.convertBytesToHex(idFactory.createNodeId(node, bytes));
 
         if (!nodes.containsKey(id)) {
             nodes.put(id, Serializer.fromBytes(id, bytes, Node.class));
@@ -98,7 +97,7 @@ public class InMemPersistenceManager ext
     @Override
     protected String writeCommit(Commit commit) throws Exception {
         byte[] bytes = Serializer.toBytes(commit);
-        String id = StringUtils.convertBytesToHex(MessageDigest.getInstance("SHA-1").digest(bytes));
+        String id = StringUtils.convertBytesToHex(idFactory.createRevisionId(commit, bytes));
 
         if (!commits.containsKey(id)) {
             commits.put(id, Serializer.fromBytes(id, bytes, Commit.class));

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=1212468&r1=1212467&r2=1212468&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
Fri Dec  9 15:05:38 2011
@@ -16,6 +16,21 @@
  */
 package org.apache.jackrabbit.mk.store;
 
+import java.io.File;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.jackrabbit.mk.model.Commit;
+import org.apache.jackrabbit.mk.model.Node;
+import org.apache.jackrabbit.mk.model.StoredCommit;
+import org.apache.jackrabbit.mk.model.StoredNode;
+import org.apache.jackrabbit.mk.store.util.Serializer;
+import org.apache.jackrabbit.mk.util.IOUtils;
+import org.apache.jackrabbit.mk.util.StringUtils;
+import org.bson.types.ObjectId;
+
 import com.mongodb.BasicDBObject;
 import com.mongodb.DB;
 import com.mongodb.DBCollection;
@@ -26,21 +41,6 @@ import com.mongodb.WriteConcern;
 import com.mongodb.gridfs.GridFS;
 import com.mongodb.gridfs.GridFSDBFile;
 import com.mongodb.gridfs.GridFSInputFile;
-import org.apache.jackrabbit.mk.model.Commit;
-import org.apache.jackrabbit.mk.model.Node;
-import org.apache.jackrabbit.mk.model.StoredCommit;
-import org.apache.jackrabbit.mk.model.StoredNode;
-import org.apache.jackrabbit.mk.store.util.Serializer;
-import org.apache.jackrabbit.mk.util.IOUtils;
-import org.apache.jackrabbit.mk.util.StringUtils;
-import org.bson.types.ObjectId;
-
-import java.io.File;
-import java.io.InputStream;
-import java.security.MessageDigest;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
 
 /**
  *
@@ -137,7 +137,7 @@ public class MongoPersistenceManager ext
     @Override
     protected String writeNode(Node node) throws Exception {
         byte[] bytes = Serializer.toBytes(node);
-        byte[] key = MessageDigest.getInstance("SHA-1").digest(bytes);
+        byte[] key = idFactory.createNodeId(node, bytes);
         String id = StringUtils.convertBytesToHex(key);
 
         BasicDBObject nodeObject;
@@ -185,7 +185,7 @@ public class MongoPersistenceManager ext
     @Override
     protected String writeCommit(Commit commit) throws Exception {
         byte[] bytes = Serializer.toBytes(commit);
-        byte[] key = MessageDigest.getInstance("SHA-1").digest(bytes);
+        byte[] key = idFactory.createRevisionId(commit, bytes);
         String id = StringUtils.convertBytesToHex(key);
 
         BasicDBObject commitObject;



Mime
View raw message