valsong commented on code in PR #439:
URL: https://github.com/apache/lucene/pull/439#discussion_r1515754118


##########
lucene/codecs/src/java/org/apache/lucene/codecs/customcompression/ZstdDictCompressionMode.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.codecs.customcompression;
+
+import com.github.luben.zstd.*;
+import java.io.IOException;
+import org.apache.lucene.codecs.compressing.CompressionMode;
+import org.apache.lucene.codecs.compressing.Compressor;
+import org.apache.lucene.codecs.compressing.Decompressor;
+import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.DataOutput;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.BytesRef;
+
+/** Zstandard Compression Mode */
+public class ZstdDictCompressionMode extends CompressionMode {
+  private static final int NUM_SUB_BLOCKS = 10;
+  private final int level;
+  public static final int defaultLevel = 6;
+  private static final int DICT_SIZE_FACTOR = 6;
+  /** default constructor */
+  ZstdDictCompressionMode() {
+    this.level = defaultLevel;
+  }
+  /** compression mode for a given compression level */
+  ZstdDictCompressionMode(int level) {
+    this.level = level;
+  }
+
+  @Override
+  public Compressor newCompressor() {
+    return new ZSTDCompressor(level);
+  }
+
+  @Override
+  public Decompressor newDecompressor() {
+    return new ZSTDDecompressor();
+  }
+
+  /** zstandard compressor */
+  private static final class ZSTDCompressor extends Compressor {
+
+    int compressionLevel;
+    byte[] compressedBuffer;
+
+    /** compressor with a given compresion level */
+    public ZSTDCompressor(int compressionLevel) {
+      this.compressionLevel = compressionLevel;
+      compressedBuffer = BytesRef.EMPTY_BYTES;
+    }
+
+    @Override
+    public void close() throws IOException {}
+
+    /*resuable compress function*/
+    private void doCompress(byte[] bytes, int off, int len, ZstdCompressCtx 
cctx, DataOutput out)
+        throws IOException {
+      if (len == 0) {
+        out.writeVInt(0);
+        return;
+      }
+      final int maxCompressedLength = (int) Zstd.compressBound(len);
+      compressedBuffer = ArrayUtil.grow(compressedBuffer, maxCompressedLength);
+
+      int compressedSize =
+          cctx.compressByteArray(compressedBuffer, 0, compressedBuffer.length, 
bytes, off, len);
+
+      out.writeVInt(compressedSize);
+      out.writeBytes(compressedBuffer, compressedSize);
+    }
+
+    @Override
+    public void compress(byte[] bytes, int off, int len, DataOutput out) 
throws IOException {
+      final int dictLength = len / (NUM_SUB_BLOCKS * DICT_SIZE_FACTOR);
+      final int blockLength = (len - dictLength + NUM_SUB_BLOCKS - 1) / 
NUM_SUB_BLOCKS;
+      out.writeVInt(dictLength);
+      out.writeVInt(blockLength);
+
+      final int end = off + len;
+
+      try (ZstdCompressCtx cctx = new ZstdCompressCtx()) {
+        cctx.setLevel(this.compressionLevel);
+
+        // dictionary compression first
+        doCompress(bytes, off, dictLength, cctx, out);
+        cctx.loadDict(new ZstdDictCompress(bytes, off, dictLength, 
this.compressionLevel));
+
+        for (int start = off + dictLength; start < end; start += blockLength) {
+          int l = Math.min(blockLength, off + len - start);
+          doCompress(bytes, start, l, cctx, out);
+        }
+      }
+    }
+  }
+
+  /** zstandard decompressor */
+  private static final class ZSTDDecompressor extends Decompressor {
+
+    byte[] compressed;
+
+    /** default decompressor */
+    public ZSTDDecompressor() {
+      compressed = BytesRef.EMPTY_BYTES;
+    }
+
+    /*resuable decompress function*/
+    private void doDecompress(
+        DataInput in, ZstdDecompressCtx dctx, BytesRef bytes, int 
decompressedLen)
+        throws IOException {
+      final int compressedLength = in.readVInt();
+      if (compressedLength == 0) {
+        return;
+      }
+
+      compressed = ArrayUtil.grow(compressed, compressedLength);
+      in.readBytes(compressed, 0, compressedLength);
+
+      bytes.bytes = ArrayUtil.grow(bytes.bytes, bytes.length + 
decompressedLen);
+      int uncompressed =
+          dctx.decompressByteArray(
+              bytes.bytes, bytes.length, decompressedLen, compressed, 0, 
compressedLength);
+
+      if (decompressedLen != uncompressed) {
+        throw new IllegalStateException(decompressedLen + " " + uncompressed);
+      }
+      bytes.length += uncompressed;
+    }
+
+    @Override
+    public void decompress(DataInput in, int originalLength, int offset, int 
length, BytesRef bytes)
+        throws IOException {
+      assert offset + length <= originalLength;
+
+      if (length == 0) {
+        bytes.length = 0;
+        return;
+      }
+      final int dictLength = in.readVInt();
+      final int blockLength = in.readVInt();
+      bytes.bytes = ArrayUtil.grow(bytes.bytes, dictLength);
+      bytes.offset = bytes.length = 0;
+
+      try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) {
+
+        // decompress dictionary first
+        doDecompress(in, dctx, bytes, dictLength);
+
+        dctx.loadDict(new ZstdDictDecompress(bytes.bytes, 0, dictLength));

Review Comment:
   You should close this ZstdDictDecompress object too.



-- 
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: issues-unsubscr...@lucene.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to