|
| 1 | +<!-- |
| 2 | + ~ Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + ~ or more contributor license agreements. See the NOTICE file |
| 4 | + ~ distributed with this work for additional information |
| 5 | + ~ regarding copyright ownership. The ASF licenses this file |
| 6 | + ~ to you under the Apache License, Version 2.0 (the |
| 7 | + ~ "License"); you may not use this file except in compliance |
| 8 | + ~ with the License. You may obtain a copy of the License at |
| 9 | + ~ |
| 10 | + ~ http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + ~ |
| 12 | + ~ Unless required by applicable law or agreed to in writing, |
| 13 | + ~ software distributed under the License is distributed on an |
| 14 | + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + ~ KIND, either express or implied. See the License for the |
| 16 | + ~ specific language governing permissions and limitations |
| 17 | + ~ under the License. |
| 18 | + --> |
| 19 | + |
| 20 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 21 | + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 22 | +<html xmlns="http://www.w3.org/1999/xhtml"> |
| 23 | +<head><title>Offset/Limit Pagination for JSON-RPC Services</title></head> |
| 24 | +<body> |
| 25 | + |
| 26 | +<h1 id="overview">Offset/Limit Pagination for JSON-RPC Services</h1> |
| 27 | + |
| 28 | +<p>Axis2 provides a generic pagination framework for JSON-RPC services |
| 29 | +backed by SQL databases. The two classes — |
| 30 | +<code>PaginationRequest</code> and <code>PaginatedResponse<T></code> — |
| 31 | +map directly to JPA/Hibernate's <code>setFirstResult(offset)</code> and |
| 32 | +<code>setMaxResults(limit)</code> pattern.</p> |
| 33 | + |
| 34 | +<p><strong>Package:</strong> <code>org.apache.axis2.json.rpc</code></p> |
| 35 | + |
| 36 | +<h2 id="wire_format">Wire Format</h2> |
| 37 | + |
| 38 | +<p>A paginated response wraps the result list with metadata:</p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +{ |
| 42 | + "response": { |
| 43 | + "data": [ ... ], |
| 44 | + "pagination": { |
| 45 | + "offset": 0, |
| 46 | + "limit": 50, |
| 47 | + "totalCount": 1247, |
| 48 | + "hasMore": true |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +</pre> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><strong>offset</strong> — zero-based index of the first item in this page</li> |
| 56 | + <li><strong>limit</strong> — maximum items requested (page size)</li> |
| 57 | + <li><strong>totalCount</strong> — total items matching the query across all pages</li> |
| 58 | + <li><strong>hasMore</strong> — true when <code>offset + limit < totalCount</code></li> |
| 59 | +</ul> |
| 60 | + |
| 61 | +<h2 id="service_integration">Service Integration</h2> |
| 62 | + |
| 63 | +<p>A typical service method delegates offset/limit to the DAO:</p> |
| 64 | + |
| 65 | +<pre> |
| 66 | +public PaginatedResponse<AssetBO> findAssets(AssetQuery query) { |
| 67 | + List<AssetBO> items = dao.findList(query.getOffset(), query.getLimit()); |
| 68 | + long total = dao.count(query); |
| 69 | + return PaginatedResponse.of(items, query.getOffset(), query.getLimit(), total); |
| 70 | +} |
| 71 | +</pre> |
| 72 | + |
| 73 | +<p>The request POJO can embed <code>PaginationRequest</code> fields directly |
| 74 | +or accept them as separate parameters:</p> |
| 75 | + |
| 76 | +<pre> |
| 77 | +// Client sends: |
| 78 | +{ |
| 79 | + "searchTerm": "AAPL", |
| 80 | + "offset": 100, |
| 81 | + "limit": 50 |
| 82 | +} |
| 83 | +</pre> |
| 84 | + |
| 85 | +<h3>Unpaginated Responses</h3> |
| 86 | + |
| 87 | +<p>For small lookup tables (e.g., a list of 15 departments), use the |
| 88 | +convenience factory to wrap the full list with <code>hasMore=false</code>:</p> |
| 89 | + |
| 90 | +<pre> |
| 91 | +return PaginatedResponse.unpaginated(departments); |
| 92 | +// → offset=0, limit=15, totalCount=15, hasMore=false |
| 93 | +</pre> |
| 94 | + |
| 95 | +<h2 id="safety">Safety: maxLimit Clamping and Input Validation</h2> |
| 96 | + |
| 97 | +<p><code>PaginationRequest</code> enforces safety constraints at the getter level:</p> |
| 98 | + |
| 99 | +<table border="1"> |
| 100 | +<tr><th>Input</th><th>Behavior</th></tr> |
| 101 | +<tr><td><code>offset < 0</code></td><td>Clamped to 0</td></tr> |
| 102 | +<tr><td><code>limit <= 0</code></td><td>Default: 50</td></tr> |
| 103 | +<tr><td><code>limit > maxLimit</code></td><td>Capped at maxLimit (default: 2000)</td></tr> |
| 104 | +</table> |
| 105 | + |
| 106 | +<p>Services that handle expensive entities can lower the cap per-operation:</p> |
| 107 | + |
| 108 | +<pre> |
| 109 | +// Large text fields — cap at 100 per page |
| 110 | +request.setMaxLimit(100); |
| 111 | +int safeLimit = request.getLimit(); // capped at 100 |
| 112 | +</pre> |
| 113 | + |
| 114 | +<h2 id="frontend_patterns">Frontend Patterns</h2> |
| 115 | + |
| 116 | +<h3>Page Controls ("Showing 151–200 of 1,247")</h3> |
| 117 | +<pre> |
| 118 | +// JavaScript / TypeScript |
| 119 | +const { offset, limit, totalCount } = pagination; |
| 120 | +const currentPage = Math.floor(offset / limit) + 1; |
| 121 | +const totalPages = Math.ceil(totalCount / limit); |
| 122 | +const showingFrom = offset + 1; |
| 123 | +const showingTo = offset + data.length; |
| 124 | +</pre> |
| 125 | + |
| 126 | +<h3>Virtual Scroll / Infinite Scroll</h3> |
| 127 | +<pre> |
| 128 | +// Load next chunk when user scrolls |
| 129 | +const nextOffset = pagination.offset + pagination.limit; |
| 130 | +if (pagination.hasMore) { |
| 131 | + fetchPage(nextOffset, pagination.limit); |
| 132 | +} |
| 133 | +</pre> |
| 134 | + |
| 135 | +<h3>SmartClient startRow/endRow</h3> |
| 136 | +<pre> |
| 137 | +// SmartClient sends startRow=300, endRow=350 |
| 138 | +// Service translates: offset = startRow, limit = endRow - startRow |
| 139 | +int offset = startRow; |
| 140 | +int limit = endRow - startRow; |
| 141 | +</pre> |
| 142 | + |
| 143 | +<h2>Why Offset/Limit Instead of Cursor</h2> |
| 144 | + |
| 145 | +<ul> |
| 146 | + <li><strong>DAO compatibility</strong> — existing Hibernate/JPA DAOs use |
| 147 | + <code>query.setFirstResult(offset)</code> and <code>query.setMaxResults(limit)</code>. |
| 148 | + Cursor pagination requires a stable sort key and stateful server-side tokens.</li> |
| 149 | + <li><strong>Frontend grids</strong> — SmartClient, AG Grid, and React Table natively |
| 150 | + speak offset/limit via <code>startRow</code>/<code>endRow</code> or |
| 151 | + <code>page</code>/<code>pageSize</code>.</li> |
| 152 | + <li><strong>totalCount</strong> — enables "Showing 1–50 of 1,247" UI patterns |
| 153 | + and page-count calculations. Cursor APIs typically omit total counts because |
| 154 | + they are expensive for the cursor model, but they are cheap when the DAO |
| 155 | + already runs <code>SELECT COUNT(*)</code>.</li> |
| 156 | +</ul> |
| 157 | + |
| 158 | +<h2>Test Coverage</h2> |
| 159 | + |
| 160 | +<p>The <code>PaginatedResponseTest</code> class provides 20 tests covering:</p> |
| 161 | +<ul> |
| 162 | + <li>First page, last page, partial last page, single page, empty result</li> |
| 163 | + <li>Null data treated as empty list</li> |
| 164 | + <li>Unpaginated convenience factory</li> |
| 165 | + <li>Negative offset clamping, zero/negative limit defaults, maxLimit enforcement</li> |
| 166 | + <li>Enterprise scenarios: 8,543-item virtual scroll, soft-delete filtering, |
| 167 | + service-specific maxLimit, SmartClient startRow/endRow translation</li> |
| 168 | + <li>Request → response round-trip simulation</li> |
| 169 | +</ul> |
| 170 | + |
| 171 | +</body> |
| 172 | +</html> |
0 commit comments