aws-sdk-java icon indicating copy to clipboard operation
aws-sdk-java copied to clipboard

dynamodbv2 document api: QueryOutcome's ItemCollection count is incorrect

Open triton3 opened this issue 11 years ago • 3 comments

The ItemCollection.getTotalCount() does not indicate the correct number of results obtained from a query. Am I wrong in thinking that this function returns the number of items that were fetched from a table after querying?

Scenario When using QuerySpec to query an item, the ItemCollection.getTotalCount() function returns 0 count. However, printing out the query results in the next step shows 1 item was in fact retrieved from the table. See example code below:

Code

    Table UsersTable = dynamoDB.getTable("Users");
    QuerySpec spec = new QuerySpec()
            .withProjectionExpression("name, email, address")
            .withKeyConditionExpression("EMail = :v_email")
            .withValueMap(new ValueMap().withString(":v_email", "[email protected]"))
            .withConsistentRead(true);

    ItemCollection<QueryOutcome> items = UsersTable.query(spec);
    int count = items.getTotalCount();               // --> HERE COUNT = 0

    System.out.println("Printing item after querying it....");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty()); // --> Prints out query result correctly
        }

triton3 avatar Oct 27 '15 20:10 triton3

The getTotalCount() method doesn't return the actual total count for the query. Instead, it returns the count of currently fetched items. As SDK is using a lazy fetch mechanism, before you fetch any results yet, the cursor for the iterator is pointing before the first page. If you print getTotalCount() after the iteration, it should be 1 in your specific case above. You can take a look at the javadoc for ItemCollection class, http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html. I quoted as below.

"An ItemCollection object maintains a cursor pointing to its current pages of data. Initially the cursor is positioned before the first page. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ItemCollection object, it can be used in a while loop to iterate through the collection. Network calls can be triggered when the collection is iterated across page boundaries."

zhangzhx avatar Oct 28 '15 20:10 zhangzhx

The getTotalCount() method doesn't return the actual total count for the query.

Would it be worth @deprecating it and adding a method with a name that more accurately represents what it does?

fernomac avatar Oct 29 '15 00:10 fernomac

Thank you for the clarification.

triton3 avatar Oct 29 '15 19:10 triton3