Why does LazyList extends AbstractList and not AbstractSequentialList?

12 views
Skip to first unread message

Brian Oxley

unread,
Dec 10, 2014, 8:30:28 PM12/10/14
to acti...@googlegroups.com
Hi,

I was looking at LazyList while researching a ListIterator wrapper of JDBC ResultSet and saw it extended AbstractList rather than AbstractSequentialList.  I think of result sets as sequential in nature, not random access, especially if rows are fetched lazily.  Why was this choice made?

Cheers,
--binkley

Igor Polevoy

unread,
Dec 10, 2014, 11:48:35 PM12/10/14
to acti...@googlegroups.com
Hi, Brian. 

LazyList is not sequential. In fact, this is a plain old Java List (POJL :)). 

You can go up and down on this list all all day long. It is a completely disconnected from any DB connection. 

This means that when you call:

List<Person> teenagers = Person.where("age > 12 and age < 19");

nothing really happens, hence the name (lazy)
However, when you try to read anything from it: 

teenagers.size()

it will execute the query, generate  model instances and will fill itself with those instances before returning from this call. 

It means that using this method of getting data from the database for reading millions of records will crash your JVM. Use it for relatively small amount of data (as much as you can see on HTML page, for instance). 

If you need to run a query to process millions of records, there is a different method: 

Person.findWith(new ModelListener() {
            @Override
            public void onModel(Model model) {
                //your code  to process one model at the time 
            }
        }, "age > 12 and age < 19");

This method of reading from DB will follow the cursor and is easy on memory 

I hope it helps

tx
Reply all
Reply to author
Forward
0 new messages