use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class RoutingHeaderHelper method getRoutingHeaderForLocalSystem.
/**
* Get the routing header for the local system
* @param table
* @param serverAddress
* @param tuple
* @throws ZookeeperException
* @throws BBoxDBException
* @throws InterruptedException
*/
public static RoutingHeader getRoutingHeaderForLocalSystem(final String table, BoundingBox boundingBox, final boolean allowEmptyHop, final InetSocketAddress serverAddress, final boolean write) throws ZookeeperException, BBoxDBException, InterruptedException {
final TupleStoreName ssTableName = new TupleStoreName(table);
final String distributionGroup = ssTableName.getDistributionGroup();
final SpacePartitioner spacepartitioner = SpacePartitionerCache.getInstance().getSpacePartitionerForGroupName(distributionGroup);
final DistributionRegion distributionRegion = spacepartitioner.getRootNode();
if (boundingBox == null) {
boundingBox = BoundingBox.FULL_SPACE;
}
final List<RoutingHop> hops = getLocalHops(boundingBox, distributionRegion, write);
if (hops == null || hops.isEmpty()) {
if (!allowEmptyHop) {
throw new BBoxDBException("Got empty result list when query for write: " + boundingBox + " / in table " + table);
}
return new RoutingHeader((short) 0, new ArrayList<>());
}
// Filter the local hop
final List<RoutingHop> connectionHop = hops.stream().filter(r -> r.getDistributedInstance().getInetSocketAddress().equals(serverAddress)).collect(Collectors.toList());
if (!allowEmptyHop && connectionHop.isEmpty()) {
throw new BBoxDBException("Unable to find host " + serverAddress + " in global routing list: " + hops);
}
return new RoutingHeader((short) 0, connectionHop);
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class QueryBoundingBoxRequest method decodeTuple.
/**
* Decode the encoded package into a object
*
* @param encodedPackage
* @return
* @throws PackageEncodeException
* @throws IOException
*/
public static QueryBoundingBoxRequest decodeTuple(final ByteBuffer encodedPackage) throws PackageEncodeException, IOException {
final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_QUERY);
if (decodeResult == false) {
throw new PackageEncodeException("Unable to decode package");
}
final byte queryType = encodedPackage.get();
if (queryType != NetworkConst.REQUEST_QUERY_BBOX) {
throw new PackageEncodeException("Wrong query type: " + queryType + " required type is: " + NetworkConst.REQUEST_QUERY_BBOX);
}
boolean pagingEnabled = false;
if (encodedPackage.get() != 0) {
pagingEnabled = true;
}
final short tuplesPerPage = encodedPackage.getShort();
final short tableLength = encodedPackage.getShort();
// 2 unused bytes
encodedPackage.get();
encodedPackage.get();
final int bboxLength = encodedPackage.getInt();
final byte[] tableBytes = new byte[tableLength];
encodedPackage.get(tableBytes, 0, tableBytes.length);
final String table = new String(tableBytes);
final byte[] bboxBytes = new byte[bboxLength];
encodedPackage.get(bboxBytes, 0, bboxBytes.length);
final BoundingBox boundingBox = BoundingBox.fromByteArray(bboxBytes);
if (encodedPackage.remaining() != 0) {
throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
}
final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
return new QueryBoundingBoxRequest(sequenceNumber, routingHeader, table, boundingBox, pagingEnabled, tuplesPerPage);
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class QueryJoinRequest method decodeTuple.
/**
* Decode the encoded package into a object
*
* @param encodedPackage
* @return
* @throws PackageEncodeException
* @throws IOException
*/
public static QueryJoinRequest decodeTuple(final ByteBuffer encodedPackage) throws PackageEncodeException, IOException {
final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_QUERY);
if (decodeResult == false) {
throw new PackageEncodeException("Unable to decode package");
}
final byte queryType = encodedPackage.get();
if (queryType != NetworkConst.REQUEST_QUERY_JOIN) {
throw new PackageEncodeException("Wrong query type: " + queryType + " required type is: " + NetworkConst.REQUEST_QUERY_JOIN);
}
boolean pagingEnabled = false;
if (encodedPackage.get() != 0) {
pagingEnabled = true;
}
final short tuplesPerPage = encodedPackage.getShort();
final int numberOfTables = encodedPackage.getInt();
final int bboxLength = encodedPackage.getInt();
final byte[] bboxBytes = new byte[bboxLength];
encodedPackage.get(bboxBytes, 0, bboxBytes.length);
final BoundingBox boundingBox = BoundingBox.fromByteArray(bboxBytes);
final List<TupleStoreName> tableNames = new ArrayList<>();
for (int i = 0; i < numberOfTables; i++) {
final short tableNameLength = encodedPackage.getShort();
final byte[] tableBytes = new byte[tableNameLength];
encodedPackage.get(tableBytes, 0, tableBytes.length);
final String tablename = new java.lang.String(tableBytes);
tableNames.add(new TupleStoreName(tablename));
}
if (encodedPackage.remaining() != 0) {
throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
}
final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
return new QueryJoinRequest(sequenceNumber, routingHeader, tableNames, boundingBox, pagingEnabled, tuplesPerPage);
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class TestNetworkClasses method testDecodeBoundingBoxAndTime.
/**
* Test decode time query
* @throws IOException
* @throws PackageEncodeException
*/
@Test(timeout = 60000)
public void testDecodeBoundingBoxAndTime() throws IOException, PackageEncodeException {
final String table = "table1";
final long timeStamp = 4711;
final BoundingBox boundingBox = new BoundingBox(10d, 20d);
final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
final QueryBoundingBoxTimeRequest queryRequest = new QueryBoundingBoxTimeRequest(sequenceNumber, ROUTING_HEADER_ROUTED, table, boundingBox, timeStamp, true, (short) 50);
byte[] encodedPackage = networkPackageToByte(queryRequest);
Assert.assertNotNull(encodedPackage);
final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedPackage);
boolean result = NetworkPackageDecoder.validateRequestPackageHeader(bb, NetworkConst.REQUEST_TYPE_QUERY);
Assert.assertTrue(result);
Assert.assertEquals(NetworkConst.REQUEST_QUERY_BBOX_AND_TIME, NetworkPackageDecoder.getQueryTypeFromRequest(bb));
final QueryBoundingBoxTimeRequest decodedPackage = QueryBoundingBoxTimeRequest.decodeTuple(bb);
Assert.assertEquals(queryRequest.getBoundingBox(), decodedPackage.getBoundingBox());
Assert.assertEquals(queryRequest.getTimestamp(), decodedPackage.getTimestamp());
Assert.assertEquals(queryRequest.getTable(), decodedPackage.getTable());
Assert.assertEquals(queryRequest.isPagingEnabled(), decodedPackage.isPagingEnabled());
Assert.assertEquals(queryRequest.getTuplesPerPage(), decodedPackage.getTuplesPerPage());
Assert.assertEquals(queryRequest.toString(), decodedPackage.toString());
}
use of org.bboxdb.commons.math.BoundingBox in project bboxdb by jnidzwetzki.
the class TestNetworkClasses method encodeAndDecodeJoinedTuple.
/**
* Test the decoding and the encoding of a joined tuple
* @throws PackageEncodeException
* @throws IOException
*/
@Test(timeout = 60000)
public void encodeAndDecodeJoinedTuple() throws IOException, PackageEncodeException {
final Tuple tuple1 = new Tuple("key1", new BoundingBox(1.3244343224, 232.232333343, 34324.343, 343243.0), "abc".getBytes(), 12);
final Tuple tuple2 = new Tuple("key2", new BoundingBox(1.32443453224, 545334.03, 34324.343, 343243.0), "abc".getBytes(), 12);
final Tuple tuple3 = new Tuple("key3", new BoundingBox(1.35433224, 5453.43, 34324.343, 343243.0), "abc".getBytes(), 12);
final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
final List<Tuple> tupleList = Arrays.asList(tuple1, tuple2, tuple3);
final List<String> tableNames = Arrays.asList("abc", "def", "geh");
final JoinedTuple joinedTuple = new JoinedTuple(tupleList, tableNames);
final JoinedTupleResponse joinedResponse = new JoinedTupleResponse(sequenceNumber, joinedTuple);
byte[] encodedVersion = networkPackageToByte(joinedResponse);
Assert.assertNotNull(encodedVersion);
final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedVersion);
final JoinedTupleResponse decodedPackage = JoinedTupleResponse.decodePackage(bb);
final JoinedTuple decodedJoinedTuple = decodedPackage.getJoinedTuple();
Assert.assertEquals(3, decodedJoinedTuple.getNumberOfTuples());
for (int i = 0; i < 3; i++) {
Assert.assertEquals(tupleList.get(i), decodedJoinedTuple.getTuple(i));
Assert.assertEquals(tableNames.get(i), decodedJoinedTuple.getTupleStoreName(i));
}
Assert.assertTrue(joinedResponse.toString().length() > 10);
}
Aggregations