diff --git a/README.md b/README.md index ca32bc0..0a4e5ee 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ To get the binaries of this library as distributed by Microsoft, ready for use w com.microsoft.azure azure-documentdb - 0.9.0 + 0.9.1 ###Option 2: Source Via Git diff --git a/pom.xml b/pom.xml index 0e5b617..c70b874 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.microsoft.azure azure-documentdb - 0.9.0 + 0.9.1 ${project.groupId}:${project.artifactId} Java SDK for Microsoft Azure DocumentDB http://azure.microsoft.com/en-us/services/documentdb/ diff --git a/src/com/microsoft/azure/documentdb/HttpConstants.java b/src/com/microsoft/azure/documentdb/HttpConstants.java index 806b7b0..9cb8f90 100644 --- a/src/com/microsoft/azure/documentdb/HttpConstants.java +++ b/src/com/microsoft/azure/documentdb/HttpConstants.java @@ -153,7 +153,7 @@ public static class Versions { public static String CURRENT_VERSION = "2014-08-21"; - public static String USER_AGENT = "documentdb-java-sdk-0.9.0"; + public static String USER_AGENT = "documentdb-java-sdk-0.9.1"; } public static class StatusCodes { diff --git a/src/com/microsoft/azure/documentdb/QueryIterable.java b/src/com/microsoft/azure/documentdb/QueryIterable.java index 4ed1c7d..5b398b8 100644 --- a/src/com/microsoft/azure/documentdb/QueryIterable.java +++ b/src/com/microsoft/azure/documentdb/QueryIterable.java @@ -6,7 +6,6 @@ import java.util.Map; import java.util.NoSuchElementException; -import org.apache.commons.lang3.StringUtils; /** * @@ -21,6 +20,7 @@ public class QueryIterable implements Iterable { private DocumentServiceRequest request = null; private ReadType readType; private Class classT; + private String initialContinuation = null; private String continuation = null; private boolean hasStarted = false; private List items = new ArrayList(); @@ -38,6 +38,14 @@ public class QueryIterable implements Iterable { this.request = request; this.readType = readType; this.classT = classT; + + if (this.request != null && this.request.getHeaders() != null) { + String continuationToken = this.request.getHeaders().get(HttpConstants.HttpHeaders.CONTINUATION); + if (!QueryIterable.isNullEmptyOrFalse(continuationToken)) { + this.initialContinuation = continuationToken; + } + } + this.reset(); } @@ -141,16 +149,10 @@ public List toList() { */ public void reset() { this.hasStarted = false; - this.continuation = null; + this.continuation = this.initialContinuation; this.items = new ArrayList(); this.currentIndex = 0; this.hasNext = true; - if (this.request != null && this.request.getHeaders() != null) { - String continuationToken = this.request.getHeaders().get(HttpConstants.HttpHeaders.CONTINUATION); - if (!StringUtils.isBlank(continuationToken)) { - this.continuation = continuationToken; - } - } } /** @@ -164,17 +166,17 @@ public List fetchNextBlock() DocumentServiceResponse response = null; List fetchedItems = null; - while (!this.isNullEmptyOrFalse(this.continuation) || !this.hasStarted) { - if (!this.isNullEmptyOrFalse(this.continuation)) { - request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION, this.continuation); + while (!QueryIterable.isNullEmptyOrFalse(this.continuation) || !this.hasStarted) { + if (!QueryIterable.isNullEmptyOrFalse(this.continuation)) { + this.request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION, this.continuation); } else { - request.getHeaders().remove(HttpConstants.HttpHeaders.CONTINUATION); + this.request.getHeaders().remove(HttpConstants.HttpHeaders.CONTINUATION); } if (this.readType == ReadType.Feed) { - response = this.client.doReadFeed(request); + response = this.client.doReadFeed(this.request); } else { - response = this.client.doQuery(request); + response = this.client.doQuery(this.request); } // A retriable exception may happen. "this.hasStarted" and "this.continuation" must not be set @@ -198,7 +200,7 @@ public List fetchNextBlock() return fetchedItems; } - private boolean isNullEmptyOrFalse(String s) { + private static boolean isNullEmptyOrFalse(String s) { return s == null || s.isEmpty() || s == "false" || s == "False"; } } diff --git a/src/com/microsoft/azure/documentdb/test/GatewayTests.java b/src/com/microsoft/azure/documentdb/test/GatewayTests.java index 74698ad..7c99f2d 100644 --- a/src/com/microsoft/azure/documentdb/test/GatewayTests.java +++ b/src/com/microsoft/azure/documentdb/test/GatewayTests.java @@ -18,6 +18,7 @@ import org.junit.Test; import org.junit.rules.TestRule; import org.junit.runner.Description; +import org.junit.runners.model.Statement; import com.microsoft.azure.documentdb.AccessCondition; import com.microsoft.azure.documentdb.AccessConditionType; @@ -38,6 +39,7 @@ import com.microsoft.azure.documentdb.MediaReadMode; import com.microsoft.azure.documentdb.Permission; import com.microsoft.azure.documentdb.PermissionMode; +import com.microsoft.azure.documentdb.QueryIterable; import com.microsoft.azure.documentdb.RequestOptions; import com.microsoft.azure.documentdb.ResourceResponse; import com.microsoft.azure.documentdb.StoredProcedure; @@ -337,9 +339,12 @@ public void testCollectionCrud() throws DocumentClientException { @Test public void testQueryIterableCrud() throws DocumentClientException { - DocumentClient client = new DocumentClient(HOST, MASTER_KEY, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session); - List documents = client.readDocuments(this.collectionForTest.getSelfLink(), null).getQueryIterable().toList(); - int beforeCreateDocumentsCount = documents.size(); + DocumentClient client = new DocumentClient(HOST, + MASTER_KEY, + ConnectionPolicy.GetDefault(), + ConsistencyLevel.Session); + List documents = client.readDocuments(this.collectionForTest.getSelfLink(), + null).getQueryIterable().toList(); final int numOfDocuments = 10; // Create 10 documents. @@ -348,9 +353,9 @@ public void testQueryIterableCrud() throws DocumentClientException { client.createDocument(this.collectionForTest.getSelfLink(), documentDefinition, null, false); } - int noOfDocumentsPerPage = numOfDocuments / 5; + int numOfDocumentsPerPage = numOfDocuments / 5; FeedOptions fo = new FeedOptions(); - fo.setPageSize(noOfDocumentsPerPage); + fo.setPageSize(numOfDocumentsPerPage); FeedResponse feedResponse; int i = 0; String continuationToken = null; @@ -365,7 +370,7 @@ public void testQueryIterableCrud() throws DocumentClientException { for (Document document : feedResponse.getQueryIterable()) { i++; currentPage.add(document.getId()); - if (i == noOfDocumentsPerPage) { + if (i == numOfDocumentsPerPage) { break; } } @@ -385,7 +390,21 @@ public void testQueryIterableCrud() throws DocumentClientException { } while (continuationToken != null); documents = client.readDocuments(this.collectionForTest.getSelfLink(), null).getQueryIterable().toList(); - Assert.assertEquals(beforeCreateDocumentsCount + numOfDocuments, documents.size()); + Assert.assertEquals(numOfDocuments, documents.size()); + + // Test fetch next block. + fo = new FeedOptions(); + fo.setPageSize(6); + + QueryIterable queryItr = + client.readDocuments(this.collectionForTest.getSelfLink(), fo).getQueryIterable(); + Assert.assertEquals(6, queryItr.fetchNextBlock().size()); + Assert.assertEquals(4, queryItr.fetchNextBlock().size()); + + // Reset the query iterable. + queryItr.reset(); + Assert.assertEquals(6, queryItr.fetchNextBlock().size()); + Assert.assertEquals(4, queryItr.fetchNextBlock().size()); } @Test