Code Monkey home page Code Monkey logo

level-store's Introduction

level-store

A streaming storage engine based on LevelDB. It is

  • faster than the fs module
  • local in contrast to Amazon S3
  • streaming from the first byte in contrast to Amazon S3
  • appending values when desired
  • resuming reads when something failed
  • in-process

LevelDB Logo

Build Status

Only need in-memory persistence and no resuming? Check out enstore.


Usage

Store a file in LevelDB under the key file and read it out again:

var levelup = require('level');
var Store = require('level-store');
var fs = require('fs');

var store = Store(levelup('/tmp/level-store'));

fs.createReadStream(__dirname + '/file.txt')
  .pipe(store.createWriteStream('file'))
  .on('close', function () {
    // file.txt is stored in leveldb now
    store.createReadStream('file').pipe(process.stdout);
  });

Live persistence

If you want to persist a stream and read from it at the same time, without reading what didn't get stored yet, you can do it like this:

// first start reading from the stored version
storage.createReadStream('stream', { live: true }).pipe(someWhere);
// then put your stream into the store
stream.pipe(storage.createWriteStream('stream'));

Resuming

When reading fails you might not want to start over again completely but rather resume after the last chunk you received. First, pass index: true as an option so you don't only get the stored chunks but also their index in the store:

store.createReadStream('file', { index: true }).on('data', console.log);
// => { index: 1363783762087, data : <Buffer aa aa> }

Now you only need store the timestamp of the last read chunk in a variable and you can resume reading after an error, passing { gt: index }:

store
  .createReadStream('file', { gt: 1363783762087, index: true })
  .on('data', console.log);
// => { index: 1363783876109, data : <Buffer bb bb> }

Indexes

You can choose from several indexing mechanisms, which are from fastest to slowest:

  • timestamp: The default index. Uses timestamps of when a chunk was written. Fast and already enough for resuming. Activate with Store(db, { index : 'timestamp' }).
  • chunks: The index is the number of chunks already written, starting at 0. Activate with Store(db, { index : 'chunks' }).
  • TODO: bytelength: The index is the bytelength of what has already been written under the given key. Slow, but very flexible. Activate with Store(db, { index : 'bytelength' }).

Capped streams

If you don't want your stream to grow infinitely and it's ok to cut old parts off, use { capped : x } to limit to stream to x chunks:

store.createWriteStream('file', { capped : 3 }).write('...');

API

Store(db[, opts])

Returns a level-store instance.

db is an instance of LevelUp.

If opts.index is set, that indexing mechanism is used intead of the default one (timestamp).

store#createReadStream(key[, opts])

A readable stream that replays the stream stored at key.

Possible options are:

  • index (Boolean|String): If true, don't emit raw chunks but rather objects having index and data fields. If a String, override the index passed to Store().
  • gt (Number|String): Emit chunks that have been stored after the given position.
  • gte (Number|String): Emit chunks that have been stored at or after the given position.
  • lt (Number|String): Emit chunks that have been stored before the given position.
  • lte (Number|String): Emit chunks that have been stored at or before the given position.
  • live (Boolean): If true, the stream will stay open, emitting new data as it comes in.
  • reverse (Boolean): If true, chunks will be emitted in reverse order.

store#createWriteStream(key[, opts])

A writable stream that persists data written to it under key. If something exists under key already it will be deleted.

Possible options are:

  • append (Boolean): If true, possibly already existing data stored under key will be appended rather than replaced.
  • capped (Number): If set, cap the stream to x chunks.
  • index (String): Override the index passed to Store().

store#delete(key[, cb])

Delete everything stored under key.

store#exists(key, cb)

Check if key exists and call cb with (err, exists).

store#head(key[, opts], cb)

Get the last chunk stored under "key". "opts" are treated like in "db#createReadStream". "cb" gets called with "(err, chunk)".

store#append(key, value[, opts][, cb])

Sugar for appending just one value to key.

If opts.index is set that overrides the index passed to Store().

Installation

With npm do

$ npm install level-store

License

Copyright (c) 2013 Julian Gruber <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

level-store's People

Contributors

juliangruber avatar

Watchers

 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.