Search in sources :

Example 91 with BoundingBox

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);
}
Also used : SpacePartitioner(org.bboxdb.distribution.partitioner.SpacePartitioner) Logger(org.slf4j.Logger) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) LoggerFactory(org.slf4j.LoggerFactory) RoutingHop(org.bboxdb.network.routing.RoutingHop) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) BoundingBox(org.bboxdb.commons.math.BoundingBox) ArrayList(java.util.ArrayList) Const(org.bboxdb.misc.Const) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) ZookeeperException(org.bboxdb.distribution.zookeeper.ZookeeperException) List(java.util.List) DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) RoutingHopHelper(org.bboxdb.network.routing.RoutingHopHelper) BBoxDBException(org.bboxdb.misc.BBoxDBException) SpacePartitionerCache(org.bboxdb.distribution.partitioner.SpacePartitionerCache) DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) RoutingHop(org.bboxdb.network.routing.RoutingHop) BBoxDBException(org.bboxdb.misc.BBoxDBException) SpacePartitioner(org.bboxdb.distribution.partitioner.SpacePartitioner)

Example 92 with BoundingBox

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);
}
Also used : BoundingBox(org.bboxdb.commons.math.BoundingBox) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) RoutingHeader(org.bboxdb.network.routing.RoutingHeader)

Example 93 with BoundingBox

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);
}
Also used : ArrayList(java.util.ArrayList) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) BoundingBox(org.bboxdb.commons.math.BoundingBox) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName)

Example 94 with BoundingBox

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());
}
Also used : QueryBoundingBoxTimeRequest(org.bboxdb.network.packages.request.QueryBoundingBoxTimeRequest) BoundingBox(org.bboxdb.commons.math.BoundingBox) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 95 with BoundingBox

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);
}
Also used : BoundingBox(org.bboxdb.commons.math.BoundingBox) JoinedTuple(org.bboxdb.storage.entity.JoinedTuple) JoinedTupleResponse(org.bboxdb.network.packages.response.JoinedTupleResponse) ByteBuffer(java.nio.ByteBuffer) JoinedTuple(org.bboxdb.storage.entity.JoinedTuple) Tuple(org.bboxdb.storage.entity.Tuple) DeletedTuple(org.bboxdb.storage.entity.DeletedTuple) Test(org.junit.Test)

Aggregations

BoundingBox (org.bboxdb.commons.math.BoundingBox)194 Test (org.junit.Test)113 Tuple (org.bboxdb.storage.entity.Tuple)61 ArrayList (java.util.ArrayList)25 JoinedTuple (org.bboxdb.storage.entity.JoinedTuple)24 DeletedTuple (org.bboxdb.storage.entity.DeletedTuple)23 BBoxDBException (org.bboxdb.misc.BBoxDBException)22 DistributionRegion (org.bboxdb.distribution.region.DistributionRegion)20 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)16 List (java.util.List)15 DistributionRegionIdMapper (org.bboxdb.distribution.region.DistributionRegionIdMapper)13 ZookeeperException (org.bboxdb.distribution.zookeeper.ZookeeperException)13 TupleStoreConfiguration (org.bboxdb.storage.entity.TupleStoreConfiguration)13 TupleListFuture (org.bboxdb.network.client.future.TupleListFuture)12 ZookeeperNotFoundException (org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException)11 EmptyResultFuture (org.bboxdb.network.client.future.EmptyResultFuture)11 IOException (java.io.IOException)10 Date (java.util.Date)10 DoubleInterval (org.bboxdb.commons.math.DoubleInterval)10 SpatialIndexReadOperator (org.bboxdb.storage.queryprocessor.operator.SpatialIndexReadOperator)10