Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 1c30125

Browse files
authored
Document batch (array and chained form), sync option and db refs (#556)
1 parent eaa2b28 commit 1c30125

2 files changed

Lines changed: 72 additions & 25 deletions

File tree

README.md

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,23 @@ If you don't want to use the prebuilt binary for the platform you are installing
5151
* [<code>db.<b>put()</b></code>](#leveldown_put)
5252
* [<code>db.<b>get()</b></code>](#leveldown_get)
5353
* [<code>db.<b>del()</b></code>](#leveldown_del)
54-
* [<code>db.<b>batch()</b></code>](#leveldown_batch)
54+
* [<code>db.<b>batch()</b></code> _(array form)_](#leveldown_batch)
55+
* [<code>db.<b>batch()</b></code> _(chained form)_](#leveldown_chainedbatch)
5556
* [<code>db.<b>approximateSize()</b></code>](#leveldown_approximateSize)
5657
* [<code>db.<b>compactRange()</b></code>](#leveldown_compactRange)
5758
* [<code>db.<b>getProperty()</b></code>](#leveldown_getProperty)
5859
* [<code>db.<b>iterator()</b></code>](#leveldown_iterator)
59-
* [<code>iterator.<b>next()</b></code>](#iterator_next)
60-
* [<code>iterator.<b>seek()</b></code>](#iterator_seek)
61-
* [<code>iterator.<b>end()</b></code>](#iterator_end)
60+
* [<code>chainedBatch</code>](#chainedbatch)
61+
* [<code>chainedBatch.<b>put()</b></code>](#chainedbatch_put)
62+
* [<code>chainedBatch.<b>del()</b></code>](#chainedbatch_del)
63+
* [<code>chainedBatch.<b>clear()</b></code>](#chainedbatch_clear)
64+
* [<code>chainedBatch.<b>write()</b></code>](#chainedbatch_write)
65+
* [<code>chainedBatch.<b>db</b></code>](#chainedbatch_db)
66+
* [<code>iterator</b></code>](#iterator)
67+
* [<code>iterator.<b>next()</b></code>](#iterator_next)
68+
* [<code>iterator.<b>seek()</b></code>](#iterator_seek)
69+
* [<code>iterator.<b>end()</b></code>](#iterator_end)
70+
* [<code>iterator.<b>db</b></code>](#iterator_db)
6271
* [<code>leveldown.<b>destroy()</b></code>](#leveldown_destroy)
6372
* [<code>leveldown.<b>repair()</b></code>](#leveldown_repair)
6473

@@ -152,18 +161,24 @@ The only property currently available on the `options` object is `sync` *(boolea
152161
The `callback` function will be called with no arguments if the operation is successful or with a single `error` argument if the operation failed for any reason.
153162

154163
<a name="leveldown_batch"></a>
155-
### `db.batch(operations[, options], callback)`
156-
<code>batch()</code> is an instance method on an existing database object. Used for very fast bulk-write operations (both *put* and *delete*). The `operations` argument should be an `Array` containing a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside LevelDB.
164+
### `db.batch(operations[, options], callback)` _(array form)_
157165

158-
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`. Any entries where the *type* is `'put'` that have a `'value'` of `undefined`, `null`, `[]`, `''` or `Buffer.alloc(0)` will be stored as a zero-length character array and therefore be fetched during reads as either `''` or `Buffer.alloc(0)` depending on how they are requested.
166+
Perform multiple _put_ and/or _del_ operations in bulk. The `operations` argument must be an `Array` containing a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation.
159167

160-
See [`levelup`](https://github.com/level/levelup#batch) for full documentation on how this works in practice.
168+
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.
161169

162-
#### `options`
170+
Any entries where the `key` or `value` (in the case of `'put'`) is `null` or `undefined` will cause an error to be returned on the `callback`. Any entries where the `type` is `'put'` that have a `value` of `[]`, `''` or `Buffer.alloc(0)` will be stored as a zero-length character array and therefore be fetched during reads as either `''` or `Buffer.alloc(0)` depending on how they are requested. See [`levelup`](https://github.com/level/levelup#batch) for full documentation on how this works in practice.
163171

164-
The only property currently available on the `options` object is `sync` *(boolean, default: `false`)*. See <a href="#leveldown_put">leveldown#put()</a> for details about this option.
172+
The optional `options` argument may contain:
165173

166-
The `callback` function will be called with no arguments if the operation is successful or with a single `error` argument if the operation failed for any reason.
174+
- `sync` *(boolean, default: `false`)*. See [`db.put()`](#leveldown_put) for details about this option.
175+
176+
The `callback` function will be called with no arguments if the batch is successful or with an `Error` if the batch failed for any reason.
177+
178+
<a name="leveldown_chainedbatch"></a>
179+
### `db.batch()` _(chained form)_
180+
181+
Returns a new [`chainedBatch`](#chainedbatch) instance.
167182

168183
<a name="leveldown_approximateSize"></a>
169184
### `db.approximateSize(start, end, callback)`
@@ -194,12 +209,9 @@ Currently, the only valid properties are:
194209
* <b><code>'leveldb.sstables'</code></b>: returns a multi-line string describing all of the *sstables* that make up contents of the current database.
195210

196211
<a name="leveldown_iterator"></a>
197-
### `iterator = db.iterator([options])`
198-
<code>iterator()</code> is an instance method on an existing database object. It returns a new **Iterator** instance.
212+
### `db.iterator([options])`
199213

200-
#### `options`
201-
202-
The optional `options` object may contain:
214+
Returns a new [`iterator`](#iterator) instance. The optional `options` object may contain:
203215

204216
* `gt` (greater than), `gte` (greater than or equal) define the lower bound of the values to be fetched and will determine the starting point where `reverse` is *not* `true`. Only records where the key is greater than (or equal to) this option will be included in the range. When `reverse` is `true` the order will be reversed, but the records returned will be the same.
205217

@@ -221,8 +233,44 @@ The optional `options` object may contain:
221233

222234
* `valueAsBuffer` *(boolean, default: `true`)*: Used to determine whether to return the `value` of each entry as a string or a Buffer.
223235

236+
<a name="chainedbatch"></a>
237+
### `chainedBatch`
238+
239+
<a name="chainedbatch_put"></a>
240+
#### `chainedBatch.put(key, value)`
241+
242+
Queue a `put` operation on this batch. This may throw if `key` or `value` is invalid, following the same rules as the [array form of `db.batch()`](#leveldown_batch).
243+
244+
<a name="chainedbatch_del"></a>
245+
#### `chainedBatch.del(key)`
246+
247+
Queue a `del` operation on this batch. This may throw if `key` is invalid.
248+
249+
<a name="chainedbatch_clear"></a>
250+
#### `chainedBatch.clear()`
251+
252+
Clear all queued operations on this batch.
253+
254+
<a name="chainedbatch_write"></a>
255+
#### `chainedBatch.write([options, ]callback)`
256+
257+
Commit the queued operations for this batch. All operations will be written atomically, that is, they will either all succeed or fail with no partial commits.
258+
259+
The optional `options` argument may contain:
260+
261+
- `sync` *(boolean, default: `false`)*. See [`db.put()`](#leveldown_put) for details about this option.
262+
263+
The `callback` function will be called with no arguments if the batch is successful or with an `Error` if the batch failed for any reason. After `write` has been called, no further operations are allowed.
264+
265+
<a name="chainedbatch_db"></a>
266+
#### `chainedBatch.db`
267+
268+
A reference to the `db` that created this chained batch.
269+
270+
### `iterator`
271+
224272
<a name="iterator_next"></a>
225-
### `iterator.next(callback)`
273+
#### `iterator.next(callback)`
226274
<code>next()</code> is an instance method on an existing iterator object, used to increment the underlying LevelDB iterator and return the entry at that location.
227275

228276
the `callback` function will be called with no arguments in any of the following situations:
@@ -239,15 +287,20 @@ Otherwise, the `callback` function will be called with the following 3 arguments
239287
* `value` - either a string or a Buffer depending on the `valueAsBuffer` argument when the `iterator()` was called.
240288

241289
<a name="iterator_seek"></a>
242-
### `iterator.seek(key)`
290+
#### `iterator.seek(key)`
243291
<code>seek()</code> is an instance method on an existing iterator object, used to seek the underlying LevelDB iterator to a given key.
244292

245293
By calling <code>seek(key)</code>, subsequent calls to <code>next(cb)</code> will return key/values larger or smaller than `key`, based on your <code>reverse</code> setting in the iterator constructor.
246294

247295
<a name="iterator_end"></a>
248-
### `iterator.end(callback)`
296+
#### `iterator.end(callback)`
249297
<code>end()</code> is an instance method on an existing iterator object. The underlying LevelDB iterator will be deleted and the `callback` function will be called with no arguments if the operation is successful or with a single `error` argument if the operation failed for any reason.
250298

299+
<a name="iterator_db"></a>
300+
#### `iterator.db`
301+
302+
A reference to the `db` that created this iterator.
303+
251304
<a name="leveldown_destroy"></a>
252305
### `leveldown.destroy(location, callback)`
253306
<code>destroy()</code> is used to completely remove an existing LevelDB database directory. You can use this function in place of a full directory *rm* if you want to be sure to only remove LevelDB-related files. If the directory only contains LevelDB files, the directory itself will be removed as well. If there are additional, non-LevelDB files in the directory, those files, and the directory, will be left alone.

chained-batch.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ ChainedBatch.prototype._clear = function () {
2020
}
2121

2222
ChainedBatch.prototype._write = function (options, callback) {
23-
// TODO (ensure docs covers the following)
24-
// Note that we're passing in options here, which we didn't do before. We
25-
// must do this so we can use the `sync` property, which we didn't handle before
26-
// since we never passed in an object at time of creation (bug) (the previous c++
27-
// used to assume we did this from the js side from the ChainedBatch()
28-
// constructor).
2923
binding.batch_write(this.context, options, callback)
3024
}
3125

0 commit comments

Comments
 (0)