Raynos / level-write-stream

A writeStream implementation for leveldb
MIT License
12 stars 2 forks source link

use writev in streams2 #2

Open mafintosh opened 9 years ago

mafintosh commented 9 years ago

In the later version of readable-stream (1.1 and above) there is a new write method available called ._writev that you can implement if you support batched writes (which leveldb does!). Basically it allows us to do something like this:

var ws = new stream.Writable({objectMode: true})

ws._writev = function (chunks, cb) {
  // convert chunks to a leveldb batch
  db.batch(batch, cb)
}

ws._write = function (data, enc, cb) {
  db.put(data.key, data.value, cb)
}

The _writev method will be called with all the objects currently buffered in the stream (usually the same as highWaterMark) waiting to be written. This means that all pending writes will be added to a leveldb batch.

If this is something you'd be interested in using for level-write-stream I'd be happy to send a PR

yoshuawuyts commented 9 years ago

This would be rad. @mafintosh are you still interested in writing a patch? If you're not interested, I could give it a shot.

mafintosh commented 9 years ago

Go for it @yoshuawuyts. I recently wrote this, https://github.com/mafintosh/bulk-write-stream if you need inspiration

yoshuawuyts commented 9 years ago

Thanks, will do! 🌴

yoshuawuyts commented 9 years ago

Ooph, doing this how bulk-write-stream does it requires a complete rewrite of the module. I'm currently writing this in a separate module, but if OK I'm definitely willing to pull it in here.

blahah commented 8 years ago

@yoshuawuyts did you ever implement this?

yoshuawuyts commented 8 years ago

@blahah nope