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);
}
}
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;
}
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;
}
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);
}
}
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();
}
}
Aggregations