git ssb

0+

cryptop / multiblob



forked from Dominic / multiblob

Commit 88ee60c886e22a1eefd21b83f5ee4576b1bbd770

live streams can be only live, or only old, or old and live

Dominic Tarr committed on 4/20/2016, 2:02:20 AM
Parent: 164aa344ae4713e3ef0dbeedb2d482eb1e98b7d8

Files changed

index.jschanged
test/live.jschanged
index.jsView
@@ -159,10 +159,12 @@
159159 var size = 0
160160
161161 deferred.resolve(pull(
162162 hasher,
163- pull.through(function (data) {
163+ pull.map(function (data) {
164+ if('string' === typeof data) data = new Buffer(data, 'utf8')
164165 size += data.length
166+ return data
165167 }),
166168 Write(tmpfile, function (err) {
167169 if(err) return cb(explain(err, 'could not write to tmpfile'))
168170
@@ -187,10 +189,17 @@
187189 return deferred
188190 },
189191 ls: function (opts) {
190192 opts = opts || {}
193+ var isOld = opts.old !== false
194+ var isLive = opts.live === true || opts.old === false
195+
196+ if(!isLive && !isOld)
197+ throw new Error('ls with neither old or new is empty')
198+
191199 var long = (opts.size || opts.long)
192- var source = pull(
200+
201+ var old = pull(
193202 glob(path.join(dir, '*', '*', '*')),
194203 long
195204 ? paramap(function (filename, cb) {
196205 fs.stat(filename, function (err, stat) {
@@ -199,18 +208,18 @@
199208 }, 32)
200209 : pull.map(toHash)
201210 )
202211
203- if(!opts.live) return source
212+ if(!isLive) return old
204213
205- return cat([
206- source,
207- pull.once({sync: true}),
208- long
214+ var live = long
209215 ? newBlob.listen()
210216 : pull(newBlob.listen(), pull.map(function (e) { return e.id }))
211- ])
212-
217+
218+ if(!isOld) return live
219+
220+ //old & live
221+ return cat([old, pull.once({sync: true}), live])
213222 },
214223 rm: function (hash, cb) {
215224 fs.unlink(toPath(dir, hash), cb)
216225 },
@@ -220,4 +229,10 @@
220229 }
221230 }
222231
223232
233+
234+
235+
236+
237+
238+
test/live.jsView
@@ -17,67 +17,87 @@
1717 while(l --) random1.push(crypto.randomBytes(1024))
1818
1919 var random2 = random1.slice().reverse()
2020
21+var random3 = random1.slice().sort(function (a, b) {
22+ return a.readDoubleBE(0) - b.readDoubleBE(0)
23+})
24+
2125 module.exports = function (alg) {
2226
23-function hasher (ary) {
24- var hasher = util.createHash(alg, true)
25- pull(pull.values(ary), hasher, pull.drain())
26- return util.encode(hasher.digest, alg)
27-}
27+ function hasher (ary) {
28+ var hasher = util.createHash(alg, true)
29+ pull(pull.values(ary), hasher, pull.drain())
30+ return util.encode(hasher.digest, alg)
31+ }
2832
29-var hash1 = hasher(random1)
33+ var hash1 = hasher(random1)
3034
31-var blobs = Blobs(dirname)
35+ var blobs = Blobs(dirname)
3236
33-function watch(stream, ary) {
34- return pull(
35- stream,
36- pull.drain(function (data) {
37- ary.push(data)
38- }, function (err) {
39- if(err) throw err
37+ function watch(stream, ary) {
38+ if(!stream) throw new Error('stream must be provided')
39+ if(!ary) throw new Error('array must be provided')
40+ return pull(
41+ stream,
42+ pull.drain(function (data) {
43+ ary.push(data)
44+ }, function (err) {
45+ if(err) throw err
46+ })
47+ )
48+ }
49+
50+ function add (file, cb) {
51+ pull(pull.values(file), blobs.add(cb))
52+ }
53+
54+ function sort (ary) {
55+ return ary.sort(function (a, b) {
56+ var ha = new Buffer(a, 'base64').toString('hex')
57+ var hb = new Buffer(b, 'base64').toString('hex')
58+ return ha < hb ? -1 : ha == hb ? 0 : 1
4059 })
41- )
42-}
60+ }
4361
44-tape('live stream', function (t) {
62+ tape('live stream', function (t) {
4563
46- var n = 3, ary1 = [], ary2 = []
64+ var n = 3
65+ var live3=[], live3new=[], old1live2=[],
66+ live2new=[], old2live1=[], live1new=[]
4767
48- watch(blobs.ls({live: true}), ary1)
68+ watch(blobs.ls({live: true}), live3)
69+ watch(blobs.ls({old: false}), live3new)
4970
50- pull(
51- pull.values(random1),
52- blobs.add(function (err, _hash) {
71+ add(random1, function (err, hash1) {
5372 if(err) throw err
54- watch(blobs.ls({live: true}), ary2)
73+ watch(blobs.ls({live: true}), old1live2)
74+ watch(blobs.ls({old: false}), live2new)
75+ add(random2, function (err, hash2) {
76+ if(err) throw err
5577
56- pull(
57- pull.values(random2),
58- blobs.add(function (err, _hash) {
59- if(err) throw err
60- pull(
61- blobs.ls({live: false}),
62- pull.collect(function (err, ary3) {
63- console.log(ary1, ary2, ary3)
64- var sync = {sync: true}
65- t.deepEqual(ary1[0], sync)
66- t.deepEqual(ary2[1], sync)
67- ary1.splice(0, 1)
68- ary2.splice(1, 1)
69- ary1.sort(); ary2.sort(); ary3.sort()
70- t.deepEqual(ary1, ary3)
71- t.deepEqual(ary2, ary3)
72- t.end()
73- })
74- )
78+ watch(blobs.ls({live: true}), old2live1)
79+ watch(blobs.ls({old: false}), live1new)
80+
81+ add(random3, function (err, hash3) {
82+
83+ pull(blobs.ls({live: true}), pull.take(4), pull.collect(function (err, old3live0) {
84+
85+ console.log('live3', [hash1, hash2, hash3])
86+ t.deepEqual(live3, [{sync: true}, hash1, hash2, hash3])
87+ t.deepEqual(old1live2, [hash1, {sync: true}, hash2, hash3])
88+ t.deepEqual(old2live1, sort([hash1, hash2]).concat([{sync: true}, hash3]))
89+ t.deepEqual(old3live0, sort([hash1, hash2, hash3]).concat([{sync: true}]))
90+
91+ t.deepEqual(live3new, [hash1, hash2, hash3])
92+ t.deepEqual(live2new, [hash2, hash3])
93+
94+ return t.end()
95+ }))
7596 })
76- )
97+ })
7798 })
78- )
79-})
80-
99+ })
81100 }
82101
83102 if(!module.parent) module.exports('blake2s')
103+

Built with git-ssb-web