From 5ee6f60c307daa66f52d9d1dd2048a6999c0a123 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Tue, 2 Apr 2019 17:48:55 +0800 Subject: [PATCH] add PageState.page() to simplify access to page info Change-Id: Idd9ccdc105dbc7f7cc37f6a4a49b8715b363b886 --- .../baidu/hugegraph/serializer/JsonSerializer.java | 8 ++++---- .../hugegraph/backend/page/PageEntryIterator.java | 4 ++-- .../com/baidu/hugegraph/backend/page/PageState.java | 12 +++++++++++- .../com/baidu/hugegraph/backend/page/QueryList.java | 6 +++--- .../backend/store/BackendEntryIterator.java | 3 ++- .../hugegraph/backend/tx/GraphIndexTransaction.java | 4 ++-- .../hugegraph/traversal/optimize/TraversalUtil.java | 8 +++++--- .../java/com/baidu/hugegraph/core/EdgeCoreTest.java | 3 +-- .../com/baidu/hugegraph/core/VertexCoreTest.java | 3 +-- 9 files changed, 31 insertions(+), 20 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java index 4275500697..cc1f45571c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java @@ -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; @@ -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); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageEntryIterator.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageEntryIterator.java index f02828299c..70b5532b28 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageEntryIterator.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageEntryIterator.java @@ -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()); @@ -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; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java index 10264c5f00..3a72d187fd 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java @@ -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; @@ -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); @@ -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; + } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java index d7f6ba78e6..2aed96b807 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/QueryList.java @@ -185,10 +185,10 @@ public PageIterator iterator(int index, String page, long pageSize) { assert index == 0; this.query.page(page); Iterator 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 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() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java index fcb95595a6..d179240050 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java @@ -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; @@ -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); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java index 640ffe581d..b7deb5837d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java @@ -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; @@ -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(); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java index 2bb6f05796..3cbdd9bd77 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java @@ -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; @@ -587,14 +588,15 @@ public static void retriveSysprop(List 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; diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java index 588e4bc096..41834ffb62 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java @@ -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; @@ -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()); } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java index bc3315dde9..56b83c167c 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java @@ -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; @@ -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()); }