-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
I've got a plan for how I'd like to proceed with solving both:
- faster, "native" secondary indexes (Native secondary indexes #2280), and
- the
purge()feature (Feature Request - Method to Purge a document #802).
My proposal: move the _query() and _viewCleanup() implementations into the adapters themselves. Yes, this will require writing it three times for IDB/WebSQL/LevelDB, but it offers the benefit of vastly improved map/reduce performance. Which I think we sorely need, because as it stands, we're about 100x slower than native indexes, and 100 is a big number (source: #3039 (comment)).
What would these _query() and _viewCleanup() functions look like? Basically like this: here's the socket-pouch custom adapter defining its own implementations and here's the modest change to mapreduce required to pull off this trick. So basically we would just need to move the _query() and _viewCleanup() implementations into their respective adapters (http already having been written) and deprecate the mapreduce plugin.
How does this affect pouchdb-find, etc.? I think I've already demonstrated with pouchdb-abstract-mapreduce that you can build pouchdb-find on top of map/reduce and it works just peachy. I did the same thing for pouchdb-quick-search. Both codebases could be very easily rewritten to use native _query() and _viewCleanup() functions.
Doesn't it sacrifice modularity to re-integrate the mapreduce plugin? A bit, but it could still be possible to build PouchDB without the _query() and _viewCleanup() functions. We'd just have to have a separate build script that uses some browserify tricks to sub out those functions for stubs that throw "not implemented" errors, and include them in separate files: idb-mapreduce.js, websql-mapreduce.js, leveldb-mapreduce.js. Not a big deal.
What's the migration plan? None at the moment, except that existing secondary indexes would have to be rebuilt. I think this is the easiest fix and not really a big ask for users. Anyway, given that the schema would look different between the old and new implementations, such a migration is inevitable.
What are the other benefits? Plenty. No more awkward filtering of mrview databases in pouchdb-all-dbs, no more IndexedDB destroy() race conditions, no more having to manage "dependentDbs" at all. As well as perf improvements and making it vastly simpler to implement purge(), since you don't have to worry about coordinating the dependent databases.
In general, I think our current map/reduce implementation was a fine solution at the time. It gave us robust secondary indexes for all three adapters, without having to write a lot of adapter-specific code. And we now have tons of map/reduce tests that are going to serve us well going forward. But speed is a priority, and purge() needs to be implemented, and in my mind this is the best way to do it.
If y'all are okay with this proposal, I'll start implementing it and could hopefully have it done within a month or two.