git ssb

1+

Dominic / offset-log



Commit ffd00da88bd46f6200bd22699acbb8a980ff260f

pass test-flumelog

Dominic Tarr committed on 11/7/2016, 10:09:47 PM
Parent: 54ab0ea7ce57ed943ff2950b28462a5aa3d72db4

Files changed

index.jschanged
index.jsView
@@ -5,8 +5,9 @@
55 var Obv = require('obv')
66 var Append = require('append-batch')
77 var Blocks = require('aligned-block-file')
88 var isInteger = Number.isInteger
9 +var ltgt = require('ltgt')
910
1011 function frame (data) {
1112 var length = data.reduce(function (total, value) { return total + value.length }, 0)
1213 var b = new Buffer(length + data.length * 8)
@@ -22,27 +23,33 @@
2223 }
2324 return b
2425 }
2526
26-function format (keys, values, key, value, cursor) {
27 +function format (seqs, values, seq, value, cursor) {
2728 return (
28- keys !== false
29 + seqs !== false
2930 ? values !== false
30- ? {key: key, value: value, seq: cursor}
31- : key
31 + ? {value: value, seq: seq}
32 + : seq
3233 : value
3334 )
3435 }
3536
3637 var k = 0
3738
38-module.exports = function (file, length) {
39 +function id (v) { return v }
40 +var id_codec = {encode: id, decode: id}
3941
42 +module.exports = function (file, length, codec) {
43 + if(!codec) codec = id_codec
4044 var since = Obv()
4145 length = length || 1024
4246 var blocks = Blocks(file, length, 'a+')
4347
4448 var append = Append(function (batch, cb) {
49 + batch = batch.map(codec.encode).map(function (e) {
50 + return Buffer.isBuffer(e) ? e : new Buffer(e)
51 + })
4552 blocks.append(frame(batch), function (err) {
4653 if(err) return cb(err)
4754 //else, get offset of last item.
4855 since.set(blocks.offset.value - (batch[batch.length - 1].length + 8))
@@ -74,21 +81,51 @@
7481 var get = reverse ? log.getPrevious : log.get
7582 var diff = reverse ? -1 : 1
7683 var live = opts.live
7784 var aborted = false
78- if(!reverse && opts.gte == null) {
79- cursor = 0
85 + var skip = false
86 +
87 + if(reverse) {
88 + if(opts.lt != null) cursor = opts.lt
89 + else if(opts.lte != null) {
90 + cursor = opts.lte; skip = true
91 + }
8092 }
81- else
82- cursor = reverse ? opts.lt : opts.gte
93 + else {
94 + if(opts.gte != null) cursor = opts.gte
95 + else if(opts.gt != null) {
96 + cursor = opts.gt; skip = true
97 + }
98 + else cursor = 0
99 + }
83100
101 + var lower = ltgt.lowerBound(opts) || 0
102 + var includeLower = ltgt.lowerBoundInclusive(opts)
103 + var upper = ltgt.upperBound(opts)
104 + var includeUpper = ltgt.upperBoundInclusive(opts)
105 +
106 +
84107 function next (cb) {
85108 if(aborted) return cb(aborted)
109 +
110 + if(!reverse && upper != null && includeUpper ? cursor > upper : cursor >= upper) {
111 + return cb(true)
112 + }
113 +
86114 get(cursor, function (err, value, length) {
87115 if(!value.length) throw new Error('read empty value')
88- var _cursor = cursor
116 + var _cursor = reverse ? cursor - length : cursor
89117 cursor += (length * diff)
90- cb(err, format(opts.keys, opts.value, _cursor, value, cursor))
118 +
119 + if(reverse && (includeLower ? cursor < lower : cursor <= lower))
120 + return cb(true)
121 +
122 + if(skip) {
123 + skip = false
124 + return next(cb)
125 + }
126 +
127 + cb(err, format(opts.seqs, opts.values, _cursor, value))
91128 })
92129 }
93130
94131 return function (abort, cb) {
@@ -121,9 +158,9 @@
121158 if(err) return cb(err)
122159 blocks.read(_offset + 4, _offset + 4 + length, function (err, value) {
123160 if(value.length !== length) throw new Error('incorrect length, expected:'+length+', was:'+value.length)
124161 setImmediate(function () {
125- cb(err, value, length + 8)
162 + cb(err, codec.decode(value), length + 8)
126163 })
127164 })
128165 })
129166 },
@@ -136,12 +173,11 @@
136173 if(_offset == 0) return cb(new Error('attempted read previous to first object'))
137174 blocks.readUInt32BE(_offset - 4, function (err, length) {
138175 if(err) return cb(err)
139176 blocks.read(_offset - 4 - length, _offset - 4, function (err, value) {
140- cb(err, value, length + 8)
177 + cb(err, codec.decode(value), length + 8)
141178 })
142179 })
143180 },
144181 }
145182 }
146183
147-

Built with git-ssb-web