Skip to content

Commit

Permalink
PageRequest should support having a null next key docId : the Page Li…
Browse files Browse the repository at this point in the history
…nk JSON String is now generated in a way that supports a null docId even when the key is not null. This bug made asJson() method to fail when docId was null. Also, as toString() was invoking asJson(), this made the debugging harder, so the toString() method should not invoke asJson() but rather basically print the Object's properties.
  • Loading branch information
YannRobert committed Oct 15, 2014
1 parent ad1c58f commit 582e65f
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 35 deletions.
85 changes: 57 additions & 28 deletions org.ektorp/src/main/java/org/ektorp/PageRequest.java
Expand Up @@ -15,7 +15,8 @@
*/
public class PageRequest {

private static final String NEXT_KEY_FIELD_NAME = "k";
private static final String NEXT_KEY_FIELD_NAME = "key";
private static final String NEXT_DOCID_FIELD_NAME = "id";
private static final String PAGE_SIZE_FIELD_NAME = "s";
private static final String BACK_FIELD_NAME = "b";
private static final String PAGE_FIELD_NAME = "p";
Expand All @@ -27,7 +28,6 @@ public class PageRequest {
private final boolean back;
private final int page;

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DB_DUPLICATE_BRANCHES")
public static ViewQuery applyPagingParameters(ViewQuery q, PageRequest pr) {
ViewQuery pagedQuery = q.clone();
if (pr.page > 0) {
Expand All @@ -39,11 +39,10 @@ public static ViewQuery applyPagingParameters(ViewQuery q, PageRequest pr) {
}
if (pr.back) {
pagedQuery.descending(!pagedQuery.isDescending());
}
}
}

int offset = pr.back ? 1 : 1;
pagedQuery.limit(pr.getPageSize() + offset);
int additionalRowsToQuery = 1;
pagedQuery.limit(pr.getPageSize() + additionalRowsToQuery);
return pagedQuery;
}

Expand Down Expand Up @@ -79,12 +78,24 @@ public static PageRequest fromLink(String link) {
JsonNode n = MAPPER.readTree(new ByteArrayInputStream(Base64
.decode(link, Base64.URL_SAFE)));

KeyIdPair key = parseNextKey(n);
JsonNode keyNode = n.get(NEXT_KEY_FIELD_NAME);
JsonNode docIdNode = n.get(NEXT_DOCID_FIELD_NAME);
String docId = null;
if (docIdNode != null) {
docId = docIdNode.asText();
}

KeyIdPair keyIdPair;
if (keyNode != null || docId != null) {
keyIdPair = new KeyIdPair(keyNode, docId);
} else {
keyIdPair = null;
}
int pageSize = n.get(PAGE_SIZE_FIELD_NAME).intValue();
boolean back = n.get(BACK_FIELD_NAME).asInt() == 1;
int page = n.get(PAGE_FIELD_NAME).asInt();
return new Builder()
.nextKey(key)
.nextKey(keyIdPair)
.pageSize(pageSize)
.back(back)
.page(page)
Expand All @@ -94,22 +105,6 @@ public static PageRequest fromLink(String link) {
}
}

private static KeyIdPair parseNextKey(JsonNode n) {
return parseKey(NEXT_KEY_FIELD_NAME, n);
}

private static KeyIdPair parseKey(String fieldName, JsonNode n) {
KeyIdPair key;
JsonNode nextKey = n.get(fieldName);
if (nextKey != null) {
String docId = nextKey.fieldNames().next();
key = new KeyIdPair(nextKey.get(docId), docId);
} else {
key = null;
}
return key;
}

public String asLink() {
try {
return Base64.encodeBytes(MAPPER.writeValueAsBytes(asJson()),
Expand All @@ -122,7 +117,12 @@ public String asLink() {
public JsonNode asJson() {
ObjectNode n = MAPPER.createObjectNode();
if (nextKey != null) {
n.putObject(NEXT_KEY_FIELD_NAME).put(nextKey.docId, nextKey.key);
if (nextKey.key != null) {
n.put(NEXT_KEY_FIELD_NAME, nextKey.key);
}
if (nextKey.docId != null) {
n.put(NEXT_DOCID_FIELD_NAME, nextKey.docId);
}
}
n.put(PAGE_SIZE_FIELD_NAME, pageSize);
n.put(BACK_FIELD_NAME, back ? 1 : 0);
Expand Down Expand Up @@ -160,7 +160,14 @@ public boolean isBack() {

@Override
public String toString() {
return asJson().toString();
StringBuilder builder = new StringBuilder();
builder.append(this.getClass().getName()).append("(");
builder.append("pageSize=").append(pageSize);
builder.append(",page=").append(page);
builder.append(",back=").append(back);
builder.append(",nextKey=").append(nextKey);
builder.append(")");
return builder.toString();
}


Expand Down Expand Up @@ -252,8 +259,21 @@ public int getNextPage() {
public int getPrevPage() {
return page - 1;
}
}


@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(this.getClass().getName()).append("(");
builder.append("pageSize=").append(pageSize);
builder.append(",page=").append(page);
builder.append(",back=").append(back);
builder.append(",nextKey=").append(nextKey);
builder.append(")");
return builder.toString();
}

}

private static final class KeyIdPair {
final JsonNode key;
final String docId;
Expand Down Expand Up @@ -294,6 +314,15 @@ public boolean equals(Object obj) {
return true;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(this.getClass().getName()).append("(");
builder.append("key=").append(key);
builder.append(",docId=").append(docId);
builder.append(")");
return builder.toString();
}

}

Expand Down
90 changes: 83 additions & 7 deletions org.ektorp/src/test/java/org/ektorp/PageRequestTest.java
@@ -1,27 +1,103 @@
package org.ektorp;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class PageRequestTest {

@Test
public void testFromLink() {
PageRequest pl = PageRequest
.firstPage(5)
.nextRequest("exampleKey", "65f996f8b1024f00a81ef10264459a1d")
.page(1).build();
.firstPage(5)
.nextRequest("exampleKey", "65f996f8b1024f00a81ef10264459a1d")
.page(1).build();

String link = pl.asLink();
assertEquals(pl, PageRequest.fromLink(link));
}

@Test
public void testFromLink2() {
PageRequest pl = PageRequest.firstPage(5);
String link = pl.asLink();
assertEquals(pl, PageRequest.fromLink(link));
}

@Test
public void testFromLinkWithNullDocId() {
PageRequest pl = PageRequest
.firstPage(5)
.nextRequest("exampleKey", null)
.page(1).build();

String link = pl.asLink();
assertEquals(pl, PageRequest.fromLink(link));
}

@Test
public void testFromLinkWithNullKey() {
PageRequest item = PageRequest
.firstPage(5)
.nextRequest(null, "65f996f8b1024f00a81ef10264459a1d")
.page(1).build();
assertEquals("org.ektorp.PageRequest(pageSize=5,page=1,back=false,nextKey=org.ektorp.PageRequest$KeyIdPair(key=null,docId=65f996f8b1024f00a81ef10264459a1d))", item.toString());
}

@Test
public void shouldToStringWorkWhenOnlyFirstPageIsSet() {
PageRequest item = PageRequest.firstPage(5);
assertEquals("org.ektorp.PageRequest(pageSize=5,page=0,back=false,nextKey=null)", item.toString());
}

@Test
public void shouldToStringWorkWithKeyAndDocId() {
PageRequest item = PageRequest
.firstPage(5)
.nextRequest("exampleKey", "65f996f8b1024f00a81ef10264459a1d")
.page(1).build();
assertEquals("org.ektorp.PageRequest(pageSize=5,page=1,back=false,nextKey=org.ektorp.PageRequest$KeyIdPair(key=\"exampleKey\",docId=65f996f8b1024f00a81ef10264459a1d))", item.toString());
}

@Test
public void shouldToStringWorkWithKeyButNullDocId() {
PageRequest item = PageRequest
.firstPage(5)
.nextRequest("exampleKey", null)
.page(1).build();
assertEquals("org.ektorp.PageRequest(pageSize=5,page=1,back=false,nextKey=org.ektorp.PageRequest$KeyIdPair(key=\"exampleKey\",docId=null))", item.toString());
}

@Test
public void shouldToJsonWorkWhenOnlyFirstPageIsSet() {
PageRequest item = PageRequest.firstPage(5);
assertEquals("{\"s\":5,\"b\":0,\"p\":0}", item.asJson().toString());
}

@Test
public void shouldToJsonWorkWithKeyAndDocId() {
PageRequest item = PageRequest
.firstPage(5)
.nextRequest("exampleKey", "65f996f8b1024f00a81ef10264459a1d")
.page(1).build();
assertEquals("{\"key\":\"exampleKey\",\"id\":\"65f996f8b1024f00a81ef10264459a1d\",\"s\":5,\"b\":0,\"p\":1}", item.asJson().toString());
}

@Test
public void shouldToJsonWorkWithKeyButNullDocId() {
PageRequest item = PageRequest
.firstPage(5)
.nextRequest("exampleKey", null)
.page(1).build();
assertEquals("{\"key\":\"exampleKey\",\"s\":5,\"b\":0,\"p\":1}", item.asJson().toString());
}

@Test
public void shouldReadPageLink() {
String pageLink = "eyJrZXkiOiIyMDE0LTEwLTA3VDEwOjI0OjU0LjAwMCswMDAwIiwiaWQiOiJmYTNjZjVlNjQyYTc5Zjg4NDI1ODg1NzcxZjQwMjZkZSIsInMiOjMwLCJiIjoxLCJwIjoyfQ==";
PageRequest item = PageRequest.fromLink(pageLink);
assertEquals("org.ektorp.PageRequest(pageSize=30,page=2,back=true,nextKey=org.ektorp.PageRequest$KeyIdPair(key=\"2014-10-07T10:24:54.000+0000\",docId=fa3cf5e642a79f88425885771f4026de))", item.toString());
}

}

0 comments on commit 582e65f

Please sign in to comment.