Search in sources :

Example 16 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class FastSerializer method growIfNeeded.

/** Resizes the internal byte buffer with a simple doubling policy, if needed. */
private final void growIfNeeded(int minimumDesired) {
    if (buffer.b().remaining() < minimumDesired) {
        // Compute the size of the new buffer
        int newCapacity = buffer.b().capacity();
        int newRemaining = newCapacity - buffer.b().position();
        while (newRemaining < minimumDesired) {
            newRemaining += newCapacity;
            newCapacity *= 2;
        }
        // Allocate and copy
        BBContainer next;
        if (isDirect) {
            next = DBBPool.allocateDirect(newCapacity);
        } else {
            next = DBBPool.wrapBB(ByteBuffer.allocate(newCapacity));
        }
        buffer.b().flip();
        next.b().put(buffer.b());
        assert next.b().remaining() == newRemaining;
        buffer.discard();
        buffer = next;
        if (callback != null)
            callback.onBufferGrow(this);
        assert (buffer.b().order() == ByteOrder.BIG_ENDIAN);
    }
}
Also used : BBContainer(org.voltcore.utils.DBBPool.BBContainer)

Example 17 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class CompressionService method getBuffersForCompression.

private static IOBuffers getBuffersForCompression(int length, boolean inputNotUsed) {
    IOBuffers buffers = m_buffers.get();
    BBContainer input = buffers.input;
    BBContainer output = buffers.output;
    final int maxCompressedLength = Snappy.maxCompressedLength(length);
    final int inputCapacity = input.b().capacity();
    final int outputCapacity = output.b().capacity();
    /*
         * A direct byte buffer might be provided in which case no input buffer is needed
         */
    boolean changedBuffer = false;
    if (!inputNotUsed && inputCapacity < length) {
        input.discard();
        input = DBBPool.allocateDirect(Math.max(inputCapacity * 2, length));
        changedBuffer = true;
    }
    if (outputCapacity < maxCompressedLength) {
        output.discard();
        output = DBBPool.allocateDirect(Math.max(outputCapacity * 2, maxCompressedLength));
        changedBuffer = true;
    }
    if (changedBuffer) {
        buffers = new IOBuffers(input, output);
        m_buffers.set(buffers);
    }
    output.b().clear();
    input.b().clear();
    return buffers;
}
Also used : BBContainer(org.voltcore.utils.DBBPool.BBContainer)

Example 18 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class CompressionService method decompressBytes.

public static byte[] decompressBytes(byte[] bytes) throws IOException {
    IOBuffers buffers = m_buffers.get();
    BBContainer input = buffers.input;
    BBContainer output = buffers.output;
    final int inputCapacity = input.b().capacity();
    if (inputCapacity < bytes.length) {
        input.discard();
        input = DBBPool.allocateDirect(Math.max(inputCapacity * 2, bytes.length));
        buffers = new IOBuffers(input, output);
        m_buffers.set(buffers);
    }
    final ByteBuffer inputBuffer = input.b();
    inputBuffer.clear();
    inputBuffer.put(bytes);
    inputBuffer.flip();
    final int uncompressedLength = Snappy.uncompressedLength(inputBuffer);
    final int outputCapacity = output.b().capacity();
    if (outputCapacity < uncompressedLength) {
        output.discard();
        output = DBBPool.allocateDirect(Math.max(outputCapacity * 2, uncompressedLength));
        buffers = new IOBuffers(input, output);
        m_buffers.set(buffers);
    }
    final ByteBuffer outputBuffer = output.b();
    outputBuffer.clear();
    final int actualUncompressedLength = Snappy.uncompress(inputBuffer, outputBuffer);
    assert (uncompressedLength == actualUncompressedLength);
    byte[] result = new byte[actualUncompressedLength];
    outputBuffer.get(result);
    return result;
}
Also used : BBContainer(org.voltcore.utils.DBBPool.BBContainer) ByteBuffer(java.nio.ByteBuffer)

Example 19 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class TestDBBPool method testChecksum.

@Test
public void testChecksum() {
    EELibraryLoader.loadExecutionEngineLibrary(true);
    final long seed = System.currentTimeMillis();
    Random r = new Random(seed);
    System.out.println("Seed is " + seed);
    for (int ii = 0; ii < 10000; ii++) {
        int nextLength = r.nextInt(4096);
        byte[] bytes = new byte[nextLength];
        r.nextBytes(bytes);
        PureJavaCrc32C checksum = new PureJavaCrc32C();
        checksum.update(bytes);
        int javaSum = (int) checksum.getValue();
        BBContainer cont = DBBPool.allocateDirect(nextLength);
        cont.b().put(bytes);
        int cSum = DBBPool.getCRC32C(cont.address(), 0, nextLength);
        cont.discard();
        assertEquals(javaSum, cSum);
    }
}
Also used : Random(java.util.Random) PureJavaCrc32C(org.apache.hadoop_voltpatches.util.PureJavaCrc32C) BBContainer(org.voltcore.utils.DBBPool.BBContainer) Test(org.junit.Test)

Example 20 with BBContainer

use of org.voltcore.utils.DBBPool.BBContainer in project voltdb by VoltDB.

the class TestMurmur3 method testMatchesNativeBytes.

@Test
public void testMatchesNativeBytes() throws Exception {
    final long seed = ByteBuffer.wrap(SecureRandom.getSeed(8)).getInt();
    Random r = new Random(seed);
    System.out.println("Seed is " + seed);
    EELibraryLoader.loadExecutionEngineLibrary(true);
    BBContainer c = DBBPool.allocateDirect(4096);
    try {
        c.b().order(ByteOrder.LITTLE_ENDIAN);
        for (int ii = 0; ii < iterations; ii++) {
            int bytesToFill = r.nextInt(maxLength + 1);
            byte[] bytes = new byte[bytesToFill];
            r.nextBytes(bytes);
            c.b().clear();
            c.b().put(bytes);
            c.b().flip();
            long nativeHash = DBBPool.getMurmur3128(c.address(), 0, bytesToFill);
            long javaHash = MurmurHash3.hash3_x64_128(c.b(), 0, bytesToFill, 0);
            if (nativeHash != javaHash) {
                fail("Failed in iteration " + ii + " native hash " + Long.toHexString(nativeHash) + " java hash " + Long.toHexString(javaHash) + " with bytes " + Encoder.base64Encode(bytes));
            }
        }
    } finally {
        c.discard();
    }
}
Also used : SecureRandom(java.security.SecureRandom) Random(java.util.Random) BBContainer(org.voltcore.utils.DBBPool.BBContainer) Test(org.junit.Test)

Aggregations

BBContainer (org.voltcore.utils.DBBPool.BBContainer)57 Test (org.junit.Test)22 ByteBuffer (java.nio.ByteBuffer)21 BinaryDequeReader (org.voltdb.utils.BinaryDeque.BinaryDequeReader)18 IOException (java.io.IOException)15 File (java.io.File)10 JSONObject (org.json_voltpatches.JSONObject)4 BinaryDequeTruncator (org.voltdb.utils.BinaryDeque.BinaryDequeTruncator)4 TruncatorResponse (org.voltdb.utils.BinaryDeque.TruncatorResponse)4 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 FileInputStream (java.io.FileInputStream)2 Collection (java.util.Collection)2 Random (java.util.Random)2 Callable (java.util.concurrent.Callable)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 PureJavaCrc32C (org.apache.hadoop_voltpatches.util.PureJavaCrc32C)2 JSONException (org.json_voltpatches.JSONException)2 JSONString (org.json_voltpatches.JSONString)2 HashinatorConfig (org.voltdb.TheHashinator.HashinatorConfig)2