jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1239234 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk: Repository.java store/MongoPersistenceManager.java
Date Wed, 01 Feb 2012 17:49:54 GMT
Author: stefan
Date: Wed Feb  1 17:49:54 2012
New Revision: 1239234

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

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.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/Repository.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/Repository.java?rev=1239234&r1=1239233&r2=1239234&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
Wed Feb  1 17:49:54 2012
@@ -51,9 +51,9 @@ public class Repository {
         }
         this.homeDir = home.getCanonicalPath();
 
-        pm = new H2PersistenceManager();
+        //pm = new H2PersistenceManager();
         //pm = new InMemPersistenceManager();
-        //pm = new MongoPersistenceManager();
+        pm = new MongoPersistenceManager();
         //pm = new BDbPersistenceManager();
         //pm = new FSPersistenceManager();
 

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=1239234&r1=1239233&r2=1239234&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
Wed Feb  1 17:49:54 2012
@@ -130,31 +130,14 @@ public class MongoPersistenceManager ext
         } else {
             key.put(ID_FIELD, id);
         }
-        final DBObject nodeObject = nodes.findOne(key);
+        final BasicDBObject nodeObject = (BasicDBObject) nodes.findOne(key);
         if (nodeObject != null) {
             // todo support partitioned child node lists
             if (BINARY_FORMAT) {
                 byte[] bytes = (byte[]) nodeObject.get(DATA_FIELD);
                 return StoredNode.deserialize(id, new BinaryBinding(new ByteArrayInputStream(bytes)));
             } else {
-                final Iterator<String> it = nodeObject.keySet().iterator();
-                final BasicDBObject children = (BasicDBObject) nodeObject.get(CHILDREN_OBJECT);
-                Iterator<ChildNodeEntry> cneIt = new Iterator<ChildNodeEntry>()
{
-                    public boolean hasNext() {
-                        return it.hasNext();
-                    }
-
-                    public ChildNodeEntry next() {
-                        String name = it.next();
-                        return new ChildNodeEntry(decodeName(name), (String) children.get(name));
-                    }
-
-                    public void remove() {
-                        throw new UnsupportedOperationException();
-                    }
-                };
-                return new StoredNode(id,
-                        decodeKeys(((BasicDBObject) nodeObject.get(PROPERTIES_OBJECT)).toMap()),
cneIt);
+                return StoredNode.deserialize(id, new DBObjectBinding(nodeObject));
             }
         } else {
             throw new NotFoundException(id);
@@ -174,15 +157,8 @@ public class MongoPersistenceManager ext
         if (BINARY_FORMAT) {
             nodeObject = new BasicDBObject(ID_FIELD, key).append(DATA_FIELD, bytes);
         } else {
-            Iterator<ChildNodeEntry> it = node.getChildNodeEntries(0, -1);
-            BasicDBObject children = new BasicDBObject();
-            while (it.hasNext()) {
-                ChildNodeEntry cne = it.next();
-                children.put(encodeName(cne.getName()), cne.getId());
-            }
-            nodeObject = new BasicDBObject(ID_FIELD, id)
-                    .append(PROPERTIES_OBJECT, new BasicDBObject(encodeKeys(node.getProperties())))
-                    .append(CHILDREN_OBJECT, children);
+            nodeObject = new BasicDBObject(ID_FIELD, id);
+            node.serialize(new DBObjectBinding(nodeObject));
         }
         try {
             nodes.insert(nodeObject);
@@ -201,17 +177,13 @@ public class MongoPersistenceManager ext
         } else {
             key.put(ID_FIELD, id);
         }
-        DBObject commitObject = commits.findOne(key);
+        BasicDBObject commitObject = (BasicDBObject) commits.findOne(key);
         if (commitObject != null) {
             if (BINARY_FORMAT) {
                 byte[] bytes = (byte[]) commitObject.get(DATA_FIELD);
                 return StoredCommit.deserialize(id, new BinaryBinding(new ByteArrayInputStream(bytes)));
             } else {
-                return new StoredCommit(id,
-                        (String) commitObject.get("parentId"),
-                        (Long) commitObject.get("ts"),
-                        (String) commitObject.get("rootNodeId"),
-                        (String) commitObject.get("msg"));
+                return StoredCommit.deserialize(id, new DBObjectBinding(commitObject));
             }
         } else {
             throw new NotFoundException(id);
@@ -230,12 +202,8 @@ public class MongoPersistenceManager ext
         if (BINARY_FORMAT) {
             commitObject = new BasicDBObject(ID_FIELD, key).append(DATA_FIELD, bytes);
         } else {
-            commitObject = new BasicDBObject(ID_FIELD, id)
-                    .append(ID_FIELD, id)
-                    .append("rootNodeId", commit.getRootNodeId())
-                    .append("ts", commit.getCommitTS())
-                    .append("parentId", commit.getParentId())
-                    .append("msg", commit.getMsg());
+            commitObject = new BasicDBObject(ID_FIELD, id);
+            commit.serialize(new DBObjectBinding(commitObject));
         }
         try {
             commits.insert(commitObject);
@@ -289,19 +257,13 @@ public class MongoPersistenceManager ext
         } else {
             key.put(ID_FIELD, id);
         }
-        DBObject bucketObject = buckets.findOne(key);
+        BasicDBObject bucketObject = (BasicDBObject) buckets.findOne(key);
         if (bucketObject != null) {
             if (BINARY_FORMAT) {
                 byte[] bytes = (byte[]) bucketObject.get(DATA_FIELD);
                 return ChildNodeEntriesBucket.deserialize(id, new BinaryBinding(new ByteArrayInputStream(bytes)));
             } else {
-                ChildNodeEntriesBucket bucket = new ChildNodeEntriesBucket();
-                for (String name : bucketObject.keySet()) {
-                    if (!name.equals(ID_FIELD)) {
-                        bucket.add(new ChildNodeEntry(decodeName(name), (String) bucketObject.get(name)));
-                    }
-                }
-                return bucket;
+                return ChildNodeEntriesBucket.deserialize(id, new DBObjectBinding(bucketObject));
             }
         } else {
             throw new NotFoundException(id);
@@ -321,11 +283,7 @@ public class MongoPersistenceManager ext
             bucketObject = new BasicDBObject(ID_FIELD, key).append(DATA_FIELD, bytes);
         } else {
             bucketObject = new BasicDBObject(ID_FIELD, id);
-            Iterator<ChildNodeEntry> it = bucket.getEntries(0, -1);
-            while (it.hasNext()) {
-                ChildNodeEntry cne = it.next();
-                bucketObject.append(encodeName(cne.getName()), cne.getId());
-            }
+            bucket.serialize(new DBObjectBinding(bucketObject));
         }
         try {
             buckets.insert(bucketObject);
@@ -448,4 +406,79 @@ public class MongoPersistenceManager ext
             return name;
         }
     }
+
+    //--------------------------------------------------------< inner classes >
+
+    protected class DBObjectBinding implements Binding {
+
+        BasicDBObject obj;
+
+        protected DBObjectBinding(BasicDBObject obj) {
+            this.obj = obj;
+        }
+
+        @Override
+        public void write(String key, String value) throws Exception {
+            obj.append(encodeName(key), value);
+        }
+
+        @Override
+        public void write(String key, long value) throws Exception {
+            obj.append(encodeName(key), value);
+        }
+
+        @Override
+        public void write(String key, int value) throws Exception {
+            obj.append(encodeName(key), value);
+        }
+
+        @Override
+        public void write(String key, int count, Iterator<KeyValuePair> iterator) throws
Exception {
+            BasicDBObject childObj = new BasicDBObject();
+            while (iterator.hasNext()) {
+                KeyValuePair kvp = iterator.next();
+                childObj.append(encodeName(kvp.getKey()), kvp.getValue());
+            }
+            obj.append(encodeName(key), childObj);
+        }
+
+        @Override
+        public String readStringValue(String key) throws Exception {
+            return obj.getString(encodeName(key));
+        }
+
+        @Override
+        public long readLongValue(String key) throws Exception {
+            return obj.getLong(encodeName(key));
+        }
+
+        @Override
+        public int readIntValue(String key) throws Exception {
+            return obj.getInt(encodeName(key));
+        }
+
+        @Override
+        public Iterator<KeyValuePair> read(String key) throws Exception {
+            final BasicDBObject childObj = (BasicDBObject) obj.get(encodeName(key));
+            final Iterator<String> it = childObj.keySet().iterator();
+            return new Iterator<KeyValuePair>() {
+                @Override
+                public boolean hasNext() {
+                    return it.hasNext();
+                }
+
+                @Override
+                public KeyValuePair next() {
+                    String key = it.next();
+                    return new KeyValuePair(decodeName(key), childObj.getString(key));
+                }
+
+                @Override
+                public void remove() {
+                    throw new UnsupportedOperationException();
+                }
+            };
+        }
+    }
+
 }



Mime
View raw message