Search in sources :

Example 6 with PackageEncodeException

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

the class HandleJoinQuery method handleQuery.

@Override
public /**
 * Handle a bounding box query
 */
void handleQuery(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
    try {
        if (clientConnectionHandler.getActiveQueries().containsKey(packageSequence)) {
            logger.error("Query sequence {} is allready known, please close old query first", packageSequence);
            return;
        }
        final QueryJoinRequest queryRequest = QueryJoinRequest.decodeTuple(encodedPackage);
        final List<TupleStoreName> requestTables = queryRequest.getTables();
        final BoundingBox boundingBox = queryRequest.getBoundingBox();
        for (final TupleStoreName requestTable : requestTables) {
            if (!QueryHelper.handleNonExstingTable(requestTable, packageSequence, clientConnectionHandler)) {
                return;
            }
        }
        final OperatorTreeBuilder operatorTreeBuilder = new OperatorTreeBuilder() {

            @Override
            public Operator buildOperatorTree(final List<TupleStoreManager> storageManager) {
                if (storageManager.size() <= 1) {
                    throw new IllegalArgumentException("This operator tree needs more than one storage manager");
                }
                Operator operator1 = new SpatialIndexReadOperator(storageManager.get(0), boundingBox);
                SpatialIndexReadOperator indexReader = new SpatialIndexReadOperator(storageManager.get(1));
                operator1 = new IndexedSpatialJoinOperator(operator1, indexReader);
                for (int i = 3; i < storageManager.size(); i++) {
                    indexReader = new SpatialIndexReadOperator(storageManager.get(i));
                    operator1 = new IndexedSpatialJoinOperator(operator1, indexReader);
                }
                return operator1;
            }
        };
        final StreamClientQuery clientQuery = new StreamClientQuery(operatorTreeBuilder, queryRequest.isPagingEnabled(), queryRequest.getTuplesPerPage(), clientConnectionHandler, packageSequence, requestTables);
        clientConnectionHandler.getActiveQueries().put(packageSequence, clientQuery);
        clientConnectionHandler.sendNextResultsForQuery(packageSequence, packageSequence);
    } catch (PackageEncodeException e) {
        logger.warn("Got exception while decoding package", e);
        clientConnectionHandler.writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
    }
}
Also used : IndexedSpatialJoinOperator(org.bboxdb.storage.queryprocessor.operator.IndexedSpatialJoinOperator) Operator(org.bboxdb.storage.queryprocessor.operator.Operator) SpatialIndexReadOperator(org.bboxdb.storage.queryprocessor.operator.SpatialIndexReadOperator) StreamClientQuery(org.bboxdb.network.server.StreamClientQuery) IndexedSpatialJoinOperator(org.bboxdb.storage.queryprocessor.operator.IndexedSpatialJoinOperator) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse) QueryJoinRequest(org.bboxdb.network.packages.request.QueryJoinRequest) SpatialIndexReadOperator(org.bboxdb.storage.queryprocessor.operator.SpatialIndexReadOperator) BoundingBox(org.bboxdb.commons.math.BoundingBox) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) OperatorTreeBuilder(org.bboxdb.storage.queryprocessor.OperatorTreeBuilder) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) List(java.util.List)

Example 7 with PackageEncodeException

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

the class QueryBoundingBoxContinuousRequest method decodeTuple.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 * @throws IOException
 */
public static QueryBoundingBoxContinuousRequest 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_CONTINUOUS_BBOX) {
        throw new PackageEncodeException("Wrong query type: " + queryType + " required type is: " + NetworkConst.REQUEST_QUERY_CONTINUOUS_BBOX);
    }
    // 1 unused byte
    encodedPackage.get();
    final short tableLength = encodedPackage.getShort();
    final int bboxLength = encodedPackage.getInt();
    final byte[] tableBytes = new byte[tableLength];
    encodedPackage.get(tableBytes, 0, tableBytes.length);
    final String table = new String(tableBytes);
    final byte[] bboxBytes = new byte[bboxLength];
    encodedPackage.get(bboxBytes, 0, bboxBytes.length);
    final BoundingBox boundingBox = BoundingBox.fromByteArray(bboxBytes);
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
    }
    final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
    return new QueryBoundingBoxContinuousRequest(sequenceNumber, routingHeader, table, boundingBox);
}
Also used : BoundingBox(org.bboxdb.commons.math.BoundingBox) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) RoutingHeader(org.bboxdb.network.routing.RoutingHeader)

Example 8 with PackageEncodeException

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

the class QueryBoundingBoxContinuousRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tableBytes = table.getFullnameBytes();
        final byte[] bboxBytes = box.toByteArray();
        final ByteBuffer bb = ByteBuffer.allocate(8);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(getQueryType());
        bb.put(NetworkConst.UNUSED_BYTE);
        bb.putShort((short) tableBytes.length);
        bb.putInt((int) bboxBytes.length);
        final long bodyLength = bb.capacity() + tableBytes.length + bboxBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(tableBytes);
        outputStream.write(bboxBytes);
        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 9 with PackageEncodeException

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

the class QueryBoundingBoxTimeRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tableBytes = table.getFullnameBytes();
        final byte[] bboxBytes = box.toByteArray();
        final ByteBuffer bb = ByteBuffer.allocate(20);
        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.put(NetworkConst.UNUSED_BYTE);
        bb.put(NetworkConst.UNUSED_BYTE);
        bb.putInt((int) bboxBytes.length);
        bb.putLong(timestamp);
        final long bodyLength = bb.capacity() + tableBytes.length + bboxBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(tableBytes);
        outputStream.write(bboxBytes);
        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 10 with PackageEncodeException

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

the class QueryBoundingBoxTimeRequest method decodeTuple.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 * @throws IOException
 */
public static QueryBoundingBoxTimeRequest 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_BBOX_AND_TIME) {
        throw new PackageEncodeException("Wrong query type: " + queryType + " required type is: " + NetworkConst.REQUEST_QUERY_BBOX_AND_TIME);
    }
    boolean pagingEnabled = false;
    if (encodedPackage.get() != 0) {
        pagingEnabled = true;
    }
    final short tuplesPerPage = encodedPackage.getShort();
    final short tableLength = encodedPackage.getShort();
    // 2 unused bytes
    encodedPackage.get();
    encodedPackage.get();
    final int bboxLength = encodedPackage.getInt();
    final long timestamp = encodedPackage.getLong();
    final byte[] tableBytes = new byte[tableLength];
    encodedPackage.get(tableBytes, 0, tableBytes.length);
    final String table = new String(tableBytes);
    final byte[] bboxBytes = new byte[bboxLength];
    encodedPackage.get(bboxBytes, 0, bboxBytes.length);
    final BoundingBox boundingBox = BoundingBox.fromByteArray(bboxBytes);
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
    }
    final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
    return new QueryBoundingBoxTimeRequest(sequenceNumber, routingHeader, table, boundingBox, timestamp, pagingEnabled, tuplesPerPage);
}
Also used : BoundingBox(org.bboxdb.commons.math.BoundingBox) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) RoutingHeader(org.bboxdb.network.routing.RoutingHeader)

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