ionutzpi commented on code in PR #1473:
URL: https://github.com/apache/jackrabbit-oak/pull/1473#discussion_r1618372804


##########
oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java:
##########
@@ -38,24 +43,58 @@
 import org.apache.jackrabbit.oak.plugins.memory.StringPropertyState;
 import org.apache.jackrabbit.oak.plugins.value.Conversions;
 import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * PropertyState implementation with lazy parsing of the JSOP encoded value.
  */
 final class DocumentPropertyState implements PropertyState {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(DocumentPropertyState.class);
+
     private final DocumentNodeStore store;
 
     private final String name;
 
-    private final String value;
+    private String value;
 
     private PropertyState parsed;
+    private byte[] compressedValue;
+    private final Compression compression;
+
+    private static final int DEFAULT_COMPRESSION_THRESHOLD = 
Integer.getInteger("oak.mongo.compressionThreshold", 1024);
 
     DocumentPropertyState(DocumentNodeStore store, String name, String value) {
+        this(store, name, value, Compression.GZIP);
+    }
+
+    DocumentPropertyState(DocumentNodeStore store, String name, String value, 
Compression compression) {
         this.store = store;
         this.name = name;
-        this.value = value;
+        this.compression = compression;
+        int size = value.getBytes().length;
+        if (compression != null && size > DEFAULT_COMPRESSION_THRESHOLD ) {
+            try {
+                compressedValue = compress(value.getBytes());

Review Comment:
   Done



##########
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyStateTest.java:
##########
@@ -75,10 +82,97 @@ public void multiValuedBinarySize() throws Exception {
         assertEquals(Type.BINARIES, p.getType());
         assertEquals(3, p.count());
 
+        assertNull(((DocumentPropertyState) p).getCompressedValue());
+
+        reads.clear();
+        assertEquals(BLOB_SIZE, p.size(0));
+        // must not read the blob via stream
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void multiValuedAboveThresholdSize() throws Exception {
+        NodeBuilder builder = ns.getRoot().builder();
+        List<Blob> blobs = newArrayList();
+        for (int i = 0; i < 13; i++) {
+            blobs.add(builder.createBlob(new RandomStream(BLOB_SIZE, i)));
+        }
+        builder.child(TEST_NODE).setProperty("p", blobs, Type.BINARIES);
+        TestUtils.merge(ns, builder);
+
+        PropertyState p = 
ns.getRoot().getChildNode(TEST_NODE).getProperty("p");
+        assertEquals(Type.BINARIES, Objects.requireNonNull(p).getType());
+        assertEquals(13, p.count());
+
+        assertNotNull(((DocumentPropertyState) p).getCompressedValue());
+
         reads.clear();
         assertEquals(BLOB_SIZE, p.size(0));
         // must not read the blob via stream
         assertEquals(0, reads.size());
     }
 
+    @Test
+    public void stringBelowThresholdSize() throws Exception {
+        NodeBuilder builder = ns.getRoot().builder();
+        builder.child(TEST_NODE).setProperty("p", "dummy", Type.STRING);
+        TestUtils.merge(ns, builder);
+
+        PropertyState p = 
ns.getRoot().getChildNode(TEST_NODE).getProperty("p");
+        assertEquals(Type.STRING, Objects.requireNonNull(p).getType());
+        assertEquals(1, p.count());
+
+        assertNull(((DocumentPropertyState) p).getCompressedValue());
+
+        reads.clear();
+        assertEquals(5, p.size(0));
+        // must not read the string via stream
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void stringAboveThresholdSize() throws Exception {
+        NodeBuilder builder = ns.getRoot().builder();
+        builder.child(TEST_NODE).setProperty("p", STRING_HUGEVALUE, 
Type.STRING);
+        TestUtils.merge(ns, builder);
+
+        PropertyState p = 
ns.getRoot().getChildNode(TEST_NODE).getProperty("p");
+        assertEquals(Type.STRING, Objects.requireNonNull(p).getType());
+        assertEquals(1, p.count());
+
+        assertNotNull(((DocumentPropertyState) p).getCompressedValue());
+
+        reads.clear();
+        assertEquals(1050, p.size(0));
+        // must not read the string via streams
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void compressValueThrowsException() throws CommitFailedException, 
IOException {
+        DocumentNodeStore mockDocumentStore = mock(DocumentNodeStore.class);
+        Compression mockCompression = mock(Compression.class);
+        
when(mockCompression.getOutputStream(any(OutputStream.class))).thenThrow(new 
IOException("Compression failed"));
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", STRING_HUGEVALUE, 
mockCompression);
+
+        verify(mockCompression, 
times(1)).getOutputStream(any(OutputStream.class));
+
+    }
+
+    @Test
+    public void uncompressValueThrowsException() throws CommitFailedException, 
IOException {
+
+        DocumentNodeStore mockDocumentStore = mock(DocumentNodeStore.class);
+        Compression mockCompression = mock(Compression.class);
+        OutputStream mockOutputStream= mock(OutputStream.class);
+        
when(mockCompression.getOutputStream(any(OutputStream.class))).thenReturn(mockOutputStream);
+        
when(mockCompression.getInputStream(any(InputStream.class))).thenThrow(new 
IOException("Compression failed"));
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", STRING_HUGEVALUE, 
mockCompression);
+        documentPropertyState.getValue(Type.STRING);

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to