Skip to content

Commit

Permalink
add PageState.page() to simplify access to page info (#429)
Browse files Browse the repository at this point in the history
Change-Id: Idd9ccdc105dbc7f7cc37f6a4a49b8715b363b886
  • Loading branch information
javeme authored and zhoney committed Apr 3, 2019
1 parent c4ca312 commit 768b24f
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.page.PageState;
import com.baidu.hugegraph.backend.store.Shard;
import com.baidu.hugegraph.iterator.Metadatable;
import com.baidu.hugegraph.schema.EdgeLabel;
Expand Down Expand Up @@ -105,11 +106,10 @@ private String writeIterator(String label, Iterator<?> itor,
if (itor instanceof GraphTraversal<?, ?>) {
page = TraversalUtil.page((GraphTraversal<?, ?>) itor);
} else if (itor instanceof Metadatable) {
page = (String) ((Metadatable) itor).metadata("page");
page = PageState.page(itor);
} else {
throw new HugeException(
"Error type '%s' of paging iterator '%s'",
itor.getClass(), itor);
throw new HugeException("Invalid paging iterator: %s",
itor.getClass());
}
if (page != null) {
page = String.format(",\"page\": \"%s\"", page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private boolean fetch() {
assert this.results != null;

if (this.results.iterator().hasNext()) {
if (results.page() == null) {
if (this.results.page() == null) {
this.pageState.increase();
} else {
this.pageState.page(this.results.page());
Expand All @@ -96,7 +96,7 @@ public BackendEntry next() {

@Override
public Object metadata(String meta, Object... args) {
if ("page".equals(meta)) {
if (PageState.PAGE.equals(meta)) {
if (this.pageState.offset() >= this.queries.total()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
package com.baidu.hugegraph.backend.page;

import java.util.Base64;
import java.util.Iterator;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.backend.serializer.BytesBuffer;
import com.baidu.hugegraph.iterator.Metadatable;
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.E;

public final class PageState {

public static final String PAGE = "page";
public static final String PAGE_NONE = "";

private int offset;
Expand Down Expand Up @@ -82,7 +85,7 @@ public static PageState fromString(String page) {
public static PageState fromBytes(byte[] bytes) {
if (bytes.length == 0) {
// The first page
return new PageState(0, "");
return new PageState(0, PAGE_NONE);
}
try {
BytesBuffer buffer = BytesBuffer.wrap(bytes);
Expand All @@ -94,4 +97,11 @@ public static PageState fromBytes(byte[] bytes) {
e, Bytes.toHex(bytes));
}
}

public static String page(Iterator<?> iterator) {
E.checkState(iterator instanceof Metadatable,
"Invalid paging iterator: %s", iterator.getClass());
Object page = ((Metadatable) iterator).metadata(PAGE);
return (String) page;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ public PageIterator iterator(int index, String page, long pageSize) {
assert index == 0;
this.query.page(page);
Iterator<BackendEntry> iterator = fetcher.apply(this.query);
// Must iterate all entries before get the next page
// Must iterate all entries before getting the next page info
List<BackendEntry> results = IteratorUtils.list(iterator);
page = (String) ((Metadatable) iterator).metadata("page");
return new PageIterator(results.iterator(), page);
return new PageIterator(results.iterator(),
PageState.page(iterator));
}

public int total() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

import com.baidu.hugegraph.backend.page.PageState;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.exception.LimitExceedException;
import com.baidu.hugegraph.exception.NotSupportException;
Expand Down Expand Up @@ -81,7 +82,7 @@ public BackendEntry next() {

@Override
public Object metadata(String meta, Object... args) {
if ("page".equals(meta)) {
if (PageState.PAGE.equals(meta)) {
return this.pageState();
}
throw new NotSupportException("Invalid meta '%s'", meta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.baidu.hugegraph.backend.page.IdHolder;
import com.baidu.hugegraph.backend.page.IdHolderList;
import com.baidu.hugegraph.backend.page.PageIds;
import com.baidu.hugegraph.backend.page.PageState;
import com.baidu.hugegraph.backend.query.Condition;
import com.baidu.hugegraph.backend.query.Condition.Relation;
import com.baidu.hugegraph.backend.query.ConditionQuery;
Expand Down Expand Up @@ -486,8 +487,7 @@ private PageIds doIndexQueryOnce(IndexLabel indexLabel,
"The entries must be Metadatable when query " +
"in paging, but got '%s'",
entries.getClass().getName());
Object page = ((Metadatable) entries).metadata("page");
return new PageIds(ids, (String) page);
return new PageIds(ids, PageState.page(entries));
} finally {
locks.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.page.PageState;
import com.baidu.hugegraph.backend.query.Condition;
import com.baidu.hugegraph.backend.query.Condition.Relation;
import com.baidu.hugegraph.backend.query.Condition.RelationType;
Expand Down Expand Up @@ -587,14 +588,15 @@ public static void retriveSysprop(List<HasContainer> hasContainers,

public static String page(GraphTraversal<?, ?> traversal) {
QueryHolder holder = rootStep(traversal);
E.checkState(holder != null, "Invalid traversal: %s", traversal);
return (String) holder.metadata("page");
E.checkState(holder != null,
"Invalid paging traversal: %s", traversal.getClass());
return (String) holder.metadata(PageState.PAGE);
}

public static QueryHolder rootStep(GraphTraversal<?, ?> traversal) {
for (final Step<?, ?> step : traversal.asAdmin().getSteps()) {
if (step instanceof QueryHolder) {
return ((QueryHolder) step);
return (QueryHolder) step;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import com.baidu.hugegraph.config.CoreOptions;
import com.baidu.hugegraph.exception.LimitExceedException;
import com.baidu.hugegraph.exception.NotFoundException;
import com.baidu.hugegraph.iterator.Metadatable;
import com.baidu.hugegraph.schema.SchemaManager;
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.testutil.FakeObjects.FakeEdge;
Expand Down Expand Up @@ -1769,7 +1768,7 @@ public void testScanEdgeInPaging() {
while (iterator.hasNext()) {
edges.add(iterator.next());
}
page = (String) ((Metadatable) iterator).metadata("page");
page = PageState.page(iterator);
}
Assert.assertEquals(18, edges.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import com.baidu.hugegraph.backend.store.Shard;
import com.baidu.hugegraph.backend.tx.GraphTransaction;
import com.baidu.hugegraph.exception.NoIndexException;
import com.baidu.hugegraph.iterator.Metadatable;
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.schema.SchemaManager;
import com.baidu.hugegraph.schema.VertexLabel;
Expand Down Expand Up @@ -3031,7 +3030,7 @@ public void testScanVertexInPaging() {
while (iterator.hasNext()) {
vertexes.add(iterator.next());
}
page = (String) ((Metadatable) iterator).metadata("page");
page = PageState.page(iterator);
}
Assert.assertEquals(10, vertexes.size());
}
Expand Down

0 comments on commit 768b24f

Please sign in to comment.