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