[GitHub] [incubator-iotdb] SilverNarcissus commented on a change in pull request #1448: [IOTDB-716] add lZ4 compress method

2020-07-13 Thread GitBox


SilverNarcissus commented on a change in pull request #1448:
URL: https://github.com/apache/incubator-iotdb/pull/1448#discussion_r453668989



##
File path: 
tsfile/src/main/java/org/apache/iotdb/tsfile/compress/ICompressor.java
##
@@ -52,6 +52,8 @@ static ICompressor getCompressor(CompressionType name) {
 return new NoCompressor();
   case SNAPPY:
 return new SnappyCompressor();
+  case LZ4:
+return new IOTDBLZ4Compressor();

Review comment:
   The package I import is called LZ4Compressor, so I can't use this name

##
File path: 
tsfile/src/main/java/org/apache/iotdb/tsfile/compress/IUnCompressor.java
##
@@ -184,4 +186,82 @@ public CompressionType getCodecName() {
   return CompressionType.SNAPPY;
 }
   }
+
+
+  class LZ4UnCompressor implements IUnCompressor {
+
+private static final Logger logger = 
LoggerFactory.getLogger(LZ4Compressor.class);
+private static final int MAX_COMPRESS_RATIO = 255;
+private LZ4SafeDecompressor decompressor;
+
+
+public LZ4UnCompressor() {
+  LZ4Factory factory = LZ4Factory.fastestInstance();
+  decompressor = factory.safeDecompressor();
+}
+
+@Override
+public int getUncompressedLength(byte[] array, int offset, int length) 
throws IOException {
+  throw new UnsupportedOperationException("unsupported get uncompress 
length");
+}
+
+@Override
+public int getUncompressedLength(ByteBuffer buffer) throws IOException {
+  throw new UnsupportedOperationException("unsupported get uncompress 
length");
+}
+
+/**
+ * We don't recommend using this method because we have to allocate 
MAX_COMPRESS_RATIO *
+ * compressedSize to ensure uncompress safety, you can use other method if 
you know the
+ * uncompressed size
+ */
+@Override
+public byte[] uncompress(byte[] bytes) {
+  if (bytes == null) {
+return new byte[0];
+  }
+
+  try {
+return decompressor.decompress(bytes, MAX_COMPRESS_RATIO * 
bytes.length);
+  } catch (RuntimeException e) {
+logger.error(

Review comment:
   Sure~

##
File path: 
tsfile/src/main/java/org/apache/iotdb/tsfile/compress/IUnCompressor.java
##
@@ -184,4 +186,82 @@ public CompressionType getCodecName() {
   return CompressionType.SNAPPY;
 }
   }
+
+
+  class LZ4UnCompressor implements IUnCompressor {
+
+private static final Logger logger = 
LoggerFactory.getLogger(LZ4Compressor.class);
+private static final int MAX_COMPRESS_RATIO = 255;
+private LZ4SafeDecompressor decompressor;
+
+
+public LZ4UnCompressor() {
+  LZ4Factory factory = LZ4Factory.fastestInstance();
+  decompressor = factory.safeDecompressor();
+}
+
+@Override
+public int getUncompressedLength(byte[] array, int offset, int length) 
throws IOException {
+  throw new UnsupportedOperationException("unsupported get uncompress 
length");
+}
+
+@Override
+public int getUncompressedLength(ByteBuffer buffer) throws IOException {
+  throw new UnsupportedOperationException("unsupported get uncompress 
length");
+}
+
+/**
+ * We don't recommend using this method because we have to allocate 
MAX_COMPRESS_RATIO *
+ * compressedSize to ensure uncompress safety, you can use other method if 
you know the
+ * uncompressed size
+ */
+@Override
+public byte[] uncompress(byte[] bytes) {
+  if (bytes == null) {
+return new byte[0];
+  }
+
+  try {
+return decompressor.decompress(bytes, MAX_COMPRESS_RATIO * 
bytes.length);
+  } catch (RuntimeException e) {
+logger.error(
+"tsfile-compression LZ4UnCompressor: errors occurs when uncompress 
input byte", e);
+  }
+  return new byte[0];
+}
+
+@Override
+public int uncompress(byte[] byteArray, int offset, int length, byte[] 
output, int outOffset)
+throws IOException {
+  try {
+return decompressor.decompress(byteArray, offset, length, output, 
offset);
+  }
+  catch (RuntimeException e){
+logger.error(
+"tsfile-compression LZ4UnCompressor: errors occurs when uncompress 
input byte", e);
+throw new IOException(e);
+  }
+}
+
+@Override
+public int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) {
+  if (compressed == null || !compressed.hasRemaining()) {
+return 0;
+  }
+
+  try {
+decompressor.decompress(compressed, uncompressed);
+return compressed.limit();
+  } catch (RuntimeException e) {
+logger.error(

Review comment:
   Sure~





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.

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




[GitHub] [incubator-iotdb] SilverNarcissus commented on a change in pull request #1448: [IOTDB-716] add lZ4 compress method

2020-07-02 Thread GitBox


SilverNarcissus commented on a change in pull request #1448:
URL: https://github.com/apache/incubator-iotdb/pull/1448#discussion_r449116645



##
File path: tsfile/pom.xml
##
@@ -53,6 +53,11 @@
 commons-io
 commons-io
 
+
+net.jpountz.lz4
+lz4
+1.3.0
+

Review comment:
   Sure~ I will fix it~





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.

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