jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r1237333 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk: model/ store/ store/util/
Date Sun, 29 Jan 2012 16:39:11 GMT
Author: stefan
Date: Sun Jan 29 16:39:10 2012
New Revision: 1237333

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

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BinaryBinding.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractCommit.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Commit.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Node.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredCommit.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredNode.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Binding.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/util/Serializer.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractCommit.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractCommit.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractCommit.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractCommit.java
Sun Jan 29 16:39:10 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.mk.model;
 
+import org.apache.jackrabbit.mk.store.Binding;
+
 /**
  *
  */
@@ -58,4 +60,11 @@ public abstract class AbstractCommit imp
     public String getMsg() {
         return msg;
     }
+
+    public void serialize(Binding binding) throws Exception {
+        binding.write("", rootNodeId);
+        binding.write("", commitTS);
+        binding.write("", msg == null ? "" : msg);
+        binding.write("", parentId);
+    }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/AbstractNode.java
Sun Jan 29 16:39:10 2012
@@ -17,8 +17,8 @@
 package org.apache.jackrabbit.mk.model;
 
 import org.apache.jackrabbit.mk.store.Binding;
-import org.apache.jackrabbit.mk.store.RevisionProvider;
 import org.apache.jackrabbit.mk.store.NotFoundException;
+import org.apache.jackrabbit.mk.store.RevisionProvider;
 import org.apache.jackrabbit.mk.util.AbstractRangeIterator;
 import org.apache.jackrabbit.mk.util.EmptyIterator;
 import org.apache.jackrabbit.mk.util.PathUtils;
@@ -207,9 +207,23 @@ public abstract class AbstractNode imple
             }
         }
     }
-    
-    public String serialize(Binding binding) throws Exception {
-        // todo implement
-        throw new Exception("not yet implemented") ;
+
+    public void serialize(Binding binding) throws Exception {
+        binding.write(":props", properties.size(),
+                new AbstractRangeIterator<Binding.KeyValuePair>(properties.keySet().iterator(),
0, -1) {
+                    @Override
+                    protected Binding.KeyValuePair doNext() {
+                        String key = (String) it.next();
+                        return new Binding.KeyValuePair(key, properties.get(key));
+                    }
+                });
+        binding.write(":children", childEntries.getCount(),
+                new AbstractRangeIterator<Binding.KeyValuePair>(childEntries.getEntries(0,
-1), 0, -1) {
+                    @Override
+                    protected Binding.KeyValuePair doNext() {
+                        ChildNodeEntry cne = (ChildNodeEntry) it.next();
+                        return new Binding.KeyValuePair(cne.getName(), cne.getId());
+                    }
+                });
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Commit.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Commit.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Commit.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Commit.java
Sun Jan 29 16:39:10 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.mk.model;
 
+import org.apache.jackrabbit.mk.store.Binding;
+
 /**
  *
  */
@@ -28,4 +30,6 @@ public interface Commit {
     public long getCommitTS();
 
     public String getMsg();
+
+    void serialize(Binding binding) throws Exception;
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Node.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Node.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Node.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/Node.java
Sun Jan 29 16:39:10 2012
@@ -45,5 +45,5 @@ public interface Node {
 
     void diff(Node other, NodeDiffHandler handler);
     
-    String serialize(Binding binding) throws Exception;
+    void serialize(Binding binding) throws Exception;
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredCommit.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredCommit.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredCommit.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredCommit.java
Sun Jan 29 16:39:10 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.mk.model;
 
+import org.apache.jackrabbit.mk.store.Binding;
+
 /**
  *
  */
@@ -23,6 +25,14 @@ public class StoredCommit extends Abstra
 
     private final String id;
 
+    public static StoredCommit deserialize(String id, Binding binding) throws Exception {
+        String rootNodeId = binding.readStringValue("rootNodeId");
+        long commitTS = binding.readLongValue("commitTS");
+        String msg = binding.readStringValue("msg");
+        String parentId = binding.readStringValue("parentId");
+        return new StoredCommit(id, "".equals(parentId) ? null : parentId, commitTS, rootNodeId,
"".equals(msg) ? null : msg);
+    }
+
     public StoredCommit(String id, String parentId, long commitTS, String rootNodeId, String
msg) {
         this.id = id;
         this.parentId = parentId;

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredNode.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredNode.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/model/StoredNode.java
Sun Jan 29 16:39:10 2012
@@ -33,10 +33,24 @@ public class StoredNode extends Abstract
     private final String id;
 
     public static StoredNode deserialize(String id, Binding binding) throws Exception {
-        // todo implement
-        throw new Exception("not yet implemented");
+        StoredNode newInstance = new StoredNode(id);
+        Iterator<Binding.KeyValuePair> iter = binding.read(":props");
+        while (iter.hasNext()) {
+            Binding.KeyValuePair kvp = iter.next();
+            newInstance.properties.put(kvp.getKey(), kvp.getValue());
+        }
+        iter = binding.read(":children");
+        while (iter.hasNext()) {
+            Binding.KeyValuePair kvp = iter.next();
+            newInstance.childEntries.add(new ChildNodeEntry(kvp.getKey(), kvp.getValue()));
+        }
+        return newInstance;
     }
 
+    private StoredNode(String id) {
+        this.id = id;
+    }
+    
     public StoredNode(String id, Map<String, String> properties, Iterator<ChildNodeEntry>
cneIt) {
         this.id = id;
         this.properties.putAll(properties);

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BinaryBinding.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BinaryBinding.java?rev=1237333&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BinaryBinding.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/BinaryBinding.java
Sun Jan 29 16:39:10 2012
@@ -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 org.apache.jackrabbit.mk.util.IOUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ *
+ */
+public class BinaryBinding implements Binding {
+
+    protected InputStream in;
+    protected OutputStream out;
+    
+    public BinaryBinding(InputStream in, OutputStream out) {
+        this.in = in;
+        this.out = out;
+    }
+
+    public void write(String key, String value) throws Exception {
+        IOUtils.writeString(out, key);
+        IOUtils.writeString(out, value);
+    }
+
+    public void write(String key, long value) throws Exception {
+        IOUtils.writeString(out, key);
+        IOUtils.writeVarLong(out, value);
+    }
+
+    public void write(String key, int value) throws Exception {
+        IOUtils.writeString(out, key);
+        IOUtils.writeVarInt(out, value);
+    }
+
+    public void write(String key, int count, Iterator<KeyValuePair> iterator) throws
Exception {
+        IOUtils.writeString(out, key);
+        IOUtils.writeVarInt(out, count);
+        while (iterator.hasNext()) {
+            KeyValuePair kvp = iterator.next();
+            write(kvp.getKey(), kvp.getValue());
+        }
+    }
+
+    public String readStringValue(String key) throws Exception {
+        if (!IOUtils.readString(in).equals(key)) {
+            throw new Exception("incompatible serialization format");
+        }
+        return IOUtils.readString(in);
+    }
+
+    public long readLongValue(String key) throws Exception {
+        if (!IOUtils.readString(in).equals(key)) {
+            throw new Exception("incompatible serialization format");
+        }
+        return IOUtils.readVarLong(in);
+    }
+
+    public int readIntValue(String key) throws Exception {
+        if (!IOUtils.readString(in).equals(key)) {
+            throw new Exception("incompatible serialization format");
+        }
+        return IOUtils.readVarInt(in);
+    }
+
+    public Iterator<KeyValuePair> read(String key) throws Exception {
+        if (!IOUtils.readString(in).equals(key)) {
+            throw new Exception("incompatible serialization format");
+        }
+        final int size = IOUtils.readVarInt(in);
+        return new Iterator<KeyValuePair>() {
+            int count = size;
+
+            public boolean hasNext() {
+                return count > 0;
+            }
+
+            public KeyValuePair next() {
+                if (count-- > 0) {
+                    try {
+                        String key = IOUtils.readString(in);
+                        String value = IOUtils.readString(in);
+                        return new KeyValuePair(key, value);
+                    } catch (IOException e) {
+                        throw new RuntimeException("deserialization failed", e);        
              
+                    }
+                }
+                throw new NoSuchElementException();
+            }
+
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+        };
+    }
+}

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Binding.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Binding.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Binding.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/Binding.java
Sun Jan 29 16:39:10 2012
@@ -16,20 +16,38 @@
  */
 package org.apache.jackrabbit.mk.store;
 
-import java.util.Map;
+import java.util.Iterator;
 
 /**
  *
  */
 public interface Binding {
     
-    void write(String name, String value) throws Exception;
-    void write(String name, long value) throws Exception;
-    void write(String name, int value) throws Exception;
-    void write(String name, Map<String, String> col) throws Exception;
+    void write(String key, String value) throws Exception;
+    void write(String key, long value) throws Exception;
+    void write(String key, int value) throws Exception;
+    void write(String key, int count, Iterator<KeyValuePair> iterator) throws Exception;
     
-    String readString(String name) throws Exception;
-    long readLong(String name) throws Exception;
-    int readInt(String name) throws Exception;
-    void read(String name, Map<String, String> col) throws Exception;
+    String readStringValue(String key) throws Exception;
+    long readLongValue(String key) throws Exception;
+    int readIntValue(String key) throws Exception;
+    Iterator<KeyValuePair> read(String key) throws Exception;
+    
+    static class KeyValuePair {
+        String key;
+        String value;
+        
+        public KeyValuePair(String key, String value) {
+            this.key = key;
+            this.value = value;
+        }
+
+        public String getKey() {
+            return key;
+        }
+
+        public String getValue() {
+            return value;
+        }
+    }
 }

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=1237333&r1=1237332&r2=1237333&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
Sun Jan 29 16:39:10 2012
@@ -28,6 +28,8 @@ import org.apache.jackrabbit.mk.store.ut
 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.sql.Connection;
@@ -120,6 +122,10 @@ public class H2PersistenceManager extend
                 stmt.setBytes(1, StringUtils.convertHexToBytes(id));
                 ResultSet rs = stmt.executeQuery();
                 if (rs.next()) {
+/*
+                    ByteArrayInputStream in = new ByteArrayInputStream(rs.getBytes(1));
+                    return StoredNode.deserialize(id, new BinaryBinding(in, null));
+*/
                     return Serializer.fromBytes(id, rs.getBytes(1), Node.class);
                 } else {
                     throw new NotFoundException(id);
@@ -134,6 +140,11 @@ public class H2PersistenceManager extend
 
     @Override
     protected String writeNode(Node node) throws Exception {
+/*
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        node.serialize(new BinaryBinding(null, out));
+        byte[] bytes = out.toByteArray();
+*/
         byte[] bytes = Serializer.toBytes(node);
         byte[] rawId = idFactory.createNodeId(node, bytes);
         String id = StringUtils.convertBytesToHex(rawId);
@@ -166,6 +177,10 @@ public class H2PersistenceManager extend
                 stmt.setBytes(1, StringUtils.convertHexToBytes(id));
                 ResultSet rs = stmt.executeQuery();
                 if (rs.next()) {
+/*
+                    ByteArrayInputStream in = new ByteArrayInputStream(rs.getBytes(1));
+                    return StoredCommit.deserialize(id, new BinaryBinding(in, null));
+*/
                     return Serializer.fromBytes(id, rs.getBytes(1), Commit.class);
                 } else {
                     throw new NotFoundException(id);
@@ -180,6 +195,11 @@ public class H2PersistenceManager extend
     
     @Override
     protected String writeCommit(Commit commit) throws Exception {
+/*
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        commit.serialize(new BinaryBinding(null, out));
+        byte[] bytes = out.toByteArray();
+*/
         byte[] bytes = Serializer.toBytes(commit);
         byte[] rawId = idFactory.createRevisionId(commit, bytes);
         String id = StringUtils.convertBytesToHex(rawId);

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/util/Serializer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/util/Serializer.java?rev=1237333&r1=1237332&r2=1237333&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/util/Serializer.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/store/util/Serializer.java
Sun Jan 29 16:39:10 2012
@@ -140,7 +140,7 @@ public class Serializer {
         return new StoredCommit(id, "".equals(parentId) ? null : parentId, commitTS, rootNodeId,
"".equals(msg) ? null : msg);
     }
 
-    //---------------------------------------------------------------< Commit >
+    //-----------------------------------------------< ChildNodeEntriesBucket >
 
     public static byte[] toBytes(ChildNodeEntriesBucket bucket) {
         ByteArrayOutputStream out = new ByteArrayOutputStream();



Mime
View raw message