com.amazonaws.services.dynamodbv2.model.UpdateItemRequest Java Examples
The following examples show how to use
com.amazonaws.services.dynamodbv2.model.UpdateItemRequest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void updateItemAllNewOverwrite() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(item0); item1.put("asdf", new AttributeValue("wef")); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("wef"))); Map<String, AttributeValue> result1 = t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key0) .withAttributeUpdates(updates) .withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true); assertEquals(result1, item1); t1.commit(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key0, item1, true); }
Example #2
Source File: LowLevelItemCRUDExample.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
private static void updateAddNewAttribute() { try { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Id", new AttributeValue().withN("121")); Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>(); expressionAttributeValues.put(":val1", new AttributeValue().withS("Some value")); ReturnValue returnValues = ReturnValue.ALL_NEW; UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(key) .withUpdateExpression("set NewAttribute = :val1") .withExpressionAttributeValues(expressionAttributeValues).withReturnValues(returnValues); UpdateItemResult result = client.updateItem(updateItemRequest); // Check the response. System.out.println("Printing item after adding new attribute..."); printItem(result.getAttributes()); } catch (AmazonServiceException ase) { System.err.println("Failed to add new attribute in " + tableName); System.err.println(ase.getMessage()); } }
Example #3
Source File: RequestTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void roundTripUpdateAll() { UpdateItem r1 = new UpdateItem(); Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(HASH_ATTR_NAME, new AttributeValue("a")); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("attr_ss", new AttributeValueUpdate().withAction("PUT").withValue(new AttributeValue().withSS("a", "b"))); updates.put("attr_n", new AttributeValueUpdate().withAction("PUT").withValue(new AttributeValue().withN("1"))); updates.put("attr_ns", new AttributeValueUpdate().withAction("PUT").withValue(new AttributeValue().withNS("1", "2"))); updates.put("attr_b", new AttributeValueUpdate().withAction("PUT").withValue(new AttributeValue().withB(ByteBuffer.wrap(new String("asdf").getBytes())))); updates.put("attr_bs", new AttributeValueUpdate().withAction("PUT").withValue(new AttributeValue().withBS(ByteBuffer.wrap(new String("asdf").getBytes()), ByteBuffer.wrap(new String("asdf").getBytes())))); r1.setRequest(new UpdateItemRequest() .withTableName(TABLE_NAME) .withKey(key) .withAttributeUpdates(updates)); byte[] r1Bytes = Request.serialize("123", r1).array(); Request r2 = Request.deserialize("123", ByteBuffer.wrap(r1Bytes)); byte[] r2Bytes = Request.serialize("123", r2).array(); assertArrayEquals(r1Bytes, r2Bytes); }
Example #4
Source File: GenericDynamoDBTest.java From strongbox with Apache License 2.0 | 6 votes |
@Test public void testUpdateEntryDoesNotExist() throws Exception { RawSecretEntry rawSecretEntry = constructRawEntry(SECRET_NAME); RawSecretEntry alternativeRawSecretEntry = constructAlternativeRawSecretEntry(SECRET_NAME); UpdateItemRequest expectedUpdateRequest = constructUpdateItemRequest(rawSecretEntry, true, Optional.of(alternativeRawSecretEntry)); when(mockDynamoDBClient.updateItem(expectedUpdateRequest)).thenThrow( new ConditionalCheckFailedException("")); boolean exceptionThrown = false; try { dynamoDB.update(rawSecretEntry, alternativeRawSecretEntry); } catch (DoesNotExistException e) { assertEquals(e.getMessage(), "Precondition to update entry in DynamoDB failed:{1={S: secret1,}, 2={N: 1,}}"); exceptionThrown = true; } assertTrue(exceptionThrown); // Check all the expected calls to AWS were made. verify(mockDynamoDBClient, times(1)).updateItem(expectedUpdateRequest); }
Example #5
Source File: RequestTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void roundTripUpdateAllJSON() { UpdateItem r1 = new UpdateItem(); Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(HASH_ATTR_NAME, new AttributeValue("a")); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("attr_m", new AttributeValueUpdate().withAction("PUT").withValue(new AttributeValue().withM(JSON_M_ATTR_VAL))); r1.setRequest(new UpdateItemRequest() .withTableName(TABLE_NAME) .withKey(key) .withAttributeUpdates(updates)); byte[] r1Bytes = Request.serialize("123", r1).array(); Request r2 = Request.deserialize("123", ByteBuffer.wrap(r1Bytes)); byte[] r2Bytes = Request.serialize("123", r2).array(); assertArrayEquals(r1Bytes, r2Bytes); }
Example #6
Source File: DynamoDbDelegate.java From dynamodb-janusgraph-storage-backend with Apache License 2.0 | 6 votes |
UpdateItemResult updateItem(final UpdateItemRequest request) throws BackendException { setUserAgent(request); UpdateItemResult result; final int bytes; if (request.getUpdateExpression() != null) { bytes = calculateExpressionBasedUpdateSize(request); } else { bytes = calculateItemUpdateSizeInBytes(request.getAttributeUpdates()); } getBytesHistogram(UPDATE_ITEM, request.getTableName()).update(bytes); final int wcu = computeWcu(bytes); timedWriteThrottle(UPDATE_ITEM, request.getTableName(), wcu); final Timer.Context apiTimerContext = getTimerContext(UPDATE_ITEM, request.getTableName()); try { result = client.updateItem(request); } catch (Exception e) { throw processDynamoDbApiException(e, UPDATE_ITEM, request.getTableName()); } finally { apiTimerContext.stop(); } meterConsumedCapacity(UPDATE_ITEM, result.getConsumedCapacity()); return result; }
Example #7
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void missingTableName() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME); try { t1.updateItem(new UpdateItemRequest() .withKey(key1)); fail(); } catch (InvalidRequestException e) { assertTrue(e.getMessage(), e.getMessage().contains("TableName must not be null")); } assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, false); t1.rollback(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, false); t1.delete(Long.MAX_VALUE); }
Example #8
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void updateItemAllNewInsert() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(key1); item1.put("asdf", new AttributeValue("wef")); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("wef"))); Map<String, AttributeValue> result1 = t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key1) .withAttributeUpdates(updates) .withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true); assertEquals(result1, item1); t1.commit(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, item1, true); }
Example #9
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void updateItemAllOldOverwrite() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(item0); item1.put("asdf", new AttributeValue("wef")); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("wef"))); Map<String, AttributeValue> result1 = t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key0) .withAttributeUpdates(updates) .withReturnValues(ReturnValue.ALL_OLD)).getAttributes(); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true); assertEquals(result1, item0); t1.commit(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key0, item1, true); }
Example #10
Source File: StreamsAdapterDemoHelper.java From aws-dynamodb-examples with Apache License 2.0 | 6 votes |
public static void updateItem(AmazonDynamoDBClient client, String tableName, String id, String val) { java.util.Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Id", new AttributeValue().withN(id)); Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>(); AttributeValueUpdate update = new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withS(val)); attributeUpdates.put("attribute-2", update); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withTableName(tableName) .withKey(key) .withAttributeUpdates(attributeUpdates); client.updateItem(updateItemRequest); }
Example #11
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void updateItemAllOldInsert() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(key1); item1.put("asdf", new AttributeValue("wef")); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("wef"))); Map<String, AttributeValue> result1 = t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key1) .withAttributeUpdates(updates) .withReturnValues(ReturnValue.ALL_OLD)).getAttributes(); assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true); assertNull(result1); t1.commit(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, item1, true); }
Example #12
Source File: DynamoDBManager.java From dynamodb-geo with Apache License 2.0 | 6 votes |
public UpdatePointResult updatePoint(UpdatePointRequest updatePointRequest) { long geohash = S2Manager.generateGeohash(updatePointRequest.getGeoPoint()); long hashKey = S2Manager.generateHashKey(geohash, config.getHashKeyLength()); UpdateItemRequest updateItemRequest = updatePointRequest.getUpdateItemRequest(); updateItemRequest.setTableName(config.getTableName()); AttributeValue hashKeyValue = new AttributeValue().withN(String.valueOf(hashKey)); updateItemRequest.getKey().put(config.getHashKeyAttributeName(), hashKeyValue); updateItemRequest.getKey().put(config.getRangeKeyAttributeName(), updatePointRequest.getRangeKeyValue()); // Geohash and geoJson cannot be updated. updateItemRequest.getAttributeUpdates().remove(config.getGeohashAttributeName()); updateItemRequest.getAttributeUpdates().remove(config.getGeoJsonAttributeName()); UpdateItemResult updateItemResult = config.getDynamoDBClient().updateItem(updateItemRequest); UpdatePointResult updatePointResult = new UpdatePointResult(updateItemResult); return updatePointResult; }
Example #13
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void getThenUpdateExistingItem() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> item0a = new HashMap<String, AttributeValue>(item0); item0a.put("wef", new AttributeValue("new attr")); Map<String, AttributeValueUpdate> updates1 = new HashMap<String, AttributeValueUpdate>(); updates1.put("wef", new AttributeValueUpdate(new AttributeValue("new attr"), AttributeAction.PUT)); Map<String, AttributeValue> getResult = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem(); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false); assertEquals(item0, getResult); Map<String, AttributeValue> updateResult = t1.updateItem(new UpdateItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0) .withAttributeUpdates(updates1).withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0a, t1.getId(), false, true); assertEquals(item0a, updateResult); t1.commit(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key0, item0a, true); }
Example #14
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 6 votes |
@Test public void getThenUpdateNewItem() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(key1); item1.put("asdf", new AttributeValue("didn't exist")); Map<String, AttributeValueUpdate> updates1 = new HashMap<String, AttributeValueUpdate>(); updates1.put("asdf", new AttributeValueUpdate(new AttributeValue("didn't exist"), AttributeAction.PUT)); Map<String, AttributeValue> getResult = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key1)).getItem(); assertItemLocked(INTEG_HASH_TABLE_NAME, key1, t1.getId(), true, false); assertNull(getResult); Map<String, AttributeValue> updateResult = t1.updateItem(new UpdateItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key1) .withAttributeUpdates(updates1).withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true); assertEquals(item1, updateResult); t1.commit(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, item1, true); }
Example #15
Source File: TransactionItem.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
/** * Marks the transaction item as either COMMITTED or ROLLED_BACK, but only if it was in the PENDING state. * It will also condition on the expected version. * * @param targetState * @param expectedVersion * @throws ConditionalCheckFailedException if the transaction doesn't exist, isn't PENDING, is finalized, * or the expected version doesn't match (if specified) */ public void finish(final State targetState, final int expectedVersion) throws ConditionalCheckFailedException { txAssert(State.COMMITTED.equals(targetState) || State.ROLLED_BACK.equals(targetState),"Illegal state in finish(): " + targetState, "txItem", txItem); Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(2); expected.put(AttributeName.STATE.toString(), new ExpectedAttributeValue().withValue(new AttributeValue().withS(STATE_PENDING))); expected.put(AttributeName.FINALIZED.toString(), new ExpectedAttributeValue().withExists(false)); expected.put(AttributeName.VERSION.toString(), new ExpectedAttributeValue().withValue(new AttributeValue().withN(Integer.toString(expectedVersion)))); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put(AttributeName.STATE.toString(), new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue(stateToString(targetState)))); updates.put(AttributeName.DATE.toString(), new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(txManager.getCurrentTimeAttribute())); UpdateItemRequest finishRequest = new UpdateItemRequest() .withTableName(txManager.getTransactionTableName()) .withKey(txKey) .withAttributeUpdates(updates) .withReturnValues(ReturnValue.ALL_NEW) .withExpected(expected); UpdateItemResult finishResult = txManager.getClient().updateItem(finishRequest); txItem = finishResult.getAttributes(); if(txItem == null) { throw new TransactionAssertionException(txId, "Unexpected null tx item after committing " + targetState); } }
Example #16
Source File: TransactionDynamoDBFacade.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Override public UpdateItemResult updateItem(UpdateItemRequest request) throws AmazonServiceException, AmazonClientException { Map<String, ExpectedAttributeValue> expectedValues = request.getExpected(); checkExpectedValues(request.getTableName(), request.getKey(), expectedValues); // conditional checks are handled by the above call request.setExpected(null); return txn.updateItem(request); }
Example #17
Source File: Transaction.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
protected static void releaseReadLock(String txId, TransactionManager txManager, String tableName, Map<String, AttributeValue> key) { Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(); expected.put(AttributeName.TXID.toString(), new ExpectedAttributeValue().withValue(new AttributeValue(txId))); expected.put(AttributeName.TRANSIENT.toString(), new ExpectedAttributeValue().withExists(false)); expected.put(AttributeName.APPLIED.toString(), new ExpectedAttributeValue().withExists(false)); try { Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(1); updates.put(AttributeName.TXID.toString(), new AttributeValueUpdate().withAction(AttributeAction.DELETE)); updates.put(AttributeName.DATE.toString(), new AttributeValueUpdate().withAction(AttributeAction.DELETE)); UpdateItemRequest update = new UpdateItemRequest() .withTableName(tableName) .withAttributeUpdates(updates) .withKey(key) .withExpected(expected); txManager.getClient().updateItem(update); } catch (ConditionalCheckFailedException e) { try { expected.put(AttributeName.TRANSIENT.toString(), new ExpectedAttributeValue().withValue(new AttributeValue().withS(BOOLEAN_TRUE_ATTR_VAL))); DeleteItemRequest delete = new DeleteItemRequest() .withTableName(tableName) .withKey(key) .withExpected(expected); txManager.getClient().deleteItem(delete); } catch (ConditionalCheckFailedException e1) { // Ignore, means it was definitely rolled back // Re-read to ensure that it wasn't applied Map<String, AttributeValue> item = getItem(txManager, tableName, key); txAssert(! (item != null && txId.equals(getOwner(item)) && item.containsKey(AttributeName.APPLIED.toString())), "Item should not have been applied. Unable to release lock", "item", item); } } }
Example #18
Source File: Transaction.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
/** * Releases the lock for the item. If the item was inserted only to acquire the lock (if the item didn't exist before * for a DeleteItem or LockItem), it will be deleted now. * * Otherwise, all of the attributes uses for the transaction (tx id, transient flag, applied flag) will be removed. * * Conditions on our transaction id owning the item * * To be used once the transaction has committed only. * @param request */ protected void unlockItemAfterCommit(Request request) { try { Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(); expected.put(AttributeName.TXID.toString(), new ExpectedAttributeValue().withValue(new AttributeValue(txId))); if(request instanceof PutItem || request instanceof UpdateItem) { Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put(AttributeName.TXID.toString(), new AttributeValueUpdate().withAction(AttributeAction.DELETE)); updates.put(AttributeName.TRANSIENT.toString(), new AttributeValueUpdate().withAction(AttributeAction.DELETE)); updates.put(AttributeName.APPLIED.toString(), new AttributeValueUpdate().withAction(AttributeAction.DELETE)); updates.put(AttributeName.DATE.toString(), new AttributeValueUpdate().withAction(AttributeAction.DELETE)); UpdateItemRequest update = new UpdateItemRequest() .withTableName(request.getTableName()) .withKey(request.getKey(txManager)) .withAttributeUpdates(updates) .withExpected(expected); txManager.getClient().updateItem(update); } else if(request instanceof DeleteItem) { DeleteItemRequest delete = new DeleteItemRequest() .withTableName(request.getTableName()) .withKey(request.getKey(txManager)) .withExpected(expected); txManager.getClient().deleteItem(delete); } else if(request instanceof GetItem) { releaseReadLock(request.getTableName(), request.getKey(txManager)); } else { throw new TransactionAssertionException(txId, "Unknown request type: " + request.getClass()); } } catch (ConditionalCheckFailedException e) { // ignore, unlock already happened // TODO if we really want to be paranoid we could condition on applied = 1, and then here // we would have to read the item again and make sure that applied was 1 if we owned the lock (and assert otherwise) } }
Example #19
Source File: Transaction.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
/** * Adds an UpdateItem request to the transaction * * @param request * @throws DuplicateRequestException if the item in the request is already involved in this transaction * @throws ItemNotLockedException when another transaction is confirmed to have the lock on the item in the request * @throws TransactionCompletedException when the transaction has already completed * @throws TransactionNotFoundException if the transaction does not exist * @throws TransactionException on unexpected errors or unresolvable OCC contention */ public UpdateItemResult updateItem(UpdateItemRequest request) throws DuplicateRequestException, ItemNotLockedException, TransactionCompletedException, TransactionNotFoundException, TransactionException { UpdateItem wrappedRequest = new UpdateItem(); wrappedRequest.setRequest(request); Map<String, AttributeValue> item = driveRequest(wrappedRequest); stripSpecialAttributes(item); return new UpdateItemResult().withAttributes(item); }
Example #20
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Test public void missingKey() { Transaction t1 = manager.newTransaction(); try { t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME)); fail(); } catch (InvalidRequestException e) { assertTrue(e.getMessage(), e.getMessage().contains("The request key cannot be empty")); } t1.rollback(); t1.delete(Long.MAX_VALUE); }
Example #21
Source File: TransactionItem.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
/** * Completes a transaction by marking its "Finalized" attribute. This leaves the completed transaction item around * so that the party who created the transaction can see whether it was completed or rolled back. They can then either * delete the transaction record when they're done, or they can run a sweeper process to go and delete the completed transactions * later on. * * @param expectedCurrentState * @throws ConditionalCheckFailedException if the transaction is completed, doesn't exist anymore, or even if it isn't committed or rolled back */ public void complete(final State expectedCurrentState) throws ConditionalCheckFailedException { Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>(2); if(State.COMMITTED.equals(expectedCurrentState)) { expected.put(AttributeName.STATE.toString(), new ExpectedAttributeValue(new AttributeValue(STATE_COMMITTED))); } else if(State.ROLLED_BACK.equals(expectedCurrentState)) { expected.put(AttributeName.STATE.toString(), new ExpectedAttributeValue(new AttributeValue(STATE_ROLLED_BACK))); } else { throw new TransactionAssertionException(txId, "Illegal state in finish(): " + expectedCurrentState + " txItem " + txItem); } Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put(AttributeName.FINALIZED.toString(), new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue(Transaction.BOOLEAN_TRUE_ATTR_VAL))); updates.put(AttributeName.DATE.toString(), new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(txManager.getCurrentTimeAttribute())); UpdateItemRequest completeRequest = new UpdateItemRequest() .withTableName(txManager.getTransactionTableName()) .withKey(txKey) .withAttributeUpdates(updates) .withReturnValues(ReturnValue.ALL_NEW) .withExpected(expected); txItem = txManager.getClient().updateItem(completeRequest).getAttributes(); }
Example #22
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Test public void basicNewItemCommit() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME); t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key1)); assertItemLocked(INTEG_HASH_TABLE_NAME, key1, t1.getId(), true, true); t1.commit(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, key1, true); t1.delete(Long.MAX_VALUE); }
Example #23
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Test public void getItemCommittedUpdated() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("asdf"))); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(item0); item1.put("asdf", new AttributeValue("asdf")); t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withAttributeUpdates(updates) .withKey(key0)); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true); Map<String, AttributeValue> item = manager.getItem(new GetItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key0), IsolationLevel.COMMITTED).getItem(); assertNoSpecialAttributes(item); assertEquals(item0, item); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true); t1.commit(); item = manager.getItem(new GetItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key0), IsolationLevel.COMMITTED).getItem(); assertNoSpecialAttributes(item); assertEquals(item1, item); }
Example #24
Source File: TransactionDynamoDBFacade.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Override public UpdateItemResult updateItem(String tableName, Map<String, AttributeValue> key, Map<String, AttributeValueUpdate> attributeUpdates) throws AmazonServiceException, AmazonClientException { return updateItem(new UpdateItemRequest() .withTableName(tableName) .withKey(key) .withAttributeUpdates(attributeUpdates)); }
Example #25
Source File: TransactionDynamoDBFacade.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Override public UpdateItemResult updateItem(String tableName, Map<String, AttributeValue> key, Map<String, AttributeValueUpdate> attributeUpdates, String returnValues) throws AmazonServiceException, AmazonClientException { return updateItem(new UpdateItemRequest() .withTableName(tableName) .withKey(key) .withAttributeUpdates(attributeUpdates) .withReturnValues(returnValues)); }
Example #26
Source File: RequestTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Test public void validUpdate() { UpdateItem r = new UpdateItem(); Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); item.put(HASH_ATTR_NAME, new AttributeValue("a")); r.setRequest(new UpdateItemRequest() .withTableName(TABLE_NAME) .withKey(item)); r.validate("1", new MockTransactionManager(HASH_SCHEMA)); }
Example #27
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Test public void getItemCommittedMissingImage() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("asdf"))); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(item0); item1.put("asdf", new AttributeValue("asdf")); t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withAttributeUpdates(updates) .withKey(key0)); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true); dynamodb.getRequestsToTreatAsDeleted.add(new GetItemRequest() .withTableName(manager.getItemImageTableName()) .addKeyEntry(AttributeName.IMAGE_ID.toString(), new AttributeValue(t1.getTxItem().txId + "#" + 1)) .withConsistentRead(true)); try { manager.getItem(new GetItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key0), IsolationLevel.COMMITTED).getItem(); fail("Should have thrown an exception."); } catch (TransactionException e) { assertEquals("null - Ran out of attempts to get a committed image of the item", e.getMessage()); } }
Example #28
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Test public void getItemCommittedUpdatedAndApplied() { Transaction t1 = new Transaction(UUID.randomUUID().toString(), manager, true) { @Override protected void doCommit() { //Skip cleaning up the transaction so we can validate reading. } }; Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("asdf", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("asdf"))); Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(item0); item1.put("asdf", new AttributeValue("asdf")); t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withAttributeUpdates(updates) .withKey(key0)); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true); t1.commit(); Map<String, AttributeValue> item = manager.getItem(new GetItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key0), IsolationLevel.COMMITTED).getItem(); assertNoSpecialAttributes(item); assertEquals(item1, item); assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true); }
Example #29
Source File: RequestTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
private void invalidRequestTest(UpdateItemRequest request, String expectedExceptionMessage) { UpdateItem r = new UpdateItem(); r.setRequest(request); try { r.validate("1", new MockTransactionManager(HASH_SCHEMA)); fail(); } catch (InvalidRequestException e) { assertTrue(e.getMessage().contains(expectedExceptionMessage)); } }
Example #30
Source File: TransactionsIntegrationTest.java From dynamodb-transactions with Apache License 2.0 | 5 votes |
@Test public void basicNewItemRollback() { Transaction t1 = manager.newTransaction(); Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME); t1.updateItem(new UpdateItemRequest() .withTableName(INTEG_HASH_TABLE_NAME) .withKey(key1)); assertItemLocked(INTEG_HASH_TABLE_NAME, key1, t1.getId(), true, true); t1.rollback(); assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, false); t1.delete(Long.MAX_VALUE); }