Search in sources :

Example 11 with BBContainer

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

the class VoltFile method moveSubRootContents.

/*
     * Merge one directory into another via copying. Useful for simulating
     * node removal or files being moved from node to node or duplicated.
     */
public static void moveSubRootContents(File fromSubRoot, File toSubRoot) throws IOException {
    assert (fromSubRoot.exists() && fromSubRoot.isDirectory());
    assert (toSubRoot.exists() && toSubRoot.isDirectory());
    for (File file : fromSubRoot.listFiles()) {
        File fInOtherSubroot = new File(toSubRoot, file.getName());
        if (file.isDirectory()) {
            if (!fInOtherSubroot.exists()) {
                if (!fInOtherSubroot.mkdir()) {
                    throw new IOException("Can't create directory " + fInOtherSubroot);
                }
            }
            moveSubRootContents(file, fInOtherSubroot);
        } else {
            if (fInOtherSubroot.exists()) {
                throw new IOException(fInOtherSubroot + " already exists");
            }
            if (!fInOtherSubroot.createNewFile()) {
                throw new IOException();
            }
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(fInOtherSubroot);
            FileChannel inputChannel = fis.getChannel();
            FileChannel outputChannel = fos.getChannel();
            BBContainer bufC = DBBPool.allocateDirect(8192);
            ByteBuffer buf = bufC.b();
            try {
                while (inputChannel.read(buf) != -1) {
                    buf.flip();
                    outputChannel.write(buf);
                    buf.clear();
                }
            } finally {
                // These calls to close() also close the channels.
                fis.close();
                fos.close();
                bufC.discard();
            }
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) BBContainer(org.voltcore.utils.DBBPool.BBContainer) IOException(java.io.IOException) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 12 with BBContainer

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

the class ParameterSet method fromArrayNoCopy.

public static ParameterSet fromArrayNoCopy(Object... params) {
    byte[][][] encodedStringArrays = new byte[params.length][][];
    byte[][] encodedStrings = new byte[params.length][];
    int size = 2;
    for (int ii = 0; ii < params.length; ii++) {
        Object obj = params[ii];
        if ((obj == null) || (obj == JSONObject.NULL)) {
            size++;
            continue;
        }
        //everything has a type even arrays and null
        size += 1;
        Class<?> cls = obj.getClass();
        if (cls.isArray()) {
            if (obj instanceof byte[]) {
                final byte[] b = (byte[]) obj;
                size += 4 + b.length;
                continue;
            }
            VoltType type;
            try {
                type = VoltType.typeFromClass(cls.getComponentType());
            } catch (VoltTypeException e) {
                obj = getAKosherArray((Object[]) obj);
                cls = obj.getClass();
                type = VoltType.typeFromClass(cls.getComponentType());
            }
            // component type, array length
            size += 1 + 2;
            switch(type) {
                case SMALLINT:
                    size += 2 * ((short[]) obj).length;
                    break;
                case INTEGER:
                    size += 4 * ((int[]) obj).length;
                    break;
                case BIGINT:
                    size += 8 * ((long[]) obj).length;
                    break;
                case FLOAT:
                    size += 8 * ((double[]) obj).length;
                    break;
                case STRING:
                    String[] strings = (String[]) obj;
                    byte[][] arrayEncodedStrings = new byte[strings.length][];
                    for (int zz = 0; zz < strings.length; zz++) {
                        if (strings[zz] == null) {
                            size += 4;
                        } else {
                            arrayEncodedStrings[zz] = strings[zz].getBytes(Constants.UTF8ENCODING);
                            size += 4 + arrayEncodedStrings[zz].length;
                        }
                    }
                    encodedStringArrays[ii] = arrayEncodedStrings;
                    break;
                case TIMESTAMP:
                    size += 8 * ((TimestampType[]) obj).length;
                    break;
                case DECIMAL:
                    size += 16 * ((BigDecimal[]) obj).length;
                    break;
                case VOLTTABLE:
                    for (VoltTable vt : (VoltTable[]) obj) {
                        size += vt.getSerializedSize();
                    }
                    break;
                case VARBINARY:
                    for (byte[] buf : (byte[][]) obj) {
                        // length prefix
                        size += 4;
                        if (buf != null) {
                            size += buf.length;
                        }
                    }
                    break;
                case GEOGRAPHY_POINT:
                    size += VoltType.GEOGRAPHY_POINT.getLengthInBytesForFixedTypesWithoutCheck() * ((GeographyPointValue[]) obj).length;
                    break;
                case GEOGRAPHY:
                    for (GeographyValue gv : (GeographyValue[]) obj) {
                        // length prefix
                        size += 4;
                        if (gv != null) {
                            size += gv.getLengthInBytes();
                        }
                    }
                    break;
                default:
                    throw new RuntimeException("FIXME: Unsupported type " + type);
            }
            continue;
        }
        // Handle NULL mappings not encoded by type.min_value convention
        if (obj == VoltType.NULL_TIMESTAMP) {
            size += 8;
            continue;
        } else if (obj == VoltType.NULL_STRING_OR_VARBINARY) {
            size += 4;
            continue;
        } else if (obj == VoltType.NULL_DECIMAL) {
            size += 16;
            continue;
        } else if (obj == VoltType.NULL_POINT) {
            size += VoltType.GEOGRAPHY_POINT.getLengthInBytesForFixedTypesWithoutCheck();
            continue;
        } else if (obj == VoltType.NULL_GEOGRAPHY) {
            size += 4;
            continue;
        } else if (obj instanceof BBContainer) {
            size += 4 + ((BBContainer) obj).b().remaining();
            continue;
        }
        VoltType type = VoltType.typeFromClass(cls);
        switch(type) {
            case TINYINT:
                size++;
                break;
            case SMALLINT:
                size += 2;
                break;
            case INTEGER:
                size += 4;
                break;
            case BIGINT:
                size += 8;
                break;
            case FLOAT:
                size += 8;
                break;
            case STRING:
                byte[] encodedString = ((String) obj).getBytes(Constants.UTF8ENCODING);
                size += 4 + encodedString.length;
                encodedStrings[ii] = encodedString;
                break;
            case TIMESTAMP:
                size += 8;
                break;
            case DECIMAL:
                size += 16;
                break;
            case GEOGRAPHY_POINT:
                size += VoltType.GEOGRAPHY_POINT.getLengthInBytesForFixedTypesWithoutCheck();
                break;
            case GEOGRAPHY:
                size += 4 + ((GeographyValue) obj).getLengthInBytes();
                break;
            case VOLTTABLE:
                size += ((VoltTable) obj).getSerializedSize();
                break;
            default:
                throw new RuntimeException("FIXME: Unsupported type " + type);
        }
    }
    return new ParameterSet(params, size, encodedStrings, encodedStringArrays);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) JSONString(org.json_voltpatches.JSONString) BBContainer(org.voltcore.utils.DBBPool.BBContainer) JSONObject(org.json_voltpatches.JSONObject)

Example 13 with BBContainer

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

the class PartitionDRGateway method onBinaryDR.

public long onBinaryDR(int partitionId, long startSequenceNumber, long lastSequenceNumber, long lastSpUniqueId, long lastMpUniqueId, EventType eventType, ByteBuffer buf) {
    final BBContainer cont = DBBPool.wrapBB(buf);
    DBBPool.registerUnsafeMemory(cont.address());
    cont.discard();
    return -1;
}
Also used : BBContainer(org.voltcore.utils.DBBPool.BBContainer)

Example 14 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 15 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)

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