Code Monkey home page Code Monkey logo

sublevelup's Introduction

SublevelUP

Separated sections of LevelUP.

SublevelUP models "sublevel" as separated table sections, providing idiomatic mapping to both key prefix sublevels and table based backends.

SublevelUP is a "subclass" of LevelUP, which means full compatibility with latest LevelUP interface.

Build Status

npm install sublevelup

Key-prefix sublevel

The "standard" way of creating sublevels.

Given the fact that LevelDB stores entries lexicographically sorted by keys, it is possible to creating a "sandboxed" section using ranged key prefix. Unlike separated LevelDB instances, atomic batched write works across sublevels, which is an important primitive for consistency.

SublevelUP encodes key prefix using ! padding with # separator. That means nested sublevels are also separated.

var level = require('level')
var sublevel = require('sublevelup')

//Key-prefix: passing LevelUP to Sublevel
var db = sublevel(level('./db')) //prefix !!

var hello = sublevel(db, 'hello') //prefix !hello!
var foo = db.sublevel('foo') //prefix !foo!
var fooBar = sublevel(foo, 'bar') //prefix !foo#bar!
var fooBarBla = fooBar.sublevel('bla') //prefix !foo#bar#bla!

Table-based sublevel

The prefix approach, while a good fit for LevelDB, ill-suited for using SQL database as a LevelUP backend. Concatenating every sublevels into one single table is not ideal for B-tree index that most SQL database uses. There is also no reason not to utilise tables since they serve the exact same purpose.

SublevelUP provides idiomatic mapping to tables based LevelDOWN modules where location argument defines the table DOWN(table). Atomic batched write works across table-based sublevels, given most SQL databases support transactions across tables.

Table prefix are encoded using _ separator.

MyDOWN for example, a MySQL backend suits well as a table-based sublevel.

var sublevel = require('sublevelup')
var mydown = require('mydown')('db', {
  host: 'localhost',
  user: 'root',
  password: 'secret'
})

//Table-based: passing LevelDOWN to Sublevel
var db = sublevel(mydown) //table _

var hello = sublevel(db, 'hello') //table hello
var foo = db.sublevel('foo') //table foo
var fooBar = sublevel(foo, 'bar') //table foo_bar
var fooBarBla = fooBar.sublevel('bla') //table foo_bar_bla

Batch prefix

batch() is a transactional operation that works across sublevels, by setting the prefix: sub property.

var db = sublevel(mydown)
var a = db.sublevel('a')
var b = db.sublevel('b')

// batch from a
a.batch([
  { type: 'put', key: 'foo', value: 'a' },
  { type: 'put', key: 'foo', value: 'b', prefix: b }, //put into b
], function () {
  a.get('foo', function (err, val) { }) // val === 'a'
  b.get('foo', function (err, val) { }) // val === 'b'
})

Custom encoding

It is possible to create custom codec for sublevel prefix or table name by passing options.prefixEncoding for encode/decode function, such as bytewise:

var level = require('level')
var sublevel = require('sublevelup')

var bytewise = require('bytewise-core')
var codec = {
  encode: function (arr) {
    return bytewise.encode(arr).toString('binary')
  },
  decode: function (str) {
    return bytewise.decode(new Buffer(str, 'binary'))
  }
}

//Key-prefix Sublevel with custom codec
var db = sublevel(level('./db'), { prefixEncoding: codec })

API

Sublevel inherits methods of LevelUP plus following:

sub = sublevel(db, [name], [options])

db is a

  • LevelUP instance for key-prefix sublevel
  • LevelDOWN constructor for table-based sublevel
  • Sublevel instance for nesting sublevel under name

sub = db.sublevel(name, [options])

Nesting sublevel under name.

License

MIT

sublevelup's People

Contributors

cshum avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.