use of org.bboxdb.network.packages.request.QueryKeyRequest in project bboxdb by jnidzwetzki.
the class TestNetworkClasses method testDecodeKeyQuery.
/**
* Test decoding and encoding of the key query
* @throws IOException
* @throws PackageEncodeException
*/
@Test(timeout = 60000)
public void testDecodeKeyQuery() throws IOException, PackageEncodeException {
final String table = "1_mygroup_table1";
final String key = "key1";
final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
final QueryKeyRequest queryKeyRequest = new QueryKeyRequest(sequenceNumber, ROUTING_HEADER_UNROUTED, table, key, false, (short) 10);
final byte[] encodedPackage = networkPackageToByte(queryKeyRequest);
Assert.assertNotNull(encodedPackage);
final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedPackage);
final boolean result = NetworkPackageDecoder.validateRequestPackageHeader(bb, NetworkConst.REQUEST_TYPE_QUERY);
Assert.assertTrue(result);
final QueryKeyRequest decodedPackage = QueryKeyRequest.decodeTuple(bb);
Assert.assertEquals(queryKeyRequest.getKey(), decodedPackage.getKey());
Assert.assertEquals(queryKeyRequest.getTable(), decodedPackage.getTable());
Assert.assertEquals(queryKeyRequest.isPagingEnabled(), decodedPackage.isPagingEnabled());
Assert.assertEquals(queryKeyRequest.getTuplesPerPage(), decodedPackage.getTuplesPerPage());
Assert.assertEquals(NetworkConst.REQUEST_QUERY_KEY, NetworkPackageDecoder.getQueryTypeFromRequest(bb));
Assert.assertEquals(queryKeyRequest.toString(), decodedPackage.toString());
}
use of org.bboxdb.network.packages.request.QueryKeyRequest 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);
}
}
Aggregations