Search in sources :

Example 41 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project thunder by RohanNagar.

the class UsersDaoTest method testSuccessfulDelete.

@Test
public void testSuccessfulDelete() {
    when(table.getItem(anyString(), anyString())).thenReturn(item);
    PilotUser result = usersDao.delete("email");
    verify(table, times(1)).getItem(anyString(), anyString());
    verify(table, times(1)).deleteItem(any(DeleteItemSpec.class));
    assertEquals(user, result);
}
Also used : DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) PilotUser(com.sanction.thunder.models.PilotUser) Test(org.junit.Test)

Example 42 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project thunder by RohanNagar.

the class UsersDaoTest method testSuccessfulUpdate.

@Test
public void testSuccessfulUpdate() {
    when(table.getItem(anyString(), anyString())).thenReturn(item);
    PilotUser result = usersDao.update(null, user);
    verify(table, times(1)).getItem(anyString(), anyString());
    verify(table, times(1)).putItem(any(Item.class), any(Expected.class));
    assertEquals(user, result);
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Expected(com.amazonaws.services.dynamodbv2.document.Expected) PilotUser(com.sanction.thunder.models.PilotUser) Test(org.junit.Test)

Example 43 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project thunder by RohanNagar.

the class UsersDao method delete.

/**
 * Delete a PilotUser in the data store.
 *
 * @param email The email of the user to delete.
 * @return The PilotUser object that was deleted or {@code null} if the delete failed.
 * @throws DatabaseException If the user is not found or if the database is down.
 */
public PilotUser delete(String email) {
    checkNotNull(email);
    // Get the item that will be deleted to return it
    Item item;
    try {
        item = table.getItem("email", email);
    } catch (AmazonClientException e) {
        LOG.error("The database is currently unresponsive.", e);
        throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN);
    }
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("email", email).withExpected(new Expected("email").exists());
    try {
        table.deleteItem(deleteItemSpec);
    } catch (ConditionalCheckFailedException e) {
        LOG.warn("The email {} was not found in the database.", email, e);
        throw new DatabaseException("The user to delete was not found.", DatabaseError.USER_NOT_FOUND);
    } catch (AmazonClientException e) {
        LOG.error("The database is currently unresponsive.", e);
        throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN);
    }
    return fromJson(mapper, item.getJSON("document"));
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) Expected(com.amazonaws.services.dynamodbv2.document.Expected) AmazonClientException(com.amazonaws.AmazonClientException) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 44 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project thunder by RohanNagar.

the class UsersDao method findByEmail.

/**
 * Find a PilotUser from the data store.
 *
 * @param email The email of the user to find.
 * @return The requested PilotUser or {@code null} if it does not exist.
 * @throws DatabaseException If the user does not exist or if the database is down.
 */
public PilotUser findByEmail(String email) {
    checkNotNull(email);
    Item item;
    try {
        item = table.getItem("email", email);
    } catch (AmazonClientException e) {
        LOG.error("The database is currently unresponsive.", e);
        throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN);
    }
    if (item == null) {
        LOG.warn("The email {} was not found in the database.", email);
        throw new DatabaseException("The user was not found.", DatabaseError.USER_NOT_FOUND);
    }
    return fromJson(mapper, item.getJSON("document"));
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) AmazonClientException(com.amazonaws.AmazonClientException)

Example 45 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project thunder by RohanNagar.

the class UsersDao method update.

/**
 * Update a PilotUser in the data store.
 *
 * @param existingEmail The existing email of the user.
 *                     This must not be {@code null} if the email is to be changed.
 * @param user The user object to update. Must have the same email as the one to update.
 * @return The PilotUser object that was updated or {@code null} if the updated failed.
 * @throws DatabaseException If the user is not found, the database is down, or the update fails.
 */
public PilotUser update(@Nullable String existingEmail, PilotUser user) {
    checkNotNull(user);
    // Different emails means we need to delete and insert
    if (existingEmail != null && !existingEmail.equals(user.getEmail().getAddress())) {
        LOG.info("User to update has new email. The user will be deleted and then reinserted.");
        delete(existingEmail);
        return insert(user);
    }
    // Compute the new data
    long now = Instant.now().toEpochMilli();
    String newVersion = UUID.randomUUID().toString();
    String document = toJson(mapper, user);
    // Get the old version
    Item item;
    try {
        item = table.getItem("email", user.getEmail().getAddress());
    } catch (AmazonClientException e) {
        LOG.error("The database is currently unresponsive.", e);
        throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN);
    }
    if (item == null) {
        LOG.warn("The user {} was not found in the database.", user.getEmail().getAddress());
        throw new DatabaseException("The user was not found.", DatabaseError.USER_NOT_FOUND);
    }
    String oldVersion = item.getString("version");
    Item newItem = item.withString("version", newVersion).withLong("update_time", now).withJSON("document", document);
    try {
        table.putItem(newItem, new Expected("version").eq(oldVersion));
    } catch (ConditionalCheckFailedException e) {
        LOG.error("The user was updated while this update was in progress." + " Aborting to avoid race condition.", e);
        throw new DatabaseException("The user to update is at an unexpected stage.", DatabaseError.CONFLICT);
    } catch (AmazonServiceException e) {
        LOG.error("The database rejected the update request.", e);
        throw new DatabaseException("The database rejected the update request.", DatabaseError.REQUEST_REJECTED);
    } catch (AmazonClientException e) {
        LOG.error("The database is currently unresponsive.", e);
        throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN);
    }
    return user;
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Expected(com.amazonaws.services.dynamodbv2.document.Expected) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)20 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)17 AmazonClientException (com.amazonaws.AmazonClientException)13 AmazonServiceException (com.amazonaws.AmazonServiceException)13 HashMap (java.util.HashMap)13 Test (org.junit.Test)12 Item (com.amazonaws.services.dynamodbv2.document.Item)11 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)5 Expected (com.amazonaws.services.dynamodbv2.document.Expected)5 AttributeValueUpdate (com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate)5 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)4 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)4 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)4 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)4 List (java.util.List)4 ToString (lombok.ToString)4 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)3 BatchGetItemOutcome (com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome)3