use of org.bboxdb.network.server.ClientQuery in project bboxdb by jnidzwetzki.
the class HandleContinuousBoundingBoxQuery 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 QueryBoundingBoxContinuousRequest queryRequest = QueryBoundingBoxContinuousRequest.decodeTuple(encodedPackage);
final TupleStoreName requestTable = queryRequest.getTable();
if (!QueryHelper.handleNonExstingTable(requestTable, packageSequence, clientConnectionHandler)) {
return;
}
final BoundingBox boundingBox = queryRequest.getBoundingBox();
final ClientQuery clientQuery = new ContinuousBoundingBoxClientQuery(boundingBox, clientConnectionHandler, packageSequence, 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.network.server.ClientQuery in project bboxdb by jnidzwetzki.
the class HandleKeyQuery method handleQuery.
@Override
public /**
* Handle a key query
*/
void handleQuery(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
final Runnable queryRunable = new ExceptionSafeRunnable() {
@Override
public void runThread() throws Exception {
try {
if (clientConnectionHandler.getActiveQueries().containsKey(packageSequence)) {
logger.error("Query sequence {} is allready known, please close old query first", packageSequence);
return;
}
final QueryKeyRequest queryKeyRequest = QueryKeyRequest.decodeTuple(encodedPackage);
final TupleStoreName requestTable = queryKeyRequest.getTable();
if (!QueryHelper.handleNonExstingTable(requestTable, packageSequence, clientConnectionHandler)) {
return;
}
final String key = queryKeyRequest.getKey();
final ClientQuery clientQuery = new KeyClientQuery(key, queryKeyRequest.isPagingEnabled(), queryKeyRequest.getTuplesPerPage(), clientConnectionHandler, packageSequence, 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));
}
}
@Override
protected void afterExceptionHook() {
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
clientConnectionHandler.writeResultPackageNE(responsePackage);
}
};
// Submit the runnable to our pool
if (clientConnectionHandler.getThreadPool().isShutdown()) {
logger.warn("Thread pool is shutting down, don't execute query: {}", packageSequence);
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_SHUTDOWN);
clientConnectionHandler.writeResultPackage(responsePackage);
} else {
clientConnectionHandler.getThreadPool().submit(queryRunable);
}
}
use of org.bboxdb.network.server.ClientQuery in project bboxdb by jnidzwetzki.
the class CancelQueryHandler method handleRequest.
@Override
public /**
* Cancel the given query
*/
boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
try {
final CancelQueryRequest nextPagePackage = CancelQueryRequest.decodeTuple(encodedPackage);
final short queryToCancel = nextPagePackage.getQuerySequence();
logger.debug("Cancel query {} requested", queryToCancel);
final Map<Short, ClientQuery> activeQueries = clientConnectionHandler.getActiveQueries();
if (!activeQueries.containsKey(queryToCancel)) {
logger.error("Unable to cancel query {} - not found", queryToCancel);
clientConnectionHandler.writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_NOT_FOUND));
} else {
final ClientQuery clientQuery = activeQueries.remove(queryToCancel);
clientQuery.close();
clientConnectionHandler.writeResultPackage(new SuccessResponse(packageSequence));
logger.info("Sending success for canceling query {} (request package {})", queryToCancel, packageSequence);
}
} catch (PackageEncodeException e) {
logger.warn("Error getting next page for a query", e);
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
clientConnectionHandler.writeResultPackage(responsePackage);
}
return true;
}
Aggregations