|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datasketches.kll2; |
| 21 | + |
| 22 | +import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR; |
| 23 | +import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR_SINGLE_ITEM; |
| 24 | +import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryK; |
| 25 | +import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryLevelZeroSortedFlag; |
| 26 | +import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryM; |
| 27 | +import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryMinK; |
| 28 | +import static org.apache.datasketches.kll.KllPreambleUtil.getMemoryN; |
| 29 | +import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_EMPTY; |
| 30 | +import static org.apache.datasketches.kll.KllSketch.SketchStructure.COMPACT_SINGLE; |
| 31 | + |
| 32 | +import java.lang.reflect.Array; |
| 33 | +import java.util.Comparator; |
| 34 | + |
| 35 | +import org.apache.datasketches.common.ArrayOfBooleansSerDe; |
| 36 | +import org.apache.datasketches.common.ArrayOfItemsSerDe; |
| 37 | +import org.apache.datasketches.common.SketchesArgumentException; |
| 38 | +import org.apache.datasketches.memory.Memory; |
| 39 | +import org.apache.datasketches.memory.WritableMemory; |
| 40 | + |
| 41 | +/** |
| 42 | + * This class implements an off-heap, read-only KllItemsSketch using WritableMemory. |
| 43 | + * |
| 44 | + * <p>Please refer to the documentation in the package-info:<br> |
| 45 | + * {@link org.apache.datasketches.kll}</p> |
| 46 | + * |
| 47 | + * @author Lee Rhodes, Kevin Lang |
| 48 | + */ |
| 49 | +@SuppressWarnings("unchecked") |
| 50 | +final class KllDirectCompactItemsSketch<T> extends KllItemsSketch<T> { |
| 51 | + private Memory mem; |
| 52 | + |
| 53 | + /** |
| 54 | + * Internal implementation of the wrapped Memory KllSketch. |
| 55 | + * @param memVal the MemoryValadate object |
| 56 | + * @param comparator to compare items |
| 57 | + * @param serDe Serializer / deserializer for items of type <i>T</i> and <i>T[]</i>. |
| 58 | + */ |
| 59 | + KllDirectCompactItemsSketch( //called below and KllItemsSketch |
| 60 | + final KllMemorSegmentValidate memVal, |
| 61 | + final Comparator<? super T> comparator, |
| 62 | + final ArrayOfItemsSerDe<T> serDe) { |
| 63 | + super(memVal.sketchStructure, comparator, serDe); |
| 64 | + this.mem = memVal.srcMem; |
| 65 | + readOnly = true; |
| 66 | + levelsArr = memVal.levelsArr; //always converted to writable form. |
| 67 | + } |
| 68 | + |
| 69 | + //End of constructors |
| 70 | + |
| 71 | + @Override |
| 72 | + String getItemAsString(final int index) { |
| 73 | + if (isEmpty()) { return "Null"; } |
| 74 | + return serDe.toString(getTotalItemsArray()[index]); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public int getK() { |
| 79 | + return getMemorySegmentK(mem); |
| 80 | + } |
| 81 | + |
| 82 | + //MinMax Methods |
| 83 | + |
| 84 | + @Override |
| 85 | + public T getMaxItem() { |
| 86 | + if (sketchStructure == COMPACT_EMPTY || isEmpty()) { |
| 87 | + throw new SketchesArgumentException(EMPTY_MSG); |
| 88 | + } |
| 89 | + if (sketchStructure == COMPACT_SINGLE) { |
| 90 | + return serDe.deserializeFromMemory(mem, DATA_START_ADR_SINGLE_ITEM, 1)[0]; |
| 91 | + } |
| 92 | + //sketchStructure == COMPACT_FULL |
| 93 | + final int baseOffset = DATA_START_ADR + getNumLevels() * Integer.BYTES; |
| 94 | + final int offset = baseOffset + serDe.sizeOf(mem, baseOffset, 1); //size of minItem |
| 95 | + |
| 96 | + return serDe.deserializeFromMemory(mem, offset, 1)[0]; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + String getMaxItemAsString() { |
| 101 | + if (isEmpty()) { return "Null"; } |
| 102 | + return serDe.toString(getMaxItem()); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public T getMinItem() { |
| 107 | + if (sketchStructure == COMPACT_EMPTY || isEmpty()) { |
| 108 | + throw new SketchesArgumentException(EMPTY_MSG); |
| 109 | + } |
| 110 | + if (sketchStructure == COMPACT_SINGLE) { |
| 111 | + return serDe.deserializeFromMemory(mem, DATA_START_ADR_SINGLE_ITEM, 1)[0]; |
| 112 | + } |
| 113 | + //sketchStructure == COMPACT_FULL |
| 114 | + final int offset = DATA_START_ADR + getNumLevels() * Integer.BYTES; |
| 115 | + return serDe.deserializeFromMemory(mem, offset, 1)[0]; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + String getMinItemAsString() { |
| 120 | + if (isEmpty()) { return "Null"; } |
| 121 | + return serDe.toString(getMinItem()); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public long getN() { |
| 126 | + if (sketchStructure == COMPACT_EMPTY) { return 0; } |
| 127 | + if (sketchStructure == COMPACT_SINGLE) { return 1; } |
| 128 | + return getMemorySegmentN(mem); |
| 129 | + } |
| 130 | + |
| 131 | + //restricted |
| 132 | + |
| 133 | + private int getCompactDataOffset() { //Sketch cannot be empty |
| 134 | + return sketchStructure == COMPACT_SINGLE |
| 135 | + ? DATA_START_ADR_SINGLE_ITEM |
| 136 | + : DATA_START_ADR + getNumLevels() * Integer.BYTES + getMinMaxSizeBytes(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + int getM() { |
| 141 | + return getMemorySegmentM(mem); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + int getMinK() { |
| 146 | + if (sketchStructure == COMPACT_EMPTY || sketchStructure == COMPACT_SINGLE) { return getMemorySegmentK(mem); } |
| 147 | + return getMemorySegmentMinK(mem); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + byte[] getMinMaxByteArr() { //this is only used by COMPACT_FULL |
| 152 | + final int offset = DATA_START_ADR + getNumLevels() * Integer.BYTES; |
| 153 | + final int bytesMinMax = serDe.sizeOf(mem, offset, 2); |
| 154 | + final byte[] byteArr = new byte[bytesMinMax]; |
| 155 | + mem.getByteArray(offset, byteArr, 0, bytesMinMax); |
| 156 | + return byteArr; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + int getMinMaxSizeBytes() { //this is only used by COMPACT_FULL |
| 161 | + final int offset = DATA_START_ADR + getNumLevels() * Integer.BYTES; |
| 162 | + if (serDe instanceof ArrayOfBooleansSerDe) { return 2; } |
| 163 | + return serDe.sizeOf(mem, offset, 2); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + T[] getRetainedItemsArray() { |
| 168 | + final int numRet = getNumRetained(); |
| 169 | + if (sketchStructure == COMPACT_EMPTY || getN() == 0) { |
| 170 | + return (T[]) Array.newInstance(serDe.getClassOfT(), numRet); |
| 171 | + } |
| 172 | + final int offset = getCompactDataOffset(); //both single & full |
| 173 | + return serDe.deserializeFromMemory(mem, offset, numRet); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + byte[] getRetainedItemsByteArr() { |
| 178 | + if (sketchStructure == COMPACT_EMPTY || getN() == 0) { return new byte[0]; } |
| 179 | + final int offset = getCompactDataOffset(); //both single & full |
| 180 | + final int bytes = serDe.sizeOf(mem, offset, getNumRetained()); |
| 181 | + final byte[] byteArr = new byte[bytes]; |
| 182 | + mem.getByteArray(offset, byteArr, 0, bytes); |
| 183 | + return byteArr; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + int getRetainedItemsSizeBytes() { |
| 188 | + if (sketchStructure == COMPACT_EMPTY || getN() == 0) { return 0; } |
| 189 | + final int offset = getCompactDataOffset(); //both single & full |
| 190 | + return serDe.sizeOf(mem, offset, getNumRetained()); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + T getSingleItem() { |
| 195 | + if (getN() != 1) { throw new SketchesArgumentException(NOT_SINGLE_ITEM_MSG); } |
| 196 | + final int offset = getCompactDataOffset(); //both single & full |
| 197 | + return (serDe.deserializeFromMemory(mem, offset, 1)[0]); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + byte[] getSingleItemByteArr() { |
| 202 | + if (getN() != 1) { throw new SketchesArgumentException(NOT_SINGLE_ITEM_MSG); } |
| 203 | + final int offset = getCompactDataOffset(); //both single & full |
| 204 | + final int bytes = serDe.sizeOf(mem, offset, 1); |
| 205 | + final byte[] byteArr = new byte[bytes]; |
| 206 | + mem.getByteArray(offset, byteArr, 0, bytes); |
| 207 | + return byteArr; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + int getSingleItemSizeBytes() { |
| 212 | + if (getN() != 1) { throw new SketchesArgumentException(NOT_SINGLE_ITEM_MSG); } |
| 213 | + final int offset = getCompactDataOffset(); //both single & full |
| 214 | + final int bytes = serDe.sizeOf(mem, offset, 1); |
| 215 | + return bytes; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + T[] getTotalItemsArray() { |
| 220 | + final int k = getK(); |
| 221 | + if (getN() == 0) { return (T[]) Array.newInstance(serDe.getClassOfT(), k); } |
| 222 | + if (getN() == 1) { |
| 223 | + final T[] itemsArr = (T[]) Array.newInstance(serDe.getClassOfT(), k); |
| 224 | + itemsArr[k - 1] = getSingleItem(); |
| 225 | + return itemsArr; |
| 226 | + } |
| 227 | + final int offset = getCompactDataOffset(); |
| 228 | + final int numRetItems = getNumRetained(); |
| 229 | + final int numCapItems = levelsArr[getNumLevels()]; |
| 230 | + final T[] retItems = serDe.deserializeFromMemory(mem, offset, numRetItems); |
| 231 | + final T[] capItems = (T[]) Array.newInstance(serDe.getClassOfT(), numCapItems); |
| 232 | + System.arraycopy(retItems, 0, capItems, levelsArr[0], numRetItems); |
| 233 | + return capItems; |
| 234 | + } |
| 235 | + |
| 236 | + @Override |
| 237 | + WritableMemory getWritableMemory() { |
| 238 | + return (WritableMemory)mem; |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + void incN(final int increment) { |
| 243 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + boolean isLevelZeroSorted() { |
| 248 | + return getMemorySegmentLevelZeroSortedFlag(mem); |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + void setItemsArray(final Object[] ItemsArr) { |
| 253 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + void setItemsArrayAt(final int index, final Object item) { |
| 258 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 259 | + } |
| 260 | + |
| 261 | + @Override |
| 262 | + void setLevelZeroSorted(final boolean sorted) { |
| 263 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 264 | + } |
| 265 | + |
| 266 | + @Override |
| 267 | + void setMaxItem(final Object item) { |
| 268 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 269 | + } |
| 270 | + |
| 271 | + @Override |
| 272 | + void setMinItem(final Object item) { |
| 273 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 274 | + } |
| 275 | + |
| 276 | + @Override |
| 277 | + void setMinK(final int minK) { |
| 278 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 279 | + } |
| 280 | + |
| 281 | + @Override |
| 282 | + void setN(final long n) { |
| 283 | + throw new SketchesArgumentException(UNSUPPORTED_MSG); |
| 284 | + } |
| 285 | + |
| 286 | +} |
0 commit comments