use of org.bboxdb.network.packages.NetworkRequestPackage in project bboxdb by jnidzwetzki.
the class BBoxDBConnection method flushPendingCompressionPackages.
/**
* Write all pending compression packages to server, called by the maintainance thread
*/
public void flushPendingCompressionPackages() {
final List<NetworkRequestPackage> packagesToWrite = new ArrayList<>();
synchronized (pendingCompressionPackages) {
if (pendingCompressionPackages.isEmpty()) {
return;
}
packagesToWrite.addAll(pendingCompressionPackages);
pendingCompressionPackages.clear();
}
if (logger.isDebugEnabled()) {
logger.debug("Chunk size is: {}", packagesToWrite.size());
}
final NetworkRequestPackage compressionEnvelopeRequest = new CompressionEnvelopeRequest(NetworkConst.COMPRESSION_TYPE_GZIP, packagesToWrite);
try {
writePackageToSocket(compressionEnvelopeRequest);
} catch (PackageEncodeException | IOException e) {
logger.error("Got an exception while write pending compression packages to server", e);
terminateConnection();
}
}
use of org.bboxdb.network.packages.NetworkRequestPackage in project bboxdb by jnidzwetzki.
the class CompressionEnvelopeRequest method writeToOutputStream.
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
try {
if (compressionType != NetworkConst.COMPRESSION_TYPE_GZIP) {
throw new PackageEncodeException("Unknown compression method: " + compressionType);
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final OutputStream os = new GZIPOutputStream(baos);
// Write packages
for (final NetworkRequestPackage networkRequestPackage : networkRequestPackages) {
networkRequestPackage.writeToOutputStream(os);
}
os.close();
final byte[] compressedBytes = baos.toByteArray();
// Header
final ByteBuffer bb = ByteBuffer.allocate(4);
bb.order(Const.APPLICATION_BYTE_ORDER);
bb.put(compressionType);
bb.putShort((short) networkRequestPackages.size());
// Body length
final long bodyLength = bb.capacity() + compressedBytes.length;
final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
// Write body
outputStream.write(bb.array());
outputStream.write(compressedBytes);
return headerLength + bodyLength;
} catch (IOException e) {
throw new PackageEncodeException("Got an IO Exception while writing compressed data");
}
}
use of org.bboxdb.network.packages.NetworkRequestPackage in project bboxdb by jnidzwetzki.
the class OperationFutureImpl method cancelOldFuture.
/**
* Cancel the old future
* @param future
*/
private void cancelOldFuture(final NetworkOperationFuture future) {
final NetworkRequestPackage transmittedPackage = future.getTransmittedPackage();
if (transmittedPackage == null) {
return;
}
if (!transmittedPackage.needsToBeCanceled()) {
return;
}
// Only successfull futures needs to be canceled
if (future.isFailed()) {
return;
}
final BBoxDBConnection connection = future.getConnection();
final BBoxDBClient bboxDBClient = connection.getBboxDBClient();
bboxDBClient.cancelQuery(transmittedPackage.getSequenceNumber());
}
use of org.bboxdb.network.packages.NetworkRequestPackage in project bboxdb by jnidzwetzki.
the class TestTupleListFuture method getFuture.
/**
* @return
*/
private static NetworkOperationFuture getFuture() {
final BBoxDBConnection connection = Mockito.mock(BBoxDBConnection.class);
final Supplier<NetworkRequestPackage> supplier = () -> (null);
final NetworkOperationFuture networkOperationFuture = new NetworkOperationFuture(connection, supplier);
return networkOperationFuture;
}
use of org.bboxdb.network.packages.NetworkRequestPackage in project bboxdb by jnidzwetzki.
the class TestCompressionRatio method handleCompressedData.
/**
* Handle compressed packages
* @param tableName
* @param buffer
* @return
* @throws PackageEncodeException
* @throws IOException
*/
protected long handleCompressedData(final TupleStoreName tableName, final List<Tuple> buffer) {
final RoutingHeader routingHeader = new RoutingHeader(false);
final List<NetworkRequestPackage> packages = buffer.stream().map(t -> new InsertTupleRequest((short) 4, routingHeader, tableName, t)).collect(Collectors.toList());
final CompressionEnvelopeRequest compressionEnvelopeRequest = new CompressionEnvelopeRequest(NetworkConst.COMPRESSION_TYPE_GZIP, packages);
buffer.clear();
return packageToBytes(compressionEnvelopeRequest);
}
Aggregations