|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.db.compression; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import javax.management.openmbean.ArrayType; |
| 23 | +import javax.management.openmbean.CompositeData; |
| 24 | +import javax.management.openmbean.CompositeDataSupport; |
| 25 | +import javax.management.openmbean.CompositeType; |
| 26 | +import javax.management.openmbean.OpenDataException; |
| 27 | +import javax.management.openmbean.OpenType; |
| 28 | +import javax.management.openmbean.SimpleType; |
| 29 | +import javax.management.openmbean.TabularDataSupport; |
| 30 | +import javax.management.openmbean.TabularType; |
| 31 | + |
| 32 | +public class CompressionDictionaryDetailsTabularData |
| 33 | +{ |
| 34 | + public static final String KEYSPACE_NAME = "Keyspace"; |
| 35 | + public static final String TABLE_NAME = "Table"; |
| 36 | + public static final String DICT_ID_NAME = "DictId"; |
| 37 | + public static final String DICT_NAME = "Dict"; |
| 38 | + public static final String KIND_NAME = "Kind"; |
| 39 | + public static final String CHECKSUM_NAME = "Checksum"; |
| 40 | + public static final String SIZE_NAME = "Size"; |
| 41 | + |
| 42 | + |
| 43 | + private static final String[] ITEM_NAMES = new String[]{ KEYSPACE_NAME, |
| 44 | + TABLE_NAME, |
| 45 | + DICT_ID_NAME, |
| 46 | + DICT_NAME, |
| 47 | + KIND_NAME, |
| 48 | + CHECKSUM_NAME, |
| 49 | + SIZE_NAME }; |
| 50 | + |
| 51 | + private static final String[] ITEM_DESCS = new String[]{ "keyspace", |
| 52 | + "table", |
| 53 | + "dictionary_id", |
| 54 | + "dictionary_bytes", |
| 55 | + "kind", |
| 56 | + "checksum", |
| 57 | + "size" }; |
| 58 | + |
| 59 | + private static final String TYPE_NAME = "DictionaryDetails"; |
| 60 | + private static final String ROW_DESC = "DictionaryDetails"; |
| 61 | + private static final OpenType<?>[] ITEM_TYPES; |
| 62 | + private static final CompositeType COMPOSITE_TYPE; |
| 63 | + public static final TabularType TABULAR_TYPE; |
| 64 | + |
| 65 | + static |
| 66 | + { |
| 67 | + try |
| 68 | + { |
| 69 | + ITEM_TYPES = new OpenType[]{ SimpleType.STRING, // keyspace |
| 70 | + SimpleType.STRING, // table |
| 71 | + SimpleType.LONG, // dict id |
| 72 | + new ArrayType<String[]>(SimpleType.BYTE, true), // dict bytes |
| 73 | + SimpleType.STRING, // kind |
| 74 | + SimpleType.INTEGER, // checksum |
| 75 | + SimpleType.INTEGER }; // size of dict bytes |
| 76 | + |
| 77 | + COMPOSITE_TYPE = new CompositeType(TYPE_NAME, ROW_DESC, ITEM_NAMES, ITEM_DESCS, ITEM_TYPES); |
| 78 | + TABULAR_TYPE = new TabularType(TYPE_NAME, ROW_DESC, COMPOSITE_TYPE, ITEM_NAMES); |
| 79 | + } |
| 80 | + catch (OpenDataException e) |
| 81 | + { |
| 82 | + throw new RuntimeException(e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static void from(String keyspace, |
| 87 | + String table, |
| 88 | + CompressionDictionary dictionary, |
| 89 | + TabularDataSupport result) |
| 90 | + { |
| 91 | + result.put(from(keyspace, table, dictionary)); |
| 92 | + } |
| 93 | + |
| 94 | + public static CompositeData from(String keyspace, String table, CompressionDictionary dictionary) |
| 95 | + { |
| 96 | + try |
| 97 | + { |
| 98 | + return new CompositeDataSupport(COMPOSITE_TYPE, |
| 99 | + ITEM_NAMES, |
| 100 | + new Object[] |
| 101 | + { |
| 102 | + keyspace, |
| 103 | + table, |
| 104 | + dictionary.dictId().id, |
| 105 | + dictionary.rawDictionary(), |
| 106 | + dictionary.kind().name(), |
| 107 | + dictionary.checksum(), |
| 108 | + dictionary.size(), |
| 109 | + }); |
| 110 | + } |
| 111 | + catch (OpenDataException e) |
| 112 | + { |
| 113 | + throw new RuntimeException(e); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public static CompositeData from(CompressionDictionaryPojo pojo) |
| 118 | + { |
| 119 | + try |
| 120 | + { |
| 121 | + return new CompositeDataSupport(COMPOSITE_TYPE, |
| 122 | + ITEM_NAMES, |
| 123 | + new Object[] |
| 124 | + { |
| 125 | + pojo.keyspace, |
| 126 | + pojo.table, |
| 127 | + pojo.dictId, |
| 128 | + pojo.dict, |
| 129 | + pojo.kind, |
| 130 | + pojo.checksum, |
| 131 | + pojo.size |
| 132 | + }); |
| 133 | + } |
| 134 | + catch (OpenDataException e) |
| 135 | + { |
| 136 | + throw new RuntimeException(e); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Deserializes data to convenience object to work further with. |
| 142 | + * |
| 143 | + * @param compositeData data to create pojo from |
| 144 | + * @return deserialized composite data to convenience object |
| 145 | + * @throws IllegalArgumentException if values in deserialized object are invalid. |
| 146 | + * @see CompressionDictionaryPojo#validate() |
| 147 | + */ |
| 148 | + public static CompressionDictionaryPojo from(CompositeData compositeData) |
| 149 | + { |
| 150 | + String keyspace = (String) compositeData.get(CompressionDictionaryDetailsTabularData.KEYSPACE_NAME); |
| 151 | + String table = (String) compositeData.get(CompressionDictionaryDetailsTabularData.TABLE_NAME); |
| 152 | + long dictId = (Long) compositeData.get(CompressionDictionaryDetailsTabularData.DICT_ID_NAME); |
| 153 | + byte[] dictionaryBytes = (byte[]) compositeData.get(CompressionDictionaryDetailsTabularData.DICT_NAME); |
| 154 | + String kind = (String) compositeData.get(CompressionDictionaryDetailsTabularData.KIND_NAME); |
| 155 | + int checksum = (Integer) compositeData.get(CompressionDictionaryDetailsTabularData.CHECKSUM_NAME); |
| 156 | + int size = (Integer) compositeData.get(CompressionDictionaryDetailsTabularData.SIZE_NAME); |
| 157 | + |
| 158 | + CompressionDictionaryPojo pojo = new CompressionDictionaryPojo(); |
| 159 | + pojo.keyspace = keyspace; |
| 160 | + pojo.table = table; |
| 161 | + pojo.dictId = dictId; |
| 162 | + pojo.dict = dictionaryBytes; |
| 163 | + pojo.kind = kind; |
| 164 | + pojo.checksum = checksum; |
| 165 | + pojo.size = size; |
| 166 | + |
| 167 | + pojo.validate(); |
| 168 | + |
| 169 | + return pojo; |
| 170 | + } |
| 171 | + |
| 172 | + public static class CompressionDictionaryPojo |
| 173 | + { |
| 174 | + public String keyspace; |
| 175 | + public String table; |
| 176 | + public long dictId; |
| 177 | + public byte[] dict; |
| 178 | + public String kind; |
| 179 | + public int checksum; |
| 180 | + public int size; |
| 181 | + |
| 182 | + /** |
| 183 | + * Dictionary is valid if, keyspace and table are specified, dictionary id is strictly positive integer, |
| 184 | + * dictionary byte array is not nor not empty, |
| 185 | + * kind corresponds to {@code Kind}, checksum and size are bigger than 0. |
| 186 | + */ |
| 187 | + public void validate() |
| 188 | + { |
| 189 | + if (keyspace == null) |
| 190 | + throw new IllegalArgumentException("keyspace not specified"); |
| 191 | + if (table == null) |
| 192 | + throw new IllegalArgumentException("table not specified"); |
| 193 | + if (dictId <= 0) |
| 194 | + throw new IllegalArgumentException("Provided dictionary id is lower than 0, it is '" + dictId + "'.'"); |
| 195 | + if (dict == null || dict.length == 0) |
| 196 | + throw new IllegalArgumentException("Provided dictionary byte array is null or empty."); |
| 197 | + if (kind == null) |
| 198 | + throw new IllegalArgumentException("Provided kind is null."); |
| 199 | + try |
| 200 | + { |
| 201 | + CompressionDictionary.Kind.valueOf(kind); |
| 202 | + } |
| 203 | + catch (IllegalArgumentException ex) |
| 204 | + { |
| 205 | + throw new IllegalArgumentException("There is no such dictionary kind like '" + kind + "'. Available kinds: " + Arrays.asList(CompressionDictionary.Kind.values())); |
| 206 | + } |
| 207 | + if (checksum <= 0) |
| 208 | + throw new IllegalArgumentException("Checksum has to be strictly positive number, it is '" + checksum + "'."); |
| 209 | + if (size <= 0) |
| 210 | + throw new IllegalArgumentException("Size has to be strictly positive number, it is '" + size + "'."); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments