Search in sources :

Example 1 with FixedBitSingleValueMultiColWriter

use of com.linkedin.pinot.core.io.writer.impl.FixedBitSingleValueMultiColWriter in project pinot by linkedin.

the class FixedBitWidthRowColDataFileWriterTest method testSingleColSigned.

@Test
public void testSingleColSigned() throws Exception {
    int maxBits = 1;
    while (maxBits < 32) {
        LOGGER.trace("START test maxBits:" + maxBits);
        final String fileName = getClass().getName() + "_single_col_fixed_bit_" + maxBits + ".dat";
        final File file = new File(fileName);
        file.delete();
        final int rows = 100;
        final int cols = 1;
        final int[] columnSizesInBits = new int[] { maxBits };
        final FixedBitSingleValueMultiColWriter writer = new FixedBitSingleValueMultiColWriter(file, rows, cols, columnSizesInBits, new boolean[] { true });
        final int[] data = new int[rows];
        final Random r = new Random();
        writer.open();
        final int maxValue = (int) Math.pow(2, maxBits);
        int offset = maxValue - 1;
        int requiredBits = maxBits + 1;
        final CustomBitSet set = CustomBitSet.withBitLength(rows * cols * requiredBits);
        for (int i = 0; i < rows; i++) {
            int value = r.nextInt(maxValue);
            if (Math.random() > .5) {
                value = -1 * value;
            }
            data[i] = value;
            writer.setInt(i, 0, data[i]);
            int offsetValue = value + offset;
            for (int bitPos = requiredBits - 1; bitPos >= 0; bitPos--) {
                if ((offsetValue & (1 << bitPos)) != 0) {
                    set.setBit(i * requiredBits + (requiredBits - bitPos - 1));
                }
            }
        }
        LOGGER.trace("bits expected:" + rows * cols * maxBits + " bytes expected:" + (rows * cols * maxBits + 7) / 8);
        writer.close();
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        LOGGER.trace("file size:" + raf.length());
        final byte[] b = new byte[(int) raf.length()];
        raf.read(b);
        final byte[] byteArray = set.toByteArray();
        if (byteArray.length != raf.length()) {
            final byte[] temp = set.toByteArray();
            System.err.println("byteArray length:" + temp.length + "   from file:" + b.length);
        }
        LOGGER.trace("byteArray length:" + byteArray.length + "   from file:" + b.length);
        Assert.assertEquals(byteArray.length, b.length);
        Assert.assertEquals(byteArray, b);
        raf.close();
        LOGGER.trace("END test maxBits:" + maxBits);
        maxBits = maxBits + 1;
        file.delete();
        set.close();
    }
}
Also used : Random(java.util.Random) RandomAccessFile(java.io.RandomAccessFile) FixedBitSingleValueMultiColWriter(com.linkedin.pinot.core.io.writer.impl.FixedBitSingleValueMultiColWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CustomBitSet(com.linkedin.pinot.core.util.CustomBitSet) Test(org.testng.annotations.Test)

Example 2 with FixedBitSingleValueMultiColWriter

use of com.linkedin.pinot.core.io.writer.impl.FixedBitSingleValueMultiColWriter in project pinot by linkedin.

the class FixedBitWidthRowColDataFileWriterTest method testSingleColUnsigned.

@Test
public void testSingleColUnsigned() throws Exception {
    int maxBits = 1;
    while (maxBits < 32) {
        LOGGER.debug("START test maxBits:" + maxBits);
        final String fileName = getClass().getName() + "_single_col_fixed_bit_" + maxBits + ".dat";
        final File file = new File(fileName);
        file.delete();
        final int rows = 100;
        final int cols = 1;
        final int[] columnSizesInBits = new int[] { maxBits };
        final FixedBitSingleValueMultiColWriter writer = new FixedBitSingleValueMultiColWriter(file, rows, cols, columnSizesInBits);
        final int[] data = new int[rows];
        final Random r = new Random();
        writer.open();
        final int maxValue = (int) Math.pow(2, maxBits);
        final CustomBitSet set = CustomBitSet.withBitLength(rows * cols * maxBits);
        for (int i = 0; i < rows; i++) {
            data[i] = r.nextInt(maxValue);
            writer.setInt(i, 0, data[i]);
            final int value = data[i];
            for (int bitPos = maxBits - 1; bitPos >= 0; bitPos--) {
                if ((value & (1 << bitPos)) != 0) {
                    set.setBit(i * maxBits + (maxBits - bitPos - 1));
                }
            }
        }
        LOGGER.debug("bits expected:" + rows * cols * maxBits + " bytes expected:" + (rows * cols * maxBits + 7) / 8);
        writer.close();
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        //      System.out.println("file size:" + raf.length());
        final byte[] b = new byte[(int) raf.length()];
        raf.read(b);
        final byte[] byteArray = set.toByteArray();
        if (byteArray.length != raf.length()) {
            final byte[] temp = set.toByteArray();
            System.err.println("byteArray length:" + temp.length + "   from file:" + b.length);
        }
        LOGGER.trace("byteArray length:" + byteArray.length + "   from file:" + b.length);
        Assert.assertEquals(byteArray.length, b.length);
        Assert.assertEquals(byteArray, b);
        raf.close();
        LOGGER.trace("END test maxBits:" + maxBits);
        maxBits = maxBits + 1;
        file.delete();
        set.close();
    }
}
Also used : Random(java.util.Random) RandomAccessFile(java.io.RandomAccessFile) FixedBitSingleValueMultiColWriter(com.linkedin.pinot.core.io.writer.impl.FixedBitSingleValueMultiColWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CustomBitSet(com.linkedin.pinot.core.util.CustomBitSet) Test(org.testng.annotations.Test)

Example 3 with FixedBitSingleValueMultiColWriter

use of com.linkedin.pinot.core.io.writer.impl.FixedBitSingleValueMultiColWriter in project pinot by linkedin.

the class FixedBitRowColDataWriterReaderTest method testSingleColUnsigned.

@Test
public void testSingleColUnsigned() throws Exception {
    int maxBits = 1;
    while (maxBits < 32) {
        LOGGER.debug("START test maxBits:" + maxBits);
        final String fileName = getClass().getName() + "_single_col_fixed_bit_" + maxBits + ".dat";
        final File file = new File(fileName);
        file.delete();
        final int rows = 100;
        final int cols = 1;
        final int[] columnSizesInBits = new int[] { maxBits };
        final FixedBitSingleValueMultiColWriter writer = new FixedBitSingleValueMultiColWriter(file, rows, cols, columnSizesInBits, new boolean[] { true });
        final int[] data = new int[rows];
        final Random r = new Random();
        writer.open();
        final int maxValue = (int) Math.pow(2, maxBits);
        for (int i = 0; i < rows; i++) {
            data[i] = r.nextInt(maxValue) * ((Math.random() > .5) ? 1 : -1);
            writer.setInt(i, 0, data[i]);
        }
        writer.close();
        // Test heap mode
        PinotDataBuffer heapBuffer = PinotDataBuffer.fromFile(file, ReadMode.heap, FileChannel.MapMode.READ_ONLY, "testing");
        FixedBitSingleValueMultiColReader heapReader = new FixedBitSingleValueMultiColReader(heapBuffer, rows, cols, columnSizesInBits, new boolean[] { true });
        for (int i = 0; i < rows; i++) {
            Assert.assertEquals(heapReader.getInt(i, 0), data[i]);
        }
        heapReader.close();
        heapBuffer.close();
        // Test mmap mode
        PinotDataBuffer mmapBuffer = PinotDataBuffer.fromFile(file, ReadMode.mmap, FileChannel.MapMode.READ_ONLY, "mmap-testing");
        FixedBitSingleValueMultiColReader mmapReader = new FixedBitSingleValueMultiColReader(mmapBuffer, rows, cols, columnSizesInBits, new boolean[] { true });
        for (int i = 0; i < rows; i++) {
            Assert.assertEquals(mmapReader.getInt(i, 0), data[i]);
        }
        mmapReader.close();
        mmapBuffer.close();
        maxBits = maxBits + 1;
        file.delete();
    }
}
Also used : Random(java.util.Random) FixedBitSingleValueMultiColWriter(com.linkedin.pinot.core.io.writer.impl.FixedBitSingleValueMultiColWriter) PinotDataBuffer(com.linkedin.pinot.core.segment.memory.PinotDataBuffer) FixedBitSingleValueMultiColReader(com.linkedin.pinot.core.io.reader.impl.FixedBitSingleValueMultiColReader) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

FixedBitSingleValueMultiColWriter (com.linkedin.pinot.core.io.writer.impl.FixedBitSingleValueMultiColWriter)3 File (java.io.File)3 Random (java.util.Random)3 Test (org.testng.annotations.Test)3 CustomBitSet (com.linkedin.pinot.core.util.CustomBitSet)2 RandomAccessFile (java.io.RandomAccessFile)2 FixedBitSingleValueMultiColReader (com.linkedin.pinot.core.io.reader.impl.FixedBitSingleValueMultiColReader)1 PinotDataBuffer (com.linkedin.pinot.core.segment.memory.PinotDataBuffer)1