bytespace
Efficient keypath subspaces prefixed with bytewise tuples
Last updated 4 years ago by deanlandolt .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install bytespace 
SYNC missed versions from official npm registry.

bytespace

Efficient keypath subspaces prefixed with bytewise tuples.

build status

This library is very much like level-sublevel, and is API-compatible in several key way, but intents to be simpler in principle. A bytespace is essentially just a collection of wrappers over levelup methods to control key encoding behavior.

Prefixes are constructed as bytewise-encoded arrays, which allows you to use arbirarily complex keys in your subspace prefixes (any encoding supported by bytewise), and any keyEncoding you prefer for your subspace suffix keyspace.

Usage

var bytespace = require('bytespace')
var levelup = require('levelup')
var db = levelup('./mydb')

// all the standard levelup options
var options = {
  // keyEncoding defaults to "utf8" just like levelup
  keyEncoding: require('bytewise'),
  valueEncoding: 'json'
}

// same API as levelup
var appDb = bytespace(db, 'myapp', options)

// you can mount subspaces within subspaces
var nestedDb = bytespace(myapp, 'nested')

// namespace can be any bytewise-serializable value
var testDb = bytespace(appDb, new Date())

// subspaces may also be mounted sublevel-style
var subDb = testDb.sublevel('another')

Rooted keypaths

The subspace db instance itself is essentially a keyspace chroot -- a jail you cannot escape with just a reference to the subspace. While a subspace must be provided a reference to a backing db to initialize, this capability should not be surfaced on any properties or methods of the subspace. The capabilities of a subspace are restricted to the subset of of keyspace allocated to it.

Nested subspaces

When instantiating a subspace, it will test the provided db reference to determine if it's a bytespace instance. If so, it will call the sublevel method with the provided options to create the new subspace. Rather than running through the encode/decode process mulitple times, the responsibility of encoding and decoding keys is delegated to the root subspace. All keys will be correctly prefixed to the appropriate subspace.

The sublevel method is API-compatible with level-sublevel, though we also take an extra options argument to allow levelup db options to be provided to configure subspaces separate from their ancestor spaces.

Remote subspaces

Since bytespace is mostly just a set of levelup method wrappers this allows you to use over a multilevel-backed database, creating arbitrary subspaces on the client at runtime. If the multilevel client database has access to a createLiveStream method you can even create live streams observing ranges within your sublevel, all without the server having to know the sublevel layout ahead of time.

Hooks

Precommit and postcommit hooks are implemented using the pre and post methods from level-sublevel's API. The optional range argument is not yet implemented but it is otherwise API-compatible, allowing a bytespace instance with libraries expected a sublevel instance.

Encoding

Subspace keys are encoded as bytewise-prefixed arrays. This allows subspace keys to be appended as the last element of a namespace without overhead. Encoded subspace keys can be appended to the precomputed namespace buffer with a single Buffer.concat operation. Mounting a subspace adds another element to the prefix tuple. This serialization ensures that keys of different subspaces cannot interleave.

When encoding keys, the encoded namespace buffer can be efficiently concatenated with a subspace key. When decoding, the namespace portion can be sliced off and ignored. Testing for subspace inclusion is also just a single buffer slice.

Current Tags

  • 0.11.0                                ...           latest (4 years ago)

32 Versions

  • 0.11.0                                ...           4 years ago
  • 0.10.0                                ...           4 years ago
  • 0.9.5                                ...           4 years ago
  • 0.9.3                                ...           4 years ago
  • 0.9.2                                ...           4 years ago
  • 0.9.1                                ...           4 years ago
  • 0.9.0                                ...           4 years ago
  • 0.8.5                                ...           4 years ago
  • 0.8.4                                ...           4 years ago
  • 0.8.3                                ...           4 years ago
  • 0.8.2                                ...           4 years ago
  • 0.8.1                                ...           4 years ago
  • 0.8.0                                ...           4 years ago
  • 0.7.6                                ...           4 years ago
  • 0.7.5                                ...           4 years ago
  • 0.7.4                                ...           4 years ago
  • 0.7.3                                ...           4 years ago
  • 0.7.2                                ...           4 years ago
  • 0.7.1                                ...           4 years ago
  • 0.7.0                                ...           4 years ago
  • 0.6.4                                ...           4 years ago
  • 0.6.3                                ...           4 years ago
  • 0.6.2                                ...           4 years ago
  • 0.6.1                                ...           4 years ago
  • 0.6.0                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.1                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 33
Dependencies (4)
Dev Dependencies (9)

Copyright 2014 - 2016 © taobao.org |