You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`'compression'`*(boolean, default: `true`)*: If `true`, all *compressible* data will be run through the Snappy compression algorithm before being stored. Snappy is very fast and shouldn't gain much speed by disabling so leave this on unless you have good reason to turn it off.
191
193
192
-
*`'cacheSize'`*(number, default: `8 * 1024 * 1024`)*: The size (in bytes) of the in-memory [LRU](http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used) cache with frequently used uncompressed block contents.
194
+
*`'cacheSize'`*(number, default: `8 * 1024 * 1024`)*: The size (in bytes) of the in-memory [LRU](http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used) cache with frequently used uncompressed block contents.
193
195
194
196
*`'keyEncoding'` and `'valueEncoding'`*(string, default: `'utf8'`)*: The encoding of the keys and values passed through Node.js' `Buffer` implementation (see [Buffer#toString()](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end)).
195
197
<p><code>'utf8'</code> is the default encoding for both keys and values so you can simply pass in strings and expect strings from your <code>get()</code> operations. You can also pass <code>Buffer</code> objects as keys and/or values and conversion will be performed.</p>
@@ -251,7 +253,7 @@ db.get('foo', function (err, value) {
251
253
252
254
Encoding of the `key` object will adhere to the `'keyEncoding'` option provided to <ahref="#ctor"><code>levelup()</code></a>, although you can provide alternative encoding settings in the options for `get()` (it's recommended that you stay consistent in your encoding of keys and values in a single store).
253
255
254
-
LevelDB will by default fill the in-memory LRU Cache with data from a call to get. Disabling this is done by setting `fillCache` to `false`.
256
+
LevelDB will by default fill the in-memory LRU Cache with data from a call to get. Disabling this is done by setting `fillCache` to `false`.
A **WriteStream** can be obtained by calling the `createWriteStream()` method. The resulting stream is a complete Node.js-style [Writable Stream](http://nodejs.org/docs/latest/api/stream.html#stream_writable_stream) which accepts objects with `'key'` and `'value'` pairs on its `write()` method.
462
-
463
-
The WriteStream will buffer writes and submit them as a `batch()` operations where writes occur *within the same tick*.
The standard `write()`, `end()`, `destroy()` and `destroySoon()` methods are implemented on the WriteStream. `'drain'`, `'error'`, `'close'` and `'pipe'` events are emitted.
483
-
484
-
You can specify encodings both for the whole stream and individual entries:
460
+
<aname="writeStreams"></a>
461
+
#### What happened to `db.createWriteStream`?
485
462
486
-
To set the encoding for the whole stream, provide an options object as the first parameter to `createWriteStream()` with `'keyEncoding'` and/or `'valueEncoding'`.
487
-
488
-
To set the encoding for an individual entry:
489
-
490
-
```js
491
-
writeStream.write({
492
-
key :newBuffer([1, 2, 3])
493
-
, value : { some:'json' }
494
-
, keyEncoding :'binary'
495
-
, valueEncoding :'json'
496
-
})
497
-
```
498
-
499
-
#### write({ type: 'put' })
500
-
501
-
If individual `write()` operations are performed with a `'type'` property of `'del'`, they will be passed on as `'del'` operations to the batch.
502
-
503
-
```js
504
-
var ws =db.createWriteStream()
505
-
506
-
ws.on('error', function (err) {
507
-
console.log('Oh my!', err)
508
-
})
509
-
ws.on('close', function () {
510
-
console.log('Stream closed')
511
-
})
512
-
513
-
ws.write({ type:'del', key:'name' })
514
-
ws.write({ type:'del', key:'dob' })
515
-
ws.write({ type:'put', key:'spouse' })
516
-
ws.write({ type:'del', key:'occupation' })
517
-
ws.end()
518
-
```
519
-
520
-
#### db.createWriteStream({ type: 'del' })
521
-
522
-
If the *WriteStream* is created with a `'type'` option of `'del'`, all `write()` operations will be interpreted as `'del'`, unless explicitly specified as `'put'`.
A ReadStream can be piped directly to a WriteStream, allowing for easy copying of an entire database. A simple `copy()` operation is included in LevelUP that performs exactly this on two open databases:
The ReadStream is also [fstream](https://github.com/isaacs/fstream)-compatible which means you should be able to pipe to and from fstreams. So you can serialize and deserialize an entire database to a directory where keys are filenames and values are their contents, or even into a *tar* file using [node-tar](https://github.com/isaacs/node-tar). See the [fstream functional test](https://github.com/rvagg/node-levelup/blob/master/test/functional/fstream-test.js) for an example. *(Note: I'm not really sure there's a great use-case for this but it's a fun example and it helps to harden the stream implementations.)*
553
-
554
-
KeyStreams and ValueStreams can be treated like standard streams of raw data. If `'keyEncoding'` or `'valueEncoding'` is set to `'binary'` the `'data'` events will simply be standard Node `Buffer` objects straight out of the data store.
463
+
Yes we have removed `db.createWriteStream` but not to worry, there is good reason for this. *Disclaimper*: if you are not in a stage where you are worried about performance but want a streaming interface
464
+
into your database, please checkout [`level-ws`][level-ws].
555
465
466
+
TODO: talk about performance and multiple writeStream implementations
@@ -614,7 +525,7 @@ require('leveldown').destroy('./huge.db', function (err) { console.log('done!')
614
525
615
526
> If a DB cannot be opened, you may attempt to call this method to resurrect as much of the contents of the database as possible. Some data may be lost, so be careful when calling this function on a database that contains important information.
616
527
617
-
You will find information on the *repair* operation in the *LOG* file inside the store directory.
528
+
You will find information on the *repair* operation in the *LOG* file inside the store directory.
618
529
619
530
A `repair()` can also be used to perform a compaction of the LevelDB log into table files.
620
531
@@ -736,3 +647,5 @@ LevelUP is licensed under the MIT license. All rights not explicitly granted in
736
647
737
648
=======
738
649
*LevelUP builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the [New BSD Licence](http://opensource.org/licenses/BSD-3-Clause).*
0 commit comments