@Override public Optional<Group> loadByKey(String key) { Table table = dynamoDB.getTable(this.groupTableName); QuerySpec querySpec = new QuerySpec() .withKeyConditionExpression(HASH_KEY + " = :k_app_key") .withValueMap(new ValueMap() .withString(":k_app_key", key) ) .withMaxResultSize(1) .withConsistentRead(true); DynamoDbCommand<ItemCollection<QueryOutcome>> cmd = new DynamoDbCommand<>("loadByKey", () -> queryTable(table, querySpec), () -> { throw new RuntimeException("loadByKey"); }, dynamodbNamespaceGraphQueryHystrix, metrics); final ItemCollection<QueryOutcome> items = cmd.execute(); final IteratorSupport<Item, QueryOutcome> iterator = items.iterator(); if (iterator.hasNext()) { return Optional.of(GroupSupport.toGroup(iterator.next().getString("json"))); } return Optional.empty(); }
public MAVLinkRecordIterable(IteratorSupport<Item, QueryOutcome> it) { this.itemIterator = it; }
@Test public void shouldQueryIndex_withAttributeQueryOnHashPartOfCompoundIndex() { // Given final ItemId itemId = new ItemId(randomId()); final ItemConfiguration itemConfiguration = new ItemConfiguration(StubWithGlobalSecondaryIndexItem.class, tableName); itemConfiguration.registerIndexes((Arrays.asList(new CompoundIndexDefinition("gsi", "gsiSupportingValue")))); final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration); when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations); final Table mockTable = mock(Table.class); when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable); final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate( mockDatabaseSchemaHolder); dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient); final Index mockIndex = mock(Index.class); when(mockTable.getIndex(anyString())).thenReturn(mockIndex); final ItemCollection<QueryOutcome> mockOutcome = mock(ItemCollection.class); when(mockIndex.query(any(QuerySpec.class))).thenReturn(mockOutcome); final IteratorSupport<Item, QueryOutcome> mockIterator = mock(IteratorSupport.class); final Item mockItem = new Item(); mockItem.withString(randomString(), randomString()); when(mockOutcome.iterator()).thenReturn(mockIterator); when(mockIterator.hasNext()).thenReturn(true, false); when(mockIterator.next()).thenReturn(mockItem); // When final Collection<StubWithGlobalSecondaryIndexItem> stubWithGlobalSecondaryIndexItemCollection = dynamoDocumentStoreTemplate .fetch(new AttributeQuery("gsi", new Condition(Operators.EQUALS, itemId.value())), StubWithGlobalSecondaryIndexItem.class); // Then assertTrue(stubWithGlobalSecondaryIndexItemCollection.size() == 1); final ArgumentCaptor<QuerySpec> querySpecCaptor = ArgumentCaptor.forClass(QuerySpec.class); verify(mockIndex).query(querySpecCaptor.capture()); }
@Test public void shouldQueryIndex_withCompoundAttributeQuery() { // Given final ItemId itemId = new ItemId(randomId()); final ItemConfiguration itemConfiguration = new ItemConfiguration(StubWithGlobalSecondaryIndexItem.class, tableName); itemConfiguration.registerIndexes((Arrays.asList(new CompoundIndexDefinition("gsi", "gsiSupportingValue")))); final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration); when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations); final Table mockTable = mock(Table.class); when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable); final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate( mockDatabaseSchemaHolder); dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient); final Index mockIndex = mock(Index.class); when(mockTable.getIndex(anyString())).thenReturn(mockIndex); final ItemCollection<QueryOutcome> mockOutcome = mock(ItemCollection.class); when(mockIndex.query(any(QuerySpec.class))).thenReturn(mockOutcome); final IteratorSupport<Item, QueryOutcome> mockIterator = mock(IteratorSupport.class); final Item mockItem = new Item(); mockItem.withString(randomString(), randomString()); when(mockOutcome.iterator()).thenReturn(mockIterator); when(mockIterator.hasNext()).thenReturn(true, false); when(mockIterator.next()).thenReturn(mockItem); // When final Collection<StubWithGlobalSecondaryIndexItem> stubWithGlobalSecondaryIndexItemCollection = dynamoDocumentStoreTemplate .fetch(new CompoundAttributeQuery("gsi", new Condition(Operators.EQUALS, itemId.value()), "gsiSupportingValue", new Condition(Operators.EQUALS, String.valueOf(randomInt(10)))), StubWithGlobalSecondaryIndexItem.class); // Then assertTrue(stubWithGlobalSecondaryIndexItemCollection.size() == 1); final ArgumentCaptor<QuerySpec> querySpecCaptor = ArgumentCaptor.forClass(QuerySpec.class); verify(mockIndex).query(querySpecCaptor.capture()); assertNotNull(querySpecCaptor.getValue().getRangeKeyCondition()); }
@Test public void shouldQueryIndex_withACompoundAttributeQueryOnACompoundGSIWithAHashValueTheSameAsThePrimaryKeyDefinition() { // Given final ItemId itemId = new ItemId(randomId()); final ItemConfiguration itemConfiguration = new ItemConfiguration(StubWithGlobalSecondaryIndexItem.class, tableName, new PrimaryKeyDefinition("gsi")); itemConfiguration.registerIndexes((Arrays.asList(new CompoundIndexDefinition("gsi", "gsiSupportingValue")))); final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration); when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations); final Table mockTable = mock(Table.class); when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable); final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate( mockDatabaseSchemaHolder); dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient); final Index mockIndex = mock(Index.class); when(mockTable.getIndex(anyString())).thenReturn(mockIndex); final ItemCollection<QueryOutcome> mockOutcome = mock(ItemCollection.class); when(mockIndex.query(any(QuerySpec.class))).thenReturn(mockOutcome); final IteratorSupport<Item, QueryOutcome> mockIterator = mock(IteratorSupport.class); final Item mockItem = new Item(); mockItem.withString(randomString(), randomString()); when(mockOutcome.iterator()).thenReturn(mockIterator); when(mockIterator.hasNext()).thenReturn(true, false); when(mockIterator.next()).thenReturn(mockItem); // When final Collection<StubWithGlobalSecondaryIndexItem> stubWithGlobalSecondaryIndexItemCollection = dynamoDocumentStoreTemplate .fetch(new CompoundAttributeQuery("gsi", new Condition(Operators.EQUALS, itemId.value()), "gsiSupportingValue", new Condition(Operators.EQUALS, String.valueOf(randomInt(10)))), StubWithGlobalSecondaryIndexItem.class); // Then assertTrue(stubWithGlobalSecondaryIndexItemCollection.size() == 1); final ArgumentCaptor<QuerySpec> querySpecCaptor = ArgumentCaptor.forClass(QuerySpec.class); verify(mockIndex).query(querySpecCaptor.capture()); assertNotNull(querySpecCaptor.getValue().getRangeKeyCondition()); }