Search in sources :

Example 1 with BoundingBox

use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.

the class HandleBoundingBoxQuery 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 already known, please close old query first", packageSequence);
            return;
        }
        final QueryBoundingBoxRequest queryRequest = QueryBoundingBoxRequest.decodeTuple(encodedPackage);
        final TupleStoreName requestTable = queryRequest.getTable();
        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 1 storage manager");
                }
                final BoundingBox boundingBox = queryRequest.getBoundingBox();
                final SpatialIndexReadOperator operator = new SpatialIndexReadOperator(storageManager.get(0), boundingBox);
                return operator;
            }
        };
        final StreamClientQuery clientQuery = new StreamClientQuery(operatorTreeBuilder, queryRequest.isPagingEnabled(), queryRequest.getTuplesPerPage(), clientConnectionHandler, packageSequence, Arrays.asList(requestTable));
        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 : QueryBoundingBoxRequest(org.bboxdb.network.packages.request.QueryBoundingBoxRequest) 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) StreamClientQuery(org.bboxdb.network.server.StreamClientQuery) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) List(java.util.List) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Example 2 with BoundingBox

use of org.bboxdb.commons.math.BoundingBox 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 3 with BoundingBox

use of org.bboxdb.commons.math.BoundingBox 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 4 with BoundingBox

use of org.bboxdb.commons.math.BoundingBox 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)

Example 5 with BoundingBox

use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.

the class NetworkTupleEncoderDecoder method decode.

/**
 * Convert a ByteBuffer into a TupleAndTable object
 * @param encodedPackage
 * @return
 */
public static TupleAndTable decode(final ByteBuffer encodedPackage) {
    final short tableLength = encodedPackage.getShort();
    final short keyLength = encodedPackage.getShort();
    final int bBoxLength = encodedPackage.getInt();
    final int dataLength = 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[] keyBytes = new byte[keyLength];
    encodedPackage.get(keyBytes, 0, keyBytes.length);
    final String key = new String(keyBytes);
    final byte[] boxBytes = new byte[bBoxLength];
    encodedPackage.get(boxBytes, 0, boxBytes.length);
    final byte[] dataBytes = new byte[dataLength];
    encodedPackage.get(dataBytes, 0, dataBytes.length);
    final BoundingBox boundingBox = BoundingBox.fromByteArray(boxBytes);
    Tuple tuple = null;
    if (TupleHelper.isDeletedTuple(boxBytes, dataBytes)) {
        tuple = new DeletedTuple(key, timestamp);
    } else {
        tuple = new Tuple(key, boundingBox, dataBytes, timestamp);
    }
    return new TupleAndTable(tuple, table);
}
Also used : DeletedTuple(org.bboxdb.storage.entity.DeletedTuple) BoundingBox(org.bboxdb.commons.math.BoundingBox) TupleAndTable(org.bboxdb.storage.entity.TupleAndTable) DeletedTuple(org.bboxdb.storage.entity.DeletedTuple) Tuple(org.bboxdb.storage.entity.Tuple)

Aggregations

BoundingBox (org.bboxdb.commons.math.BoundingBox)194 Test (org.junit.Test)113 Tuple (org.bboxdb.storage.entity.Tuple)61 ArrayList (java.util.ArrayList)25 JoinedTuple (org.bboxdb.storage.entity.JoinedTuple)24 DeletedTuple (org.bboxdb.storage.entity.DeletedTuple)23 BBoxDBException (org.bboxdb.misc.BBoxDBException)22 DistributionRegion (org.bboxdb.distribution.region.DistributionRegion)20 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)16 List (java.util.List)15 DistributionRegionIdMapper (org.bboxdb.distribution.region.DistributionRegionIdMapper)13 ZookeeperException (org.bboxdb.distribution.zookeeper.ZookeeperException)13 TupleStoreConfiguration (org.bboxdb.storage.entity.TupleStoreConfiguration)13 TupleListFuture (org.bboxdb.network.client.future.TupleListFuture)12 ZookeeperNotFoundException (org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException)11 EmptyResultFuture (org.bboxdb.network.client.future.EmptyResultFuture)11 IOException (java.io.IOException)10 Date (java.util.Date)10 DoubleInterval (org.bboxdb.commons.math.DoubleInterval)10 SpatialIndexReadOperator (org.bboxdb.storage.queryprocessor.operator.SpatialIndexReadOperator)10