/** Loads the remaining entities (if any) that were not loaded before. Applies to cached lazy lists only. */ public void loadRemaining() { checkCached(); int size = entities.size(); for (int i = 0; i < size; i++) { get(i); } }
/** * Executes the query and returns the result as a list iterator; make sure to close it to close the underlying * cursor. The cursor is closed once the iterator is fully iterated through. */ public CloseableListIterator<T> listIterator() { return listLazyUncached().listIteratorAutoClose(); }
@Override public int indexOf(Object object) { loadRemaining(); return entities.indexOf(object); }
private void openRandomArticle() { LazyList<Article> articles = getQueryBuilder().listLazyUncached(); if(!articles.isEmpty()) { long id = articles.get(new Random().nextInt(articles.size())).getId(); openArticle(id); } else { Toast.makeText(getActivity(), R.string.no_articles, Toast.LENGTH_SHORT).show(); } articles.close(); }
public void testRawQueryLazyList() { ArrayList<TestEntity> list = insert(2); LazyList<TestEntity> listLazy = dao.queryRawCreate("").listLazy(); assertEquals(list.size(), listLazy.size()); assertNull(listLazy.peek(0)); assertNull(listLazy.peek(1)); assertNotNull(listLazy.get(1)); assertNull(listLazy.peek(0)); assertNotNull(listLazy.peek(1)); assertNotNull(listLazy.get(0)); assertNotNull(listLazy.peek(0)); assertNotNull(listLazy.peek(1)); }
public void testEmpty() { insert(1); LazyList<TestEntity> listLazy = dao.queryBuilder().where(Properties.SimpleInteger.eq(-1)).build().listLazy(); assertTrue(listLazy.isEmpty()); assertTrue(listLazy.isClosed()); try { listLazy.get(0); fail("Not empty"); } catch (RuntimeException e) { // Expected, OK } }
@Override public void call(Subscriber<? super T> subscriber) { try { LazyList<T> lazyList = query.forCurrentThread().listLazyUncached(); try { for (T entity : lazyList) { if (subscriber.isUnsubscribed()) { break; } subscriber.onNext(entity); } } finally { lazyList.close(); } if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } catch (Throwable e) { Exceptions.throwIfFatal(e); subscriber.onError(e); } } });
public void testAutoClose() { insert(10); LazyList<TestEntity> lazyList = dao.queryBuilder().build().listLazyUncached(); CloseableListIterator<TestEntity> iterator = lazyList.listIteratorAutoClose(); while (iterator.hasNext()) { assertFalse(lazyList.isClosed()); iterator.next(); } assertTrue(lazyList.isClosed()); }
public void testGetForCurrentThread_TwoThreads() throws InterruptedException { insert(3); createQueryFromOtherThread(); Query<TestEntity> query = queryFromOtherThread.forCurrentThread(); assertNotSame(queryFromOtherThread, query); query.setLimit(10); query.setOffset(0); assertEquals(getSimpleInteger(1), (int) query.uniqueOrThrow().getSimpleInteger()); int expected = getSimpleInteger(2); query.setParameter(0, expected); assertEquals(expected, (int) query.list().get(0).getSimpleInteger()); assertEquals(expected, (int) query.listLazy().get(0).getSimpleInteger()); assertEquals(expected, (int) query.listLazyUncached().get(0).getSimpleInteger()); assertEquals(expected, (int) query.unique().getSimpleInteger()); assertEquals(expected, (int) query.uniqueOrThrow().getSimpleInteger()); }
/** * Executes the query and returns the result as a list that lazy loads the entities on first access. Entities are * cached, so accessing the same entity more than once will not result in loading an entity from the underlying * cursor again.Make sure to close it to close the underlying cursor. */ public LazyList<T> listLazy() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); return new LazyList<T>(daoAccess, cursor, true); }
entity = entities.get(location); if (entity == null) { entity = loadEntity(location); entities.set(location, entity); lock.lock(); try { return loadEntity(location); } finally { lock.unlock();
public void testUncached() { insert(1); LazyList<TestEntity> listLazy = dao.queryBuilder().build().listLazyUncached(); assertFalse(listLazy.isEmpty()); assertFalse(listLazy.isClosed()); TestEntity entity1 = listLazy.get(0); TestEntity entity2 = listLazy.get(0); assertEquals(entity1.getId(), entity2.getId()); if (identityScopeForDao == null) { assertNotSame(entity1, entity2); } else { assertSame(entity1, entity2); } assertFalse(listLazy.isClosed()); try { listLazy.loadRemaining(); fail("Not empty"); } catch (DaoException expected) { // Expected, OK } listLazy.close(); assertTrue(listLazy.isClosed()); }
public void testSizeAndGetAndPeak() { ArrayList<TestEntity> list = insert(2); LazyList<TestEntity> listLazy = dao.queryBuilder().build().listLazy(); assertEquals(list.size(), listLazy.size()); assertNull(listLazy.peek(0)); assertNull(listLazy.peek(1)); assertNotNull(listLazy.get(1)); assertNull(listLazy.peek(0)); assertNotNull(listLazy.peek(1)); assertNotNull(listLazy.get(0)); assertNotNull(listLazy.peek(0)); assertNotNull(listLazy.peek(1)); }
@Override public void call(Subscriber<? super T> subscriber) { try { LazyList<T> lazyList = query.forCurrentThread().listLazyUncached(); try { for (T entity : lazyList) { if (subscriber.isUnsubscribed()) { break; } subscriber.onNext(entity); } } finally { lazyList.close(); } if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } catch (Throwable e) { Exceptions.throwIfFatal(e); subscriber.onError(e); } } });
/** * Executes the query and returns the result as a list that lazy loads the entities on every access (uncached). * Make sure to close the list to close the underlying cursor. */ public LazyList<T> listLazyUncached() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); return new LazyList<T>(daoAccess, cursor, false); }
entity = entities.get(location); if (entity == null) { entity = loadEntity(location); entities.set(location, entity); lock.lock(); try { return loadEntity(location); } finally { lock.unlock();
@Override public List<E> subList(int start, int end) { checkCached(); for (int i = start; i < end; i++) { get(i); } return entities.subList(start, end); }