stefan-egli commented on code in PR #1526:
URL: https://github.com/apache/jackrabbit-oak/pull/1526#discussion_r1644291222


##########
oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java:
##########
@@ -38,24 +45,64 @@
 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 PropertyState parsed;
+    private final byte[] compressedValue;
+    private final Compression compression;
+
+    private static final int DEFAULT_COMPRESSION_THRESHOLD = 
SystemPropertySupplier
+            .create("oak.documentMK.stringCompressionThreshold ", 
-1).loggingTo(LOG).get();
 
     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;
+        if (compression != null && DEFAULT_COMPRESSION_THRESHOLD == -1) {

Review Comment:
   if `compression` is null, then it would go into `else` and run into a NPE



##########
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyStateTest.java:
##########
@@ -81,4 +100,179 @@ public void multiValuedBinarySize() throws Exception {
         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());
+
+        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());
+
+        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());
+
+        reads.clear();
+        assertEquals(10050, p.size(0));
+        // must not read the string via streams
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void compressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+        DocumentNodeStore mockDocumentStore = mock(DocumentNodeStore.class);
+        Compression mockCompression = mock(Compression.class);
+        
when(mockCompression.getOutputStream(any(OutputStream.class))).thenThrow(new 
IOException("Compression failed"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", "\"" + STRING_HUGEVALUE + "\"", 
mockCompression);
+
+        assertEquals(documentPropertyState.getValue(Type.STRING), 
STRING_HUGEVALUE);
+
+        verify(mockCompression, 
times(1)).getOutputStream(any(OutputStream.class));
+
+        compressionThreshold.set(null, -1);
+
+    }
+
+    @Test
+    public void uncompressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+
+        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"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", STRING_HUGEVALUE, 
mockCompression);
+
+        assertEquals(documentPropertyState.getValue(Type.STRING), "{}");
+
+        verify(mockCompression, 
times(1)).getInputStream(any(InputStream.class));
+
+        compressionThreshold.set(null, -1);
+    }
+
+    @Test
+    public void adefaultValueSetToMinusOne() throws NoSuchMethodException, 
InvocationTargetException, IllegalAccessException, NoSuchFieldException {
+        DocumentNodeStore store = mock(DocumentNodeStore.class);
+
+        DocumentPropertyState state = new DocumentPropertyState(store, 
"propertyNameNew", "\"" + STRING_HUGEVALUE + "\"", Compression.GZIP);
+
+        // Get the private method
+        Method getCompressedValueMethod = 
DocumentPropertyState.class.getDeclaredMethod("getCompressedValue");
+
+        // Make the method accessible
+        getCompressedValueMethod.setAccessible(true);
+        byte[] result = (byte[]) getCompressedValueMethod.invoke(state);
+
+        assertNull(result);
+        assertEquals(state.getValue(Type.STRING), STRING_HUGEVALUE);
+    }
+
+    @Test
+    public void stringAboveThresholdSizeNoCompression() throws 
NoSuchMethodException, InvocationTargetException, IllegalAccessException, 
NoSuchFieldException {
+        DocumentNodeStore store = mock(DocumentNodeStore.class);
+
+        // Get the private method
+        Method getCompressedValueMethod = 
DocumentPropertyState.class.getDeclaredMethod("getCompressedValue");
+
+        // Make the method accessible
+        getCompressedValueMethod.setAccessible(true);
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState state = new DocumentPropertyState(store, 
"propertyName", "\"" + STRING_HUGEVALUE + "\"", Compression.NONE);
+
+        byte[] result = (byte[]) getCompressedValueMethod.invoke(state);
+
+        assertEquals(result.length, STRING_HUGEVALUE.length() + 2 );
+
+        assertEquals(state.getValue(Type.STRING), STRING_HUGEVALUE);
+        assertEquals(STRING_HUGEVALUE, state.getValue(Type.STRING));
+
+        // Reset the value of the field
+        compressionThreshold.set(null, -1);
+    }
+
+    @Test
+    public void performanceTest() {
+        String testString = RandomStringUtils.random(10050, "dummytest");
+        DocumentNodeStore store = mock(DocumentNodeStore.class);
+        DocumentPropertyState state = new DocumentPropertyState(store, 
"propertyName", "\"" + testString + "\"", Compression.GZIP);
+
+        Runtime runtime = Runtime.getRuntime();
+
+        long startMemory = runtime.totalMemory() - runtime.freeMemory(); // 
Get initial memory usage
+        long startTime = System.nanoTime();
+
+        String compressedValue = state.getValue(Type.STRING);
+
+        long endTime = System.nanoTime();
+        long endMemory = runtime.totalMemory() - runtime.freeMemory(); // Get 
final memory usage
+
+        long duration = (endTime - startTime);  // divide by 1000000 to get 
milliseconds.
+        long usedMemory = endMemory - startMemory; // Calculate the used 
memory during the operation
+
+        System.out.println("Time taken for compression: " + duration + " 
nanoseconds");
+        System.out.println("Memory used for compression: " + usedMemory + " 
bytes");
+
+        // Assert that the compressed value is not null or empty
+        assertTrue(compressedValue != null && !compressedValue.isEmpty());
+    }
+}

Review Comment:
   (assuming this was originally referring to `testInterestingStrings`)
   
   > The test failed. The compression didn't work properly.
   
   So is the plan to still fix this? I don't think this test should be expected 
to throw `ComparisonFailure`



##########
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyStateTest.java:
##########
@@ -81,4 +100,225 @@ public void multiValuedBinarySize() throws Exception {
         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());
+
+        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());
+
+        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());
+
+        reads.clear();
+        assertEquals(10050, p.size(0));
+        // must not read the string via streams
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void compressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+        DocumentNodeStore mockDocumentStore = mock(DocumentNodeStore.class);
+        Compression mockCompression = mock(Compression.class);
+        
when(mockCompression.getOutputStream(any(OutputStream.class))).thenThrow(new 
IOException("Compression failed"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", "\"" + STRING_HUGEVALUE + "\"", 
mockCompression);
+
+        assertEquals(documentPropertyState.getValue(Type.STRING), 
STRING_HUGEVALUE);
+
+        verify(mockCompression, 
times(1)).getOutputStream(any(OutputStream.class));
+
+        compressionThreshold.set(null, -1);
+
+    }
+
+    @Test
+    public void uncompressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+
+        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"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", STRING_HUGEVALUE, 
mockCompression);
+
+        assertEquals(documentPropertyState.getValue(Type.STRING), "{}");
+
+        verify(mockCompression, 
times(1)).getInputStream(any(InputStream.class));
+
+        compressionThreshold.set(null, -1);
+    }
+
+    @Test
+    public void adefaultValueSetToMinusOne() throws NoSuchMethodException, 
InvocationTargetException, IllegalAccessException, NoSuchFieldException {
+        DocumentNodeStore store = mock(DocumentNodeStore.class);
+
+        DocumentPropertyState state = new DocumentPropertyState(store, 
"propertyNameNew", "\"" + STRING_HUGEVALUE + "\"", Compression.GZIP);
+
+        // Get the private method
+        Method getCompressedValueMethod = 
DocumentPropertyState.class.getDeclaredMethod("getCompressedValue");
+
+        // Make the method accessible
+        getCompressedValueMethod.setAccessible(true);
+        byte[] result = (byte[]) getCompressedValueMethod.invoke(state);
+
+        assertNull(result);
+        assertEquals(state.getValue(Type.STRING), STRING_HUGEVALUE);
+    }
+
+    @Test
+    public void stringAboveThresholdSizeNoCompression() throws 
NoSuchMethodException, InvocationTargetException, IllegalAccessException, 
NoSuchFieldException {
+        DocumentNodeStore store = mock(DocumentNodeStore.class);
+
+        // Get the private method
+        Method getCompressedValueMethod = 
DocumentPropertyState.class.getDeclaredMethod("getCompressedValue");
+
+        // Make the method accessible
+        getCompressedValueMethod.setAccessible(true);
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState state = new DocumentPropertyState(store, 
"propertyName", "\"" + STRING_HUGEVALUE + "\"", Compression.NONE);
+
+        byte[] result = (byte[]) getCompressedValueMethod.invoke(state);
+
+        assertEquals(result.length, STRING_HUGEVALUE.length() + 2 );
+
+        assertEquals(state.getValue(Type.STRING), STRING_HUGEVALUE);
+        assertEquals(STRING_HUGEVALUE, state.getValue(Type.STRING));
+
+        // Reset the value of the field
+        compressionThreshold.set(null, -1);
+    }
+
+    @Test(expected = ComparisonFailure.class)
+    public void testInterestingStrings() throws NoSuchFieldException, 
IllegalAccessException {
+
+        DocumentNodeStore store = mock(DocumentNodeStore.class);
+        String testString = "\"\"simple:foo\", \"cr:a\\n\\b\", 
\"dquote:a\\\"b\", \"bs:a\\\\b\", \"euro:a\\u201c\", 
\"gclef:\\uD834\\uDD1E\",\n" +
+                "                \"tab:a\\tb\", \"nul:a\\u0000b\", 
\"brokensurrogate:\\ud800\"";
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, 10);
+
+        DocumentPropertyState state = new DocumentPropertyState(store, 
"propertyName", testString, Compression.GZIP);
+
+        String value = state.getValue(Type.STRING);
+
+        assertEquals(testString, value);
+
+    }
+
+    @Test
+    public void performanceTest() throws NoSuchFieldException, 
IllegalAccessException {

Review Comment:
   I fail to see the use of this versus running oak-benchmark tests with 
different values of `-Doak.documentMK.stringCompressionThreshold=1024` ?



##########
oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java:
##########
@@ -38,24 +45,64 @@
 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 PropertyState parsed;
+    private final byte[] compressedValue;
+    private final Compression compression;
+
+    private static final int DEFAULT_COMPRESSION_THRESHOLD = 
SystemPropertySupplier
+            .create("oak.documentMK.stringCompressionThreshold ", 
-1).loggingTo(LOG).get();

Review Comment:
   I would suggest to refactor this. 
   One aspect is that this variable shouldn't be called "default", as it is 
actually the real configured value - the default is always -1, that can be a 
separate, hard-code const.
   A second aspect is how this variable can be controlled from tests. This has 
been a controversial topic before - in my opinion if we like to test different 
values of say "compressionThreshold", then there should be a clean way to 
achieve that. A `final` in my view isn't ideal, as compilers can in theory 
inline final variables if they so wish. What I think can be an option is to add 
an explicit (static) setter method that is only used for testing - and that 
allows overwriting this field explicitly. With this, the need for explicit test 
order plus the various reflections in the junit class disappear.



##########
oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java:
##########
@@ -128,7 +188,7 @@ public boolean equals(Object object) {
         } else if (object instanceof DocumentPropertyState) {
             DocumentPropertyState other = (DocumentPropertyState) object;
             return this.name.equals(other.name)
-                    && this.value.equals(other.value);
+                    && this.getValue().equals(other.getValue());

Review Comment:
   is there a way that equals wouldn't have to uncompress?



##########
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyStateTest.java:
##########
@@ -18,26 +18,45 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 
+import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.Compression;
 import org.apache.jackrabbit.oak.spi.blob.BlobStore;
 import org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.*;
+import org.junit.runners.MethodSorters;
 
 import static org.apache.jackrabbit.guava.common.collect.Lists.newArrayList;
 import static org.apache.jackrabbit.guava.common.collect.Sets.newHashSet;
 import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class DocumentPropertyStateTest {
 
     private static final int BLOB_SIZE = 16 * 1024;
+    public static final int COMPRESSED_SIZE = 543;

Review Comment:
   nitpick : could probably be private?



##########
oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java:
##########
@@ -116,7 +163,20 @@ public int count() {
      */
     @NotNull
     String getValue() {
-        return value;
+        return value != null ? value : decompress(this.compressedValue);
+    }
+
+    private String decompress(byte[] value) {
+        try {
+            return new String(compression.getInputStream(new 
ByteArrayInputStream(value)).readAllBytes(), StandardCharsets.UTF_8);
+        } catch (IOException e) {
+            LOG.error("Failed to decompress property {} value: ", getName(), 
e);
+            return "\"{}\"";
+        }
+    }
+
+    private byte[] getCompressedValue() {

Review Comment:
   this method is currently unused but instead the junit class does reflection 
to access `compressedValue`. If this method was made less protected it could 
actually replace that reflection use.



##########
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyStateTest.java:
##########
@@ -81,4 +100,179 @@ public void multiValuedBinarySize() throws Exception {
         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());
+
+        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());
+
+        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());
+
+        reads.clear();
+        assertEquals(10050, p.size(0));
+        // must not read the string via streams
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void compressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+        DocumentNodeStore mockDocumentStore = mock(DocumentNodeStore.class);
+        Compression mockCompression = mock(Compression.class);
+        
when(mockCompression.getOutputStream(any(OutputStream.class))).thenThrow(new 
IOException("Compression failed"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", "\"" + STRING_HUGEVALUE + "\"", 
mockCompression);
+
+        assertEquals(documentPropertyState.getValue(Type.STRING), 
STRING_HUGEVALUE);
+
+        verify(mockCompression, 
times(1)).getOutputStream(any(OutputStream.class));
+
+        compressionThreshold.set(null, -1);
+
+    }
+
+    @Test
+    public void uncompressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+
+        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"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", STRING_HUGEVALUE, 
mockCompression);
+
+        assertEquals(documentPropertyState.getValue(Type.STRING), "{}");
+
+        verify(mockCompression, 
times(1)).getInputStream(any(InputStream.class));
+
+        compressionThreshold.set(null, -1);
+    }
+
+    @Test
+    public void adefaultValueSetToMinusOne() throws NoSuchMethodException, 
InvocationTargetException, IllegalAccessException, NoSuchFieldException {

Review Comment:
   I'd suggest to avoid relying on test order in this case - and ensure the 
test class doesn't leave `DocumentPropertyState` in a changed state. Otherwise 
other test classes will be affected unintentionally.



##########
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyStateTest.java:
##########
@@ -81,4 +100,225 @@ public void multiValuedBinarySize() throws Exception {
         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());
+
+        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());
+
+        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());
+
+        reads.clear();
+        assertEquals(10050, p.size(0));
+        // must not read the string via streams
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void compressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+        DocumentNodeStore mockDocumentStore = mock(DocumentNodeStore.class);
+        Compression mockCompression = mock(Compression.class);
+        
when(mockCompression.getOutputStream(any(OutputStream.class))).thenThrow(new 
IOException("Compression failed"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);

Review Comment:
   see other comments, this reflection could be avoided with corresponding 
changes in the target class (same applies to other test methods)



##########
oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyStateTest.java:
##########
@@ -81,4 +100,225 @@ public void multiValuedBinarySize() throws Exception {
         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());
+
+        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());
+
+        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());
+
+        reads.clear();
+        assertEquals(10050, p.size(0));
+        // must not read the string via streams
+        assertEquals(0, reads.size());
+    }
+
+    @Test
+    public void compressValueThrowsException() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+        DocumentNodeStore mockDocumentStore = mock(DocumentNodeStore.class);
+        Compression mockCompression = mock(Compression.class);
+        
when(mockCompression.getOutputStream(any(OutputStream.class))).thenThrow(new 
IOException("Compression failed"));
+
+        Field compressionThreshold = 
DocumentPropertyState.class.getDeclaredField("DEFAULT_COMPRESSION_THRESHOLD");
+        compressionThreshold.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(compressionThreshold, 
compressionThreshold.getModifiers() & ~Modifier.FINAL);
+
+        compressionThreshold.set(null, DEFAULT_COMPRESSION_THRESHOLD);
+
+        DocumentPropertyState documentPropertyState = new 
DocumentPropertyState(mockDocumentStore, "p", "\"" + STRING_HUGEVALUE + "\"", 
mockCompression);
+
+        assertEquals(documentPropertyState.getValue(Type.STRING), 
STRING_HUGEVALUE);
+
+        verify(mockCompression, 
times(1)).getOutputStream(any(OutputStream.class));
+
+        compressionThreshold.set(null, -1);
+

Review Comment:
   nitpick : I'd consider removing some of the newlines (here and in other test 
methods), some seem unnecessary.



-- 
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: oak-dev-unsubscr...@jackrabbit.apache.org

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


Reply via email to