rishabhdaim commented on code in PR #1473: URL: https://github.com/apache/jackrabbit-oak/pull/1473#discussion_r1606849052
########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java: ########## @@ -38,24 +43,59 @@ 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 byte[] compressedValue; + private final Compression compression = Compression.GZIP; + + public static final int DEFAULT_COMPRESSION_THRESHOLD = Integer.getInteger("oak.mongo.compressionThreshold", 1024); DocumentPropertyState(DocumentNodeStore store, String name, String value) { this.store = store; this.name = name; - this.value = value; + + int size = value.getBytes().length; + if (size > DEFAULT_COMPRESSION_THRESHOLD) { + compressedValue = compress(value.getBytes()); Review Comment: If there is an exception while compressing, we should return the uncompressed value rather than throwing the exception. ########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java: ########## @@ -38,24 +43,59 @@ 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 byte[] compressedValue; + private final Compression compression = Compression.GZIP; + + public static final int DEFAULT_COMPRESSION_THRESHOLD = Integer.getInteger("oak.mongo.compressionThreshold", 1024); DocumentPropertyState(DocumentNodeStore store, String name, String value) { this.store = store; this.name = name; - this.value = value; + + int size = value.getBytes().length; + if (size > DEFAULT_COMPRESSION_THRESHOLD) { + compressedValue = compress(value.getBytes()); + this.value = null; + } else { + this.value = value; + } + } + + private byte[] compress(byte[] value) { + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + OutputStream compressionOutputStream = compression.getOutputStream(out); + compressionOutputStream.write(value); + compressionOutputStream.close(); + return out.toByteArray(); + } catch (IOException e) { + throw new RuntimeException("Failed to compress data", e); + } + } + + private String decompress(byte[] value) { + try { + return new String(compression.getInputStream(new ByteArrayInputStream(value)).readAllBytes()); + } catch (IOException e) { + throw new RuntimeException("Failed to decompress data", e); Review Comment: please log the exception. ########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java: ########## @@ -38,24 +43,59 @@ 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 byte[] compressedValue; + private final Compression compression = Compression.GZIP; + + public static final int DEFAULT_COMPRESSION_THRESHOLD = Integer.getInteger("oak.mongo.compressionThreshold", 1024); Review Comment: I would keep this private. ########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentPropertyState.java: ########## @@ -38,24 +43,59 @@ 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 byte[] compressedValue; + private final Compression compression = Compression.GZIP; + + public static final int DEFAULT_COMPRESSION_THRESHOLD = Integer.getInteger("oak.mongo.compressionThreshold", 1024); DocumentPropertyState(DocumentNodeStore store, String name, String value) { this.store = store; this.name = name; - this.value = value; + + int size = value.getBytes().length; + if (size > DEFAULT_COMPRESSION_THRESHOLD) { + compressedValue = compress(value.getBytes()); + this.value = null; + } else { + this.value = value; + } + } + + private byte[] compress(byte[] value) { + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + OutputStream compressionOutputStream = compression.getOutputStream(out); + compressionOutputStream.write(value); + compressionOutputStream.close(); + return out.toByteArray(); + } catch (IOException e) { + throw new RuntimeException("Failed to compress data", e); Review Comment: please log the exception. -- 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