jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r1433888 - /jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoCommit.java
Date Wed, 16 Jan 2013 10:38:17 GMT
Author: mreutegg
Date: Wed Jan 16 10:38:17 2013
New Revision: 1433888

URL: http://svn.apache.org/viewvc?rev=1433888&view=rev
Log:
OAK-558: Compress affected paths and diff field in MongoCommit 

Modified:
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoCommit.java

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoCommit.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoCommit.java?rev=1433888&r1=1433887&r2=1433888&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoCommit.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/model/MongoCommit.java
Wed Jan 16 10:38:17 2013
@@ -16,10 +16,20 @@
  */
 package org.apache.jackrabbit.mongomk.impl.model;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
 
 import org.apache.jackrabbit.mongomk.api.instruction.Instruction;
 import org.apache.jackrabbit.mongomk.api.model.Commit;
@@ -42,6 +52,7 @@ public class MongoCommit extends BasicDB
     public static final String KEY_TIMESTAMP = "ts";
 
     private final List<Instruction> instructions;
+    private List<String> affectedPaths;
 
     private static final long serialVersionUID = 6656294757102309827L;
 
@@ -62,11 +73,15 @@ public class MongoCommit extends BasicDB
     @Override
     @SuppressWarnings("unchecked")
     public List<String> getAffectedPaths() {
-        return (List<String>) get(KEY_AFFECTED_PATH);
+        if (affectedPaths == null) {
+            affectedPaths = inflate((byte[]) get(KEY_AFFECTED_PATH));
+        }
+        return affectedPaths;
     }
 
     public void setAffectedPaths(List<String> affectedPaths) {
-        put(KEY_AFFECTED_PATH, affectedPaths);
+        this.affectedPaths = affectedPaths;
+        put(KEY_AFFECTED_PATH, compress(affectedPaths));
     }
 
     @Override
@@ -93,11 +108,16 @@ public class MongoCommit extends BasicDB
 
     @Override
     public String getDiff() {
-        return getString(KEY_DIFF);
+        List<String> data = inflate((byte[]) get(KEY_DIFF));
+        if (data == null || data.isEmpty()) {
+            return null;
+        } else {
+            return data.get(0);
+        }
     }
 
     public void setDiff(String diff) {
-        put(KEY_DIFF, diff);
+        put(KEY_DIFF, compress(Collections.singleton(diff)));
     }
 
     public boolean isFailed() {
@@ -164,4 +184,48 @@ public class MongoCommit extends BasicDB
     public List<Instruction> getInstructions() {
         return Collections.unmodifiableList(instructions);
     }
+
+    //-----------------------------< internal >---------------------------------
+
+    private List<String> inflate(byte[] bytes) {
+        if (bytes == null) {
+            return null;
+        }
+        List<String> data = new ArrayList<String>();
+        try {
+            DataInputStream in = new DataInputStream(new GZIPInputStream(
+                    new ByteArrayInputStream(bytes)));
+            int num = in.readInt();
+            for (int i = 0; i < num; i++) {
+                int len = in.readInt();
+                byte[] buffer = new byte[len];
+                in.readFully(buffer);
+                data.add(new String(buffer, "UTF-8"));
+            }
+        } catch (IOException e) {
+            // FIXME: shoulnd't actually happen with byte[] and UTF-8
+            throw new RuntimeException(e);
+        }
+        return data;
+    }
+
+    private byte[] compress(Collection<String> strings) {
+        try {
+            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
+            OutputStream out = new GZIPOutputStream(byteOut);
+            DataOutputStream dataOut = new DataOutputStream(out);
+            dataOut.writeInt(strings.size());
+            for (String s : strings) {
+                byte[] buffer = s.getBytes("UTF-8");
+                dataOut.writeInt(buffer.length);
+                dataOut.write(buffer);
+            }
+            dataOut.close();
+            out.close();
+            return byteOut.toByteArray();
+        } catch (IOException e) {
+            // FIXME: shoulnd't actually happen with byte[] and UTF-8
+            throw new RuntimeException(e);
+        }
+    }
 }
\ No newline at end of file



Mime
View raw message