Search in sources :

Example 1 with Condition

use of com.amazonaws.services.dynamodbv2.model.Condition in project camel by apache.

the class DdbStreamConsumer method createExchanges.

private Queue<Exchange> createExchanges(List<Record> records, String lastSeenSequenceNumber) {
    Queue<Exchange> exchanges = new ArrayDeque<>();
    BigIntComparisons condition = null;
    BigInteger providedSeqNum = null;
    if (lastSeenSequenceNumber != null) {
        providedSeqNum = new BigInteger(lastSeenSequenceNumber);
        condition = BigIntComparisons.Conditions.LT;
    }
    switch(getEndpoint().getIteratorType()) {
        case AFTER_SEQUENCE_NUMBER:
            condition = BigIntComparisons.Conditions.LT;
            providedSeqNum = new BigInteger(getEndpoint().getSequenceNumberProvider().getSequenceNumber());
            break;
        case AT_SEQUENCE_NUMBER:
            condition = BigIntComparisons.Conditions.LTEQ;
            providedSeqNum = new BigInteger(getEndpoint().getSequenceNumberProvider().getSequenceNumber());
            break;
        default:
    }
    for (Record record : records) {
        BigInteger recordSeqNum = new BigInteger(record.getDynamodb().getSequenceNumber());
        if (condition == null || condition.matches(providedSeqNum, recordSeqNum)) {
            exchanges.add(getEndpoint().createExchange(record));
        }
    }
    return exchanges;
}
Also used : Exchange(org.apache.camel.Exchange) BigInteger(java.math.BigInteger) Record(com.amazonaws.services.dynamodbv2.model.Record) ArrayDeque(java.util.ArrayDeque)

Example 2 with Condition

use of com.amazonaws.services.dynamodbv2.model.Condition in project camel by apache.

the class ShardIteratorHandler method getShardIterator.

String getShardIterator(String resumeFromSequenceNumber) {
    ShardIteratorType iteratorType = getEndpoint().getIteratorType();
    String sequenceNumber = getEndpoint().getSequenceNumber();
    if (resumeFromSequenceNumber != null) {
        // Reset things as we're in an error condition.
        currentShard = null;
        currentShardIterator = null;
        iteratorType = ShardIteratorType.AFTER_SEQUENCE_NUMBER;
        sequenceNumber = resumeFromSequenceNumber;
    }
    // either return a cached one or get a new one via a GetShardIterator request.
    if (currentShardIterator == null) {
        ListStreamsResult streamsListResult = getClient().listStreams(new ListStreamsRequest().withTableName(getEndpoint().getTableName()));
        // XXX assumes there is only one stream
        final String streamArn = streamsListResult.getStreams().get(0).getStreamArn();
        DescribeStreamResult streamDescriptionResult = getClient().describeStream(new DescribeStreamRequest().withStreamArn(streamArn));
        shardList.addAll(streamDescriptionResult.getStreamDescription().getShards());
        LOG.trace("Current shard is: {} (in {})", currentShard, shardList);
        if (currentShard == null) {
            currentShard = resolveNewShard(iteratorType, resumeFromSequenceNumber);
        } else {
            currentShard = shardList.nextAfter(currentShard);
        }
        shardList.removeOlderThan(currentShard);
        LOG.trace("Next shard is: {} (in {})", currentShard, shardList);
        GetShardIteratorResult result = getClient().getShardIterator(buildGetShardIteratorRequest(streamArn, iteratorType, sequenceNumber));
        currentShardIterator = result.getShardIterator();
    }
    LOG.trace("Shard Iterator is: {}", currentShardIterator);
    return currentShardIterator;
}
Also used : ShardIteratorType(com.amazonaws.services.dynamodbv2.model.ShardIteratorType) ListStreamsResult(com.amazonaws.services.dynamodbv2.model.ListStreamsResult) GetShardIteratorResult(com.amazonaws.services.dynamodbv2.model.GetShardIteratorResult) ListStreamsRequest(com.amazonaws.services.dynamodbv2.model.ListStreamsRequest) DescribeStreamResult(com.amazonaws.services.dynamodbv2.model.DescribeStreamResult) DescribeStreamRequest(com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest)

Example 3 with Condition

use of com.amazonaws.services.dynamodbv2.model.Condition in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method constructTimeCondition.

private Condition constructTimeCondition(FilterCriteria filter) {
    boolean hasBegin = filter.getBeginDate() != null;
    boolean hasEnd = filter.getEndDate() != null;
    final Condition timeCondition;
    if (!hasBegin && !hasEnd) {
        timeCondition = null;
    } else if (!hasBegin && hasEnd) {
        timeCondition = new Condition().withComparisonOperator(ComparisonOperator.LE).withAttributeValueList(new AttributeValue().withS(AbstractDynamoDBItem.DATEFORMATTER.format(filter.getEndDate())));
    } else if (hasBegin && !hasEnd) {
        timeCondition = new Condition().withComparisonOperator(ComparisonOperator.GE).withAttributeValueList(new AttributeValue().withS(AbstractDynamoDBItem.DATEFORMATTER.format(filter.getBeginDate())));
    } else {
        timeCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN).withAttributeValueList(new AttributeValue().withS(AbstractDynamoDBItem.DATEFORMATTER.format(filter.getBeginDate())), new AttributeValue().withS(AbstractDynamoDBItem.DATEFORMATTER.format(filter.getEndDate())));
    }
    return timeCondition;
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue)

Example 4 with Condition

use of com.amazonaws.services.dynamodbv2.model.Condition in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method createQueryExpression.

/**
     * Construct dynamodb query from filter
     *
     * @param filter
     * @return DynamoDBQueryExpression corresponding to the given FilterCriteria
     */
private DynamoDBQueryExpression<DynamoDBItem<?>> createQueryExpression(Class<? extends DynamoDBItem<?>> dtoClass, FilterCriteria filter) {
    DynamoDBItem<?> item = getDynamoDBHashKey(dtoClass, filter.getItemName());
    final DynamoDBQueryExpression<DynamoDBItem<?>> queryExpression = new DynamoDBQueryExpression<DynamoDBItem<?>>().withHashKeyValues(item).withScanIndexForward(filter.getOrdering() == Ordering.ASCENDING).withLimit(filter.getPageSize());
    Condition timeFilter = maybeAddTimeFilter(queryExpression, filter);
    maybeAddStateFilter(filter, queryExpression);
    logger.debug("Querying: {} with {}", filter.getItemName(), timeFilter);
    return queryExpression;
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition)

Example 5 with Condition

use of com.amazonaws.services.dynamodbv2.model.Condition in project camel by apache.

the class ShardList method atAfterSeq.

Shard atAfterSeq(String sequenceNumber, BigIntComparisons condition) {
    BigInteger atAfter = new BigInteger(sequenceNumber);
    List<Shard> sorted = new ArrayList<>();
    sorted.addAll(shards.values());
    Collections.sort(sorted, StartingSequenceNumberComparator.INSTANCE);
    for (Shard shard : sorted) {
        if (shard.getSequenceNumberRange().getEndingSequenceNumber() != null) {
            BigInteger end = new BigInteger(shard.getSequenceNumberRange().getEndingSequenceNumber());
            // essentially: after < end or after <= end
            if (condition.matches(atAfter, end)) {
                return shard;
            }
        }
    }
    if (shards.size() > 0) {
        return sorted.get(sorted.size() - 1);
    }
    throw new IllegalStateException("Unable to find a shard with appropriate sequence numbers for " + sequenceNumber + " in " + shards);
}
Also used : ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) Shard(com.amazonaws.services.dynamodbv2.model.Shard)

Aggregations

Condition (com.amazonaws.services.dynamodbv2.model.Condition)6 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)5 DynamoDBScanExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression)2 ComparisonOperator (com.amazonaws.services.dynamodbv2.model.ComparisonOperator)2 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)2 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 DescribeStreamRequest (com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest)1 DescribeStreamResult (com.amazonaws.services.dynamodbv2.model.DescribeStreamResult)1 GetShardIteratorResult (com.amazonaws.services.dynamodbv2.model.GetShardIteratorResult)1 ListStreamsRequest (com.amazonaws.services.dynamodbv2.model.ListStreamsRequest)1 ListStreamsResult (com.amazonaws.services.dynamodbv2.model.ListStreamsResult)1 Record (com.amazonaws.services.dynamodbv2.model.Record)1 Shard (com.amazonaws.services.dynamodbv2.model.Shard)1 ShardIteratorType (com.amazonaws.services.dynamodbv2.model.ShardIteratorType)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Exchange (org.apache.camel.Exchange)1