use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkReadOnlyRebuildResize.
@Test
public void checkReadOnlyRebuildResize() {
int k = 1 << 12;
int u = 2 * k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
try (WritableHandle wdh = WritableMemory.allocateDirect(bytes / 2)) {
// will request
WritableMemory wmem = wdh.getWritable();
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
for (int i = 0; i < u; i++) {
sketch.update(i);
}
double est1 = sketch.getEstimate();
byte[] ser = sketch.toByteArray();
Memory mem = Memory.wrap(ser);
UpdateSketch roSketch = (UpdateSketch) Sketches.wrapSketch(mem);
double est2 = roSketch.getEstimate();
assertEquals(est2, est1);
try {
roSketch.rebuild();
fail();
} catch (SketchesReadOnlyException e) {
// expected
}
try {
roSketch.reset();
fail();
} catch (SketchesReadOnlyException e) {
// expected
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkUpperAndLowerBounds.
@Test
public void checkUpperAndLowerBounds() {
int k = 512;
int u = 2 * k;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
for (int i = 0; i < u; i++) {
usk.update(i);
}
double est = usk.getEstimate();
double ub = usk.getUpperBound(1);
double lb = usk.getLowerBound(1);
assertTrue(ub > est);
assertTrue(lb < est);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkDQStoCompactForms.
@Test
public void checkDQStoCompactForms() {
int k = 512;
int u = 4 * k;
boolean estimating = (u > k);
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
// for internal checks
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
assertEquals(usk.getClass().getSimpleName(), "DirectQuickSelectSketch");
assertTrue(usk.isDirect());
assertTrue(usk.hasMemory());
assertFalse(usk.isCompact());
assertFalse(usk.isOrdered());
for (int i = 0; i < u; i++) {
usk.update(i);
}
// forces size back to k
sk1.rebuild();
// get baseline values
double uskEst = usk.getEstimate();
double uskLB = usk.getLowerBound(2);
double uskUB = usk.getUpperBound(2);
assertEquals(usk.isEstimationMode(), estimating);
CompactSketch csk;
csk = usk.compact(false, null);
assertEquals(csk.getEstimate(), uskEst);
assertEquals(csk.getLowerBound(2), uskLB);
assertEquals(csk.getUpperBound(2), uskUB);
assertEquals(csk.isEmpty(), false);
assertEquals(csk.isEstimationMode(), estimating);
assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
csk = usk.compact(true, null);
assertEquals(csk.getEstimate(), uskEst);
assertEquals(csk.getLowerBound(2), uskLB);
assertEquals(csk.getUpperBound(2), uskUB);
assertEquals(csk.isEmpty(), false);
assertEquals(csk.isEstimationMode(), estimating);
assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
int bytes = usk.getCompactBytes();
assertEquals(bytes, (k * 8) + (Family.COMPACT.getMaxPreLongs() << 3));
byte[] memArr2 = new byte[bytes];
WritableMemory mem2 = WritableMemory.writableWrap(memArr2);
csk = usk.compact(false, mem2);
assertEquals(csk.getEstimate(), uskEst);
assertEquals(csk.getLowerBound(2), uskLB);
assertEquals(csk.getUpperBound(2), uskUB);
assertEquals(csk.isEmpty(), false);
assertEquals(csk.isEstimationMode(), estimating);
assertEquals(csk.getClass().getSimpleName(), "DirectCompactSketch");
mem2.clear();
csk = usk.compact(true, mem2);
assertEquals(csk.getEstimate(), uskEst);
assertEquals(csk.getLowerBound(2), uskLB);
assertEquals(csk.getUpperBound(2), uskUB);
assertEquals(csk.isEmpty(), false);
assertEquals(csk.isEstimationMode(), estimating);
assertEquals(csk.getClass().getSimpleName(), "DirectCompactSketch");
csk.toString(false, true, 0, false);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkErrorBounds.
@Test
public void checkErrorBounds() {
int k = 512;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
// Exact mode
for (int i = 0; i < k; i++) {
usk.update(i);
}
double est = usk.getEstimate();
double lb = usk.getLowerBound(2);
double ub = usk.getUpperBound(2);
assertEquals(est, ub, 0.0);
assertEquals(est, lb, 0.0);
// Est mode
int u = 100 * k;
for (int i = k; i < u; i++) {
usk.update(i);
// test duplicate rejection
usk.update(i);
}
est = usk.getEstimate();
lb = usk.getLowerBound(2);
ub = usk.getUpperBound(2);
assertTrue(est <= ub);
assertTrue(est >= lb);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableHandle in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkMoveAndResize.
@Test
public void checkMoveAndResize() {
int k = 1 << 12;
int u = 2 * k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
try (WritableHandle wdh = WritableMemory.allocateDirect(bytes / 2)) {
// will request
WritableMemory wmem = wdh.getWritable();
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
assertTrue(sketch.isSameResource(wmem));
for (int i = 0; i < u; i++) {
sketch.update(i);
}
assertFalse(sketch.isSameResource(wmem));
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Aggregations