Search in sources :

Example 1 with PagedTransferableEntity

use of org.bboxdb.storage.entity.PagedTransferableEntity in project bboxdb by jnidzwetzki.

the class PageEndHandler method handleServerResult.

/**
 * Handle the end of a page
 * @return
 */
@Override
public boolean handleServerResult(final BBoxDBConnection bBoxDBConnection, final ByteBuffer encodedPackage, final NetworkOperationFuture future) throws PackageEncodeException {
    if (logger.isDebugEnabled()) {
        logger.debug("Handle page end package");
    }
    final PageEndResponse result = PageEndResponse.decodePackage(encodedPackage);
    final short sequenceNumber = result.getSequenceNumber();
    final List<PagedTransferableEntity> resultList = bBoxDBConnection.getResultBuffer().remove(sequenceNumber);
    // Collect tuples of the next page in new list
    bBoxDBConnection.getResultBuffer().put(sequenceNumber, new ArrayList<>());
    if (future == null) {
        logger.warn("Got handleMultiTupleEnd and pendingCall is empty");
        return true;
    }
    if (resultList == null) {
        logger.warn("Got handleMultiTupleEnd and resultList is empty (package {})", sequenceNumber);
        future.setFailedState();
        future.fireCompleteEvent();
        return true;
    }
    ResponseHandlerHelper.castAndSetFutureResult(future, resultList, false);
    return true;
}
Also used : PagedTransferableEntity(org.bboxdb.storage.entity.PagedTransferableEntity) PageEndResponse(org.bboxdb.network.packages.response.PageEndResponse)

Example 2 with PagedTransferableEntity

use of org.bboxdb.storage.entity.PagedTransferableEntity in project bboxdb by jnidzwetzki.

the class MultipleTupleEndHandler method handleServerResult.

@Override
public boolean handleServerResult(final BBoxDBConnection bBoxDBConnection, final ByteBuffer encodedPackage, final NetworkOperationFuture future) throws PackageEncodeException {
    if (logger.isDebugEnabled()) {
        logger.debug("Handle multiple tuple end package");
    }
    final MultipleTupleEndResponse result = MultipleTupleEndResponse.decodePackage(encodedPackage);
    final short sequenceNumber = result.getSequenceNumber();
    final Map<Short, List<PagedTransferableEntity>> resultBuffer = bBoxDBConnection.getResultBuffer();
    final List<PagedTransferableEntity> resultList = resultBuffer.remove(sequenceNumber);
    if (future == null) {
        logger.error("Got handleMultiTupleEnd and future is null (package {}) ", sequenceNumber);
        return true;
    }
    if (resultList == null) {
        logger.error("Got handleMultiTupleEnd and resultList is empty (package {})", sequenceNumber);
        future.setFailedState();
        future.fireCompleteEvent();
        return true;
    }
    ResponseHandlerHelper.castAndSetFutureResult(future, resultList, true);
    return true;
}
Also used : PagedTransferableEntity(org.bboxdb.storage.entity.PagedTransferableEntity) MultipleTupleEndResponse(org.bboxdb.network.packages.response.MultipleTupleEndResponse) List(java.util.List)

Example 3 with PagedTransferableEntity

use of org.bboxdb.storage.entity.PagedTransferableEntity in project bboxdb by jnidzwetzki.

the class ResponseHandlerHelper method castAndSetFutureResult.

/**
 * Cast the content on the result list according to the future type
 * @param future
 * @param resultList
 * @param completeResult
 * @throws PackageEncodeException
 */
public static void castAndSetFutureResult(final NetworkOperationFuture future, final List<PagedTransferableEntity> resultList, final boolean completeResult) throws PackageEncodeException {
    if (resultList.isEmpty()) {
        future.setCompleteResult(completeResult);
        future.setOperationResult(new ArrayList<>());
        future.fireCompleteEvent();
        return;
    }
    final PagedTransferableEntity firstElement = resultList.get(0);
    if (firstElement instanceof Tuple) {
        final List<Tuple> tupleList = new ArrayList<>();
        for (final PagedTransferableEntity entity : resultList) {
            tupleList.add((Tuple) entity);
        }
        future.setCompleteResult(completeResult);
        future.setOperationResult(tupleList);
        future.fireCompleteEvent();
    } else if (firstElement instanceof JoinedTuple) {
        final List<JoinedTuple> tupleList = new ArrayList<>();
        for (final PagedTransferableEntity entity : resultList) {
            tupleList.add((JoinedTuple) entity);
        }
        future.setCompleteResult(completeResult);
        future.setOperationResult(tupleList);
        future.fireCompleteEvent();
    } else {
        throw new PackageEncodeException("Unknown future type: " + firstElement);
    }
}
Also used : PagedTransferableEntity(org.bboxdb.storage.entity.PagedTransferableEntity) ArrayList(java.util.ArrayList) JoinedTuple(org.bboxdb.storage.entity.JoinedTuple) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) List(java.util.List) ArrayList(java.util.ArrayList) JoinedTuple(org.bboxdb.storage.entity.JoinedTuple) Tuple(org.bboxdb.storage.entity.Tuple)

Example 4 with PagedTransferableEntity

use of org.bboxdb.storage.entity.PagedTransferableEntity in project bboxdb by jnidzwetzki.

the class TupleHandler method handleServerResult.

/**
 * Handle a single tuple as result
 * @return
 */
@Override
public boolean handleServerResult(final BBoxDBConnection bBoxDBConnection, final ByteBuffer encodedPackage, final NetworkOperationFuture future) throws PackageEncodeException {
    if (logger.isDebugEnabled()) {
        logger.debug("Handle tuple package");
    }
    final TupleResponse singleTupleResponse = TupleResponse.decodePackage(encodedPackage);
    final short sequenceNumber = singleTupleResponse.getSequenceNumber();
    // Tuple is part of a multi tuple result
    final Map<Short, List<PagedTransferableEntity>> resultBuffer = bBoxDBConnection.getResultBuffer();
    if (resultBuffer.containsKey(sequenceNumber)) {
        final List<PagedTransferableEntity> packageResult = resultBuffer.get(sequenceNumber);
        packageResult.add(singleTupleResponse.getTuple());
        // tuple result or a multiple tuple result
        return false;
    }
    // Single tuple is returned
    if (future != null) {
        future.setOperationResult(Arrays.asList(singleTupleResponse.getTuple()));
        future.fireCompleteEvent();
    }
    return true;
}
Also used : TupleResponse(org.bboxdb.network.packages.response.TupleResponse) PagedTransferableEntity(org.bboxdb.storage.entity.PagedTransferableEntity) List(java.util.List)

Aggregations

PagedTransferableEntity (org.bboxdb.storage.entity.PagedTransferableEntity)4 List (java.util.List)3 ArrayList (java.util.ArrayList)1 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)1 MultipleTupleEndResponse (org.bboxdb.network.packages.response.MultipleTupleEndResponse)1 PageEndResponse (org.bboxdb.network.packages.response.PageEndResponse)1 TupleResponse (org.bboxdb.network.packages.response.TupleResponse)1 JoinedTuple (org.bboxdb.storage.entity.JoinedTuple)1 Tuple (org.bboxdb.storage.entity.Tuple)1