Index one or more hypercores
npm install multi-core-indexer


**⚠️ This is an Alpha release and the API will likely change. Do not use in
production. ⚠️**
Index one or more
hypercores
You can use this module to index one or more hypercores. The batch() function
will be called with every downloaded entry in the hypercore(s), and will be
called as new entries are downloaded or appended. The index state is persisted,
so indexing will continue where it left off between restarts.
This module is useful if you want to create an index of items in multiple
Hypercores. There is no
guarantee of ordering, so the indexer needs to be able to index unordered data.
Sparse hypercores are supported, and undownloaded entries in the hypercore will
be indexed when they are downloaded.
- Install
- Usage
- API
- Maintainers
- Contributing
- License
``bash`
npm install multi-core-indexer
`js
const MultiCoreIndexer = require('multi-core-indexer')
const raf = require('random-access-file')
const Hypercore = require('hypercore')
function createStorage(key) {
return raf(./${key})
}
function batch(entries) {
for (const { key, block, index } of entries) {
console.log(Block ${index} of core ${key.toString('hex')} is ${block})
}
}
const cores = [
new Hypercore('./core1'),
new Hypercore('./core2'),
]
const indexer = new MultiCoreIndex(cores, { storage: createStorage, batch })
``
_Required_\
Type: Array
An array of Hypercores
to index. All Hypercores must share the same value encoding (binary, utf-8json
or ).
_Required_\
Type: object
#### opts.batch
_Required_\
Type: (entries: Array<{ key: Buffer, block: Buffer | string | Json, index: number }>) => Promise
Called with an array of entries as they are read from the hypercores. The next
batch will not be called until batch() resolves. Entries will be queued (andopts.maxBatch
batched) as fast as they can be read, up to . block is thekey
block of data from the hypercore, is the public key where the block isindex
from, and is the index of the block within the hypercore.
Note: Currently if batch throws an error, things will break, and the
entries will still be persisted as indexed. This will be fixed in a later
release.
#### opts.storage
_Required_\
Type: (key: string) => RandomAccessStorage
A function that will be called with a hypercore public key as a hex-encoded
string, that should return a
random-access-storage instance. This
is used to store the index state of each hypercore. (Index state is stored as a
bitfield).
#### opts.reindex
_Optional_\
Type: boolean
If true, the cores, and any new ones that are added, will be reindexed from
scratch.
#### opts.maxBatch
_Optional_\
Type: number
The max size of each batch in bytes.
#### opts.byteLength
_Optional_\
Type: (entry: { key: Buffer, block: Buffer | string | Json, index: number }) => number
Optional function that calculates the byte size of input data. By default, if
the value encoding of the underlying Hypercore is binary or utf-8, this willjson
be the byte length of all the blocks in the batch. If the value encoding is then this will be the number of entries in a batch.
Type: IndexState: { current: 'idle' | 'indexing' | 'closing' | 'closed', remaining: number, entriesPerSecond: number }
A getter that returns the current IndexState, the same as the value emitted by the index-state event. This getter is useful for checking the state of the indexer before it has emitted any events.
#### core
_Required_\
Type: Hypercore
Add a hypercore to the indexer. Must have the same value encoding as other
hypercores already in the indexer.
Rejects if called after the indexer is closed.
Resolves when indexing state is 'idle'.
Resolves if the indexer is closed before this resolves. Rejects if called
after the indexer is closed.
Stop the indexer and flush index state to storage. This will not close the
underlying storage - it is up to the consumer to do that.
No-op if called more than once.
Unlink all index files.
This should only be called after close() has resolved, and rejects if not.
#### onState
_Required_\
Type: (indexState: { current: 'idle' | 'indexing', remaining: number, entriesPerSecond: number }) => void
Event listener for the current indexing state. entriesPerSecond is the currentremaining
average number of entries being processed per second. This is calculated as a
moving average with a decay factor of 5. is the number of entriesremaining / entriesPerSecond
remaining to index. To estimate time remaining in seconds, use.
#### handler
_Required_\
Type: () => void
Event listener for when the indexer re-starts indexing (e.g. when unindexed
blocks become available, either through an append or a download).
#### handler
_Required_\
Type: () => void`
Event listener for when the indexer has completed indexing of available data.
Note: During sync this can be emitted before sync is complete because the
indexer has caught up with currently downloaded data, and the indexer will start
indexing again as new data is downloaded.
PRs accepted.
Small note: If editing the README, please conform to the
standard-readme specification.
MIT © 2022 Digital Democracy