Search in sources :

Example 6 with ByteBufferInputStream

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());
}
Also used : ByteBufferInputStream(org.apache.druid.io.ByteBufferInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 7 with ByteBufferInputStream

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());
}
Also used : ByteBufferInputStream(org.apache.druid.io.ByteBufferInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 8 with ByteBufferInputStream

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());
}
Also used : ByteBufferInputStream(org.apache.druid.io.ByteBufferInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8 ByteBufferInputStream (org.apache.druid.io.ByteBufferInputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Test (org.junit.Test)7 Preconditions (com.google.common.base.Preconditions)1 ByteSource (com.google.common.io.ByteSource)1 Ints (com.google.common.primitives.Ints)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 ArrayList (java.util.ArrayList)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Channels (org.apache.druid.io.Channels)1 IAE (org.apache.druid.java.util.common.IAE)1