yifan-c commented on code in PR #4399:
URL: https://github.com/apache/cassandra/pull/4399#discussion_r2414979062


##########
src/java/org/apache/cassandra/db/compression/CompressionDictionary.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.db.compression;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.EOFException;
+import java.io.IOException;
+import java.util.Objects;
+import javax.annotation.Nullable;
+
+import com.google.common.hash.Hasher;
+import com.google.common.hash.Hashing;
+
+import org.apache.cassandra.cql3.UntypedResultSet;
+
+public interface CompressionDictionary extends AutoCloseable
+{
+    /**
+     * Get the dictionary id
+     *
+     * @return dictionary id
+     */
+    DictId identifier();
+
+    /**
+     * Get the raw bytes of the compression dictionary
+     *
+     * @return raw compression dictionary
+     */
+    byte[] rawDictionary();
+
+    /**
+     * Get the kind of the compression algorithm
+     *
+     * @return compression algorithm kind
+     */
+    default Kind kind()
+    {
+        return identifier().kind;
+    }
+
+    /**
+     * Write compression dictionary to file
+     *
+     * @param out file output stream
+     * @throws IOException on any I/O exception when writing to the file
+     */
+    default void serialize(DataOutput out) throws IOException
+    {
+        DictId dictId = identifier();
+        int ordinal = dictId.kind.ordinal();
+        out.writeByte(ordinal);
+        out.writeLong(dictId.id);
+        byte[] dict = rawDictionary();
+        out.writeInt(dict.length);
+        out.write(dict);
+        int checksum = calculateChecksum((byte) ordinal, dictId.id, dict);
+        out.writeInt(checksum);
+    }
+
+    /**
+     * A factory method to create concrete CompressionDictionary from the file 
content
+     *
+     * @param input   file input stream
+     * @param manager compression dictionary manager that caches the 
dictionaries
+     * @return compression dictionary; otherwise, null if there is no 
dictionary
+     * @throws IOException on any I/O exception when reading from the file
+     */
+    @Nullable
+    static CompressionDictionary deserialize(DataInput input, @Nullable 
CompressionDictionaryManager manager) throws IOException
+    {
+        int kindOrdinal;
+        try
+        {
+            kindOrdinal = input.readByte();
+        }
+        catch (EOFException eof)
+        {
+            // no dictionary
+            return null;
+        }
+
+        if (kindOrdinal < 0 || kindOrdinal >= Kind.values().length)
+        {
+            throw new IOException("Invalid compression dictionary kind: " + 
kindOrdinal);
+        }
+        Kind kind = Kind.values()[kindOrdinal];
+        long id = input.readLong();
+        DictId dictId = new DictId(kind, id);
+
+        if (manager != null)
+        {
+            CompressionDictionary dictionary = manager.get(dictId);
+            if (dictionary != null)
+            {
+                return dictionary;
+            }
+        }
+
+        int length = input.readInt();
+        byte[] dict = new byte[length];
+        input.readFully(dict);
+        int checksum = input.readInt();
+        int calculatedChecksum = calculateChecksum((byte) kindOrdinal, id, 
dict);
+        if (checksum != calculatedChecksum)
+        {
+            throw new IOException("Compression dictionary checksum does not 
match");
+        }
+
+        CompressionDictionary dictionary = null;

Review Comment:
   Addressed



-- 
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