Search in sources :

Example 56 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class CompressionEnvelopeRequest method decodePackage.

/**
 * Decode the encoded package into a uncompressed byte stream
 *
 * @param encodedPackage
 * @return
 * @throws IOException
 * @throws PackageEncodeException
 */
public static InputStream decodePackage(final ByteBuffer encodedPackage) throws PackageEncodeException {
    final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_COMPRESSION);
    if (decodeResult == false) {
        throw new PackageEncodeException("Unable to decode package");
    }
    final byte compressionType = encodedPackage.get();
    if (compressionType != NetworkConst.COMPRESSION_TYPE_GZIP) {
        throw new PackageEncodeException("Unknown compression type: " + compressionType);
    }
    // Skip 3 bytes - Header
    encodedPackage.getShort();
    encodedPackage.get();
    final byte[] compressedBytes = new byte[encodedPackage.remaining()];
    encodedPackage.get(compressedBytes, 0, encodedPackage.remaining());
    final byte[] uncompressedBytes = NetworkHelper.uncompressBytes(compressionType, compressedBytes);
    final ByteArrayInputStream bis = new ByteArrayInputStream(uncompressedBytes);
    return bis;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException)

Example 57 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class TestCompressionRatio method runExperiment.

/**
 * Run the experiment with the given batch size
 * @param batchSize
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws PackageEncodeException
 */
protected long runExperiment(final Integer batchSize) throws ClassNotFoundException, IOException, PackageEncodeException {
    final TupleStoreName tableName = new TupleStoreName("2_group1_table1");
    final List<Long> experimentSize = new ArrayList<>();
    final List<Tuple> buffer = new ArrayList<>();
    final TupleFileReader tupleFile = new TupleFileReader(filename, format);
    tupleFile.addTupleListener(t -> {
        if (batchSize == 0) {
            final long size = handleUncompressedData(tableName, t);
            experimentSize.add(size);
        } else if (buffer.size() == batchSize) {
            final long size = handleCompressedData(tableName, buffer);
            experimentSize.add(size);
        } else {
            buffer.add(t);
        }
    });
    try {
        tupleFile.processFile();
    } catch (IOException e) {
        logger.error("Got an IO-Exception while reading file", e);
        System.exit(-1);
    }
    return experimentSize.stream().mapToLong(i -> i).sum();
}
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) TupleFileReader(org.bboxdb.tools.TupleFileReader) ArrayList(java.util.ArrayList) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) IOException(java.io.IOException) Tuple(org.bboxdb.storage.entity.Tuple)

Example 58 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class TestCompressionRatio method packageToBytes.

/**
 * Convert the given package into a byte stream
 * @param networkPackage
 * @return
 */
protected long packageToBytes(final NetworkRequestPackage networkPackage) {
    try {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        networkPackage.writeToOutputStream(os);
        os.close();
        return os.toByteArray().length;
    } catch (IOException e) {
        logger.error("Got an IO-Exception while closing stream", e);
        System.exit(-1);
    } catch (PackageEncodeException e) {
        logger.error("Got an Package encode exception", e);
        System.exit(-1);
    }
    // Unreachable code
    return -1;
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 59 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class CreateDistributionGroupHandler method handleRequest.

@Override
public /**
 * Create a new distribution group
 */
boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
    try {
        final CreateDistributionGroupRequest createPackage = CreateDistributionGroupRequest.decodeTuple(encodedPackage);
        final String distributionGroup = createPackage.getDistributionGroup();
        logger.info("Create distribution group: {}", distributionGroup);
        final DistributionGroupAdapter distributionGroupAdapter = ZookeeperClientFactory.getZookeeperClient().getDistributionGroupAdapter();
        final DistributionRegionAdapter distributionRegionAdapter = ZookeeperClientFactory.getZookeeperClient().getDistributionRegionAdapter();
        final List<String> knownGroups = distributionGroupAdapter.getDistributionGroups();
        if (knownGroups.contains(distributionGroup)) {
            logger.error("Untable to create distributon group {}, already exists", distributionGroup);
            final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_DGROUP_EXISTS);
            clientConnectionHandler.writeResultPackage(responsePackage);
            return true;
        }
        distributionGroupAdapter.createDistributionGroup(distributionGroup, createPackage.getDistributionGroupConfiguration());
        final SpacePartitioner spacePartitioner = SpacePartitionerCache.getInstance().getSpacePartitionerForGroupName(distributionGroup);
        final DistributionRegion region = spacePartitioner.getRootNode();
        distributionRegionAdapter.setStateForDistributionRegion(region, DistributionRegionState.ACTIVE);
        clientConnectionHandler.writeResultPackage(new SuccessResponse(packageSequence));
    } catch (Exception e) {
        logger.warn("Error while create distribution group", e);
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
        clientConnectionHandler.writeResultPackage(responsePackage);
    }
    return true;
}
Also used : DistributionGroupAdapter(org.bboxdb.distribution.zookeeper.DistributionGroupAdapter) SuccessResponse(org.bboxdb.network.packages.response.SuccessResponse) CreateDistributionGroupRequest(org.bboxdb.network.packages.request.CreateDistributionGroupRequest) DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) DistributionRegionAdapter(org.bboxdb.distribution.zookeeper.DistributionRegionAdapter) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse) SpacePartitioner(org.bboxdb.distribution.partitioner.SpacePartitioner)

Example 60 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class CreateTableHandler method handleRequest.

@Override
public /**
 * Handle the create table call
 */
boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
    try {
        final CreateTableRequest createPackage = CreateTableRequest.decodeTuple(encodedPackage);
        final TupleStoreName requestTable = createPackage.getTable();
        logger.info("Got create call for table: {}", requestTable.getFullname());
        final TupleStoreAdapter tupleStoreAdapter = ZookeeperClientFactory.getZookeeperClient().getTupleStoreAdapter();
        if (tupleStoreAdapter.isTableKnown(requestTable)) {
            logger.warn("Table name is already known {}", requestTable.getFullname());
            final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_TABLE_EXISTS);
            clientConnectionHandler.writeResultPackage(responsePackage);
        } else {
            tupleStoreAdapter.writeTuplestoreConfiguration(requestTable, createPackage.getTupleStoreConfiguration());
            clientConnectionHandler.writeResultPackage(new SuccessResponse(packageSequence));
        }
    } catch (Exception e) {
        logger.warn("Error while delete tuple", e);
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
        clientConnectionHandler.writeResultPackage(responsePackage);
    }
    return true;
}
Also used : SuccessResponse(org.bboxdb.network.packages.response.SuccessResponse) TupleStoreAdapter(org.bboxdb.distribution.zookeeper.TupleStoreAdapter) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) CreateTableRequest(org.bboxdb.network.packages.request.CreateTableRequest) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Aggregations

PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)68 IOException (java.io.IOException)31 ByteBuffer (java.nio.ByteBuffer)21 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)16 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)14 RoutingHeader (org.bboxdb.network.routing.RoutingHeader)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ArrayList (java.util.ArrayList)9 BoundingBox (org.bboxdb.commons.math.BoundingBox)9 List (java.util.List)8 SuccessResponse (org.bboxdb.network.packages.response.SuccessResponse)6 Tuple (org.bboxdb.storage.entity.Tuple)6 StreamClientQuery (org.bboxdb.network.server.StreamClientQuery)5 OperatorTreeBuilder (org.bboxdb.storage.queryprocessor.OperatorTreeBuilder)5 NetworkRequestPackage (org.bboxdb.network.packages.NetworkRequestPackage)4 Operator (org.bboxdb.storage.queryprocessor.operator.Operator)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ExceptionSafeRunnable (org.bboxdb.commons.concurrent.ExceptionSafeRunnable)3 CompressionEnvelopeRequest (org.bboxdb.network.packages.request.CompressionEnvelopeRequest)3 ClientQuery (org.bboxdb.network.server.ClientQuery)3