Level/levelup-2-API
https://github.com/Level/levelup
Special Notes
levelup(db[, options[, callback]])
The main entry point for creating a new levelup
instance.
创建新的levelup
实例的主要入口
db
must be anabstract-leveldown
compliant store. db必须是抽象leveldown兼容存储器options
is passed on to the underlying store when opened and is specific to the type of store being used 在开启时options值被传递给其下面的存储器,并且指定被使用的存储器的类型
Calling levelup(db)
will also open the underlying store. This is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form function (err, db) {}
where db
is the levelup
instance. If you don't provide a callback, any read & write operations are simply queued internally until the store is fully opened.
调用levelup(db)也
将会开启底层存储器。这是个异步操作,如果你提供了回调函数将会被触发。这个回调将使用function (err, db) {}
格式,db即levelup
实例。如果你没有提供回调函数,任何读写操作将仅进行简单内部排序,直至存储器完全打开
This leads to two alternative ways of managing a levelup
instance:
这将导致两种可选的处理levelup
实例的方法:
levelup(leveldown(location), options, function (err, db) {
if (err) throw err db.get('foo', function (err, value) {
if (err) return console.log('foo does not exist')
console.log('got foo =', value)
})
})
Versus the equivalent:
与之相等的是:
// Will throw if an error occurs
var db = levelup(leveldown(location), options) db.get('foo', function (err, value) {
if (err) return console.log('foo does not exist')
console.log('got foo =', value)
})
db.open([callback])
Opens the underlying store. In general you should never need to call this method directly as it's automatically called by levelup()
.
打开底层的存储器。通常来说,你应该不需要直接调用这个方法,因为它会自动被levelup()函数调用
However, it is possible to reopen the store after it has been closed with close()
, although this is not generally advised.
可是,在被close()函数关闭后还是可以使用这个重启存储器的,虽然通常是不推荐使用的
If no callback is passed, a promise is returned.
如果没有传递回调函数,将返回promise
db.close([callback])
close()
closes the underlying store. The callback will receive any error encountered during closing as the first argument.
close()
函数将关闭底层存储器。这个回调函数将收到在关闭时任何遇见的错误,并将其作为第一个参数
You should always clean up your levelup
instance by calling close()
when you no longer need it to free up resources. A store cannot be opened by multiple instances of levelup
simultaneously.
在你不在需要levelup
实例去释放资源时,你应该总是要通过调用 close()
函数来清理它
If no callback is passed, a promise is returned.
如果没有传递回调函数,将返回promise
db.put(key, value[, options][, callback])
put()
is the primary method for inserting data into the store. Both key
and value
can be of any type as far as levelup
is concerned.
put()
函数是主要的插入数据到存储器的方法。key
和value
可为任意levelup
包含的类型
options
is passed on to the underlying store.
options值将会传递给底层的存储器
If no callback is passed, a promise is returned.
如果没有传递回调函数,将返回promise
db.get(key[, options][, callback])
get()
is the primary method for fetching data from the store. The key
can be of any type. If it doesn't exist in the store then the callback or promise will receive an error. A not-found err object will be of type 'NotFoundError'
so you can err.type == 'NotFoundError'
or you can perform a truthy test on the property err.notFound
.
get()
函数是主要的从存储器中获取数据的方法。key值可以是任意类型。如果它不存在于存储器中,那么回调函数或promise将收到error。一个没有找到的错误将被定义为'NotFoundError'
方法,所以你可以err.type == 'NotFoundError'
或在属性err.notFound
中执行一个可信的测试,如下
db.get('foo', function (err, value) {
if (err) {
if (err.notFound) {
// handle a 'NotFoundError' here
return
}
// I/O or other error, pass it up the callback chain
return callback(err)
} // .. handle `value` here
})
options
is passed on to the underlying store.
options值将会传递给底层的存储器
If no callback is passed, a promise is returned.
如果没有传递回调函数,将返回promise
db.del(key[, options][, callback])
del()
is the primary method for removing data from the store.
del()
是主要的从存储器中移除数据的方法
db.del('foo', function (err) {
if (err)
// handle I/O or other error
});
options
is passed on to the underlying store.
options值将会传递给底层的存储器
If no callback is passed, a promise is returned.
如果没有传递回调函数,将返回promise
db.batch(array[, options][, callback])
(array form)
batch()
can be used for very fast bulk-write operations (both put and delete). The array
argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside the underlying store.
为了快速进行块写操作(如 put和delete操作),可以使用batch()
函数。array
参数应该包含顺序执行的操作序列,虽然在底层的存储器中他们被当作一个整体,即一个原子操作来执行
Each operation is contained in an object having the following properties: type
, key
, value
, where the type is either 'put'
or 'del'
. In the case of 'del'
the value
property is ignored. Any entries with a key
of null
or undefined
will cause an error to be returned on the callback
and any type: 'put'
entry with a value
of null
or undefined
will return an error.
每一个被包含在对象中的操作都有这如下属性:type
, key
, value
,type
要么是put,要么是del。在del的情况下,value将会被忽略。任何带着key为null或undefined的条目将导致在callback
函数中返回一个错误。而任何带着value为null或undefined的条目的type为put的操作将返回一个错误
var ops = [
{ type: 'del', key: 'father' },
{ type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' },
{ type: 'put', key: 'dob', value: '16 February 1941' },
{ type: 'put', key: 'spouse', value: 'Kim Young-sook' },
{ type: 'put', key: 'occupation', value: 'Clown' }
] db.batch(ops, function (err) {//即进行批处理
if (err) return console.log('Ooops!', err)
console.log('Great success dear leader!')
})
options
is passed on to the underlying store.
options值将会传递给底层的存储器
If no callback is passed, a promise is returned.
如果没有传递回调函数,将返回promise
db.batch()
(chained form)
batch()
, when called with no arguments will return a Batch
object which can be used to build, and eventually commit, an atomic batch operation. Depending on how it's used, it is possible to obtain greater performance when using the chained form of batch()
over the array form.
batch()
函数当不带着参数调用时将返回一个用于构建的Batch
对象,并且最后将提交一个原子批处理操作。当使用batch()
的链接形式来覆盖其数组格式时,获取更高的性能是有可能的,这将取决于它是怎么使用的
db.batch()
.del('father')
.put('name', 'Yuri Irsenovich Kim')
.put('dob', '16 February 1941')
.put('spouse', 'Kim Young-sook')
.put('occupation', 'Clown')
.write(function () { console.log('Done!') })
batch.put(key, value)
Queue a put operation on the current batch, not committed until a write()
is called on the batch.
在当前的batch对象下排序一个put操作,不提交,直至write()
函数被调用
This method may throw
a WriteError
if there is a problem with your put (such as the value
being null
or undefined
).
如果这里带着put函数的问题(如value
值为null或undefined),这个方法将抛出一个WriteError
batch.del(key)
Queue a del operation on the current batch, not committed until a write()
is called on the batch.
在当前的batch对象下排序一个del操作,不提交,直至write()
函数被调用
This method may throw
a WriteError
if there is a problem with your delete.
如果这里带着delete操作的问题,这个方法将抛出一个WriteError
batch.clear()
Clear all queued operations on the current batch, any previous operations will be discarded.
在当前的batch对象下清理所有排序操作,任何以前的操作将被抛弃
batch.length
The number of queued operations on the current batch.
在当前的batch对象下排序的操作数目
batch.write([options][, callback])
Commit the queued operations for this batch. All operations not cleared will be written to the underlying store atomically, that is, they will either all succeed or fail with no partial commits.
提交batch对象中排序的操作。所有没有没清除的操作将会被自动写到底层的存储器中,这个操作要么全部成功要么是没有部分提交的失败
The optional options
object is passed to the .write()
operation of the underlying batch object.
可选的options
对象被传递给底层的batch对象的.write()
操作
If no callback is passed, a promise is returned.
如果没有传递回调函数,将返回promise
db.isOpen()
A levelup
instance can be in one of the following states:
levelup
实例可能是如下的几种状态之一:
- "new" - newly created, not opened or closed 新创建的,没被打开或关闭过
- "opening" - waiting for the underlying store to be opened 等待底层的存储器被打开
- "open" - successfully opened the store, available for use 成功打开存储器,可以使用
- "closing" - waiting for the store to be closed 等待存储器被关闭
- "closed" - store has been successfully closed, should not be used 存储器被成功关闭,不应该被使用
isOpen()
will return true
only when the state is "open".
isOpen()
函数将只在状态为"open"时返回true
db.isClosed()
See isOpen()
有关状态的说法看
isOpen()
isClosed()
will return true
only when the state is "closing" or "closed", it can be useful for determining if read and write operations are permissible.
isClosed()函数将只在状态为 "closing"或"closed"时返回true,它可以被用于决定是否读写操作被允许
db.createReadStream([options])
Returns a Readable Stream of key-value pairs. A pair is an object with key
and value
properties. By default it will stream all entries in the underlying store from start to end. Use the options described below to control the range, direction and results.
返回一个键值对的可读流。一对是一个带着key和
value
属性的对象。默认情况下,它将从头到尾流遍底层存储中的所有条目。使用下面描述的options去控制范围、方向和结果。
db.createReadStream()
.on('data', function (data) {
console.log(data.key, '=', data.value)
})
.on('error', function (err) {
console.log('Oh my!', err)
})
.on('close', function () {
console.log('Stream closed')
})
.on('end', function () {
console.log('Stream ended')
})
You can supply an options object as the first parameter to createReadStream()
with the following properties:
你可以使用任何带着下面的属性的options对象作为createReadStream()
函数的第一个参数
gt
(greater than),gte
(greater than or equal) define the lower bound of the range to be streamed. Only entries where the key is greater than (or equal to) this option will be included in the range. Whenreverse=true
the order will be reversed, but the entries streamed will be the same.gt
(大于),gt
e(大于或等于)定义了流范围的下界。只有key大于(或等于)这个option的条目会被包含进这个范围。当reverse=true,这个顺序将会被反转,但是条目流是相同的
lt
(less than),lte
(less than or equal) define the higher bound of the range to be streamed. Only entries where the key is less than (or equal to) this option will be included in the range. Whenreverse=true
the order will be reversed, but the entries streamed will be the same.lt(小于),
lte
(小于或等于)定义了流范围的上界。只有key小于(或等于)这个option的条目会被包含进这个范围。当reverse=true,这个顺序将会被反转,但是条目流是相同的
reverse
(boolean, default:false
): stream entries in reverse order. Beware that due to the way that stores like LevelDB work, a reverse seek can be slower than a forward seek. 以相反顺序输入流条目。注意,因为像LevelDB这样的存储器的工作方式,反向查找将慢于正向查找limit
(number, default:-1
): limit the number of entries collected by this stream. This number represents a maximum number of entries and may not be reached if you get to the end of the range first. A value of-1
means there is no limit. Whenreverse=true
the entries with the highest keys will be returned instead of the lowest keys. 限制被流选择的条目的数量。这个数量代表了条目的最大数量,如果先到了范围的结尾处,它可能不会被触发。-1值意味着这里没有限制。当设置reverse=true
,条目将会从最高值的keys的方向被返回,而不是最低的keys方向。keys
(boolean, default:true
): whether the results should contain keys. If set totrue
andvalues
set tofalse
then results will simply be keys, rather than objects with akey
property. Used internally by thecreateKeyStream()
method. 是否结果应该包含keys。如果设置为true,且values设置为false,结果将只有keys,而不是带有key属性的对象。通过createKeyStream()
方法内部使用values
(boolean, default:true
): whether the results should contain values. If set totrue
andkeys
set tofalse
then results will simply be values, rather than objects with avalue
property. Used internally by thecreateValueStream()
method. 是否结果应该包含values。如果设置为true,且keys设置为false,结果将只有values,而不是带有value属性的对象。通过createKeyStream()
方法内部使用
Legacy options:遗留options
start
: instead usegte 可用于替换
gte
end
: instead uselte 可用于替换
lte
db.createKeyStream([options])
Returns a Readable Stream of keys rather than key-value pairs. Use the same options as described for createReadStream
to control the range and direction.
返回一个keys的可读流,而不是键值对。使用和在createReadStream
中描述的相同的options去控制范围了方向
You can also obtain this stream by passing an options object to createReadStream()
with keys
set to true
and values
set to false
. The result is equivalent; both streams operate in object mode.
你也可以通过传递一个keys设置为true,values
设置为false的options对象给createReadStream()来获得该流。这两个的结果是相等的;他们的流都在对象模块中被操作
db.createKeyStream()
.on('data', function (data) {
console.log('key=', data)
}) // same as:
db.createReadStream({ keys: true, values: false })
.on('data', function (data) {
console.log('key=', data)
})
db.createValueStream([options])
Returns a Readable Stream of values rather than key-value pairs. Use the same options as described for createReadStream
to control the range and direction.
返回一个values的可读流,而不是键值对。使用和在createReadStream
中描述的相同的options去控制范围了方向
You can also obtain this stream by passing an options object to createReadStream()
with values
set to true
and keys
set to false
. The result is equivalent; both streams operate in object mode.
你也可以通过传递一个keys设置为false,values
设置为true的options对象给createReadStream()来获得该流。这两个的结果是相等的;他们的流都在对象模块中被操作
db.createValueStream()
.on('data', function (data) {
console.log('value=', data)
}) // same as:
db.createReadStream({ keys: false, values: true })
.on('data', function (data) {
console.log('value=', data)
})
db.iterator([options])
Returns an abstract-leveldown
iterator, which is what powers the readable streams above. Options are the same as the range options of createReadStream()
and are passed to the underlying store.
返回一个抽象leveldown迭代器,用于加强上面的可读流。其options与createReadStream()的范围options是相同的,并且被传递给底层的存储器
What happened to db.createWriteStream
?
db.createWriteStream()
has been removed in order to provide a smaller and more maintainable core. It primarily existed to create symmetry with db.createReadStream()
but through much discussion, removing it was the best course of action.
为了提供一个更小且可保存的代码,db.createWriteStream()
就被移除了。它主要存在是为了与db.createReadStream()
方法对称,但是通过更多的讨论后,移除它会更好。
The main driver for this was performance. While db.createReadStream()
performs well under most use cases, db.createWriteStream()
was highly dependent on the application keys and values. Thus we can't provide a standard implementation and encourage more write-stream
implementations to be created to solve the broad spectrum of use cases.
移除它的主要驱动是其性能。在大多数情况下,db.createReadStream()
执行得很好,db.createWriteStream()
则更多依赖于应用的keys和values。因此,我们不能提供一个标准的实现,并鼓励创建更多的写流实现来解决广泛的用例范围
Check out the implementations that the community has already produced here.
请查看社区已经在here生成的实现。
Level/levelup-2-API的更多相关文章
- Level/levelup-1-简介
https://github.com/Level/levelup A node.js wrapper for abstract-leveldown compliant stores 一个为实现抽象le ...
- Kafka实战系列--Kafka API使用体验
前言: kafka是linkedin开源的消息队列, 淘宝的metaq就是基于kafka而研发. 而消息队列作为一个分布式组件, 在服务解耦/异步化, 扮演非常重要的角色. 本系列主要研究kafka的 ...
- tesseract api C++使用例子
转自:https://code.google.com/p/tesseract-ocr/wiki/APIExample APIExample API examples Updated Aug 12, 2 ...
- Process Order API - How To Scripts
In this Document Purpose Questions and Answers References APPLIES TO: Oracle Order Management ...
- criteo marketing api 相关
官网登陆地址:https://marketing.criteo.com/ 官网api介绍:https://marketing.criteo.com/e/s/article?article=360001 ...
- Elasticsearch Java API的基本使用
说明 在明确了ES的基本概念和使用方法后,我们来学习如何使用ES的Java API. 本文假设你已经对ES的基本概念已经有了一个比较全面的认识. 客户端 你可以用Java客户端做很多事情: 执行标准的 ...
- (转载)中文Appium API 文档
该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...
- HTML5 Communication API
本文探讨用于构建实时(real-time)跨域(cross-origin)通信的两个重要模块:跨文档消息通讯和XMLHttpRequest Level 2.通过它们,我们可以构建引人注目的Web应用, ...
- Dynamics CRM 2015 Update 1 系列(3): API的那些事 - Old APIs VS New APIs
今天我们来看看API的变化.新系统中,去掉了一些经常使用的数据处理API,比如:SetStateRequest, SetBusinessUnitRequest, SetParentBusinessUn ...
随机推荐
- 基于Spark GraphX计算二度关系
关系计算问题描述 二度关系是指用户与用户通过关注者为桥梁发现到的关注者之间的关系.目前微博通过二度关系实现了潜在用户的推荐.用户的一度关系包含了关注.好友两种类型,二度关系则得到关注的关注.关注的好友 ...
- 7. Reverse Integer 反向输出整数 easy 9. Palindrome Number 判断是否是水仙花数 easy
Given a 32-bit signed integer, reverse digits of an integer. 将32位整数反向输出. Note:Assume we are dealing ...
- 第N次学习javaIO之后
io按流分 输入流.输出流 io按类型分(是类型吧) 字节流.字符流 ------------------------------------- 先说说一直以来混淆什么时候用输入流,什么时候用输出流. ...
- Java SE 8 的流库学习笔记
前言:流提供了一种让我们可以在比集合更高的概念级别上指定计算的数据视图.如: //使用foreach迭代 long count = 0; for (String w : words) { if (w. ...
- CentOS-Linux系统下安装Tomcat
步骤1:解压Tomcat 命令: unzip apache-tomcat-8.5.20.zip 步骤2:将tomcat 移动到“/usr/local/src/java/tomcat8.5”下并重命名 ...
- python学习之老男孩python全栈第九期_day017知识点总结——初识递归、算法
一. 递归函数 如果一个函数在内部调用自身本身,这个函数就是递归函数. 最大递归深度默认是997 -- python从内存角度出发做得限制(而不是程序真的报错),最大深度可以修改 def func(n ...
- 关于iFrame特性总计和iFrame跨域解决办法
1.iframe 定义和用法 iframe 元素会创建包含另外一个文档的内联框架(即行内框架). HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML 1. ...
- 51NOD1847:奇怪的数学题
传送门 Sol 设 \(f(d)\) 表示 \(d\) 所有约数中第二大的,\(low_d\) 表示 \(d\) 的最小质因子 \[f(d)=\frac{d}{low_d}\] 那么 \[\sum_{ ...
- JavaScript - 收藏集 - 掘金
Angular 中的响应式编程 -- 浅淡 Rx 的流式思维 - 掘金第一节:初识Angular-CLI第二节:登录组件的构建第三节:建立一个待办事项应用第四节:进化!模块化你的应用第五节:多用户版本 ...
- BAT脚本编写教程简单入门篇
BAT脚本编写教程简单入门篇 批处理文件最常用的几个命令: echo表示显示此命令后的字符 echo on 表示在此语句后所有运行的命令都显示命令行本身 echo off 表示在此语句后所有运行的命 ...