Skip to content
Permalink
Browse files
Interim 15
  • Loading branch information
leerho committed Apr 13, 2022
1 parent 1b54257 commit a8717410b2a4e1351a006ffdc1ed015c717e2ebb
Showing 19 changed files with 647 additions and 739 deletions.
@@ -21,6 +21,7 @@

import static org.apache.datasketches.kll.KllPreambleUtil.DATA_START_ADR;
import static org.apache.datasketches.kll.KllPreambleUtil.DOUBLES_SKETCH_BIT_MASK;
import static org.apache.datasketches.kll.KllPreambleUtil.FLAGS_BYTE_ADR;
import static org.apache.datasketches.kll.KllPreambleUtil.PREAMBLE_INTS_FULL;
import static org.apache.datasketches.kll.KllPreambleUtil.SERIAL_VERSION_UPDATABLE;
import static org.apache.datasketches.kll.KllPreambleUtil.UPDATABLE_BIT_MASK;
@@ -56,10 +57,6 @@
* @author Lee Rhodes, Kevin Lang
*/
final class KllDirectDoublesSketch extends KllDoublesSketch {
final boolean updatableMemory;
WritableMemory levelsArrUpdatable;
WritableMemory minMaxArrUpdatable;
WritableMemory itemsArrUpdatable;

/**
* The constructor with Memory that can be off-heap.
@@ -70,10 +67,7 @@ final class KllDirectDoublesSketch extends KllDoublesSketch {
KllDirectDoublesSketch(final WritableMemory wmem, final MemoryRequestServer memReqSvr,
final KllMemoryValidate memVal) {
super(wmem, memReqSvr);
updatableMemory = memVal.updatableMemory && memReqSvr != null;
levelsArrUpdatable = memVal.levelsArrUpdatable;
minMaxArrUpdatable = memVal.minMaxArrUpdatable;
itemsArrUpdatable = memVal.itemsArrUpdatable;
levelsArr = memVal.levelsArr;
}

/**
@@ -115,79 +109,53 @@ public long getN() {
return getMemoryN(wmem);
}

@Override
public void reset() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int k = getK();
setN(0);
setMinK(k);
setNumLevels(1);
setLevelsArray(new int[] {k, k});
setLevelZeroSorted(false);
final int newLevelsArrLen = 2 * Integer.BYTES;
final int newItemsArrLen = k;
KllHelper.memorySpaceMgmt(this, newLevelsArrLen, newItemsArrLen);
levelsArrUpdatable.putIntArray(0L, new int[] {k, k}, 0, 2);
if (sketchType == SketchType.DOUBLES_SKETCH) {
minMaxArrUpdatable.putDoubleArray(0L, new double[] {Double.NaN, Double.NaN}, 0, 2);
itemsArrUpdatable.putDoubleArray(0L, new double[k], 0, k);
} else {
minMaxArrUpdatable.putFloatArray(0L, new float[] {Float.NaN, Float.NaN}, 0, 2);
itemsArrUpdatable.putFloatArray(0L, new float[k], 0, k);
}
}

@Override
public byte[] toUpdatableByteArray() {
final int bytes = (int) wmem.getCapacity();
final int bytes = getCurrentUpdatableSerializedSizeBytes();
final long n = getN();
final byte flags = (byte)(UPDATABLE_BIT_MASK | DOUBLES_SKETCH_BIT_MASK
| ((n == 0) ? 1 : 0) | ((n == 1) ? 4 : 0));
final byte[] byteArr = new byte[bytes];
wmem.getByteArray(0, byteArr, 0, bytes);
byteArr[FLAGS_BYTE_ADR] = flags;
return byteArr;
}

@Override
double[] getDoubleItemsArray() {
final int items = getItemsArrLengthItems();
final double[] itemsArr = new double[items];
itemsArrUpdatable.getDoubleArray(0, itemsArr, 0, items);
final int offset = DATA_START_ADR + getLevelsArray().length * Integer.BYTES + 2 * Double.BYTES;
wmem.getDoubleArray(offset, itemsArr, 0, items);
return itemsArr;
}

@Override
double getDoubleItemsArrayAt(final int index) {
return itemsArrUpdatable.getDouble((long)index * Double.BYTES);
final int offset =
DATA_START_ADR + getLevelsArray().length * Integer.BYTES + 2 * Double.BYTES + index * Double.BYTES;
return wmem.getDouble(offset);
}

int getItemsArrLengthItems() {
return getLevelsArray()[getNumLevels()];
}

@Override
int[] getLevelsArray() {
final int numInts = getNumLevels() + 1;
final int[] myLevelsArr = new int[numInts];
levelsArrUpdatable.getIntArray(0, myLevelsArr, 0, numInts);
return myLevelsArr;
}

@Override
int getLevelsArrayAt(final int index) {
return levelsArrUpdatable.getInt((long)index * Integer.BYTES);
}

@Override
int getM() {
return getMemoryM(wmem);
}

@Override
double getMaxDoubleValue() {
return minMaxArrUpdatable.getDouble(Double.BYTES);
final int offset = DATA_START_ADR + getLevelsArray().length * Integer.BYTES + Double.BYTES;
return wmem.getDouble(offset);
}

@Override
double getMinDoubleValue() {
return minMaxArrUpdatable.getDouble(0);
final int offset = DATA_START_ADR + getLevelsArray().length * Integer.BYTES;
return wmem.getDouble(offset);
}

@Override
@@ -202,14 +170,14 @@ int getNumLevels() {

@Override
void incN() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
long n = getMemoryN(wmem);
setMemoryN(wmem, ++n);
}

@Override
void incNumLevels() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
int numLevels = getMemoryNumLevels(wmem);
setMemoryNumLevels(wmem, ++numLevels);
}
@@ -221,95 +189,54 @@ boolean isLevelZeroSorted() {

@Override
void setDoubleItemsArray(final double[] doubleItems) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable.putDoubleArray(0, doubleItems, 0, doubleItems.length);
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = DATA_START_ADR + getLevelsArray().length * Integer.BYTES + 2 * Double.BYTES;
wmem.putDoubleArray(offset, doubleItems, 0, doubleItems.length);
}

@Override
void setDoubleItemsArrayAt(final int index, final double value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable.putDouble((long)index * Double.BYTES, value);
}

@Override
void setItemsArrayUpdatable(final WritableMemory itemsMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable = itemsMem;
}

@Override
void setLevelsArray(final int[] levelsArr) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable.putIntArray(0, levelsArr, 0, levelsArr.length);
}

@Override
void setLevelsArrayAt(final int index, final int value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable.putInt((long)index * Integer.BYTES, value);
}

@Override
void setLevelsArrayAtMinusEq(final int index, final int minusEq) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = index * Integer.BYTES;
final int curV = levelsArrUpdatable.getInt(offset);
levelsArrUpdatable.putInt(offset, curV - minusEq);
}

@Override
void setLevelsArrayAtPlusEq(final int index, final int plusEq) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = index * Integer.BYTES;
final int curV = levelsArrUpdatable.getInt(offset);
levelsArrUpdatable.putInt(offset, curV + plusEq);
}

@Override
void setLevelsArrayUpdatable(final WritableMemory levelsMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable = levelsMem;
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset =
DATA_START_ADR + getLevelsArray().length * Integer.BYTES + 2 * Double.BYTES + index * Double.BYTES;
wmem.putDouble(offset, value);
}

@Override
void setLevelZeroSorted(final boolean sorted) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryLevelZeroSortedFlag(wmem, sorted);
}

@Override
void setMaxDoubleValue(final double value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable.putDouble(Double.BYTES, value);
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = DATA_START_ADR + getLevelsArray().length * Integer.BYTES + Double.BYTES;
wmem.putDouble(offset, value);
}

@Override
void setMinDoubleValue(final double value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable.putDouble(0, value);
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = DATA_START_ADR + getLevelsArray().length * Integer.BYTES;
wmem.putDouble(offset, value);
}

@Override
void setMinK(final int minK) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryMinK(wmem, minK);
}

@Override
void setMinMaxArrayUpdatable(final WritableMemory minMaxMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable = minMaxMem;
}

@Override
void setN(final long n) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryN(wmem, n);
}

@Override
void setNumLevels(final int numLevels) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
if (readOnly) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryNumLevels(wmem, numLevels);
}

0 comments on commit a871741

Please sign in to comment.