Higher level storage provider for hypercore based data structures
constRAM=require('random-access-memory')constmemdb=require('memdb')consthypercore=require('hypercore')consthypertrie=require('hypertrie')consthyperdrive=require('hyperdrive')constGarden=require('coregarden')constgarden=Garden(RAM,memdb(),{mappers: {
hypercore,
hypertrie,
hyperdrive
}})try{constmyTrie=awaitgarden.plant('hypertrie')constmeta=awaitgarden.getMeta(myTrie.key)console.log(meta)awaitgarden.closeCore(mTrie)}catch(err){console.error('Something went wrong',err)}storage - RandomAccess a base storage
lvl - LevelDown a leveldb instance
opts.mappershash where keys are type names in form of strings and the
values should be factory functions that generate said type
example:
{hypercore: (storage,key,opts)=>require('hypercore')(storage,keyopts)}Appends type and factory function to internal mappers.
Returns Promise of stored metadata for registered key
returns Promise of previously stored core, loads a core into memory if not previously loaded.
Throws DeletedCoreError if key was previously purged.
Throws BannedCoreError if key was previously banned.
Throws NotFoundError if key refences an unknown core.
returns Promise of a core's total size on storage in bytes
returns Promise that is either resolved or will be resolved as soon as metadata is persisted to disc.
Allocates a new storage space pot for given type.
The mapper factoryFn can be overridden by providing a function as the second argument to plant. Word of caution, overriding the factory function like this can produce cores that won't be possible to re-open from storage.
Only use this if you are locally creating a competely new core and want to initialize it before making it available for replication.
ex.
// In most cases you should use:
const emptyfeed = garden.plant('hypercore', key)
// Sometimes you might want to take extra initialization steps during datastructure creation.
const nonEmptyFeed = garden.plant('hypercore', (storage, plant, factoryFn) => {
const core = factoryFn(storage) // shortcut for the registered mapper function
core.ready(() => {
core.append(Buffer.from('My app-specific header/identifier'))
plant(core)
})
})
returns Promise that resolves to the core when it's ready and stored.
Closes all associated resources and also all opened cores. Callback is invoked once the garden is fully closed. You should not use the garden instance after it's closed, rather initialize a new one. This method is part of the replic8 interface.
boolean indicator if this instance has been closed
Closes all resources related to the feed and throws away all refences to let it be garbage collected.
Also invokes core.close(cb) if the datastructure has a close function
Calling Garden#get(key) reopens the core.
returns a promise of a list of all metadata entries.
Returns a promise of a list of all cores and their metadata
Marks a key as banned in meta-data which causes garden to throw
BannedCoreError if the key is attempted to be added again.
By default the ban operation also purges all the cores files from storage. Returns a promise that fullfills when core has been successfully banned and optionally purged.
Returns a promise of a boolean.
Purges the storage from all files created by the core. Returns a promise that resolves once the operation has completed.
Returns a pomise of a list of all file-entries created by the key
CoreGarden does not attempt to provide replication for all stored cores,
but instead implements the following methods from the Replic8 interface:
share() shares all registered cores.
describe() Decorates shared cores with the result of getMeta(key)
accept() When invoked garden checks if supplied key exists, if not then it
creates the core.
resolve() Resolves known keys into core references.
Example how to replicate cores stored in Garden:
constreplic8=require('replic8')conststack=replic8(exchangeKey)stack.use(mGardenInstance)conststream=stack.replicate()GNU LGPL 3.0