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