com.amazonaws.services.dynamodbv2.model.AttributeAction Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.model.AttributeAction. 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: GenericDynamoDB.java    From strongbox with Apache License 2.0 6 votes vote down vote up
private Map<String, AttributeValueUpdate> createAttributes(Entry entry) {
    Map<String, AttributeValueUpdate> attributes = new HashMap<>();
    attributes.put(SCHEMA_VERSION_FIELD_NAME, new AttributeValueUpdate()
            .withAction(AttributeAction.PUT)
            .withValue(new AttributeValue().withN(SCHEMA_VERSION)));

    attributes.put(OPTIMISTIC_LOCK_FIELD_NAME, new AttributeValueUpdate()
            .withAction(AttributeAction.PUT)
            .withValue(new AttributeValue().withS(sha(entry))));

    for (Map.Entry<Integer, String> e : attributeMappings.entrySet()) {

        Object value = getValue(entry, e.getValue());
        if (value != null) {
            attributes.put(e.getKey().toString(),
                    new AttributeValueUpdate()
                            .withAction(AttributeAction.PUT)
                            .withValue(getAttribute(value)));
        }
    }
    return attributes;
}
 
Example #2
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@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 #7
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@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 #8
Source File: StreamsAdapterDemoHelper.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
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 #9
Source File: AmazonDynamoDBStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 6 votes vote down vote up
@Test
public void test_updateItem_WithAllParameters() throws Exception {
  createTable();
  putItem(TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE);

  String UPDATE_ATTRIBUTE_VALUE = "UpdateAttributeValue1";

  Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
  key.put(TEST_ATTRIBUTE, new AttributeValue()
    .withS(TEST_ATTRIBUTE_VALUE));
  Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>();
  attributeUpdates.put(TEST_ATTRIBUTE, new AttributeValueUpdate()
    .withAction(AttributeAction.PUT)
    .withValue(new AttributeValue()
      .withS(UPDATE_ATTRIBUTE_VALUE)));
  String returnValues = "";

  UpdateItemResult result = dynamoDb.updateItem(TEST_TABLE_NAME, key, attributeUpdates, returnValues);
  Double units = result.getConsumedCapacity().getCapacityUnits();

  GetItemResult getItemResult = getItem(TEST_ATTRIBUTE, UPDATE_ATTRIBUTE_VALUE);
  String updatedValue = getItemResult.getItem().get(TEST_ATTRIBUTE).getS();

  assertThat(units.doubleValue(), equalTo(1.0));
  assertThat(updatedValue, equalTo(UPDATE_ATTRIBUTE_VALUE));
}
 
Example #10
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: TransactionExamples.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * This example shows that reads can be performed in a transaction, and read locks can be upgraded to write locks. 
 */
public void readThenWrite() {
    print("\n*** readThenWrite() ***\n");
    
    Transaction t1 = txManager.newTransaction();
    
    // Perform a GetItem request on the transaction
    print("Reading Item1");
    Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>();
    key1.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("Item1"));
    
    Map<String, AttributeValue> item1 = t1.getItem(new GetItemRequest()
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME)).getItem();
    print("Item1: " + item1);
    
    // Now call UpdateItem to add a new attribute.
    // Notice that the library supports ReturnValues in writes
    print("Updating Item1");
    Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>();
    updates.put("Color", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("Green")));
    
    item1 = t1.updateItem(new UpdateItemRequest()
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME)
        .withAttributeUpdates(updates)
        .withReturnValues(ReturnValue.ALL_NEW)).getAttributes();
    print("Item1 is now: " + item1);
    
    t1.commit();
    
    t1.delete();
}
 
Example #12
Source File: TransactionItem.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #13
Source File: TransactionItem.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #14
Source File: DyNum.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Make an update.
 * @param action The action
 * @return The update
 */
public AttributeValueUpdate update(final AttributeAction action) {
    return new AttributeValueUpdate()
        .withAction(action)
        .withValue(
            new AttributeValue().withN(
                Long.toString(this.longValue())
            )
        );
}
 
Example #15
Source File: Transaction.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #16
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@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 #17
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@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 #18
Source File: DyUsage.java    From jare with MIT License 5 votes vote down vote up
/**
 * Save XML.
 * @param xml The XML to save
 * @throws IOException If fails
 */
private void save(final String xml) throws IOException {
    this.item.put(
        "usage",
        new AttributeValueUpdate()
            .withValue(new AttributeValue().withS(xml))
            .withAction(AttributeAction.PUT)
    );
}
 
Example #19
Source File: DyUsage.java    From jare with MIT License 5 votes vote down vote up
/**
 * Save total.
 * @param total Total usage
 * @throws IOException If fails
 */
private void save(final long total) throws IOException {
    this.item.put(
        "total",
        new AttributeValueUpdate()
            .withValue(new AttributeValue().withN(Long.toString(total)))
            .withAction(AttributeAction.PUT)
    );
}
 
Example #20
Source File: GeoDynamoDBServlet.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
private void updatePoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey"));

	String schoolName = requestObject.getString("schoolName");
	AttributeValueUpdate schoolNameValueUpdate = null;

	String memo = requestObject.getString("memo");
	AttributeValueUpdate memoValueUpdate = null;

	if (schoolName == null || schoolName.equalsIgnoreCase("")) {
		schoolNameValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.DELETE);
	} else {
		AttributeValue schoolNameAttributeValue = new AttributeValue().withS(schoolName);
		schoolNameValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(
				schoolNameAttributeValue);
	}

	if (memo == null || memo.equalsIgnoreCase("")) {
		memoValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.DELETE);
	} else {
		AttributeValue memoAttributeValue = new AttributeValue().withS(memo);
		memoValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(memoAttributeValue);
	}

	UpdatePointRequest updatePointRequest = new UpdatePointRequest(geoPoint, rangeKeyAttributeValue);
	updatePointRequest.getUpdateItemRequest().addAttributeUpdatesEntry("schoolName", schoolNameValueUpdate);
	updatePointRequest.getUpdateItemRequest().addAttributeUpdatesEntry("memo", memoValueUpdate);

	UpdatePointResult updatePointResult = geoDataManager.updatePoint(updatePointRequest);

	printUpdatePointResult(updatePointResult, out);
}
 
Example #21
Source File: StreamsAdapterDemoHelper.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void updateItem(AmazonDynamoDB dynamoDBClient, 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);
    dynamoDBClient.updateItem(updateItemRequest);
}
 
Example #22
Source File: DDBTransaction.java    From Doradus with Apache License 2.0 5 votes vote down vote up
private void updateRowColumnDeletes(String storeName,
                                    Map<String, AttributeValue> key,
                                    List<String> colNames) {
    Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<>();
    for (String colName : colNames) {
        attributeUpdates.put(colName, new AttributeValueUpdate().withAction(AttributeAction.DELETE));
    }
    m_service.updateRow(storeName, key, attributeUpdates);
}
 
Example #23
Source File: DDBTransaction.java    From Doradus with Apache License 2.0 5 votes vote down vote up
private void updateRowColumnUpdates(String storeName,
                                    Map<String, AttributeValue> key,
                                    List<DColumn> colList) {
    Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<>();
    for (DColumn col : colList) {
        AttributeValue attrValue = mapColumnValue(storeName, col);
        attributeUpdates.put(col.getName(), new AttributeValueUpdate(attrValue, AttributeAction.PUT));
    }
    m_service.updateRow(storeName, key, attributeUpdates);
}
 
Example #24
Source File: SingleUpdateBuilder.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 5 votes vote down vote up
public SingleUpdateBuilder put(final StaticBuffer column, final StaticBuffer value) {
    updates.put(encodeKeyBuffer(column),
            new AttributeValueUpdate()
                    .withAction(AttributeAction.PUT)
                    .withValue(encodeValue(value)));
    return this;
}
 
Example #25
Source File: Transaction.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
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 #26
Source File: TransactionItem.java    From dynamodb-transactions with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a request object (input param) to the transaction item.  Enforces that request are unique for a given table name and primary key.
 * Doesn't let you do more than one write per item.  However you can upgrade a read lock to a write.
 * @param callerRequest 
 * @throws ConditionalCheckFailedException if the tx item changed out from under us.  If you get this you must throw this TransactionItem away.
 * @throws DuplicateRequestException If you get this you do not need to throw away the item.
 * @throws InvalidRequestException If the request would add too much data to the transaction
 * @return true if the request was added, false if it didn't need to be added (because it was a duplicate lock request)
 */
public synchronized boolean addRequest(Request callerRequest) throws ConditionalCheckFailedException, DuplicateRequestException {
    // 1. Ensure the request is unique (modifies the internal data structure if it is unique)
    //    However, do not not short circuit.  If we're doing a read in a resumed transaction, it's important to ensure we're returning
    //    any writes that happened before. 
    addRequestToMap(callerRequest);
    
    callerRequest.setRid(version);
    
    // 2. Write request to transaction item
    ByteBuffer requestBytes = Request.serialize(txId, callerRequest);
    AttributeValueUpdate txItemUpdate = new AttributeValueUpdate()
        .withAction(AttributeAction.ADD)
        .withValue(new AttributeValue().withBS(Arrays.asList(requestBytes)));
    
    Map<String, AttributeValueUpdate> txItemUpdates = new HashMap<String, AttributeValueUpdate>();
    txItemUpdates.put(AttributeName.REQUESTS.toString(), txItemUpdate);    
    txItemUpdates.put(AttributeName.VERSION.toString(), new AttributeValueUpdate().withAction(AttributeAction.ADD).withValue(new AttributeValue().withN("1")));
    txItemUpdates.put(AttributeName.DATE.toString(), new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(txManager.getCurrentTimeAttribute()));
    
    Map<String, ExpectedAttributeValue> expected = new HashMap<String, ExpectedAttributeValue>();
    expected.put(AttributeName.STATE.toString(), new ExpectedAttributeValue(new AttributeValue(STATE_PENDING)));
    expected.put(AttributeName.VERSION.toString(), new ExpectedAttributeValue(new AttributeValue().withN(Integer.toString(version))));
    
    UpdateItemRequest txItemUpdateRequest = new UpdateItemRequest()
        .withTableName(txManager.getTransactionTableName())
        .withKey(txKey)
        .withExpected(expected)
        .withReturnValues(ReturnValue.ALL_NEW)
        .withAttributeUpdates(txItemUpdates);
    
    try {
        txItem = txManager.getClient().updateItem(txItemUpdateRequest).getAttributes();
        int newVersion = Integer.parseInt(txItem.get(AttributeName.VERSION.toString()).getN());
        txAssert(newVersion == version + 1, txId, "Unexpected version number from update result");
        version = newVersion;
    } catch (AmazonServiceException e) {
        if("ValidationException".equals(e.getErrorCode())) {
            removeRequestFromMap(callerRequest);
            throw new InvalidRequestException("The amount of data in the transaction cannot exceed the DynamoDB item size limit", 
                txId, callerRequest.getTableName(), callerRequest.getKey(txManager), callerRequest);
        } else {
            throw e;
        }
    }
    return true;
}
 
Example #27
Source File: Request.java    From dynamodb-transactions with Apache License 2.0 4 votes vote down vote up
@JsonIgnore
public abstract void setAction(AttributeAction attributeAction);
 
Example #28
Source File: TransactionExamples.java    From dynamodb-transactions with Apache License 2.0 4 votes vote down vote up
/**
 * Demonstrates the 3 levels of supported read isolation: Uncommitted, Committed, Locked
 */
public void reading() {
    print("\n*** reading() ***\n");
    
    // First, create a new transaction and update Item1, but don't commit yet.
    print("Starting a transaction to modify Item1");
    Transaction t1 = txManager.newTransaction();
    
    Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>();
    key1.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("Item1"));
    
    // Show the current value before any changes
    print("Getting the current value of Item1 within the transaction.  This is the strongest form of read isolation.");
    print("  However, you can't trust the value you get back until your transaction commits!");
    Map<String, AttributeValue> item1 = t1.getItem(new GetItemRequest()
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME)).getItem();
    print("Before any changes, Item1 is: " + item1);

    // Show the current value before any changes
    print("Changing the Color of Item1, but not committing yet");
    Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>();
    updates.put("Color", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("Purple")));
    
    item1 = t1.updateItem(new UpdateItemRequest()
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME)
        .withAttributeUpdates(updates)
        .withReturnValues(ReturnValue.ALL_NEW)).getAttributes();
    print("Item1 is not yet committed, but if committed, will be: " + item1);
    
    // Perform an Uncommitted read
    print("The weakest (but cheapest) form of read is Uncommitted, where you can get back changes that aren't yet committed");
    print("  And might be rolled back!");
    item1 = txManager.getItem(new GetItemRequest() // Uncommitted reads happen on the transaction manager, not on a transaction.
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME), 
        IsolationLevel.UNCOMMITTED).getItem(); // Note the isloationLevel
    print("The read, which could return changes that will be rolled back, returned: " + item1);
    
    // Perform a Committed read
    print("A strong read is Committed.  This means that you are guaranteed to read only committed changes,");
    print("  but not necessarily the *latest* committed change!");
    item1 = txManager.getItem(new GetItemRequest() // Uncommitted reads happen on the transaction manager, not on a transaction.
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME), 
        IsolationLevel.COMMITTED).getItem(); // Note the isloationLevel
    print("The read, which should return the same value of the original read, returned: " + item1);
    
    // Now start a new transaction and do a read, write, and read in it
    Transaction t2 = txManager.newTransaction();
    
    print("Getting Item1, but this time under a new transaction t2");
    item1 = t2.getItem(new GetItemRequest()
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME)).getItem();
    print("Before any changes, in t2, Item1 is: " + item1);
    print(" This just rolled back transaction t1! Notice that this is the same value as *before* t1!");

    updates = new HashMap<String, AttributeValueUpdate>();
    updates.put("Color", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("Magenta")));
    
    print("Updating item1 again, but now under t2");
    item1 = t2.updateItem(new UpdateItemRequest()
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME)
        .withAttributeUpdates(updates)
        .withReturnValues(ReturnValue.ALL_NEW)).getAttributes();
    print("Item1 is not yet committed, but if committed, will now be: " + item1);
    
    print("Getting Item1, again, under lock in t2.  Notice that the transaction library makes your write during this transaction visible to future reads.");
    item1 = t2.getItem(new GetItemRequest()
        .withKey(key1)
        .withTableName(EXAMPLE_TABLE_NAME)).getItem();
    print("Under transaction t2, Item1 is going to be: " + item1);
    
    print("Committing t2");
    t2.commit();
    
    try {
        print("Committing t1 (this will fail because it was rolled back)");
        t1.commit();   
        throw new RuntimeException("Should have been rolled back");
    } catch (TransactionRolledBackException e) {
        print("t1 was rolled back as expected.  I hope you didn't act on the GetItem you did under the lock in t1!");
    }
}
 
Example #29
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 4 votes vote down vote up
@Test
public void applyOnlyOnce() {
    Transaction t1 = new Transaction(UUID.randomUUID().toString(), manager, true) {
        @Override
        protected Map<String, AttributeValue> applyAndKeepLock(Request request, Map<String, AttributeValue> lockedItem) {
            super.applyAndKeepLock(request, lockedItem);
            throw new FailedYourRequestException();
        }  
    };
    
    Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME);
    Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue> (key1);
    item1.put("attr1", new AttributeValue().withN("1"));
    
    Map<String, AttributeValue> key2 = newKey(INTEG_HASH_TABLE_NAME);
    
    Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>();
    updates.put("attr1", new AttributeValueUpdate().withAction(AttributeAction.ADD).withValue(new AttributeValue().withN("1")));
    
    UpdateItemRequest update = new UpdateItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withAttributeUpdates(updates)
        .withKey(key1);
    
    try {
        t1.updateItem(update);
        fail();
    } catch (FailedYourRequestException e) { } 
    
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    assertOldItemImage(t1.getId(), INTEG_HASH_TABLE_NAME, key1, key1, false);
    
    Transaction t2 = manager.resumeTransaction(t1.getId());
    
    t2.getItem(new GetItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key2));
    
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    assertOldItemImage(t1.getId(), INTEG_HASH_TABLE_NAME, key1, key1, false);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key2, t1.getId(), true, false);
    assertOldItemImage(t1.getId(), INTEG_HASH_TABLE_NAME, key2, null, false);
    
    t2.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, item1, true);
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key2, false);
    
    assertOldItemImage(t1.getId(), INTEG_HASH_TABLE_NAME, key1, null, false);
    assertOldItemImage(t1.getId(), INTEG_HASH_TABLE_NAME, key2, null, false);
    
    t2.delete(Long.MAX_VALUE);
    assertTransactionDeleted(t2);
}
 
Example #30
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 4 votes vote down vote up
@Test
public void getItemCommittedConcurrentCommit() {
    //Test reading an item while simulating another transaction committing concurrently.
    //To do this we skip cleanup, make the item image appear to be deleted,
    //and then make the reader get the uncommitted version of the transaction 
    //row for the first read and then actual updated version for later reads.
    
    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);
    
    GetItemRequest txItemRequest = new GetItemRequest()
        .withTableName(manager.getTransactionTableName())
        .addKeyEntry(AttributeName.TXID.toString(), new AttributeValue(t1.getTxItem().txId))
        .withConsistentRead(true);
    
    //Save the copy of the transaction before commit. 
    GetItemResult uncommittedTransaction = dynamodb.getItem(txItemRequest);
    
    t1.commit();
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true);
    
    dynamodb.getRequestsToStub.put(txItemRequest, new LinkedList<GetItemResult>(Collections.singletonList(uncommittedTransaction)));
    //Stub out the image so it appears deleted
    dynamodb.getRequestsToTreatAsDeleted.add(new GetItemRequest()
        .withTableName(manager.getItemImageTableName())
        .addKeyEntry(AttributeName.IMAGE_ID.toString(), new AttributeValue(t1.getTxItem().txId + "#" + 1))
        .withConsistentRead(true));
    
    Map<String, AttributeValue> item = manager.getItem(new GetItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key0), IsolationLevel.COMMITTED).getItem();
    assertNoSpecialAttributes(item);
    assertEquals(item1, item);
}