helun/Ektorp — Issue #67: Issues with paging backwards
Author: johnrodey
Created: 2012-02-21 17:04:52+00:00
State: open

1) Cannot page back to page 1.
2) Using links behaves differently from using the page request directly.

This sample code illustrates how page 2 repeats again instead of paging back to page 1

public void simpleTest1() {
try {
ViewQuery query = new ViewQuery();
query.viewName("byUserName");
query.designDocId("_design/MyDocument");
query.key("client_a");

CouchDbConnector dbConnector = new StdCouchDbConnector("mydb", new StdCouchDbInstance(new StdHttpClient.Builder().url("http://localhost:5984").build()));

PageRequest pageRequest = PageRequest.firstPage(5);

Page page = dbConnector.queryForPage(query, pageRequest, JsonNode.class);
List list = page.getRows();
System.out.println("Page 1");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, page.getNextPageRequest(), JsonNode.class);
list = page.getRows();
System.out.println("Page 2");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, page.getNextPageRequest(), JsonNode.class);
list = page.getRows();
System.out.println("Page 3");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, page.getNextPageRequest(), JsonNode.class);
list = page.getRows();
System.out.println("Page 4");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, page.getPreviousPageRequest(), JsonNode.class);
list = page.getRows();
System.out.println("Page 3");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, page.getPreviousPageRequest(), JsonNode.class);
list = page.getRows();
System.out.println("Page 2");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, page.getPreviousPageRequest(), JsonNode.class);
list = page.getRows();
System.out.println("Page 1");
for (JsonNode node : list) {
System.out.println(node);
}

} catch (Exception e) {
e.printStackTrace();
}
}

OUTPUTS:
Page 1
{"_id":"1329759612408"}
{"_id":"1329759622144"}
{"_id":"1329759622300"}
{"_id":"1329759622456"}
{"_id":"1329759622612"}
Page 2
{"_id":"1329759622752"}
{"_id":"1329759622908"}
{"_id":"1329759623064"}
{"_id":"1329759623252"}
{"_id":"1329759623361"}
Page 3
{"_id":"1329759623517"}
{"_id":"1329759623658"}
{"_id":"1329759623829"}
{"_id":"1329759624345"}
{"_id":"1329759624532"}
Page 4
{"_id":"1329759624704"}
{"_id":"1329759624876"}
{"_id":"1329759625032"}
Page 3
{"_id":"1329759623517"}
{"_id":"1329759623658"}
{"_id":"1329759623829"}
{"_id":"1329759624345"}
{"_id":"1329759624532"}
Page 2
{"_id":"1329759622752"}
{"_id":"1329759622908"}
{"_id":"1329759623064"}
{"_id":"1329759623252"}
{"_id":"1329759623361"}
Page 1
{"_id":"1329759622752"} //REPEAT ISSUE
{"_id":"1329759622908"}
{"_id":"1329759623064"}
{"_id":"1329759623252"}
{"_id":"1329759623361"}

This sample code illustrates how using links while back paging does not return pages in same order as forward paging:

public void simpleTest2() {
try {
ViewQuery query = new ViewQuery();
query.viewName("byUserName");
query.designDocId("_design/MyDocument");
query.key("client_a");

CouchDbConnector dbConnector = new StdCouchDbConnector("mydb", new StdCouchDbInstance(new StdHttpClient.Builder().url("http://localhost:5984").build()));

PageRequest pageRequest = PageRequest.firstPage(5);

Page page = dbConnector.queryForPage(query, pageRequest, JsonNode.class);
List list = page.getRows();
System.out.println("Page 1");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, PageRequest.fromLink(page.getNextPageRequest().asLink()), JsonNode.class);
list = page.getRows();
System.out.println("Page 2");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, PageRequest.fromLink(page.getNextPageRequest().asLink()), JsonNode.class);
list = page.getRows();
System.out.println("Page 3");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, PageRequest.fromLink(page.getNextPageRequest().asLink()), JsonNode.class);
list = page.getRows();
System.out.println("Page 4");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, PageRequest.fromLink(page.getPreviousPageRequest().asLink()), JsonNode.class);
list = page.getRows();
System.out.println("Page 3");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, PageRequest.fromLink(page.getPreviousPageRequest().asLink()), JsonNode.class);
list = page.getRows();
System.out.println("Page 2");
for (JsonNode node : list) {
System.out.println(node);
}

page = dbConnector.queryForPage(query, PageRequest.fromLink(page.getPreviousPageRequest().asLink()), JsonNode.class);
list = page.getRows();
System.out.println("Page 1");
for (JsonNode node : list) {
System.out.println(node);
}

} catch (Exception e) {
e.printStackTrace();
}
}

OUTPUTS:
Page 1
{"_id":"1329759612408"}
{"_id":"1329759622144"}
{"_id":"1329759622300"}
{"_id":"1329759622456"}
{"_id":"1329759622612"}
Page 2
{"_id":"1329759622752"}
{"_id":"1329759622908"}
{"_id":"1329759623064"}
{"_id":"1329759623252"}
{"_id":"1329759623361"}
Page 3
{"_id":"1329759623517"}
{"_id":"1329759623658"}
{"_id":"1329759623829"}
{"_id":"1329759624345"}
{"_id":"1329759624532"}
Page 4
{"_id":"1329759624704"}
{"_id":"1329759624876"}
{"_id":"1329759625032"}
Page 3
{"_id":"1329759622752"} //DIFFERS FROM PAGE 3 BEFORE
{"_id":"1329759622908"}
{"_id":"1329759623064"}
{"_id":"1329759623252"}
{"_id":"1329759623361"}
Page 2
{"_id":"1329759623517"} //DIFFERS FROM PAGE 2 BEFORE
{"_id":"1329759623658"}
{"_id":"1329759623829"}
{"_id":"1329759624345"}
{"_id":"1329759624532"}
Page 1
{"_id":"1329759623517"} //STILL HAS REPEAT ISSUE
{"_id":"1329759623658"}
{"_id":"1329759623829"}
{"_id":"1329759624345"}
{"_id":"1329759624532"}