Query last index of a View even when the index is updating

I’m working with CBL 1.4 on Android.

I used to update my main View before every query (as it’s done by default) but recently I switched to a faster method by updating the view periodically in the background and not before every query.

This resulted in faster queries, except during the times the view is actually getting updated. It seems like the queries and View indexing are processed in the same thread.

I was wondering if there is a way of always querying a view’s last index instead of waiting for it to update its index.

My database can be very change-heavy and it needs to stay updated, but search speed is priority number 1. In other words, I don’t care if the results of a query aren’t updated with the latest docs for a few minutes as long as the queries are fast.

Any suggestions for this case?

The underlying data store allows only one writer at a time (regardless of thread), which is why you’re seeing the slowdown.

There is a Query option to skip updating the index, but it’s been a long time since I worked with 1.4 so I don’t recall its name … I think “stale”?

Sure! I use the stale option to query the View without updating the index, but I gotta update the index at some point.

I update it periodically, the problem being: whenever a view’s index is updating, queries made to that View will have to wait until the index is updated, regardless of the option “stale”

I haven’t delved into the code yet but my guess is that a view’s index is stored in the database, and there is no possibility of having it on memory (not that it would be a good idea anyway)

So another idea I came up with is doubling the view with a different name and alterning the view indexing between two views, but this seems like a huge waste of resources.

I’m kind of at a loss here :sweat_smile:

Hm, I would expect that you could query a view via one database instance while another instance is updating the index. SQLite allows reads while another connection is in the midst of a transaction. I know we changed the iOS/Mac codebase at one point, 1.4 or earlier, to support this. It’s possible that Android doesn’t have similar logic and is blocking reads even when it doesn’t have to? Unfortunately I’m not familiar with the Android codebase.

1 Like

It works! I had tried to do this earlier but I must have done something wrong because the index wasn’t being updated. Thank you for your fast responses!

I haven’t delved into the code yet but my guess is that a view’s index is stored in the database. I know we changed the iOS/Mac codebase at one point, 1.4 or earlier, to support this.

Yes, everything is in one file (unless you’re using the deprecated ForestDB storage.)