1. 程式人生 > >Level/levelup-1-簡介

Level/levelup-1-簡介

https://github.com/Level/levelup

A node.js wrapper for abstract-leveldown compliant stores

一個為實現抽象leveldown相容儲存器的node.js封裝器

 

levelup

Introduction

Fast and simple storage. A Node.js wrapper for abstract-leveldown compliant stores, which follow the characteristics of LevelDB.

快速簡單儲存。一個為實現抽象leveldown相容儲存器的node.js封裝器,遵循了

LevelDB的特性

LevelDB is a simple key-value store built by Google. It's used in Google Chrome and many other products. LevelDB supports arbitrary byte arrays as both keys and values, singular get, put and delete operations, batched put and delete, bi-directional iterators and simple compression using the very fast 

Snappy algorithm.

LevelDB是一個Google構建的簡單的鍵值儲存。在Google Chrome和其他產品中被使用。LevelDB支援抽象位元組陣列作為鍵和值,單一get, put delete操作,批處理的put 和delete操作,雙向迭代和使用快速Snappy
演算法進行簡單壓縮

LevelDB stores entries sorted lexicographically by keys. This makes the streaming interface of levelup - which exposes LevelDB iterators as 

Readable Streams - a very powerful query mechanism.

LevelDB儲存按照鍵的字母順序進行排序的條目。這構造了levelup的流介面-暴露了levelup作為可讀流的迭代器-一個十分有效的查詢機制

The most common store is leveldown which provides a pure C++ binding to LevelDB. Many alternative stores are availablesuch as level.js in the browser or memdown for an in-memory store. They typically support strings and Buffers for both keys and values. For a richer set of data types you can wrap the store with encoding-down.

最普遍的儲存器是提供了存C++連結到LevelDBleveldown。有很多交替儲存是可用的,如在瀏覽器中的level.js,或者是記憶體儲存的memdown.他們的鍵和值基本上之處字串和Buffers型別。對於更豐富的資料型別集,你可以使用encoding-down來封裝該儲存

The level package is the recommended way to get started. It conveniently bundles levelup, leveldown and encoding-down. Its main export is levelup - i.e. you can do var db = require('level').

level包是推薦的入門方法。它有著levelup, leveldownencoding-down三部分。

主要的介面是level-比如使用var db = require('level')

 

 

Supported Platforms支援平臺

We aim to support Active LTS and Current Node.js releases as well as browsers. For support of the underlying store, please see the respective documentation.

我們的目的是支援可用的LTS和當前的node.js以及瀏覽器。為了支援下面的儲存,請檢視相應的文件

Sauce Test Status

 

Usage使用

If you are upgrading: please see UPGRADING.md.

如果更新:請看UPGRADING.md

First you need to install levelup! No stores are included so you must also install leveldown (for example).

首先安裝levelup!沒有包含的儲存器,所有還要安裝leveldown

$ npm install levelup leveldown

 

All operations are asynchronous. If you do not provide a callback, a Promise is returned.

所有操作都是非同步的。如果你麼有提供回撥函式,那麼將返回一個Promise

var levelup = require('levelup')
var leveldown = require('leveldown')

// 1) Create our store,建立儲存器
var db = levelup(leveldown('./mydb'))

// 2) Put a key & value,儲存鍵name&值levelup
db.put('name', 'levelup', function (err) {
  if (err) return console.log('Ooops!', err) // some kind of I/O error

  // 3) Fetch by key,通過鍵name獲取值levelup
  db.get('name', function (err, value) {
    if (err) return console.log('Ooops!', err) // likely the key was not found

    // Ta da!
    console.log('name=' + value)
  })
})

 

 

 API

詳細內容看本部落格Level/levelup-2-API

 

 

Promise Support

levelup ships with native Promise support out of the box.

帶有本地Promise的levelup支援開箱即用

Each function accepting a callback returns a promise if the callback is omitted. This applies for:

每一個接受回撥函式的函式,在回撥函式被省略時返回promise。其應用在:

  • db.get(key[, options])
  • db.put(key, value[, options])
  • db.del(key[, options])
  • db.batch(ops[, options])
  • db.batch().write()

The only exception is the levelup constructor itself, which if no callback is passed will lazily open the underlying store in the background.

唯一的特例是levelup自己的建構函式,如果沒有回撥函式將懶得在後臺開啟底層的儲存器

Example:

var db = levelup(leveldown('./my-db'))

db.put('foo', 'bar') .then(function () { return db.get('foo') }) .then(function (value) { console.log(value) }) .catch(function (err) { console.error(err) })

Or using async/await:

const main = async () => {
  const db = levelup(leveldown('./my-db')) await db.put('foo', 'bar') console.log(await db.get('foo')) }

 

 

Events事件

levelup is an EventEmitter and emits the following events.

Event Description Arguments
put Key has been updated key, value (any)
del Key has been deleted key (any)
batch Batch has executed operations (array)
opening Underlying store is opening -
open Store has opened -
ready Alias of open -
closing Store is closing -
closed Store has closed. -

For example you can do:

db.on('put', function (key, value) {
  console.log('inserted', { key, value })
})

 

 

Extending

A list of Level modules and projects can be found in the wiki. We are in the process of moving all this to awesome.

在wiki中可以找到一個級別模組和專案列表。我們正在把這一切變得更棒 

 

 

Multi-process Access

Stores like LevelDB are thread-safe but they are not suitable for accessing with multiple processes. You should only ever have a store open from a single Node.js process. Node.js clusters are made up of multiple processes so a levelup instance cannot be shared between them either.

像LevelDB的儲存器是執行緒安全的,但是當訪問多程序時它是不適用的。你應該只從單Node.js程序開啟該儲存。Node.js叢集由多程序組成,所以levelup例項也不能夠在他們之間共享

See the aformentioned wiki for modules like multilevel, that may help if you require a single store to be shared across processes.

有關multilevel之類的模組,請參閱規範的wiki,如果您需要跨程序共享單個儲存,這可能會有所幫助。