|
| 1 | +// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | + |
| 15 | +package com.amazonaws.serverless.dao; |
| 16 | + |
| 17 | + |
| 18 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; |
| 19 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression; |
| 20 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression; |
| 21 | +import com.amazonaws.services.dynamodbv2.model.AttributeValue; |
| 22 | +import com.amazonaws.serverless.domain.Event; |
| 23 | +import com.amazonaws.serverless.manager.DynamoDBManager; |
| 24 | +import org.apache.log4j.Logger; |
| 25 | + |
| 26 | +import java.util.*; |
| 27 | + |
| 28 | + |
| 29 | +public class DynamoDBEventDao implements EventDao { |
| 30 | + |
| 31 | + private static final Logger log = Logger.getLogger(DynamoDBEventDao.class); |
| 32 | + |
| 33 | + private static final DynamoDBMapper mapper = DynamoDBManager.mapper(); |
| 34 | + |
| 35 | + private static volatile DynamoDBEventDao instance; |
| 36 | + |
| 37 | + |
| 38 | + private DynamoDBEventDao() { } |
| 39 | + |
| 40 | + public static DynamoDBEventDao instance() { |
| 41 | + |
| 42 | + if (instance == null) { |
| 43 | + synchronized(DynamoDBEventDao.class) { |
| 44 | + if (instance == null) |
| 45 | + instance = new DynamoDBEventDao(); |
| 46 | + } |
| 47 | + } |
| 48 | + return instance; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public List<Event> findAllEvents() { |
| 53 | + return mapper.scan(Event.class, new DynamoDBScanExpression()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public List<Event> findEventsByCity(String city) { |
| 58 | + |
| 59 | + Map<String, AttributeValue> eav = new HashMap<>(); |
| 60 | + eav.put(":v1", new AttributeValue().withS(city)); |
| 61 | + |
| 62 | + DynamoDBQueryExpression<Event> query = new DynamoDBQueryExpression<Event>() |
| 63 | + .withIndexName(Event.CITY_INDEX) |
| 64 | + .withConsistentRead(false) |
| 65 | + .withKeyConditionExpression("city = :v1") |
| 66 | + .withExpressionAttributeValues(eav); |
| 67 | + |
| 68 | + return mapper.query(Event.class, query); |
| 69 | + |
| 70 | + |
| 71 | + // NOTE: without an index, this query would require a full table scan with a filter: |
| 72 | + /* |
| 73 | + DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() |
| 74 | + .withFilterExpression("city = :val1") |
| 75 | + .withExpressionAttributeValues(eav); |
| 76 | +
|
| 77 | + return mapper.scan(Event.class, scanExpression); |
| 78 | + */ |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public List<Event> findEventsByTeam(String team) { |
| 84 | + |
| 85 | + DynamoDBQueryExpression<Event> homeQuery = new DynamoDBQueryExpression<>(); |
| 86 | + Event eventKey = new Event(); |
| 87 | + eventKey.setHomeTeam(team); |
| 88 | + homeQuery.setHashKeyValues(eventKey); |
| 89 | + List<Event> homeEvents = mapper.query(Event.class, homeQuery); |
| 90 | + |
| 91 | + Map<String, AttributeValue> eav = new HashMap<>(); |
| 92 | + eav.put(":v1", new AttributeValue().withS(team)); |
| 93 | + DynamoDBQueryExpression<Event> awayQuery = new DynamoDBQueryExpression<Event>() |
| 94 | + .withIndexName(Event.AWAY_TEAM_INDEX) |
| 95 | + .withConsistentRead(false) |
| 96 | + .withKeyConditionExpression("awayTeam = :v1") |
| 97 | + .withExpressionAttributeValues(eav); |
| 98 | + |
| 99 | + List<Event> awayEvents = mapper.query(Event.class, awayQuery); |
| 100 | + |
| 101 | + // need to create a new list because PaginatedList from query is immutable |
| 102 | + List<Event> allEvents = new LinkedList<>(); |
| 103 | + allEvents.addAll(homeEvents); |
| 104 | + allEvents.addAll(awayEvents); |
| 105 | + allEvents.sort( (e1, e2) -> e1.getEventDate() <= e2.getEventDate() ? -1 : 1 ); |
| 106 | + |
| 107 | + return allEvents; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Optional<Event> findEventByTeamAndDate(String team, Long eventDate) { |
| 112 | + |
| 113 | + Event event = mapper.load(Event.class, team, eventDate); |
| 114 | + |
| 115 | + return Optional.ofNullable(event); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void saveOrUpdateEvent(Event event) { |
| 120 | + |
| 121 | + mapper.save(event); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void deleteEvent(String team, Long eventDate) { |
| 126 | + |
| 127 | + Optional<Event> oEvent = findEventByTeamAndDate(team, eventDate); |
| 128 | + if (oEvent.isPresent()) { |
| 129 | + mapper.delete(oEvent.get()); |
| 130 | + } |
| 131 | + else { |
| 132 | + log.error("Could not delete event, no such team and date combination"); |
| 133 | + throw new IllegalArgumentException("Delete failed for nonexistent event"); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments