Example usage for com.amazonaws.services.dynamodbv2.model ReturnValue ALL_OLD

List of usage examples for com.amazonaws.services.dynamodbv2.model ReturnValue ALL_OLD

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model ReturnValue ALL_OLD.

Prototype

ReturnValue ALL_OLD

To view the source code for com.amazonaws.services.dynamodbv2.model ReturnValue ALL_OLD.

Click Source Link

Usage

From source file:AmazonDynamoDBSample_PutThrottled.java

License:Open Source License

private static Thread newItemCreationThread(String tableName, final int thrdNo) {
    final int ITEM_COUNTS = 1000 * 1000;
    return new Thread(() -> {
        for (int c = 0; c < ITEM_COUNTS; c++) {
            Map<String, AttributeValue> item = new HashMap<>();
            String pk = String.format("pk name: thrd %d creates %dth item", thrdNo, c);
            item.put("name", new AttributeValue(pk));

            PutItemRequest req = new PutItemRequest(tableName, item);
            req.setReturnValues(ReturnValue.ALL_OLD);
            PutItemResult res = null;//  w  w  w  .  j  a v a  2s.  co m
            long t = System.currentTimeMillis();
            try {
                res = dynamoDB.putItem(req);
            } catch (Exception exob) {
                exob.printStackTrace();
            }
            assert 0 == res.getAttributes().size();
            System.out.printf("%s. takes %d ms\n", pk, System.currentTimeMillis() - t);
        }
    });
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java

License:LGPL

@Test
public void testReplaceSinglePathNumberExtant() throws Exception {
    // setup// w w  w. j a v  a 2  s.co  m
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", "peekaboo").build()));
    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": 1 } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    UpdateItemOutcome out = table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY_ATTRIBUTE_NAME, VALUE)
            .withExpressionSpec(spec).withReturnValues(ReturnValue.ALL_OLD));

    Item oldItem = Item.fromMap(InternalUtils.toSimpleMapValue(out.getUpdateItemResult().getAttributes()));
    Assert.assertTrue(oldItem.hasAttribute("a"));
    Assert.assertEquals(oldItem.getString("a"), "peekaboo");
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertEquals(item.getNumber("a").longValue(), 1L);
}

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

@Override
public E getAndDelete(K key, long version) {
    Preconditions.checkNotNull(key, "keys must not be null");
    Preconditions.checkArgument(version >= -1L, "version must be greater than or equal to -1");
    final PrimaryKey pk = createKeys(key);
    final boolean conditioning = version >= 0;
    final String actualCondition;
    final Map<String, Object> valueMap;
    final Map<String, String> nameMap;

    if (conditioning) {
        Preconditions.checkState(versionProperty != null);
        actualCondition = String.format(Locale.ENGLISH, "%s and #version = :v", conditionalDeleteCondition);
        valueMap = Collections.singletonMap(":v", version);
        nameMap = Collections.singletonMap("#version", versionProperty);
    } else {//from   w w w  . j  av a2  s.co  m
        actualCondition = conditionalDeleteCondition;
        valueMap = null;
        nameMap = null;
    }
    final DeleteItemSpec spec = new DeleteItemSpec().withPrimaryKey(pk).withNameMap(nameMap)
            .withValueMap(valueMap).withConditionExpression(actualCondition)
            .withReturnValues(ReturnValue.ALL_OLD);
    final Item item;
    try {
        item = table.deleteItem(spec).getItem();
    } catch (AmazonClientException e) {
        throw convertDynamoDBException(e, "delete",
                () -> convertConditionalCheckFailedExceptionForDelete(e, version, key));
    }
    return convertItemToDomain(item);
}

From source file:org.iternine.jeppetto.dao.dynamodb.DynamoDBQueryModelDAO.java

License:Apache License

private T updateItem(Map<String, AttributeValue> key, UpdateExpressionBuilder updateExpressionBuilder,
        ConditionExpressionBuilder conditionExpressionBuilder, ResultFromUpdate resultFromUpdate) {
    try {/* w  w  w. j  ava 2s  .  com*/
        UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(key)
                .withUpdateExpression(updateExpressionBuilder.getExpression());

        Map<String, AttributeValue> expressionAttributeValues;
        Map<String, String> expressionAttributeNames;

        if (conditionExpressionBuilder == null) {
            expressionAttributeValues = updateExpressionBuilder.getExpressionAttributeValues();
            expressionAttributeNames = updateExpressionBuilder.getExpressionAttributeNames();
        } else {
            expressionAttributeValues = new LinkedHashMap<String, AttributeValue>();
            expressionAttributeNames = new LinkedHashMap<String, String>();

            expressionAttributeValues.putAll(updateExpressionBuilder.getExpressionAttributeValues());
            expressionAttributeNames.putAll(updateExpressionBuilder.getExpressionAttributeNames());

            expressionAttributeValues.putAll(conditionExpressionBuilder.getExpressionAttributeValues());
            expressionAttributeNames.putAll(conditionExpressionBuilder.getExpressionAttributeNames());

            updateItemRequest.setConditionExpression(conditionExpressionBuilder.getExpression());
        }

        if (!expressionAttributeValues.isEmpty()) {
            updateItemRequest.setExpressionAttributeValues(expressionAttributeValues);
        }

        if (!expressionAttributeNames.isEmpty()) {
            updateItemRequest.setExpressionAttributeNames(expressionAttributeNames);
        }

        if (resultFromUpdate != ResultFromUpdate.ReturnNone) {
            updateItemRequest
                    .setReturnValues(resultFromUpdate == ResultFromUpdate.ReturnPreUpdate ? ReturnValue.ALL_OLD
                            : ReturnValue.ALL_NEW);

            UpdateItemResult result = dynamoDB.updateItem(updateItemRequest);

            T t = ConversionUtil.getObjectFromItem(result.getAttributes(), entityClass);

            ((DynamoDBPersistable) t).__markPersisted(dynamoDB.toString());

            return t;
        } else {
            dynamoDB.updateItem(updateItemRequest);

            return null;
        }
    } catch (Exception e) {
        throw new JeppettoException(e);
    }
}

From source file:org.springframework.integration.aws.metadata.DynamoDbMetaDataStore.java

License:Apache License

@Override
public String remove(String key) {
    Assert.hasText(key, "'key' must not be empty.");

    awaitForActive();//  w w  w .j  ava 2  s  . c o  m

    Item item = this.table
            .deleteItem(new DeleteItemSpec().withPrimaryKey(KEY, key).withReturnValues(ReturnValue.ALL_OLD))
            .getItem();

    return getValueIfAny(item);
}