Exemplo n.º 1
0
  @Test
  public void queryWithHashKey() {
    // Setup table with items
    KeySchema schema =
        new KeySchema(
            new KeySchemaElement()
                .withAttributeName("id")
                .withAttributeType(ScalarAttributeType.S));
    createTable(tableName, schema);
    AttributeValue hashKey = createStringAttribute();
    getClient()
        .putItem(new PutItemRequest().withItem(createGenericItem()).withTableName(tableName));
    getClient()
        .putItem(
            new PutItemRequest().withItem(createGenericItem(hashKey)).withTableName(tableName));
    getClient()
        .putItem(new PutItemRequest().withItem(createGenericItem()).withTableName(tableName));
    getClient()
        .putItem(new PutItemRequest().withItem(createGenericItem()).withTableName(tableName));

    QueryRequest request = new QueryRequest(tableName, hashKey);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull(result.getItems());
    Assert.assertNotSame(result.getItems().size(), 0);
    Assert.assertEquals(result.getItems().get(0).get("id"), hashKey);
  }
  @Test
  public void queryUpdate() {
    // Setup table with items
    KeySchema schema =
        new KeySchema(
            new KeySchemaElement()
                .withAttributeName("id")
                .withAttributeType(ScalarAttributeType.S));
    createTable(tableName, schema);

    Key key = new Key();
    key.setHashKeyElement(new AttributeValue().withS("1"));

    Map<String, AttributeValueUpdate> dynValues = new HashMap<String, AttributeValueUpdate>();
    dynValues.put(
        "count", new AttributeValueUpdate(new AttributeValue().withN("100"), AttributeAction.ADD));

    UpdateItemRequest update = new UpdateItemRequest(tableName, key, dynValues);

    getClient().updateItem(update);

    inProcessClient.save(PERSISTENCE_PATH);
    createNewInProcessClient().restore(PERSISTENCE_PATH);

    AttributeValue hashKey = new AttributeValue().withS("1");
    QueryRequest request = new QueryRequest(tableName, hashKey);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull(result.getItems());
    Assert.assertNotSame(result.getItems().size(), 0);
    Map<String, AttributeValue> row = result.getItems().get(0);
    Assert.assertEquals(row.get("id"), hashKey);
    Assert.assertEquals(row.get("count").getN(), "100");
  }
  @Test
  public void conditionalDeleteOldFieldUpdateOnHit() {
    // Setup table with items
    KeySchema schema =
        new KeySchema(
            new KeySchemaElement()
                .withAttributeName("id")
                .withAttributeType(ScalarAttributeType.S));
    createTable(tableName, schema);

    Key key = new Key();
    key.setHashKeyElement(new AttributeValue().withS("1"));

    Map<String, AttributeValueUpdate> oldValues = new HashMap<String, AttributeValueUpdate>();
    oldValues.put(
        "count", new AttributeValueUpdate(new AttributeValue().withN("100"), AttributeAction.PUT));
    oldValues.put(
        "ids",
        new AttributeValueUpdate(new AttributeValue().withS("[er, er]"), AttributeAction.ADD));

    UpdateItemRequest update = new UpdateItemRequest(tableName, key, oldValues);

    getClient().updateItem(update);

    // conditional update
    HashMap<String, AttributeValueUpdate> newValues = new HashMap<String, AttributeValueUpdate>();
    newValues.put(
        "count", new AttributeValueUpdate(new AttributeValue().withN("102"), AttributeAction.PUT));
    newValues.put(
        "ids",
        new AttributeValueUpdate(new AttributeValue().withS("[er, er]"), AttributeAction.DELETE));

    HashMap<String, ExpectedAttributeValue> expectedValues =
        new HashMap<String, ExpectedAttributeValue>();
    expectedValues.put(
        "count", new ExpectedAttributeValue().withValue(new AttributeValue().withN("100")));

    update =
        new UpdateItemRequest()
            .withTableName(tableName)
            .withKey(key)
            .withAttributeUpdates(newValues)
            .withExpected(expectedValues);
    getClient().updateItem(update);

    inProcessClient.save(PERSISTENCE_PATH);
    createNewInProcessClient().restore(PERSISTENCE_PATH);

    AttributeValue hashKey = new AttributeValue().withS("1");
    QueryRequest request = new QueryRequest(tableName, hashKey);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull(result.getItems());
    Assert.assertNotSame(result.getItems().size(), 0);
    Map<String, AttributeValue> row = result.getItems().get(0);
    Assert.assertEquals(row.get("id"), hashKey);
    Assert.assertEquals(row.get("count").getN(), "102");
    Assert.assertNull(row.get("ids"));
  }
  @Override
  protected void executeQuery() {
    if (LOG.isDebugEnabled()) {
      LOG.debug(
          String.format(
              "Querying table: %s from ExclusiveStartKey: %s", getTableName(), getLastKey()));
    }

    if (getLastKey() != null) {
      queryRequest.setExclusiveStartKey(getLastKey());
    }

    QueryResult result = getClient().query(queryRequest);
    setLastKey(result.getLastEvaluatedKey());
    setIterator(result.getItems().iterator());
  }
Exemplo n.º 5
0
  @Test
  public void queryWithHashKeyAndNumericRangeKeyConditionBETWEENTest() {
    AttributeValue hashKey = setupNumericRangeTableWithSeveralItems();

    QueryRequest request = new QueryRequest().withTableName(tableName).withHashKeyValue(hashKey);

    Condition rangeKeyCondition = new Condition();
    List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>();
    attributeValueList.add(createNumberAttribute(2));
    attributeValueList.add(createNumberAttribute(3));
    rangeKeyCondition.setAttributeValueList(attributeValueList);
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.BETWEEN);
    request.setRangeKeyCondition(rangeKeyCondition);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull("Null result.", result);
    Assert.assertNotNull("No items returned.", result.getItems());
    Assert.assertEquals("Should return two items.", 2, result.getItems().size());
  }
Exemplo n.º 6
0
  @Test
  public void queryWithHashKeyAndNumericRangeKeyConditionGETest() {
    AttributeValue hashKey = setupNumericRangeTableWithSeveralItems();

    QueryRequest request = new QueryRequest().withTableName(tableName).withHashKeyValue(hashKey);

    Condition rangeKeyCondition = new Condition();
    List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>();
    attributeValueList.add(createNumberAttribute(2));
    rangeKeyCondition.setAttributeValueList(attributeValueList);
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.GE);
    request.setRangeKeyCondition(rangeKeyCondition);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull("Null result.", result);
    Assert.assertNotNull("No items returned.", result.getItems());
    // NOTE: GE is currently a string comparison, so "11" is NOT > "2".
    Assert.assertEquals("Should return four items.", 4, result.getItems().size());
  }
Exemplo n.º 7
0
  @Test
  public void queryWithHashKeyAndNumericRangeKeyConditionEQTest() {
    AttributeValue hashKey = setupNumericRangeTableWithSeveralItems();

    QueryRequest request = new QueryRequest().withTableName(tableName).withHashKeyValue(hashKey);

    Condition rangeKeyCondition = new Condition();
    List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>();
    attributeValueList.add(createNumberAttribute(2));
    rangeKeyCondition.setAttributeValueList(attributeValueList);
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.EQ);
    request.setRangeKeyCondition(rangeKeyCondition);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull("Null result.", result);
    Assert.assertNotNull("No items returned.", result.getItems());
    Assert.assertEquals("Should return one item.", 1, result.getItems().size());
    for (Map<String, AttributeValue> item : result.getItems()) {
      Assert.assertEquals(item.get("range").getN(), "2");
    }
  }
Exemplo n.º 8
0
  @Test
  public void queryWithHashKeyAndRangeKeyConditionCONTAINSTest() {
    AttributeValue hashKey = setupTableWithSeveralItems();

    QueryRequest request = new QueryRequest().withTableName(tableName).withHashKeyValue(hashKey);

    Condition rangeKeyCondition = new Condition();
    List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>();
    attributeValueList.add(new AttributeValue().withS("ange1"));
    rangeKeyCondition.setAttributeValueList(attributeValueList);
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.CONTAINS);
    request.setRangeKeyCondition(rangeKeyCondition);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull("Null result.", result);
    Assert.assertNotNull("No items returned.", result.getItems());
    Assert.assertEquals("Should return two items.", 2, result.getItems().size());
    for (Map<String, AttributeValue> item : result.getItems()) {
      Assert.assertTrue(item.get("range").getS().contains("ange1"));
    }
  }
Exemplo n.º 9
0
  @Test
  public void queryWithHashKeyAndNumericRangeKeyConditionCONTAINSTest() {
    AttributeValue hashKey = setupNumericRangeTableWithSeveralItems();

    QueryRequest request = new QueryRequest().withTableName(tableName).withHashKeyValue(hashKey);

    Condition rangeKeyCondition = new Condition();
    List<AttributeValue> attributeValueList = new ArrayList<AttributeValue>();
    attributeValueList.add(createNumberAttribute(1));
    rangeKeyCondition.setAttributeValueList(attributeValueList);
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.CONTAINS);
    request.setRangeKeyCondition(rangeKeyCondition);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull("Null result.", result);
    Assert.assertNotNull("No items returned.", result.getItems());
    // NOTE: CONTAINS is currently a string comparison, so "1", "11", "51" all contain "1".
    Assert.assertEquals("Should return three items.", 3, result.getItems().size());
    for (Map<String, AttributeValue> item : result.getItems()) {
      Assert.assertTrue(item.get("range").getN().contains("1"));
    }
  }
Exemplo n.º 10
0
  @Test
  public void queryWithHashKeyAndAttributesToGetTest() {
    AttributeValue hashKey = setupTableWithSeveralItems();

    QueryRequest request = new QueryRequest().withTableName(tableName).withHashKeyValue(hashKey);

    List<String> attrToGet = new ArrayList<String>();
    attrToGet.add("range");
    attrToGet.add("attr1");
    attrToGet.add("bogusAttr");
    request.setAttributesToGet(attrToGet);
    QueryResult result = getClient().query(request);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.getItems());
    Assert.assertNotNull(result.getItems().get(0));
    Assert.assertFalse(
        "First item should not contain 'id'.", result.getItems().get(0).containsKey("id"));
    Assert.assertTrue("First item missing 'range'.", result.getItems().get(0).containsKey("range"));
    Assert.assertTrue("First item missing 'attr1'.", result.getItems().get(0).containsKey("attr1"));
    Assert.assertFalse(
        "First item should not contain 'bogusAttr'.",
        result.getItems().get(0).containsKey("bogusAttr"));
  }