smiklosovic commented on code in PR #4458:
URL: https://github.com/apache/cassandra/pull/4458#discussion_r2517783374


##########
src/java/org/apache/cassandra/db/compression/CompressionDictionaryDetailsTabularData.java:
##########
@@ -0,0 +1,304 @@
+/*
+ * 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.util.Arrays;
+import javax.management.openmbean.ArrayType;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.TabularType;
+
+import 
org.apache.cassandra.db.compression.CompressionDictionary.LightweightCompressionDictionary;
+import org.apache.cassandra.io.util.FileUtils;
+
+import static java.lang.String.format;
+
+public class CompressionDictionaryDetailsTabularData
+{
+    /**
+     * Position inside index names of tabular type of tabular data returned 
upon
+     * listing dictionaries where raw dictionary is expected to be located.
+     * We do not need to process this entry as listing does not contain any 
raw dictionary,
+     * only exporting does.
+     */
+    public static final int TABULAR_DATA_TYPE_RAW_DICTIONARY_INDEX = 3;
+
+    public static final String KEYSPACE_NAME = "Keyspace";
+    public static final String TABLE_NAME = "Table";
+    public static final String DICT_ID_NAME = "DictId";
+    public static final String DICT_NAME = "Dict";
+    public static final String KIND_NAME = "Kind";
+    public static final String CHECKSUM_NAME = "Checksum";
+    public static final String SIZE_NAME = "Size";
+
+
+    private static final String[] ITEM_NAMES = new String[]{ KEYSPACE_NAME,
+                                                             TABLE_NAME,
+                                                             DICT_ID_NAME,
+                                                             DICT_NAME,
+                                                             KIND_NAME,
+                                                             CHECKSUM_NAME,
+                                                             SIZE_NAME };
+
+    private static final String[] ITEM_DESCS = new String[]{ "keyspace",
+                                                             "table",
+                                                             "dictionary_id",
+                                                             
"dictionary_bytes",
+                                                             "kind",
+                                                             "checksum",
+                                                             "size" };
+
+    private static final String TYPE_NAME = "DictionaryDetails";
+    private static final String ROW_DESC = "DictionaryDetails";
+    private static final OpenType<?>[] ITEM_TYPES;
+    private static final CompositeType COMPOSITE_TYPE;
+    public static final TabularType TABULAR_TYPE;
+
+    static
+    {
+        try
+        {
+            ITEM_TYPES = new OpenType[]{ SimpleType.STRING, // keyspace
+                                         SimpleType.STRING, // table
+                                         SimpleType.LONG, // dict id
+                                         new 
ArrayType<String[]>(SimpleType.BYTE, true), // dict bytes
+                                         SimpleType.STRING, // kind
+                                         SimpleType.INTEGER, // checksum
+                                         SimpleType.INTEGER }; // size of dict 
bytes
+
+            COMPOSITE_TYPE = new CompositeType(TYPE_NAME, ROW_DESC, 
ITEM_NAMES, ITEM_DESCS, ITEM_TYPES);
+            TABULAR_TYPE = new TabularType(TYPE_NAME, ROW_DESC, 
COMPOSITE_TYPE, ITEM_NAMES);
+        }
+        catch (OpenDataException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * This method is meant to be call when listing dictionaries, we do not 
need actual dictionary byte arrays.
+     *
+     * @param dictionary lightweight dictionary to create composite data from
+     * @return composite data representing dictionary
+     */
+    public static CompositeData 
fromLightweightCompressionDictionary(LightweightCompressionDictionary 
dictionary)
+    {
+        try
+        {
+            return new CompositeDataSupport(COMPOSITE_TYPE,
+                                            ITEM_NAMES,
+                                            new Object[]
+                                            {
+                                            dictionary.keyspaceName,
+                                            dictionary.tableName,
+                                            dictionary.dictId.id,
+                                            null, // on purpose not returning 
actual dictionary
+                                            dictionary.dictId.kind.name(),
+                                            dictionary.checksum,
+                                            dictionary.size,
+                                            });
+        }
+        catch (OpenDataException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Used upon exporting a dictionary.
+     *
+     * @param keyspace   keyspace of a dictionary
+     * @param table      table of a dictionary
+     * @param dictionary dictionary itself
+     * @return composite data representing dictionary
+     */
+    public static CompositeData fromCompressionDictionary(String keyspace, 
String table, CompressionDictionary dictionary)
+    {
+        try
+        {
+            return new CompositeDataSupport(COMPOSITE_TYPE,
+                                            ITEM_NAMES,
+                                            new Object[]
+                                            {
+                                            keyspace,
+                                            table,
+                                            dictionary.dictId().id,
+                                            dictionary.rawDictionary(),
+                                            dictionary.kind().name(),
+                                            dictionary.checksum(),
+                                            dictionary.rawDictionary().length,
+                                            });
+        }
+        catch (OpenDataException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Used upon deserialisation of data to composite data, e.g. upon 
importing a dictionary from JSON
+     * string to CompositeData before they are sent over JMX to import method.
+     *
+     * @param dataObject data class object to get composite data from
+     * @return composite data representing data class
+     */
+    public static CompositeData 
fromCompressionDictionaryDataObject(CompressionDictionaryDataObject dataObject)
+    {
+        try
+        {
+            return new CompositeDataSupport(COMPOSITE_TYPE,
+                                            ITEM_NAMES,
+                                            new Object[]
+                                            {
+                                            dataObject.keyspace,
+                                            dataObject.table,
+                                            dataObject.dictId,
+                                            dataObject.dict,
+                                            dataObject.kind,
+                                            dataObject.dictChecksum,
+                                            dataObject.dictLength
+                                            });
+        }
+        catch (OpenDataException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Deserializes data to convenience object to work further with.
+     *
+     * @param compositeData data to create data object from
+     * @return deserialized composite data to convenience object
+     * @throws IllegalArgumentException if values in deserialized object are 
invalid.
+     * @see CompressionDictionaryDataObject#validate()
+     */
+    public static CompressionDictionaryDataObject 
fromCompositeData(CompositeData compositeData)
+    {
+        String keyspace = (String) 
compositeData.get(CompressionDictionaryDetailsTabularData.KEYSPACE_NAME);
+        String table = (String) 
compositeData.get(CompressionDictionaryDetailsTabularData.TABLE_NAME);
+        long dictId = (Long) 
compositeData.get(CompressionDictionaryDetailsTabularData.DICT_ID_NAME);
+        byte[] dictionaryBytes = (byte[]) 
compositeData.get(CompressionDictionaryDetailsTabularData.DICT_NAME);
+        String kind = (String) 
compositeData.get(CompressionDictionaryDetailsTabularData.KIND_NAME);
+        int checksum = (Integer) 
compositeData.get(CompressionDictionaryDetailsTabularData.CHECKSUM_NAME);
+        int size = (Integer) 
compositeData.get(CompressionDictionaryDetailsTabularData.SIZE_NAME);
+
+        CompressionDictionaryDataObject dataObject = new 
CompressionDictionaryDataObject();
+        dataObject.keyspace = keyspace;
+        dataObject.table = table;
+        dataObject.dictId = dictId;
+        dataObject.dict = dictionaryBytes;
+        dataObject.kind = kind;
+        dataObject.dictChecksum = checksum;
+        dataObject.dictLength = size;
+
+        dataObject.validate();
+
+        return dataObject;
+    }
+
+    public static class CompressionDictionaryDataObject implements Cloneable
+    {
+        public String keyspace;
+        public String table;
+        public long dictId;
+        public byte[] dict;
+        public String kind;
+        public int dictChecksum;
+        public int dictLength;

Review Comment:
   This would complicate tests a bit etc. I have made it final but then I used 
builder to copy it and builder's methods to override any value.
   
   Builder is also validating that object and constructor of this class is made 
`private` so it can ever be constructed in the code by a builder only.
   
   JSON deserialisation from String to that object works too by adding 
appropriate annotations on that constructor. We need to deserialize from String 
(file) to an instance of that object when we go to import.



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