Search in sources :

Example 1 with NetworkRequestPackage

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();
    }
}
Also used : CompressionEnvelopeRequest(org.bboxdb.network.packages.request.CompressionEnvelopeRequest) NetworkRequestPackage(org.bboxdb.network.packages.NetworkRequestPackage) ArrayList(java.util.ArrayList) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException)

Example 2 with NetworkRequestPackage

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");
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) NetworkRequestPackage(org.bboxdb.network.packages.NetworkRequestPackage) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with NetworkRequestPackage

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());
}
Also used : BBoxDBClient(org.bboxdb.network.client.BBoxDBClient) NetworkRequestPackage(org.bboxdb.network.packages.NetworkRequestPackage) BBoxDBConnection(org.bboxdb.network.client.BBoxDBConnection)

Example 4 with NetworkRequestPackage

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;
}
Also used : NetworkRequestPackage(org.bboxdb.network.packages.NetworkRequestPackage) NetworkOperationFuture(org.bboxdb.network.client.future.NetworkOperationFuture) BBoxDBConnection(org.bboxdb.network.client.BBoxDBConnection)

Example 5 with NetworkRequestPackage

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);
}
Also used : Arrays(java.util.Arrays) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) Logger(org.slf4j.Logger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NetworkConst(org.bboxdb.network.NetworkConst) InsertTupleRequest(org.bboxdb.network.packages.request.InsertTupleRequest) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) Tuple(org.bboxdb.storage.entity.Tuple) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) List(java.util.List) TupleFileReader(org.bboxdb.tools.TupleFileReader) CompressionEnvelopeRequest(org.bboxdb.network.packages.request.CompressionEnvelopeRequest) NetworkRequestPackage(org.bboxdb.network.packages.NetworkRequestPackage) CompressionEnvelopeRequest(org.bboxdb.network.packages.request.CompressionEnvelopeRequest) NetworkRequestPackage(org.bboxdb.network.packages.NetworkRequestPackage) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) InsertTupleRequest(org.bboxdb.network.packages.request.InsertTupleRequest)

Aggregations

NetworkRequestPackage (org.bboxdb.network.packages.NetworkRequestPackage)6 IOException (java.io.IOException)3 BBoxDBConnection (org.bboxdb.network.client.BBoxDBConnection)3 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 NetworkOperationFuture (org.bboxdb.network.client.future.NetworkOperationFuture)2 CompressionEnvelopeRequest (org.bboxdb.network.packages.request.CompressionEnvelopeRequest)2 OutputStream (java.io.OutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 NetworkConst (org.bboxdb.network.NetworkConst)1 BBoxDBClient (org.bboxdb.network.client.BBoxDBClient)1 TupleListFuture (org.bboxdb.network.client.future.TupleListFuture)1 TupleListFutureStore (org.bboxdb.network.client.tools.TupleListFutureStore)1 InsertTupleRequest (org.bboxdb.network.packages.request.InsertTupleRequest)1