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;
}
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();
}
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;
}
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;
}
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;
}
Aggregations