Search in sources :

Example 1 with GreaterThanEqualsPredicate

use of com.srotya.sidewinder.core.predicates.GreaterThanEqualsPredicate in project sidewinder by srotya.

the class TestByzantineReadWrite method testPredicateFilter.

@Test
public void testPredicateFilter() throws IOException {
    ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024 * 10);
    ByzantineWriter writer = new ByzantineWriter();
    writer.configure(new HashMap<>(), buf, true, startOffset, true);
    long ots = System.currentTimeMillis();
    writer.setHeaderTimestamp(ots);
    int limit = 1_000_000;
    for (int i = 0; i < limit; i++) {
        writer.addValue(ots + i * 1000, i);
    }
    Reader reader = writer.getReader();
    reader.setTimePredicate(new GreaterThanEqualsPredicate(ots + 1000));
    int c = 0;
    assertEquals(limit, reader.getPairCount());
    try {
        for (int i = 0; i < limit; i++) {
            DataPoint pair = reader.readPair();
            if (pair != null) {
                assertEquals(ots + i * 1000, pair.getTimestamp());
                assertEquals(i, pair.getLongValue());
                c++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    assertEquals(limit - 1, c);
    reader = writer.getReader();
    reader.setValuePredicate(new GreaterThanEqualsPredicate(1000));
    c = 0;
    assertEquals(limit, reader.getPairCount());
    try {
        for (int i = 0; i < limit; i++) {
            DataPoint pair = reader.readPair();
            if (pair != null) {
                assertEquals(ots + i * 1000, pair.getTimestamp());
                assertEquals(i, pair.getLongValue());
                c++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    try {
        reader.readPair();
        fail("Must throw end of stream exception");
    } catch (RejectException e) {
    }
    try {
        reader.read();
        fail("Must throw end of stream exception");
    } catch (RejectException e) {
    }
    assertEquals(limit - 1000, c);
    assertEquals(limit, reader.getCounter());
    reader = writer.getReader();
    reader.setTimePredicate(new GreaterThanEqualsPredicate(ots + 1000));
    c = 0;
    assertEquals(limit, reader.getPairCount());
    try {
        for (int i = 0; i < limit; i++) {
            long[] pair = reader.read();
            if (pair != null) {
                assertEquals(ots + i * 1000, pair[0]);
                assertEquals(i, pair[1]);
                c++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    assertEquals(limit - 1, c);
    reader = writer.getReader();
    reader.setValuePredicate(new GreaterThanEqualsPredicate(1000));
    c = 0;
    assertEquals(limit, reader.getPairCount());
    try {
        for (int i = 0; i < limit; i++) {
            long[] pair = reader.read();
            if (pair != null) {
                assertEquals(ots + i * 1000, pair[0]);
                assertEquals(i, pair[1]);
                c++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    assertEquals(limit - 1000, c);
}
Also used : DataPoint(com.srotya.sidewinder.core.storage.DataPoint) Reader(com.srotya.sidewinder.core.storage.compression.Reader) RejectException(com.srotya.sidewinder.core.storage.RejectException) GreaterThanEqualsPredicate(com.srotya.sidewinder.core.predicates.GreaterThanEqualsPredicate) ByteBuffer(java.nio.ByteBuffer) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) IOException(java.io.IOException) RejectException(com.srotya.sidewinder.core.storage.RejectException) RollOverException(com.srotya.sidewinder.core.storage.compression.RollOverException) Test(org.junit.Test)

Aggregations

GreaterThanEqualsPredicate (com.srotya.sidewinder.core.predicates.GreaterThanEqualsPredicate)1 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)1 RejectException (com.srotya.sidewinder.core.storage.RejectException)1 Reader (com.srotya.sidewinder.core.storage.compression.Reader)1 RollOverException (com.srotya.sidewinder.core.storage.compression.RollOverException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Test (org.junit.Test)1