liyafan82 commented on a change in pull request #7326:
URL: https://github.com/apache/arrow/pull/7326#discussion_r436654884



##########
File path: 
java/vector/src/main/java/org/apache/arrow/vector/compression/CompressionUtility.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.arrow.vector.compression;
+
+import org.apache.arrow.flatbuf.BodyCompressionMethod;
+import org.apache.arrow.flatbuf.CompressionType;
+import org.apache.arrow.vector.ipc.message.ArrowBodyCompression;
+
+/**
+ * Utilities for data compression/decompression.
+ */
+public class CompressionUtility {
+
+  private CompressionUtility() {
+  }
+
+  /**
+   * Creates the {@link ArrowBodyCompression} object, given the {@link 
CompressionCodec}.
+   */
+  public static ArrowBodyCompression createBodyCompression(CompressionCodec 
codec) {
+    if (codec == null) {
+      return null;
+    }
+    for (int i = 0; i < CompressionType.names.length; i++) {
+      if (CompressionType.names[i].equals(codec.getCodecName())) {

Review comment:
       Thanks for your comment. I agree with you. 
   The problem is that class `org.apache.arrow.flatbuf.CompressionType` is 
automatically generated by flatbuf, and it is not implemented by an enum. 
Instead, it has separate fields for ordinals and names. In addition, it only 
provides function to convert ordinals to names, but not vice versa. 

##########
File path: 
java/vector/src/main/java/org/apache/arrow/vector/compression/CompressionCodec.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.arrow.vector.compression;
+
+import org.apache.arrow.memory.ArrowBuf;
+
+/**
+ * The codec for compression/decompression.
+ */
+public interface CompressionCodec {
+
+  /**
+   * Given a buffer, estimate the compressed size.
+   * Please note this operation is optional, and some compression methods may 
not support it.
+   *
+   * @param input the input buffer to be estimated.
+   * @return the estimated size of the compressed data.
+   */
+  long estimateCompressedSize(ArrowBuf input);

Review comment:
       Simply compress/decompress would be OK, of course. 
   
   The estimateCompressedSize/estimateDecompressedSize APIs are mainly provided 
for performance benefits. In particular, if we can estimate the sizes with 
accuracy, we will
   1. Avoid allocating memory spaces larger than necessary.
   2. Avoid reallocating memory space during compression/decompression
   3. Avoid data copy caused by reallocations. 
   
   Due to the above benefits, I would prefer to add the APIs to the interface, 
but I am open to others' opinons and willing to change my mind if necessary. 

##########
File path: 
java/vector/src/main/java/org/apache/arrow/vector/ipc/message/MessageSerializer.java
##########
@@ -300,6 +307,20 @@ public static long writeBatchBuffers(WriteChannel out, 
ArrowRecordBatch batch) t
     return out.getCurrentPosition() - bufferStart;
   }
 
+  /**
+   * Serialize the compression body.
+   */
+  public static long writeCompressionBody(
+      WriteChannel out, ArrowBodyCompression bodyCompression) throws 
IOException {
+    long bufferStart = out.getCurrentPosition();
+    ByteBuffer buf = ByteBuffer.allocate(2);

Review comment:
       I have revised the implementation of `ArrowBodyCompression ` so that it 
is based on byte[]. Thanks a lot for the good suggestion. 

##########
File path: 
java/vector/src/main/java/org/apache/arrow/vector/compression/CompressionCodec.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.arrow.vector.compression;
+
+import org.apache.arrow.memory.ArrowBuf;
+
+/**
+ * The codec for compression/decompression.
+ */
+public interface CompressionCodec {
+
+  /**
+   * Given a buffer, estimate the compressed size.
+   * Please note this operation is optional, and some compression methods may 
not support it.
+   *
+   * @param input the input buffer to be estimated.
+   * @return the estimated size of the compressed data.
+   */
+  long estimateCompressedSize(ArrowBuf input);

Review comment:
       Maybe you mean we should remove the 
estimateCompressedSize/estimateDecompressedSize APIs from the CompressionCodec 
interface, so that the sizes will be estimated internally when calling the 
compress/decompress APIs. 
   
   The problem is that for some scenarios, the size should be enforced from the 
outside, rather than being computed internally. For example, in the 
decompression phase, if we know the buffer type (e.g. validity buffer) and the 
vector length, we can estimate the decompressed buffer size with absolute 
certainty. 




----------------------------------------------------------------
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:
[email protected]


Reply via email to