GeezerVison is showing github.com — findIds: drop no-order requirement, add offset/limit variant · heyujava/objectbox-java@48aca68
File tree
Expand file treeCollapse file tree
Open diff view settings
objectbox-java/src/main/java/io/objectbox/query tests/objectbox-java-test/src/test/java/io/objectbox/query
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/query/Query.java +11-4Lines changed: 11 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -236,13 +236,20 @@ public List<T> call() { |
236 | 236 | */ |
237 | 237 | @Nonnull |
238 | 238 | public long[] findIds() { |
239 | | - if (hasOrder) { |
240 | | - throw new UnsupportedOperationException("This method is currently only available for unordered queries"); |
241 | | - } |
| 239 | + return findIds(0,0); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Like {@link #findIds()} but with a offset/limit param, e.g. for pagination. |
| 244 | + * <p> |
| 245 | + * Note: a filter set with {@link QueryBuilder#filter} will be silently ignored! |
| 246 | + */ |
| 247 | + @Nonnull |
| 248 | + public long[] findIds(final long offset, final long limit) { |
242 | 249 | return box.internalCallWithReaderHandle(new CallWithHandle<long[]>() { |
243 | 250 | @Override |
244 | 251 | public long[] call(long cursorHandle) { |
245 | | - return nativeFindIds(handle, cursorHandle, 0, 0); |
| 252 | + return nativeFindIds(handle, cursorHandle, offset, limit); |
246 | 253 | } |
247 | 254 | }); |
248 | 255 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/query/QueryTest.java +16-1Lines changed: 16 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -458,7 +458,7 @@ public void testRemove() { |
458 | 458 | } |
459 | 459 |
|
460 | 460 | @Test |
461 | | - public void testFindKeysUnordered() { |
| 461 | + public void testFindIds() { |
462 | 462 | putTestEntitiesScalars(); |
463 | 463 | assertEquals(10, box.query().build().findIds().length); |
464 | 464 |
|
@@ -470,6 +470,21 @@ public void testFindKeysUnordered() { |
470 | 470 | assertEquals(10, keys[2]); |
471 | 471 | } |
472 | 472 |
|
| 473 | + @Test |
| 474 | + public void testFindIdsWithOrder() { |
| 475 | + putTestEntitiesScalars(); |
| 476 | + Query<TestEntity> query = box.query().orderDesc(TestEntity_.simpleInt).build(); |
| 477 | + long[] ids = query.findIds(); |
| 478 | + assertEquals(10, ids.length); |
| 479 | + assertEquals(10, ids[0]); |
| 480 | + assertEquals(1, ids[9]); |
| 481 | + |
| 482 | + ids = query.findIds(3, 2); |
| 483 | + assertEquals(2, ids.length); |
| 484 | + assertEquals(7, ids[0]); |
| 485 | + assertEquals(6, ids[1]); |
| 486 | + } |
| 487 | + |
473 | 488 | @Test |
474 | 489 | public void testOr() { |
475 | 490 | putTestEntitiesScalars(); |
|