public boolean deleteAllItems(String tableName) throws DataAccessException {
    ScanRequest request = new ScanRequest().withTableName(tableName);
    boolean deleted = false;
    ScanResult result = ddb.scan(request);
    for (Map<String, AttributeValue> item : result.getItems()) {
      Key key = DynamoDBUtil.getIdKey(item);
      deleteItem(tableName, key);
      deleted = true;
    }

    // keep repeating until we get through all matched items
    Key lastKeyEvaluated = null;
    do {
      lastKeyEvaluated = result.getLastEvaluatedKey();
      if (lastKeyEvaluated != null) {
        request = new ScanRequest(tableName).withExclusiveStartKey(lastKeyEvaluated);
        result = ddb.scan(request);
        for (Map<String, AttributeValue> item : result.getItems()) {
          Key key = DynamoDBUtil.getIdKey(item);
          deleteItem(tableName, key);
          deleted = true;
        }
      }
    } while (lastKeyEvaluated != null);

    return deleted;
  }
 private Map<String, AttributeValue> getConsistentInternal(
     String tableName, Key key, int attempt) {
   GetItemRequest request = new GetItemRequest(tableName, key);
   request.setConsistentRead(true);
   try {
     GetItemResult result = ddb.getItem(request);
     Map<String, AttributeValue> attributes = result.getItem();
     if (attributes == null || attributes.isEmpty()) {
       return null;
     }
     return attributes;
   } catch (AmazonServiceException e) {
     if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
       throw new IllegalArgumentException("no such table: " + tableName, e);
     } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
       // retry after a small pause
       DynamoDBUtil.sleepBeforeRetry(attempt);
       attempt++;
       return getConsistentInternal(tableName, key, attempt);
     } else {
       throw new DataStoreOperationException(
           "problem with table: " + tableName + ", key: " + key, e);
     }
   }
 }
 private void updateItemVersionedInternal(
     String tableName,
     Key key,
     Map<String, AttributeValueUpdate> attributes,
     String expectedVersion,
     PersistentEntity persistentEntity,
     int attempt)
     throws DataAccessException {
   UpdateItemRequest request =
       new UpdateItemRequest(tableName, key, attributes)
           .withExpected(getOptimisticVersionCondition(expectedVersion));
   try {
     ddb.updateItem(request);
   } catch (AmazonServiceException e) {
     if (DynamoDBUtil.AWS_ERR_CODE_CONDITIONAL_CHECK_FAILED.equals(e.getErrorCode())) {
       throw new OptimisticLockingException(persistentEntity, key);
     } else if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
       throw new IllegalArgumentException("no such table: " + tableName, e);
     } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
       // retry after a small pause
       DynamoDBUtil.sleepBeforeRetry(attempt);
       attempt++;
       updateItemVersionedInternal(
           tableName, key, attributes, expectedVersion, persistentEntity, attempt);
     } else {
       throw new DataStoreOperationException(
           "problem with table: " + tableName + ", key: " + key + ", attributes: " + attributes,
           e);
     }
   }
 }
 private void deleteItemInternal(String tableName, Key key, int attempt) {
   DeleteItemRequest request = new DeleteItemRequest(tableName, key);
   try {
     ddb.deleteItem(request);
   } catch (AmazonServiceException e) {
     if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
       throw new IllegalArgumentException("no such table: " + tableName, e);
     } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
       // retry after a small pause
       DynamoDBUtil.sleepBeforeRetry(attempt);
       attempt++;
       deleteItemInternal(tableName, key, attempt);
     } else {
       throw new DataStoreOperationException(
           "problem with table: " + tableName + ", key: " + key, e);
     }
   }
 }
  private List<Map<String, AttributeValue>> scanInternal(
      String tableName, Map<String, Condition> filter, int max, int attempt) {
    LinkedList<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>();
    try {
      ScanRequest request = new ScanRequest(tableName).withScanFilter(filter);
      ScanResult result = ddb.scan(request);
      items.addAll(result.getItems());

      // keep repeating until we get through all matched items
      Key lastKeyEvaluated = null;
      do {
        lastKeyEvaluated = result.getLastEvaluatedKey();
        if (lastKeyEvaluated != null) {
          request =
              new ScanRequest(tableName)
                  .withScanFilter(filter)
                  .withExclusiveStartKey(lastKeyEvaluated);
          result = ddb.scan(request);
          items.addAll(result.getItems());
        }
      } while (lastKeyEvaluated != null && items.size() < max);

      // truncate if needed
      while (items.size() > max) {
        items.removeLast();
      }

      return items;
    } catch (AmazonServiceException e) {
      if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
        throw new IllegalArgumentException("no such table: " + tableName, e);
      } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
        // retry after a small pause
        DynamoDBUtil.sleepBeforeRetry(attempt);
        attempt++;
        return scanInternal(tableName, filter, max, attempt);
      } else {
        throw new DataStoreOperationException(
            "problem with table: " + tableName + ", filter: " + filter, e);
      }
    }
  }
 private void putItemInternal(
     String tableName, Map<String, AttributeValue> attributes, int attempt)
     throws DataAccessException {
   try {
     PutItemRequest request = new PutItemRequest(tableName, attributes);
     ddb.putItem(request);
   } catch (AmazonServiceException e) {
     if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
       throw new IllegalArgumentException("no such table: " + tableName, e);
     } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) {
       // retry after a small pause
       DynamoDBUtil.sleepBeforeRetry(attempt);
       attempt++;
       putItemInternal(tableName, attributes, attempt);
     } else {
       throw new DataStoreOperationException(
           "problem with table: " + tableName + ", attributes: " + attributes, e);
     }
   }
 }