/** * Very efficient way to get just the IDs without creating any objects. IDs can later be used to lookup objects * (lookups by ID are also very efficient in ObjectBox). * <p> * Note: a filter set with {@link QueryBuilder#filter} will be silently ignored! */ @Nonnull public long[] findIds() { return findIds(0,0); }
/** * Find all Objects matching the query without actually loading the Objects. See @{@link LazyList} for details. */ @Nonnull public LazyList<T> findLazyCached() { ensureNoFilterNoComparator(); return new LazyList<>(box, findIds(), true); }
/** * Find all Objects matching the query without actually loading the Objects. See @{@link LazyList} for details. */ public LazyList<T> findLazy() { ensureNoFilterNoComparator(); return new LazyList<>(box, findIds(), false); }
@Override public void run() { LazyList<T> lazyList = new LazyList<>(box, findIds(), false); int size = lazyList.size(); for (int i = 0; i < size; i++) { T entity = lazyList.get(i); if (entity == null) { throw new IllegalStateException("Internal error: data object was null"); } if (filter != null) { if (!filter.keep(entity)) { continue; } } if (eagerRelations != null) { resolveEagerRelationForNonNullEagerRelations(entity, i); } try { consumer.accept(entity); } catch (BreakForEach breakForEach) { break; } } } });
@Test public void testFindIdsWithOrder() { putTestEntitiesScalars(); Query<TestEntity> query = box.query().orderDesc(TestEntity_.simpleInt).build(); long[] ids = query.findIds(); assertEquals(10, ids.length); assertEquals(10, ids[0]); assertEquals(1, ids[9]); ids = query.findIds(3, 2); assertEquals(2, ids.length); assertEquals(7, ids[0]); assertEquals(6, ids[1]); }
@Test public void testFindIds() { putTestEntitiesScalars(); assertEquals(10, box.query().build().findIds().length); Query<TestEntity> query = box.query().greater(simpleInt, 2006).build(); long[] keys = query.findIds(); assertEquals(3, keys.length); assertEquals(8, keys[0]); assertEquals(9, keys[1]); assertEquals(10, keys[2]); }