Mapped Index for LevelDB
A simple and flexible indexer for LevelDB, built on LevelUP and Map Reduce; allowing asynchronous index calculation.
After initialising Mapped Index, your LevelUP instance will have some new methods that let you register new indexes and fetch values from them.
// requires levelup and level-sublevel packagesconst levelup = mappedIndex = sublevel =
In this example we're using the registerIndex()
method to register two indexes. You must supply an index name (String) and a function that will parse and register individual entries for this index. Your function receives the key and the value of the entry and an emit()
function. You call emit()
with a single argument, the property for this entry that you are indexing on. The emit()
function does not need to be called for each entry, only entries relevant to your index.
Note that the register method has the signature: registerIndex([ mapDb, ] indexName, indexFn)
. So you can provide your own custom sublevel or even a totally separate LevelUP instance to store the indexing data if that suits your needs (perhaps you're a little OCD about polluting your main store with map-reduce & index cruft?)
Now we put some values into our database:
dbdbdbdb
Map Reduce processes these entries and passes them each to our index functions that we registered earlier. Our index references are stored in the same database, namespaced, so that they can be efficiently retrieved when required:
db db
Our LevelUP instance has been augmented with a getBy()
method that takes 3 arguments: the index name, the value on that index we are looking for and a callback function. Our callback will receive two arguments, an error and an array of objects containing 'key'
and 'value'
properties for each indexed entry. You will receive empty arrays where your indexed value finds no corresponding entries.
It is important to note that your entries are not stored in duplicate, only the primary keys are stored for each index entry so an additional look-up is required to fetch each complete entry.
You can also ask for a stream of your indexed entries in a similar manner:
db db
Of course this method is preferable if you are likely to have a large number of entries for each index value, otherwise getBy()
will buffer each entry before returning them to you on the callback.
Licence
level-mapped-index is Copyright (c) 2013 Rod Vagg @rvagg and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.