zendesk / android-db-commons

Some common utilities for ContentProvider/ContentResolver/Cursor and other db-related android stuff
Apache License 2.0
222 stars 28 forks source link

Eager transform() #19

Closed chalup closed 10 years ago

chalup commented 10 years ago

Currently transform() is always evaluated lazily, but the eager evaluation in background thread is also a valid scenario. You can work around this by wrapping the result with a function iterating over LazyList returned by transform, but I think this should be supported directly by the API.

I think the concepts of lazy/eager evaluation and single row/whole result should be decoupled.

chalup commented 10 years ago

Few thoughts on this issue:

  1. It doesn't make sense to have a lazy wrap - it would be always evaluated, unless for some reason you would not access the loader result at all.
  2. It doesn't make sense to have the lazy transforms and then eager transforms - both transforms would be evaluated either way.
  3. I'm also wondering if the opposite, i.e. eager transforms followed by lazy transforms make any sense. If you have a eager lightweight operation and then heavy lazy operation, it probably wouldn't make any difference if both operations were lazy (and you'd get much better performance in case of large cursors, because you won't have to iterate throught entire cursor for eager operation). And if you have heavy eager operation and lightweight lazy operation, making the second operation lazy is probably a microoptimization that doesn't do any good.
  4. It does make sense to have both lazy transform followed by wrap and eager transform followed by wrap. The former case is useful when your wrap operation doesn't access every row and the second one when it does access every row and you don't want the overhead of cache used in LazyList.

So I propose the following API: 0 to n transform() calls, that might be followed by lazy() call, which make all of the previous transforms lazy, follwed by 0 to n wrap() calls. Here is the list of possible usage:

transform().transform().transform() - eager transformations transform().transform().transform().lazy() - lazy transformations transform().transform().transform().wrap().wrap().wrap() - eager transformations followed by eager wraps transform().transform().transform().lazy().wrap().wrap().wrap() - lazy transformations followed by eager wraps wrap().wrap().wrap() - eager wraps

@partition what do you think?

chalup commented 10 years ago

Implemented in #26