use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class MurmurHash3 method hash.
// --Hash of ByteBuffer---------------------------------------------------
/**
* Hash the remaining bytes of the given ByteBuffer starting at position().
*
* @param buf The input ByteBuffer. It must be non-null and non-empty.
* @param seed A long valued seed.
* @return a 128-bit hash of the input as a long array of size 2.
*/
public static long[] hash(final ByteBuffer buf, final long seed) {
Objects.requireNonNull(buf);
final int pos = buf.position();
final int rem = buf.remaining();
checkPositive(rem);
final Memory mem = Memory.wrap(buf, ByteOrder.LITTLE_ENDIAN).region(pos, rem);
return hash(mem, seed);
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class ReservoirItemsSketchTest method checkBadMemory.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadMemory() {
byte[] bytes = new byte[4];
Memory mem = Memory.wrap(bytes);
try {
PreambleUtil.getAndCheckPreLongs(mem);
fail();
} catch (final SketchesArgumentException e) {
// expected
}
bytes = new byte[8];
// only 1 preLong worth of items in bytearray
bytes[0] = 2;
mem = Memory.wrap(bytes);
PreambleUtil.getAndCheckPreLongs(mem);
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class ReservoirItemsSketchTest method checkEmptySketch.
@Test
public void checkEmptySketch() {
final ReservoirItemsSketch<String> ris = ReservoirItemsSketch.newInstance(5);
assertTrue(ris.getSamples() == null);
final byte[] sketchBytes = ris.toByteArray(new ArrayOfStringsSerDe());
final Memory mem = Memory.wrap(sketchBytes);
// only minPreLongs bytes and should deserialize to empty
assertEquals(sketchBytes.length, Family.RESERVOIR.getMinPreLongs() << 3);
final ArrayOfStringsSerDe serDe = new ArrayOfStringsSerDe();
final ReservoirItemsSketch<String> loadedRis = ReservoirItemsSketch.heapify(mem, serDe);
assertEquals(loadedRis.getNumSamples(), 0);
println("Empty sketch:");
println(" Preamble:");
println(PreambleUtil.preambleToString(mem));
println(" Sketch:");
println(ris.toString());
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class DoublesUnionBuilderTest method checkBuilds.
@Test
public void checkBuilds() {
UpdateDoublesSketch qs1 = DoublesSketch.builder().build();
for (int i = 0; i < 1000; i++) {
qs1.update(i);
}
int bytes = qs1.getCompactStorageBytes();
WritableMemory dstMem = WritableMemory.writableWrap(new byte[bytes]);
qs1.putMemory(dstMem);
Memory srcMem = dstMem;
DoublesUnionBuilder bldr = new DoublesUnionBuilder();
bldr.setMaxK(128);
// virgin union
DoublesUnion union = bldr.build();
union = DoublesUnion.heapify(srcMem);
DoublesSketch qs2 = union.getResult();
assertEquals(qs1.getCompactStorageBytes(), qs2.getCompactStorageBytes());
union = DoublesUnion.heapify(qs2);
DoublesSketch qs3 = union.getResult();
assertEquals(qs2.getCompactStorageBytes(), qs3.getCompactStorageBytes());
assertFalse(qs2 == qs3);
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class DoublesUtilTest method checkPrintMemData2.
@Test
public void checkPrintMemData2() {
final int k = PreambleUtil.DEFAULT_K;
final int n = 0;
final DoublesSketch qs = buildAndLoadQS(k, n);
final byte[] byteArr = qs.toByteArray();
final Memory mem = Memory.wrap(byteArr);
println(DoublesUtil.memToString(true, true, mem));
}
Aggregations