Search in sources :

Example 11 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class QueryInsertTimeRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tableBytes = table.getFullnameBytes();
        final ByteBuffer bb = ByteBuffer.allocate(14);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(getQueryType());
        if (pagingEnabled) {
            bb.put((byte) 1);
        } else {
            bb.put((byte) 0);
        }
        bb.putShort(tuplesPerPage);
        bb.putLong(timestamp);
        bb.putShort((short) tableBytes.length);
        final long bodyLength = bb.capacity() + tableBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(tableBytes);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class QueryJoinRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] bboxBytes = box.toByteArray();
        final ByteBuffer bb = ByteBuffer.allocate(12);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(getQueryType());
        if (pagingEnabled) {
            bb.put((byte) 1);
        } else {
            bb.put((byte) 0);
        }
        bb.putShort(tuplesPerPage);
        bb.putInt(tables.size());
        bb.putInt(bboxBytes.length);
        final ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        for (int i = 0; i < tables.size(); i++) {
            final byte[] tablename = tables.get(i).getFullnameBytes();
            bStream.write(DataEncoderHelper.shortToByteBuffer((short) tablename.length).array());
            bStream.write(tablename);
        }
        final byte[] tablesArray = bStream.toByteArray();
        final long bodyLength = bb.capacity() + tablesArray.length + bboxBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(bboxBytes);
        outputStream.write(tablesArray);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 13 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class QueryKeyRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tableBytes = table.getFullnameBytes();
        final byte[] keyBytes = key.getBytes();
        final ByteBuffer bb = ByteBuffer.allocate(8);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(getQueryType());
        if (pagingEnabled) {
            bb.put((byte) 1);
        } else {
            bb.put((byte) 0);
        }
        bb.putShort(tuplesPerPage);
        bb.putShort((short) tableBytes.length);
        bb.putShort((short) keyBytes.length);
        final long bodyLength = bb.capacity() + tableBytes.length + keyBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(tableBytes);
        outputStream.write(keyBytes);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 14 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class QueryKeyRequest method decodeTuple.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 * @throws IOException
 */
public static QueryKeyRequest decodeTuple(final ByteBuffer encodedPackage) throws PackageEncodeException, IOException {
    final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_QUERY);
    if (decodeResult == false) {
        throw new PackageEncodeException("Unable to decode package");
    }
    final byte queryType = encodedPackage.get();
    if (queryType != NetworkConst.REQUEST_QUERY_KEY) {
        throw new PackageEncodeException("Wrong query type: " + queryType);
    }
    boolean pagingEnabled = false;
    if (encodedPackage.get() != 0) {
        pagingEnabled = true;
    }
    final short tuplesPerPage = encodedPackage.getShort();
    final short tableLength = encodedPackage.getShort();
    final short keyLength = encodedPackage.getShort();
    final byte[] tableBytes = new byte[tableLength];
    encodedPackage.get(tableBytes, 0, tableBytes.length);
    final String table = new String(tableBytes);
    final byte[] keyBytes = new byte[keyLength];
    encodedPackage.get(keyBytes, 0, keyBytes.length);
    final String key = new String(keyBytes);
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
    }
    final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
    return new QueryKeyRequest(sequenceNumber, routingHeader, table, key, pagingEnabled, tuplesPerPage);
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) RoutingHeader(org.bboxdb.network.routing.RoutingHeader)

Example 15 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class QueryVersionTimeRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tableBytes = table.getFullnameBytes();
        final ByteBuffer bb = ByteBuffer.allocate(14);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(getQueryType());
        if (pagingEnabled) {
            bb.put((byte) 1);
        } else {
            bb.put((byte) 0);
        }
        bb.putShort(tuplesPerPage);
        bb.putLong(timestamp);
        bb.putShort((short) tableBytes.length);
        final long bodyLength = bb.capacity() + tableBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(tableBytes);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)68 IOException (java.io.IOException)31 ByteBuffer (java.nio.ByteBuffer)21 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)16 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)14 RoutingHeader (org.bboxdb.network.routing.RoutingHeader)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ArrayList (java.util.ArrayList)9 BoundingBox (org.bboxdb.commons.math.BoundingBox)9 List (java.util.List)8 SuccessResponse (org.bboxdb.network.packages.response.SuccessResponse)6 Tuple (org.bboxdb.storage.entity.Tuple)6 StreamClientQuery (org.bboxdb.network.server.StreamClientQuery)5 OperatorTreeBuilder (org.bboxdb.storage.queryprocessor.OperatorTreeBuilder)5 NetworkRequestPackage (org.bboxdb.network.packages.NetworkRequestPackage)4 Operator (org.bboxdb.storage.queryprocessor.operator.Operator)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ExceptionSafeRunnable (org.bboxdb.commons.concurrent.ExceptionSafeRunnable)3 CompressionEnvelopeRequest (org.bboxdb.network.packages.request.CompressionEnvelopeRequest)3 ClientQuery (org.bboxdb.network.server.ClientQuery)3