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

The following examples show how to use com.amazonaws.services.dynamodbv2.model.GetItemRequest. 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 vote down vote up
@Test
public void getItemWithDelete() {
    Transaction t1 = manager.newTransaction();
    Map<String, AttributeValue> getResult1 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem();
    assertEquals(getResult1, item0);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    t1.deleteItem(new DeleteItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0));
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    Map<String, AttributeValue> getResult2 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem();
    assertNull(getResult2);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    t1.commit();
}
 
Example #2
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getItemCommittedInsert() {
    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"));
    
    t1.putItem(new PutItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withItem(item1));
    
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    Map<String, AttributeValue> item = manager.getItem(new GetItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key1), IsolationLevel.COMMITTED).getItem();
    assertNull(item);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    t1.rollback();
}
 
Example #3
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getItemCommittedDeleted() {
    Transaction t1 = manager.newTransaction();
    
    t1.deleteItem(new DeleteItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key0));
    
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    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, item0, t1.getId(), false, false);
    
    t1.rollback();
}
 
Example #4
Source File: SimpleNumericAttributesITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
@Test
public void performanceTest() throws Exception {
    NumberAttributeTestClass obj = getUniqueObject();
    DynamoDBMapper mapper = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo);
    mapper.save(obj);
    HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put(KEY_NAME, new AttributeValue().withS(obj.getKey()));
    GetItemResult item = dynamo.getItem(new GetItemRequest()
            .withTableName("aws-java-sdk-util-crypto").withKey(key));
    
    long start = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
        mapper.marshallIntoObject(NumberAttributeTestClass.class, item.getItem());
    }        
    
    long end = System.currentTimeMillis();
    
    System.err.println("time: " + (end - start));
}
 
Example #5
Source File: DescribeUser.java    From reinvent2013-mobile-photo-share with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the list of usernames stored in the user table.
 */
public void describeUser(String username, String userTable) {
    HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("username", new AttributeValue().withS(username));

    GetItemRequest getItemRequest = new GetItemRequest()
            .withTableName(userTable)
            .withKey(key);

    Map<String, AttributeValue> list = ddb.getItem(getItemRequest).getItem();
    if (list.isEmpty()) {
        System.err.println("No record found for username '" + username + "'");
        return;
    }

    for (Entry<String, AttributeValue> entry : list.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue().getS());
    }
}
 
Example #6
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getItemUncommittedDeleted() {
    Transaction t1 = manager.newTransaction();
    
    t1.deleteItem(new DeleteItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key0));
    
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    Map<String, AttributeValue> item = manager.getItem(new GetItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key0), IsolationLevel.UNCOMMITTED).getItem();
    assertNoSpecialAttributes(item);
    assertEquals(item0, item);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    t1.rollback();
}
 
Example #7
Source File: LowLevelItemCRUDExample.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
private static void retrieveItem() {
    try {
        
        HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Id", new AttributeValue().withN("120"));
        GetItemRequest getItemRequest = new GetItemRequest()
            .withTableName(tableName)
            .withKey(key)
            .withProjectionExpression("Id, ISBN, Title, Authors");
        
        GetItemResult result = client.getItem(getItemRequest);

        // Check the response.
        System.out.println("Printing item after retrieving it....");
        printItem(result.getItem());            
                    
    }  catch (AmazonServiceException ase) {
                System.err.println("Failed to retrieve item in " + tableName);
    }   

}
 
Example #8
Source File: LowLevelItemBinaryExample.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void retrieveItem(String threadId, String replyDateTime) throws IOException {
    HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("Id", new AttributeValue().withS(threadId));
    key.put("ReplyDateTime", new AttributeValue().withS(replyDateTime));
    
    GetItemRequest getReplyRequest = new GetItemRequest()
        .withTableName(tableName)
        .withKey(key)
        .withConsistentRead(true);
    
    GetItemResult getReplyResult = client.getItem(getReplyRequest);
    
    // Decompress the reply message and print
    Map<String, AttributeValue> reply = getReplyResult.getItem();
    String message = decompressString(reply.get("ExtendedMessage").getB());
    System.out.println("Reply message:\n"
        + " Id: " + reply.get("Id").getS() + "\n" 
        + " ReplyDateTime: " + reply.get("ReplyDateTime").getS() + "\n" 
        + " PostedBy: " + reply.get("PostedBy").getS() + "\n"
        + " Message: " + reply.get("Message").getS() + "\n"
        + " ExtendedMessage (decompressed): " + message);
}
 
Example #9
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getItemUncommittedInsert() {
    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"));
    
    t1.putItem(new PutItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withItem(item1));
    
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    Map<String, AttributeValue> item = manager.getItem(new GetItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withKey(key1), IsolationLevel.UNCOMMITTED).getItem();
    assertNoSpecialAttributes(item);
    assertEquals(item1, item);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    t1.rollback();
}
 
Example #10
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 #11
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 #12
Source File: DynamoDBManager.java    From dynamodb-geo with Apache License 2.0 6 votes vote down vote up
public GetPointResult getPoint(GetPointRequest getPointRequest) {
	long geohash = S2Manager.generateGeohash(getPointRequest.getGeoPoint());
	long hashKey = S2Manager.generateHashKey(geohash, config.getHashKeyLength());

	GetItemRequest getItemRequest = getPointRequest.getGetItemRequest();
	getItemRequest.setTableName(config.getTableName());

	AttributeValue hashKeyValue = new AttributeValue().withN(String.valueOf(hashKey));
	getItemRequest.getKey().put(config.getHashKeyAttributeName(), hashKeyValue);
	getItemRequest.getKey().put(config.getRangeKeyAttributeName(), getPointRequest.getRangeKeyValue());

	GetItemResult getItemResult = config.getDynamoDBClient().getItem(getItemRequest);
	GetPointResult getPointResult = new GetPointResult(getItemResult);

	return getPointResult;
}
 
Example #13
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getItemAfterPutItemOverwrite() {
    Transaction t1 = manager.newTransaction();
    Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(item0);
    item1.put("asdf", new AttributeValue("wef"));
    
    Map<String, AttributeValue> getResult1 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem();
    assertEquals(getResult1, item0);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item0, t1.getId(), false, false);
    
    Map<String, AttributeValue> putResult1 = t1.putItem(new PutItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withItem(item1)
        .withReturnValues(ReturnValue.ALL_OLD)).getAttributes();
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true);
    assertEquals(putResult1, item0);
    
    Map<String, AttributeValue> getResult2 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key0)).getItem();
    assertEquals(getResult2, item1);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, item1, t1.getId(), false, true);
    
    t1.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key0, item1, true);
}
 
Example #14
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getItemAfterPutItemInsert() {
    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, AttributeValue> getResult1 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key1)).getItem();
    assertNull(getResult1);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, t1.getId(), true, false);
    
    Map<String, AttributeValue> putResult1 = t1.putItem(new PutItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withItem(item1)
        .withReturnValues(ReturnValue.ALL_OLD)).getAttributes();
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    assertNull(putResult1);
    
    Map<String, AttributeValue> getResult2 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key1)).getItem();
    assertEquals(getResult2, item1);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    t1.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, item1, true);
}
 
Example #15
Source File: LowLevelItemBinaryExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void retrieveItem(String threadId, String replyDateTime) throws IOException {
    HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("Id", new AttributeValue().withS(threadId));
    key.put("ReplyDateTime", new AttributeValue().withS(replyDateTime));

    GetItemRequest getReplyRequest = new GetItemRequest().withTableName(tableName).withKey(key)
        .withConsistentRead(true);

    GetItemResult getReplyResult = client.getItem(getReplyRequest);

    // Decompress the reply message and print
    Map<String, AttributeValue> reply = getReplyResult.getItem();
    String message = decompressString(reply.get("ExtendedMessage").getB());
    System.out.println("Reply message:\n" + " Id: " + reply.get("Id").getS() + "\n" + " ReplyDateTime: "
        + reply.get("ReplyDateTime").getS() + "\n" + " PostedBy: " + reply.get("PostedBy").getS() + "\n"
        + " Message: " + reply.get("Message").getS() + "\n" + " ExtendedMessage (decompressed): " + message);
}
 
Example #16
Source File: LowLevelItemCRUDExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void retrieveItem() {
    try {

        HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Id", new AttributeValue().withN("120"));
        GetItemRequest getItemRequest = new GetItemRequest().withTableName(tableName).withKey(key)
            .withProjectionExpression("Id, ISBN, Title, Authors");

        GetItemResult result = client.getItem(getItemRequest);

        // Check the response.
        System.out.println("Printing item after retrieving it....");
        printItem(result.getItem());

    }
    catch (AmazonServiceException ase) {
        System.err.println("Failed to retrieve item in " + tableName);
    }

}
 
Example #17
Source File: TransactionItem.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the old copy of the item, with any item image saving specific attributes removed
 * 
 * @param rid
 * @return
 */
public Map<String, AttributeValue> loadItemImage(int rid) {
    txAssert(rid > 0, txId, "Expected rid > 0");
    
    Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(1);
    key.put(AttributeName.IMAGE_ID.toString(), new AttributeValue(txId + "#" + rid));
    
    Map<String, AttributeValue> item = txManager.getClient().getItem(new GetItemRequest()
        .withTableName(txManager.getItemImageTableName())
        .withKey(key)
        .withConsistentRead(true)).getItem();
    
    if(item != null) {
        item.remove(AttributeName.IMAGE_ID.toString());
    }
    
    return item;
}
 
Example #18
Source File: DynamoDBHelper.java    From amazon-kinesis-video-streams-parser-library with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the FragmentCheckpoint item from the table for the specified stream name.
 *
 * @param streamName Input stream name
 * @return FragmentCheckpoint entry. null if any exception is thrown.
 */
public Map<String, AttributeValue> getItem(final String streamName) {
    try {
        final Map<String,AttributeValue> key = new HashMap<>();
        key.put(KVS_STREAM_NAME, new AttributeValue().withS(streamName));
        final GetItemRequest getItemRequest = new GetItemRequest() {{
            setTableName(TABLE_NAME);
            setKey(key);
        }};

        return ddbClient.getItem(getItemRequest).getItem();
    } catch (final AmazonDynamoDBException e) {
        log.warn("Error while getting item from table!", e);
    }
    return null;
}
 
Example #19
Source File: ReadCommittedIsolationHandlerImpl.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
/**
 * Create a GetItemRequest for an item (in the event that you need to get the item again).
 * @param tableName The table that holds the item
 * @param item The item to get
 * @return the request
 */
protected GetItemRequest createGetItemRequest(
        final String tableName,
        final Map<String, AttributeValue> item) {
    Map<String, AttributeValue> key = txManager.createKeyMap(tableName, item);

    /*
     * Set the request to consistent read the next time around, since we may have read while locking tx
     * was cleaning up or read a stale item that is no longer locked
     */
    GetItemRequest request = new GetItemRequest()
            .withTableName(tableName)
            .withKey(key)
            .withConsistentRead(true);
    return request;
}
 
Example #20
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getItemNotExists() {
    Transaction t1 = manager.newTransaction();
    Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME);
    
    Map<String, AttributeValue> getResult1 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key1)).getItem();
    assertNull(getResult1);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, t1.getId(), true, false);
    
    Map<String, AttributeValue> getResult2 = t1.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key1)).getItem();
    assertNull(getResult2);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, t1.getId(), true, false);
    
    t1.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, false);
}
 
Example #21
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
@Test
public void getFilterAttributesToGet() {
    Transaction t1 = manager.newTransaction();
    
    Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>();
    item1.put("s_someattr", item0.get("s_someattr"));
    
    Map<String, AttributeValue> getResult1 = t1.getItem(new GetItemRequest()
        .withTableName(INTEG_HASH_TABLE_NAME)
        .withAttributesToGet("s_someattr", "notexists")
        .withKey(key0)).getItem();
    assertEquals(item1, getResult1);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key0, t1.getId(), false, false);
    
    t1.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key0, item0, true);
}
 
Example #22
Source File: RequestTest.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@Test
public void roundTripGetString() {
    GetItem r1 = new GetItem();
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put(HASH_ATTR_NAME, new AttributeValue("a"));
    r1.setRequest(new GetItemRequest()
        .withTableName(TABLE_NAME)
        .withKey(item));
    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 #23
Source File: TransactionDynamoDBFacade.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
private void checkExpectedValues(String tableName,
        Map<String, AttributeValue> itemKey,
        Map<String, ExpectedAttributeValue> expectedValues) {
    if (expectedValues != null && !expectedValues.isEmpty()) {
        for (Map.Entry<String, ExpectedAttributeValue> entry : expectedValues.entrySet()) {
            if ((entry.getValue().isExists() == null || entry.getValue().isExists() == true)
                    && entry.getValue().getValue() == null) {
                throw new IllegalArgumentException("An explicit value is required when Exists is null or true, "
                        + "but none was found in expected values for item with key " + itemKey +
                        ": " + expectedValues);
            }
        }

        // simulate by loading the item and checking the values;
        // this also has the effect of locking the item, which gives the
        // same behavior
        GetItemResult result = getItem(new GetItemRequest()
                .withAttributesToGet(expectedValues.keySet())
                .withKey(itemKey)
                .withTableName(tableName));
        Map<String, AttributeValue> item = result.getItem();
        try {
            checkExpectedValues(expectedValues, item);
        } catch (ConditionalCheckFailedException e) {
            throw new ConditionalCheckFailedException("Item " + itemKey + " had unexpected attributes: " + e.getMessage());
        }
    }
}
 
Example #24
Source File: TransactionManager.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
public GetItemResult getItem(GetItemRequest request, IsolationLevel isolationLevel) {
    if (request.getAttributesToGet() != null) {
        Set<String> attributesToGet = new HashSet<String>(request.getAttributesToGet());
        attributesToGet.addAll(Transaction.SPECIAL_ATTR_NAMES);
        request.setAttributesToGet(attributesToGet);
    }
    GetItemResult result = getClient().getItem(request);
    Map<String, AttributeValue> item = getReadIsolationHandler(isolationLevel).handleItem(result.getItem(), request.getAttributesToGet(), request.getTableName());
    Transaction.stripSpecialAttributes(item);
    result.setItem(item);
    return result;
}
 
Example #25
Source File: TransactionManagerDynamoDBFacade.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@Override
public GetItemResult getItem(String tableName,
        Map<String, AttributeValue> key, Boolean consistentRead)
        throws AmazonServiceException, AmazonClientException {
    return getItem(new GetItemRequest()
            .withTableName(tableName)
            .withKey(key)
            .withConsistentRead(consistentRead));
}
 
Example #26
Source File: TransactionManagerDynamoDBFacade.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@Override
public GetItemResult getItem(
        String tableName,
        Map<String, AttributeValue> key) throws AmazonServiceException, AmazonClientException {
    return getItem(new GetItemRequest()
            .withTableName(tableName)
            .withKey(key));
}
 
Example #27
Source File: TransactionItem.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches this transaction item from the tx table.  Uses consistent read.
 * 
 * @return the latest copy of the transaction item, or null if it has been completed (and deleted) 
 */
private Map<String, AttributeValue> get() {
    GetItemRequest getRequest = new GetItemRequest()
        .withTableName(txManager.getTransactionTableName())
        .withKey(txKey)
        .withConsistentRead(true);
    return txManager.getClient().getItem(getRequest).getItem();
}
 
Example #28
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 #29
Source File: TransactionDynamoDBFacade.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@Override
public GetItemResult getItem(String tableName,
        Map<String, AttributeValue> key, Boolean consistentRead)
        throws AmazonServiceException, AmazonClientException {
    return getItem(new GetItemRequest()
            .withTableName(tableName)
            .withKey(key)
            .withConsistentRead(consistentRead));
}
 
Example #30
Source File: TransactionsIntegrationTest.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
@Test
public void getItemAfterPutItemInsertInResumedTx() {
    Transaction t1 = new Transaction(UUID.randomUUID().toString(), manager, true) {
        @Override
        protected Map<String, AttributeValue> applyAndKeepLock(Request request, Map<String, AttributeValue> lockedItem) {
            throw new FailedYourRequestException();
        }  
    };
    
    Transaction t2 = manager.resumeTransaction(t1.getId());
    
    Map<String, AttributeValue> key1 = newKey(INTEG_HASH_TABLE_NAME);
    Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(key1);
    item1.put("asdf", new AttributeValue("wef"));
    
    try {
        // This Put needs to fail in apply
        t1.putItem(new PutItemRequest()
            .withTableName(INTEG_HASH_TABLE_NAME)
            .withItem(item1)
            .withReturnValues(ReturnValue.ALL_OLD)).getAttributes();
        fail();
    } catch (FailedYourRequestException e) {}
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, t1.getId(), true, false);
    
    // second copy of same tx
    Map<String, AttributeValue> getResult1 = t2.getItem(new GetItemRequest().withTableName(INTEG_HASH_TABLE_NAME).withKey(key1)).getItem();
    assertEquals(getResult1, item1);
    assertItemLocked(INTEG_HASH_TABLE_NAME, key1, item1, t1.getId(), true, true);
    
    t2.commit();
    
    assertItemNotLocked(INTEG_HASH_TABLE_NAME, key1, item1, true);
}