private void populateScanExpression(List<Expression<?>> expressions, Condition condition, DynamoDBScanExpression scanExpression) { List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>(); String attributeName = null; for (Expression<?> expression : expressions) { Object result = expression.accept(this, scanExpression); if (result instanceof String) { if (attributeName != null) { throw new RuntimeException("Already in use"); } attributeName = (String) result; } else if (result instanceof AttributeValue) { attributeValueList.add((AttributeValue) result); } else { throw new UnsupportedOperationException("Invalid result: " + result); } } condition.setAttributeValueList(attributeValueList); scanExpression.addFilterCondition(attributeName, condition); }
public static void fillTable() throws UnknownHostException { ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(); provisionedThroughput.setReadCapacityUnits(1L); provisionedThroughput.setWriteCapacityUnits(1L); if (!client.listTables().getTableNames().contains("User")) { CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName("User") .withKeySchema( new KeySchemaElement().withAttributeName("id") .withKeyType(KeyType.HASH)) .withProvisionedThroughput(provisionedThroughput) .withAttributeDefinitions( new AttributeDefinition().withAttributeName("id").withAttributeType( ScalarAttributeType.S)); client.createTable(createTableRequest); } else { DynamoDBScanExpression scan = new DynamoDBScanExpression(); PaginatedScanList<User> users = mapper.scan(User.class, scan); for (User user : users) { Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("id", new AttributeValue().withS(user.getId())); client.deleteItem(new DeleteItemRequest("User", key)); } } u1 = addUser("Jaakko", "Jantunen", 20, Gender.MALE, null); u2 = addUser("Jaakki", "Jantunen", 30, Gender.FEMALE, "One detail"); u3 = addUser("Jaana", "Aakkonen", 40, Gender.MALE, "No details"); u4 = addUser("Jaana", "BeekkoNen", 50, Gender.FEMALE, null); }
private static void FindBooksPricedLessThanSpecifiedValue( DynamoDBMapper mapper, String value) throws Exception { System.out.println("FindBooksPricedLessThanSpecifiedValue: Scan ProductCatalog."); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("Price", new Condition() .withComparisonOperator(ComparisonOperator.LT) .withAttributeValueList(new AttributeValue().withN(value))); scanExpression.addFilterCondition("ProductCategory", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("Book"))); List<Book> scanResult = mapper.scan(Book.class, scanExpression); for (Book book : scanResult) { System.out.println(book); } }
private static void FindBicyclesOfSpecificTypeWithMultipleThreads( DynamoDBMapper mapper, int numberOfThreads, String bicycleType) throws Exception { System.out.println("FindBicyclesOfSpecificTypeWithMultipleThreads: Scan ProductCatalog With Multiple Threads."); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("ProductCategory", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("Bicycle"))); scanExpression.addFilterCondition("BicycleType", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(bicycleType))); List<Bicycle> scanResult = mapper.parallelScan(Bicycle.class, scanExpression, numberOfThreads); for (Bicycle bicycle : scanResult) { System.out.println(bicycle); } }
public DynamoDBScanExpression buildScanExpression() { if (sort != null) { throw new UnsupportedOperationException("Sort not supported for scan expressions"); } DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); if (isHashKeySpecified()) { scanExpression.addFilterCondition( getHashKeyAttributeName(), createSingleValueCondition(getHashKeyPropertyName(), ComparisonOperator.EQ, getHashKeyAttributeValue(), getHashKeyAttributeValue().getClass(), true)); } for (Map.Entry<String, List<Condition>> conditionEntry : attributeConditions.entrySet()) { for (Condition condition : conditionEntry.getValue()) { scanExpression.addFilterCondition(conditionEntry.getKey(), condition); } } return scanExpression; }
public DynamoDBScanExpression buildScanExpression() { if (sort != null) { throw new UnsupportedOperationException("Sort not supported for scan expressions"); } DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); if (isHashKeySpecified()) { scanExpression.addFilterCondition( getHashKeyAttributeName(), createSingleValueCondition(getHashKeyPropertyName(), ComparisonOperator.EQ, getHashKeyAttributeValue(), getHashKeyAttributeValue().getClass(), true)); } if (isRangeKeySpecified()) { scanExpression.addFilterCondition( getRangeKeyAttributeName(), createSingleValueCondition(getRangeKeyPropertyName(), ComparisonOperator.EQ, getRangeKeyAttributeValue(), getRangeKeyAttributeValue().getClass(), true)); } for (Map.Entry<String, List<Condition>> conditionEntry : attributeConditions.entrySet()) { for (Condition condition : conditionEntry.getValue()) { scanExpression.addFilterCondition(conditionEntry.getKey(), condition); } } return scanExpression; }
@SuppressWarnings("unchecked") @Test(expected=IllegalArgumentException.class) public void testExecute_WhenFinderMethodIsCountingEntityWithCompositeIdList_WhenFindingByRangeKeyOnly_ScanCountDisabled() { setupCommonMocksForThisRepositoryMethod(mockPlaylistEntityMetadata, mockDynamoDBPlaylistQueryMethod, Playlist.class, "countByPlaylistName", 1, "userName", "playlistName"); Mockito.when(mockDynamoDBPlaylistQueryMethod.isCollectionQuery()).thenReturn(false); Mockito.when(mockDynamoDBPlaylistQueryMethod.isScanCountEnabled()).thenReturn(false); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); @SuppressWarnings("rawtypes") ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockDynamoDBOperations.count(classCaptor.capture(), scanCaptor.capture())).thenReturn( 100); // Execute the query Object[] parameters = new Object[] { "somePlaylistName" }; partTreeDynamoDBQuery.execute(parameters); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expected = UnsupportedOperationException.class) // Not yet supported public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleStringParameterIgnoringCase_WhenNotFindingByHashKey() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByNameIgnoringCase", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { "someName" }; partTreeDynamoDBQuery.execute(parameters); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expected = UnsupportedOperationException.class) // Not yet supported public void testExecute_WhenFinderMethodIsFindingEntityList_WithSingleStringParameter_WithSort_WhenNotFindingByHashKey() { setupCommonMocksForThisRepositoryMethod(mockUserEntityMetadata, mockDynamoDBUserQueryMethod, User.class, "findByNameOrderByNameAsc", 1, "id", null); Mockito.when(mockDynamoDBUserQueryMethod.isCollectionQuery()).thenReturn(true); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockUserScanResults.get(0)).thenReturn(mockUser); Mockito.when(mockUserScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockUserScanResults); // Execute the query Object[] parameters = new Object[] { "someName" }; partTreeDynamoDBQuery.execute(parameters); }
@Override public List<User> getUserByName(String firstName, String lastName) { String attrValueFirstName = ":v_first_name"; String attrValueLastName = ":v_last_name"; String filterExpression = String.format("%s=%s and %s=%s", Attr.FirstName, attrValueFirstName, Attr.LastName, attrValueLastName); Map<String, AttributeValue> expressionValueMap = new HashMap<>(); expressionValueMap.put(attrValueFirstName, new AttributeValue().withS(firstName)); expressionValueMap.put(attrValueLastName, new AttributeValue().withS(lastName)); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression(filterExpression) .withExpressionAttributeValues(expressionValueMap); return dbMapper.scan(User.class, scanExpression); }
private DynamoDBPlanoRequest findNextRequest() throws PlanoException { AttributeValue currentTime = DateToStringMarshaller.instance().marshall(new Date()); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("ExecutionTime < :currentTime and LockExpireTime < :currentTime") .addExpressionAttributeValuesEntry(":currentTime", currentTime) .withConsistentRead(true) .withLimit(1); List<DynamoDBPlanoRequest> dynamoDBPlanoRequests = dynamoDBMapper.scan(DynamoDBPlanoRequest.class, scanExpression); if (dynamoDBPlanoRequests == null || dynamoDBPlanoRequests.isEmpty()) { throw new PlanoException("Can not find next PlanoRequest to execute."); } DynamoDBPlanoRequest dynamoDBPlanoRequest = dynamoDBPlanoRequests.get(0); return dynamoDBPlanoRequest; }
public void sessionsCleanUpOldSessions() { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("lastHit", new Condition() .withComparisonOperator(ComparisonOperator.LT.toString()) .withAttributeValueList(new AttributeValue().withN(""+(System.currentTimeMillis()-(Session.TTL_SESSIONS_SECONDS * 1000)))) ); PaginatedScanList<Session> result = mapper.scan(Session.class, scanExpression); mapper.batchDelete(result); }
public PaginatedScanList<Account> getAllDonatedAccounts() { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("accountType",new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue[] { new AttributeValue().withS(Account.ACCOUNT_DONATE) })); PaginatedScanList<Account> accounts = mapper.scan(Account.class,scanExpression); return accounts; }
public PaginatedScanList<Account> getAllFriendsAccounts() { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("accountType",new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue[] { new AttributeValue().withS(Account.ACCOUNT_FRIENDS_OF_DUCK) })); PaginatedScanList<Account> accounts = mapper.scan(Account.class,scanExpression); return accounts; }
public PaginatedScanList<Account> getAllRedditAccounts() { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("email",new Condition() .withComparisonOperator(ComparisonOperator.CONTAINS.toString()) .withAttributeValueList(new AttributeValue[] { new AttributeValue().withS("@reddit") })); PaginatedScanList<Account> accounts = mapper.scan(Account.class,scanExpression); return accounts; }
public PaginatedScanList<Account> getAllFacebookAccounts() { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); scanExpression.addFilterCondition("email",new Condition() .withComparisonOperator(ComparisonOperator.CONTAINS.toString()) .withAttributeValueList(new AttributeValue[] { new AttributeValue().withS("#facebook") })); PaginatedScanList<Account> accounts = mapper.scan(Account.class,scanExpression); return accounts; }
private boolean adminExist() { DynamoDBScanExpression expression = new DynamoDBScanExpression() .withFilterConditionEntry("role", new Condition().withComparisonOperator(EQ.toString()).withAttributeValueList(new AttributeValue("admin"))); List<User> users = mapper.scan(User.class, expression); return !users.isEmpty(); }
/** * Find all rows in table given the entity object * * @return a list of entities found * @throws DataAccessException */ public List<T> findAll() throws DataAccessException { DynamoDBScanExpression dynamoDBScanExpression = new DynamoDBScanExpression(); DynamoDBMapperConfig config = new DynamoDBMapperConfig(DynamoDBMapperConfig.PaginationLoadingStrategy.EAGER_LOADING); PaginatedScanList<T> paginatedScanList = dynamoDBMapper.scan(getType(), dynamoDBScanExpression, config); paginatedScanList.loadAllResults(); List<T> list = new ArrayList<T>(paginatedScanList.size()); Iterator<T> iterator = paginatedScanList.iterator(); while (iterator.hasNext()) { T element = iterator.next(); list.add(element); } return list; }
@Override public Object visit(Path<?> expr, DynamoDBScanExpression scanExpression) { AnnotatedElement element = expr.getAnnotatedElement(); Annotation[] annotations = element.getAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof DynamoDBRangeKey) { return expr.getMetadata().getElement(); } } return expr.getMetadata().getElement(); }
private DynamoDBScanExpression createQuery(@Nullable QueryMetadata queryMetadata) { if (queryMetadata.getWhere() != null) { return serializer.handle(queryMetadata.getWhere()); } else { return new DynamoDBScanExpression(); } }
/** * Executes a query after building a DynamoDB specific query based on the * received one */ @Override public Result<K, T> execute(Query<K, T> query) { DynamoDBQuery<K, T> dynamoDBQuery = buildDynamoDBQuery(query); DynamoDBMapper mapper = new DynamoDBMapper( dynamoDBStoreHandler.getDynamoDbClient()); List<T> objList = null; if (DynamoDBQuery.getType().equals(DynamoDBQuery.RANGE_QUERY)) objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression()); if (DynamoDBQuery.getType().equals(DynamoDBQuery.SCAN_QUERY)) objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression()); return new DynamoDBResult<K, T>(this, query, objList); }
/** * Delete all items in the table before running the next test. Faster than creating a new table * for every test. */ @After public void baseTearDown() { List<FailedBatch> failedBatches = dynamoMapper .batchDelete(dynamoMapper.scan(DynamoSessionItem.class, new DynamoDBScanExpression())); // If we for some reason couldn't delete all items bail out so we don't affect other tests assertThat(failedBatches, empty()); }
@Override public Page<T> findAll(Pageable pageable) { if (pageable.getSort() != null) { throw new UnsupportedOperationException("Sorting not supported for find all scan operations"); } DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); // Scan to the end of the page after the requested page int scanTo = pageable.getOffset() + (2 * pageable.getPageSize()); scanExpression.setLimit(scanTo); PaginatedScanList<T> paginatedScanList = dynamoDBOperations.scan(domainType, scanExpression); Iterator<T> iterator = paginatedScanList.iterator(); int processedCount = 0; if (pageable.getOffset() > 0) { processedCount = scanThroughResults(iterator, pageable.getOffset()); if (processedCount < pageable.getOffset()) return new PageImpl<T>(new ArrayList<T>()); } // Scan ahead to retrieve the next page count List<T> results = readPageOfResults(iterator, pageable.getPageSize()); assertScanEnabled(enableScanPermissions.isFindAllPaginatedScanEnabled(), "findAll(Pageable pageable)"); assertScanCountEnabled(enableScanPermissions.isFindAllUnpaginatedScanCountEnabled(), "findAll(Pageable pageable)"); int totalCount = dynamoDBOperations.count(domainType, scanExpression); return new PageImpl<T>(results, pageable, totalCount); }
@Override public List<T> findAll() { assertScanEnabled( enableScanPermissions.isFindAllUnpaginatedScanEnabled(), "findAll"); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); return dynamoDBOperations.scan(domainType, scanExpression); }
@Override public long count() { assertScanEnabled( enableScanPermissions.isCountUnpaginatedScanEnabled(), "count"); final DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); return dynamoDBOperations.count(domainType, scanExpression); }
@Override public <T> PaginatedScanList<T> scan(Class<T> domainClass, DynamoDBScanExpression scanExpression) { PaginatedScanList<T> results = dynamoDBMapper.scan(domainClass, scanExpression); maybeEmitEvent(new AfterScanEvent<T>(results)); return results; }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expected=UnsupportedOperationException.class) public void testExecute_WhenFinderMethodIsFindingEntityWithCompositeIdList_WhenFindingByNotCompositeId() { PlaylistId playlistId = new PlaylistId(); playlistId.setUserName("someUserName"); playlistId.setPlaylistName("somePlaylistName"); setupCommonMocksForThisRepositoryMethod(mockPlaylistEntityMetadata, mockDynamoDBPlaylistQueryMethod, Playlist.class, "findByPlaylistIdNot", 1, "userName", "playlistName"); Mockito.when(mockDynamoDBPlaylistQueryMethod.isCollectionQuery()).thenReturn(true); Mockito.when(mockPlaylistEntityMetadata.getHashKey(playlistId)).thenReturn("someUserName"); Mockito.when(mockPlaylistEntityMetadata.getRangeKey(playlistId)).thenReturn("somePlaylistName"); // Mock out specific DynamoDBOperations behavior expected by this method ArgumentCaptor<DynamoDBScanExpression> scanCaptor = ArgumentCaptor.forClass(DynamoDBScanExpression.class); ArgumentCaptor<Class> classCaptor = ArgumentCaptor.forClass(Class.class); Mockito.when(mockPlaylistScanResults.get(0)).thenReturn(mockPlaylist); Mockito.when(mockPlaylistScanResults.size()).thenReturn(1); Mockito.when(mockDynamoDBOperations.scan(classCaptor.capture(), scanCaptor.capture())).thenReturn( mockPlaylistScanResults); // Execute the query Object[] parameters = new Object[] { playlistId }; partTreeDynamoDBQuery.execute(parameters); }
protected <T> List<T> scan(Class<T> type, String filterExpression, Map<String, AttributeValue> expressionValueMap) { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression(filterExpression) .withExpressionAttributeValues(expressionValueMap); return dbMapper.scan(type, scanExpression); }
@Override public List<Order> getAllOrders() { return dbMapper.scan(Order.class, new DynamoDBScanExpression()); }
@Override public List<ShoppingCart> getAllShoppingCarts() { return dbMapper.scan(ShoppingCart.class, new DynamoDBScanExpression()); }
@Override public List<Product> getAllProducts() { return dbMapper.scan(Product.class, new DynamoDBScanExpression()); }
@Override public List<User> getAllUsers() { return dbMapper.scan(User.class, new DynamoDBScanExpression()); }
public List<Recipient> getRecipients() { return mapper.scan(Recipient.class, new DynamoDBScanExpression()); }
@Override public List<Event> findAllEvents() { return mapper.scan(Event.class, new DynamoDBScanExpression()); }
public PaginatedScanList<Account> getAllAccounts() { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); PaginatedScanList<Account> accounts = mapper.scan(Account.class,scanExpression); return accounts; }
public PaginatedScanList<Domain> getAllDomains() { DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); PaginatedScanList<Domain> domains = mapper.scan(Domain.class,scanExpression); return domains; }
private void storeTable(Class tableClass, Path tempDirectory) throws IOException { LOG.info("Backup DB table: {}", tableClass.getSimpleName()); File dest = Paths.get(tempDirectory.toString(), tableClass.getName()).toFile(); List result = dynamoDBMapper.scan(tableClass, new DynamoDBScanExpression()); objectMapper.writeValue(dest, result); }
@Override public Object visit(FactoryExpression<?> expr, DynamoDBScanExpression scanExpression) { throw new UnsupportedOperationException(); }
@Override public Object visit(Operation<?> expr, DynamoDBScanExpression scanExpression) { Operator op = expr.getOperator(); Condition condition = new Condition(); if (op == Ops.EQ) { condition.setComparisonOperator(ComparisonOperator.EQ); } else if (op == Ops.STARTS_WITH) { condition.setComparisonOperator(ComparisonOperator.BEGINS_WITH); } else if (op == Ops.BETWEEN) { condition.setComparisonOperator(ComparisonOperator.BETWEEN); } else if (op == Ops.STRING_CONTAINS) { condition.setComparisonOperator(ComparisonOperator.CONTAINS); } else if (op == Ops.GOE) { condition.setComparisonOperator(ComparisonOperator.GE); } else if (op == Ops.GT) { condition.setComparisonOperator(ComparisonOperator.GT); } else if (op == Ops.LT) { condition.setComparisonOperator(ComparisonOperator.LT); } else if (op == Ops.LOE) { condition.setComparisonOperator(ComparisonOperator.LE); } else if (op == Ops.IN) { condition.setComparisonOperator(ComparisonOperator.IN); } else if (op == Ops.NE) { condition.setComparisonOperator(ComparisonOperator.NE); } else if (op == Ops.IS_NULL) { condition.setComparisonOperator(ComparisonOperator.NULL); } else if (op == Ops.IS_NOT_NULL) { condition.setComparisonOperator(ComparisonOperator.NOT_NULL); } else if (op == Ops.NOT) { Expression<?> arg = expr.getArg(0); if (arg instanceof BooleanOperation) { Operator subOp = ((BooleanOperation) arg).getOperator(); if (subOp == Ops.STRING_CONTAINS) { condition.setComparisonOperator(ComparisonOperator.NOT_CONTAINS); expr = (Operation<?>) arg; } else { throw new UnsupportedOperationException(String.valueOf(op)); } } else { throw new UnsupportedOperationException(String.valueOf(op)); } } else if (op == Ops.AND) { List<Expression<?>> args = expr.getArgs(); for (Expression<?> expression : args) { expression.accept(this, scanExpression); } return null; } else { throw new UnsupportedOperationException(String.valueOf(op)); } populateScanExpression(expr.getArgs(), condition, scanExpression); return condition; }