git ssb

1+

Dominic / offset-log



Commit 19f8415bfb3ca6c771792dd025ec7db5b6927e03

handle keys and values in a way that will be familiar to leveldbers

Dominic Tarr committed on 8/18/2016, 6:20:56 AM
Parent: 1a10bc9d9fd3eba08dd44cd0ee208bdc2651f38f

Files changed

index.jschanged
index.jsView
@@ -2,8 +2,10 @@
22
33 var isBuffer = Buffer.isBuffer
44 var Notify = require('pull-notify')
55 var Live = require('pull-live')
6 +var pull = require('pull-stream/pull')
7 +var Map = require('pull-stream/throughs/map')
68
79 var Blocks = require('block-reader')
810
911 function frame (data) {
@@ -22,8 +24,16 @@
2224 }
2325 return b
2426 }
2527
28 +function format (opts) {
29 + var keys = opts.keys === true //default to false
30 + var values = opts.value !== false //default to true
31 + return Map(function (data) {
32 + return keys && values ? data : values ? data.value : data.key
33 + })
34 +}
35 +
2636 module.exports = function (file, length) {
2737
2838 var notify = Notify()
2939 length = length || 1024
@@ -61,31 +71,32 @@
6171 var next = reverse ? (opts && opts.max || blocks.size()) : (opts && opts.min || 0)
6272 var diff = reverse ? -1 : 1
6373 var get = reverse ? log.getPrevious : log.get
6474
65- return function (abort, cb) {
75 + return pull(function (abort, cb) {
6676 if(abort) cb(abort)
6777 else if(reverse && next <= 0)
6878 cb(true)
6979 else
7080 get(next, function (err, value) {
7181 if(err) return cb(true) //err)
7282 else if(!value || !value.length) return cb(true)
7383 else {
84 + var _offset = next
7485 next = next + (value.length + 8)*diff
75- cb(null, value)
86 + cb(null, {key: _offset, value: value})
7687 }
7788 })
78- }
89 + }, format(opts))
7990 }, function (opts) {
80- return notify.listen()
91 + return pull(notify.listen(), format(opts))
8192 }),
8293 //if value is an array of buffers, then treat that as a batch.
8394 append: function (value, cb) {
8495 if(!isBuffer(value)) throw new Error('value must be a buffer')
8596 queue.push({value: value, cb: function (err, offset) {
8697 if(err) return cb(err)
87- notify(value)
98 + notify({key: offset, value: value})
8899 cb(null, offset)
89100 }})
90101 write()
91102 },
@@ -109,5 +120,4 @@
109120 },
110121 }
111122 }
112123
113-

Built with git-ssb-web