Search in sources :

Example 1 with KeyClientQuery

use of org.bboxdb.network.server.KeyClientQuery 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);
    }
}
Also used : QueryKeyRequest(org.bboxdb.network.packages.request.QueryKeyRequest) KeyClientQuery(org.bboxdb.network.server.KeyClientQuery) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) ClientQuery(org.bboxdb.network.server.ClientQuery) KeyClientQuery(org.bboxdb.network.server.KeyClientQuery) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Aggregations

ExceptionSafeRunnable (org.bboxdb.commons.concurrent.ExceptionSafeRunnable)1 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)1 QueryKeyRequest (org.bboxdb.network.packages.request.QueryKeyRequest)1 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)1 ClientQuery (org.bboxdb.network.server.ClientQuery)1 KeyClientQuery (org.bboxdb.network.server.KeyClientQuery)1 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)1