API - mtth/pal GitHub Wiki

Available databases:

  • Db, generic class supporting arbitrary serialization.
  • AvroDb, optimized for Avro serialization.

Class pal.Db(path, [opts])

  • path {String} Path to data file.
  • opts {Object} Options:
    • codecs {Object} Codecs {keyCodec, valueCodec}. These should match those used to create the file. Defaults to JSON serialization.

Load a store from an existing file. See Db.createWriteStream for how to create such a file.

db.getStatistics()

Get database information, for example:

{
  creationDate: 'Sat Nov 28 2015 21:06:49 GMT-0800 (PST)',
  numValues: 13891, // Total number of values in the store.
  indexSize: 92098, // Index size in bytes.
  dataSize: 9078324 // Data size in bytes.
}

db.get(key, [defaultValue])

  • key {...} Key to get.
  • defaultValue {...} Value returned when the key isn't found.

Retrieve a key's value.

db.createReadStream()

Get a readable stream of all the store's keys and values ({key, value}).

Db.createWriteStream(path, [opts,] [cb])

  • path {String} File path, where the file will be stored.
  • opts {Object} Options.
    • codecs {Object} Codecs {keyCodec, valueCodec}. The default is JSON serialization.
    • loadFactor {Number} Index load factor. Closer to 1 will yield a more compact index at the cost of more collisions. Defaults to 0.6.
    • noDistinct {Boolean} Allow the last value written for a key to overwrite previous ones (or delete an existing key if undefined is passed as value). By default, an error is thrown when a duplicate key is encountered.
    • compactionThreshold {Number} Only applicable when noDistinct is set. Ratio of active values to all values under which the store will be compacted (e.g. 0.9 would trigger compaction when 10% of values are orphaned). Defaults to 0.8.
  • cb(err) {Function} Function called when the store has been created. If an error occurred, it will be passed as single argument.

Returns a writable stream which will create a store on 'end'. It expects objects {key, value}.

Class pal.AvroDb(path)

  • path {String} Path to data file.

Subclass of Db, all its prototype methods are available. Note that Avro codecs are embedded in the data file, so there is no need to specify them here.

AvroDb.createWriteStream(path, schemas, [opts,] [cb])

  • path {String} File path, where the file will be stored.
  • schemas {Object} Key and value schemas {keySchema, valueSchema}.
  • opts {Object} Options.
    • loadFactor {Number} Index load factor. Closer to 1 will yield a more compact index at the cost of more collisions. Defaults to 0.6.
    • noDistinct {Boolean} Allow the last value written for a key to overwrite previous ones (or delete an existing key if undefined is passed as value). By default, an error is thrown when a duplicate key is encountered.
    • compactionThreshold {Number} Only applicable when noDistinct is set. Ratio of active values to all values under which the store will be compacted (e.g. 0.9 would trigger compaction when 10% of values are orphaned). Defaults to 0.8.
  • cb(err) {Function} Function called when the store has been created. If an error occurred, it will be passed as single argument.

Returns a writable stream which will create a store on 'end'. It expects objects {key, value}.