belliottsmith commented on code in PR #4738:
URL: https://github.com/apache/cassandra/pull/4738#discussion_r3274130518


##########
src/java/org/apache/cassandra/io/util/CompressedFrameDataOutputPlus.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.cassandra.io.util;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.WritableByteChannel;
+import java.util.List;
+import java.util.Objects;
+import java.util.zip.Checksum;
+
+import com.google.common.primitives.Shorts;
+
+import accord.utils.Invariants;
+
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.UnversionedSerializer;
+import org.apache.cassandra.io.compress.ICompressor;
+import org.apache.cassandra.io.compress.ZstdCompressor;
+import org.apache.cassandra.utils.CollectionSerializers;
+import org.apache.cassandra.utils.Crc;
+import org.apache.cassandra.utils.memory.MemoryUtil;
+
+import static 
org.apache.cassandra.io.compress.ZstdCompressor.DEFAULT_COMPRESSION_LEVEL;
+
+public class CompressedFrameDataOutputPlus extends BufferedDataOutputStreamPlus
+{
+    static final int SIZE_OF_HEADER = 10;
+    static final int DEFAULT_FRAME_SIZE = 16 << 10;
+
+    private final ICompressor compressor;
+    private final Checksum checksum;
+    private ByteBuffer compressed;
+    protected CompressedFrameDataOutputPlus(int frameSize, WritableByteChannel 
out, ICompressor compressor, Checksum checksum)
+    {
+        super(out, compressor.preferredBufferType().allocate(frameSize));
+        this.compressor = compressor;
+        this.compressed = compressor.preferredBufferType().allocate(frameSize 
+ SIZE_OF_HEADER);
+        this.checksum = checksum;
+        if (frameSize > Short.MAX_VALUE)
+            throw new IllegalArgumentException("Frame size too large");
+    }
+
+    @Override
+    protected void doFlush(int count) throws IOException
+    {
+        buffer.flip();
+        compressed.clear();
+        compressed.position(SIZE_OF_HEADER);
+        compressor.compress(buffer, compressed);
+        compressed.flip();
+        int limit = compressed.limit();
+        int length = limit - SIZE_OF_HEADER;
+        if (length > buffer.limit())
+        {
+            length = -(1 + buffer.limit());
+            compressed.clear();
+            compressed.position(SIZE_OF_HEADER);
+            buffer.position(0);
+            compressed.put(buffer);
+            compressed.flip();
+        }
+        compressed.putShort(SIZE_OF_HEADER - 2, Shorts.checkedCast(length));
+        compressed.position(SIZE_OF_HEADER);
+        checksum.update(compressed);
+        compressed.putLong(0, checksum.getValue());

Review Comment:
   Good question. I don't remember what I intended, but I don't think there's 
anything wrong with having an accumulating checksum in this case. It might be 
better in fact, since this is intended to be used for a single stream of bytes 
(not to be indexed into), just ensuring each chunk is properly covered by a 
checksum.



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

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to