use of org.apache.druid.io.ByteBufferInputStream in project druid by druid-io.
the class BloomKFilterTest method testBloomKFilterFloat.
@Test
public void testBloomKFilterFloat() throws IOException {
BloomKFilter bf = new BloomKFilter(10000);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
BloomKFilter.serialize(bytesOut, bf);
byte[] bfBytes = bytesOut.toByteArray();
ByteBuffer buffer = ByteBuffer.wrap(bfBytes);
float val = Float.NEGATIVE_INFINITY;
float val1 = 1.1f;
float val2 = 2.2f;
float val3 = Float.POSITIVE_INFINITY;
BloomKFilter.addFloat(buffer, val);
BloomKFilter rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testFloat(val));
Assert.assertFalse(rehydrated.testFloat(val1));
Assert.assertFalse(rehydrated.testFloat(val2));
Assert.assertFalse(rehydrated.testFloat(val3));
BloomKFilter.addFloat(buffer, val1);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testFloat(val));
Assert.assertTrue(rehydrated.testFloat(val1));
Assert.assertFalse(rehydrated.testFloat(val2));
Assert.assertFalse(rehydrated.testFloat(val3));
BloomKFilter.addFloat(buffer, val2);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testFloat(val));
Assert.assertTrue(rehydrated.testFloat(val1));
Assert.assertTrue(rehydrated.testFloat(val2));
Assert.assertFalse(rehydrated.testFloat(val3));
BloomKFilter.addFloat(buffer, val3);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testFloat(val));
Assert.assertTrue(rehydrated.testFloat(val1));
Assert.assertTrue(rehydrated.testFloat(val2));
Assert.assertTrue(rehydrated.testFloat(val3));
float randVal = 0;
for (int i = 0; i < COUNT; i++) {
randVal = rand.nextFloat();
BloomKFilter.addFloat(buffer, randVal);
}
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
// last value should be present
Assert.assertTrue(rehydrated.testFloat(randVal));
// most likely this value should not exist
Assert.assertFalse(rehydrated.testFloat(-120.2f));
Assert.assertEquals(7808, rehydrated.sizeInBytes());
}
use of org.apache.druid.io.ByteBufferInputStream in project druid by druid-io.
the class BloomKFilterTest method testBloomKFilterDouble.
@Test
public void testBloomKFilterDouble() throws IOException {
BloomKFilter bf = new BloomKFilter(10000);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
BloomKFilter.serialize(bytesOut, bf);
byte[] bfBytes = bytesOut.toByteArray();
ByteBuffer buffer = ByteBuffer.wrap(bfBytes);
double val = Double.NEGATIVE_INFINITY;
double val1 = 1.1d;
double val2 = 2.2d;
double val3 = Double.POSITIVE_INFINITY;
BloomKFilter.addDouble(buffer, val);
BloomKFilter rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testDouble(val));
Assert.assertFalse(rehydrated.testDouble(val1));
Assert.assertFalse(rehydrated.testDouble(val2));
Assert.assertFalse(rehydrated.testDouble(val3));
BloomKFilter.addDouble(buffer, val1);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testDouble(val));
Assert.assertTrue(rehydrated.testDouble(val1));
Assert.assertFalse(rehydrated.testDouble(val2));
Assert.assertFalse(rehydrated.testDouble(val3));
BloomKFilter.addDouble(buffer, val2);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testDouble(val));
Assert.assertTrue(rehydrated.testDouble(val1));
Assert.assertTrue(rehydrated.testDouble(val2));
Assert.assertFalse(rehydrated.testDouble(val3));
BloomKFilter.addDouble(buffer, val3);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testDouble(val));
Assert.assertTrue(rehydrated.testDouble(val1));
Assert.assertTrue(rehydrated.testDouble(val2));
Assert.assertTrue(rehydrated.testDouble(val3));
double randVal = 0;
for (int i = 0; i < COUNT; i++) {
randVal = rand.nextDouble();
BloomKFilter.addDouble(buffer, randVal);
}
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
// last value should be present
Assert.assertTrue(rehydrated.testDouble(randVal));
// most likely this value should not exist
Assert.assertFalse(rehydrated.testDouble(-120.2d));
Assert.assertEquals(7808, rehydrated.sizeInBytes());
}
use of org.apache.druid.io.ByteBufferInputStream in project druid by druid-io.
the class BloomKFilterTest method testBloomKFilterInt.
@Test
public void testBloomKFilterInt() throws IOException {
BloomKFilter bf = new BloomKFilter(10000);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
BloomKFilter.serialize(bytesOut, bf);
byte[] bfBytes = bytesOut.toByteArray();
ByteBuffer buffer = ByteBuffer.wrap(bfBytes);
int val = Integer.MIN_VALUE;
int val1 = 1;
int val2 = 2;
int val3 = Integer.MAX_VALUE;
BloomKFilter.addLong(buffer, val);
BloomKFilter rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testLong(val));
Assert.assertFalse(rehydrated.testLong(val1));
Assert.assertFalse(rehydrated.testLong(val2));
Assert.assertFalse(rehydrated.testLong(val3));
BloomKFilter.addLong(buffer, val1);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testLong(val));
Assert.assertTrue(rehydrated.testLong(val1));
Assert.assertFalse(rehydrated.testLong(val2));
Assert.assertFalse(rehydrated.testLong(val3));
BloomKFilter.addLong(buffer, val2);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testLong(val));
Assert.assertTrue(rehydrated.testLong(val1));
Assert.assertTrue(rehydrated.testLong(val2));
Assert.assertFalse(rehydrated.testLong(val3));
BloomKFilter.addLong(buffer, val3);
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
buffer.position(0);
Assert.assertTrue(rehydrated.testLong(val));
Assert.assertTrue(rehydrated.testLong(val1));
Assert.assertTrue(rehydrated.testLong(val2));
Assert.assertTrue(rehydrated.testLong(val3));
int randVal = 0;
for (int i = 0; i < COUNT; i++) {
randVal = rand.nextInt();
BloomKFilter.addLong(buffer, randVal);
}
rehydrated = BloomKFilter.deserialize(new ByteBufferInputStream(buffer));
// last value should be present
Assert.assertTrue(rehydrated.testLong(randVal));
// most likely this value should not exist
Assert.assertFalse(rehydrated.testLong(-120));
Assert.assertEquals(7808, rehydrated.sizeInBytes());
}
Aggregations