Search in sources :

Example 1 with DirectInputStream

use of com.questdb.std.DirectInputStream in project questdb by bluestreak01.

the class VariableColumnTest method testCopyBinaryColumnData.

@Test
public void testCopyBinaryColumnData() throws Exception {
    int bitHint = 8;
    try (MemoryFile smallFile = new MemoryFile(new File(temporaryFolder.getRoot(), "small.d"), bitHint, JournalMode.APPEND, false)) {
        VariableColumn col1 = new VariableColumn(smallFile, indexFile);
        int max = (int) Math.pow(2, bitHint) * 10 + 1;
        OutputStream writeStream = col1.putBin();
        for (int i = 0; i < max; i++) {
            writeStream.write(i % 255);
        }
        writeStream.flush();
        col1.commit();
        int shift = (int) Math.ceil(max / 4.0);
        for (int offset = 0; offset < max; offset += shift) {
            int readLen = max - offset;
            DirectInputStream readStream = col1.getBin(0);
            long readAddress = Unsafe.malloc(readLen);
            readStream.copyTo(readAddress, offset, readLen);
            for (int i = 0; i < readLen; i++) {
                byte expected = (byte) ((offset + i) % 255);
                byte actual = Unsafe.getUnsafe().getByte(readAddress + i);
                Assert.assertEquals(String.format("difference at index %d with read offset %d", i, offset), expected, actual);
            }
            Unsafe.free(readAddress, readLen);
        }
    }
}
Also used : DirectInputStream(com.questdb.std.DirectInputStream) OutputStream(java.io.OutputStream) File(java.io.File)

Example 2 with DirectInputStream

use of com.questdb.std.DirectInputStream in project questdb by bluestreak01.

the class VariableColumnTest method testBin1.

@Test
public void testBin1() throws Exception {
    int N = 1000;
    int SZ = 4096;
    ByteBuffer buf = ByteBuffer.allocateDirect(SZ);
    long addr = ByteBuffers.getAddress(buf);
    try {
        Rnd rnd = new Rnd();
        try (MemoryFile smallFile = new MemoryFile(new File(temporaryFolder.getRoot(), "small.d"), 8, JournalMode.APPEND, false)) {
            try (VariableColumn col = new VariableColumn(smallFile, indexFile)) {
                for (int i = 0; i < N; i++) {
                    long p = addr;
                    int n = (rnd.nextPositiveInt() % (SZ - 1)) / 8;
                    for (int j = 0; j < n; j++) {
                        Unsafe.getUnsafe().putLong(p, rnd.nextLong());
                        p += 8;
                    }
                    buf.limit(n * 8);
                    col.putBin(buf);
                    col.commit();
                    buf.clear();
                }
                Assert.assertEquals(N, col.size());
                rnd = new Rnd();
                for (int i = 0; i < N; i++) {
                    long len = col.getBinLen(i);
                    DirectInputStream is = col.getBin(i);
                    is.copyTo(addr, 0, len);
                    long p = addr;
                    int n = (rnd.nextPositiveInt() % (SZ - 1)) / 8;
                    for (int j = 0; j < n; j++) {
                        Assert.assertEquals(rnd.nextLong(), Unsafe.getUnsafe().getLong(p));
                        p += 8;
                    }
                }
            }
        }
    } finally {
        ByteBuffers.release(buf);
    }
}
Also used : DirectInputStream(com.questdb.std.DirectInputStream) Rnd(com.questdb.std.Rnd) ByteBuffer(java.nio.ByteBuffer) File(java.io.File)

Example 3 with DirectInputStream

use of com.questdb.std.DirectInputStream in project questdb by bluestreak01.

the class VariableColumnTest method testReadBinaryColumnData.

@Test
public void testReadBinaryColumnData() throws Exception {
    int bitHint = 8;
    try (MemoryFile smallFile = new MemoryFile(new File(temporaryFolder.getRoot(), "small.d"), bitHint, JournalMode.APPEND, false)) {
        VariableColumn col1 = new VariableColumn(smallFile, indexFile);
        int max = (int) Math.pow(2, bitHint) * 10 + 1;
        Rnd rnd = new Rnd();
        OutputStream writeStream = col1.putBin();
        for (int i = 0; i < max; i++) {
            writeStream.write(rnd.nextInt());
        }
        writeStream.flush();
        col1.commit();
        DirectInputStream readStream = col1.getBin(0);
        byte b;
        int count = 0;
        Rnd exp = new Rnd();
        while ((b = (byte) readStream.read()) != -1) {
            Assert.assertEquals(String.format("difference at index %d", count), (byte) exp.nextInt(), b);
            count++;
        }
    }
}
Also used : DirectInputStream(com.questdb.std.DirectInputStream) OutputStream(java.io.OutputStream) Rnd(com.questdb.std.Rnd) File(java.io.File)

Aggregations

DirectInputStream (com.questdb.std.DirectInputStream)3 File (java.io.File)3 Rnd (com.questdb.std.Rnd)2 OutputStream (java.io.OutputStream)2 ByteBuffer (java.nio.ByteBuffer)1