@Override public Iterable<MAVLinkRecord> query(String deviceId, Date startTime, Date endTime, Integer msgId) throws IOException { RangeKeyCondition timeInterval; if (startTime == null && endTime == null) { timeInterval = null; } else if (startTime == null && endTime != null) { timeInterval = new RangeKeyCondition(ATTR_TIME).le(endTime.getTime()); } else if (startTime != null && endTime == null) { timeInterval = new RangeKeyCondition(ATTR_TIME).ge(startTime.getTime()); } else { timeInterval = new RangeKeyCondition(ATTR_TIME).between(startTime.getTime(), endTime.getTime()); } QueryFilter filter = new QueryFilter(ATTR_MSG_ID).eq(msgId); return new MAVLinkRecordIterable(table.query(ATTR_DEVICE_ID, deviceId, timeInterval, filter).iterator()); }
private static <T extends Item> void addRangeKeyConditionToQuerySpec(final QuerySpec querySpec, final CompoundAttributeQuery compoundAttributeQuery, final Class<T> itemClass) { final String supportingConditionStringValue = compoundAttributeQuery.getSupportingCondition().getValues().iterator() .next(); final Operators comparisonOperator = compoundAttributeQuery.getSupportingCondition().getComparisonOperator(); final Class<?> supportingAttributeType = compoundAttributeQuery.getSupportingAttributeType(itemClass); try { final Object supportingConditionValue = supportingAttributeType.getConstructor(String.class).newInstance(supportingConditionStringValue); final RangeKeyCondition rangeKeyCondition = RangeKeyConditionBuilder .build(compoundAttributeQuery.getSupportingAttributeName(), supportingConditionValue, comparisonOperator); querySpec.withRangeKeyCondition(rangeKeyCondition); } catch (final Exception e) { throw new PersistenceResourceFailureException( String.format("Could not add range key condition for query: %s on item %s.", compoundAttributeQuery, itemClass.getSimpleName()), e); } }
public static RangeKeyCondition build(final String attributeName, final Object value, final Operators comparisonOperator) { Check.isNotEmptyOrNull("attributeName", attributeName); Check.isNotNull("conditionValue", value); final RangeKeyCondition rangeKeyCondition = new RangeKeyCondition(attributeName); switch (comparisonOperator) { case EQUALS: rangeKeyCondition.eq(value); break; case GREATER_THAN_OR_EQUALS: rangeKeyCondition.ge(value); break; case LESS_THAN_OR_EQUALS: rangeKeyCondition.le(value); break; default: throw new InvalidConditionOperatorException(String .format("Operation %s not valid for range key condition.", comparisonOperator.toString())); } return rangeKeyCondition; }
@Test public void shouldBuildRangeKeyCondition_withAttributeNameConditionValueAndEqualComparisonOperator() { // Given final String attributeName = randomString(); final String conditionValue = randomString(); final Operators operator = Operators.EQUALS; // When final RangeKeyCondition rangeKeyCondition = RangeKeyConditionBuilder.build(attributeName, conditionValue, operator); // Then assertEquals(attributeName, rangeKeyCondition.getAttrName()); assertTrue(rangeKeyCondition.getValues().length == 1); assertEquals(conditionValue, rangeKeyCondition.getValues()[0]); assertEquals(KeyConditions.EQ, rangeKeyCondition.getKeyCondition()); }
@Test public void shouldBuildRangeKeyCondition_withAttributeNameConditionValueAndLessThanOrEqualToComparisonOperator() { // Given final String attributeName = randomString(); final String conditionValue = randomString(); final Operators operator = Operators.LESS_THAN_OR_EQUALS; // When final RangeKeyCondition rangeKeyCondition = RangeKeyConditionBuilder.build(attributeName, conditionValue, operator); // Then assertEquals(attributeName, rangeKeyCondition.getAttrName()); assertTrue(rangeKeyCondition.getValues().length == 1); assertEquals(conditionValue, rangeKeyCondition.getValues()[0]); assertEquals(KeyConditions.LE, rangeKeyCondition.getKeyCondition()); }
@Test public void shouldBuildRangeKeyCondition_withAttributeNameConditionValueAndGreaterThanOrEqualToComparisonOperator() { // Given final String attributeName = randomString(); final String conditionValue = randomString(); final Operators operator = Operators.GREATER_THAN_OR_EQUALS; // When final RangeKeyCondition rangeKeyCondition = RangeKeyConditionBuilder.build(attributeName, conditionValue, operator); // Then assertEquals(attributeName, rangeKeyCondition.getAttrName()); assertTrue(rangeKeyCondition.getValues().length == 1); assertEquals(conditionValue, rangeKeyCondition.getValues()[0]); assertEquals(KeyConditions.GE, rangeKeyCondition.getKeyCondition()); }
@Override public <V> QueryItemsBuilder<T> lt(V value) { if ( "".equals(value) ) throw new UnsupportedOperationException("Attempt to compare with empty string"); _spec.withRangeKeyCondition(new RangeKeyCondition(_rkName) .lt(value)); return this; }
@Override public <V> QueryItemsBuilder<T> le(V value) { if ( "".equals(value) ) throw new UnsupportedOperationException("Attempt to compare with empty string"); _spec.withRangeKeyCondition(new RangeKeyCondition(_rkName) .le(value)); return this; }
@Override public <V> QueryItemsBuilder<T> gt(V value) { if ( "".equals(value) ) throw new UnsupportedOperationException("Attempt to compare with empty string"); _spec.withRangeKeyCondition(new RangeKeyCondition(_rkName) .gt(value)); return this; }
@Override public <V> QueryItemsBuilder<T> ge(V value) { if ( "".equals(value) ) throw new UnsupportedOperationException("Attempt to compare with empty string"); _spec.withRangeKeyCondition(new RangeKeyCondition(_rkName) .ge(value)); return this; }
@Override public <V> QueryItemsBuilder<T> between(V value1, V value2) { if ( "".equals(value1) ) throw new UnsupportedOperationException("Attempt to compare with empty string"); if ( "".equals(value2) ) throw new UnsupportedOperationException("Attempt to compare with empty string"); _spec.withRangeKeyCondition(new RangeKeyCondition(_rkName) .between(value1, value2)); return this; }
@Override public QueryItemsBuilder<T> beginsWith(String value) { if ( "".equals(value) ) throw new UnsupportedOperationException("Attempt to compare with empty string"); _spec.withRangeKeyCondition(new RangeKeyCondition(_rkName) .beginsWith(value)); return this; }
@Test public void shouldBuild_withCompoundAttributeQuery() { // Given final String attributeName = randomString(10); final String supportingAttributeName = randomString(10); final Condition condition = randomCondition(1); final Condition supportingCondition = randomCondition(1); final CompoundAttributeQuery compoundAttributeQuery = mock(CompoundAttributeQuery.class); final RangeKeyCondition mockRangeKeyCondition = mock(RangeKeyCondition.class); final String expectedValue = condition.getValues().iterator().next(); final String expectedSupportingValue = supportingCondition.getValues().iterator().next(); final Operators expectedSupportingComparisonOperator = supportingCondition.getComparisonOperator(); when(compoundAttributeQuery.getAttributeName()).thenReturn(attributeName); when(compoundAttributeQuery.getCondition()).thenReturn(condition); when(compoundAttributeQuery.getSupportingCondition()).thenReturn(supportingCondition); Mockito.<Class<?>> when(compoundAttributeQuery.getSupportingAttributeType(any())).thenReturn(String.class); when(compoundAttributeQuery.getSupportingAttributeName()).thenReturn(supportingAttributeName); when(RangeKeyConditionBuilder.build(supportingAttributeName, expectedSupportingValue, expectedSupportingComparisonOperator)).thenReturn(mockRangeKeyCondition); // When final QuerySpec querySpec = QuerySpecBuilder.build(compoundAttributeQuery, StubWithGlobalSecondaryIndexItem.class); // Then assertEquals(attributeName, querySpec.getHashKey().getName()); assertEquals(expectedValue, querySpec.getHashKey().getValue()); assertEquals(mockRangeKeyCondition, querySpec.getRangeKeyCondition()); }
@Override public <V> QueryItemsBuilder<T> eq(V value) { _spec.withRangeKeyCondition(new RangeKeyCondition(_rkName) .eq(toDDBEmptyString(value))); return this; }