git ssb

0+

cryptop / multiblob



forked from Dominic / multiblob

Commit 3fb38e34e18d878a3e24a56e505e0bafb73f8211

meta

Dominic Tarr committed on 4/20/2016, 3:29:01 AM
Parent: a90889a879dea8da10d898d7714c795c81df85e0

Files changed

index.jschanged
index.jsView
@@ -30,8 +30,36 @@
3030 function toArray (h) {
3131 return Array.isArray(h) ? h : [h]
3232 }
3333
34+function single (fn) {
35+ var waiting = {}
36+ function async (key, cb) {
37+ if(!waiting[key]) {
38+ waiting[key] = [cb]
39+ var cbs = waiting[key]
40+ fn(key, function done (err, result) {
41+ if(cbs.length)
42+ delete waiting[key]
43+ while(cbs.length) cbs.shift()(err, result)
44+ })
45+ }
46+ else
47+ waiting[value].push(cb)
48+ }
49+
50+ //dump all the things that have been done already,
51+ //when something has been added?
52+ async.done = function (key, err, value) {
53+ if(!waiting[key]) return
54+ var cbs = waiting[key]
55+ delete waiting[key]
56+ while(cbs.length) cbs.shift()(err, result)
57+ }
58+
59+ return async
60+}
61+
3462 var Blobs = module.exports = function (config) {
3563 var dir
3664 if('string' === typeof config)
3765 dir = config, config = {dir: dir}
@@ -66,32 +94,45 @@
6694 if(tmp) return cb()
6795 else waiting.push(cb)
6896 }
6997
98+ var stat = single(fs.stat)
99+
70100 var tmpdir = path.join(dir, 'tmp')
71101
72102 rimraf(tmpdir, function () {
73103 mkdirp(tmpdir, function () {
74104 tmp = true; while(waiting.length) waiting.shift()()
75105 })
76106 })
77107
108+ function toMeta(hash, stat) {
109+ if(!stat) return null
110+ return {id: hash, size: stat.size, ts: +stat.ctime}
111+ }
112+
78113 function has (hash) {
79114 return function (cb) {
80- fs.stat(toPath(dir, hash), function (err, stat) {
115+ stat(toPath(dir, hash), function (err, stat) {
81116 cb(null, !!stat)
82117 })
83118 }
84119 }
85120
86121 function size (hash) {
87122 return function (cb) {
88- fs.stat(toPath(dir, hash), function (err, stat) {
123+ stat(toPath(dir, hash), function (err, stat) {
89124 cb(null, stat ? stat.size : null)
90125 })
91126 }
92127 }
93128
129+ var meta = function (hash, cb) {
130+ stat(toPath(dir, hash), function (err, stat) {
131+ cb(err, toMeta(hash, stat))
132+ })
133+ }
134+
94135 function createTester (test) {
95136 return function (hashes, cb) {
96137 var n = !Array.isArray(hashes)
97138 cont.para(toArray(hashes).map(test)) (function (_, ary) {
@@ -107,9 +148,8 @@
107148 }
108149
109150 var listeners = []
110151
111-
112152 return {
113153 get: function (opts) {
114154 if(isHash(opts))
115155 return Read(toPath(dir, opts))
@@ -120,9 +160,9 @@
120160 'multiblob.get: {hash} is mandatory'
121161 ))
122162
123163 var stream = defer.source()
124- fs.stat(toPath(dir, hash), function (err, stat) {
164+ stat(toPath(dir, hash), function (err, stat) {
125165 if(opts.size != null && opts.size !== stat.size)
126166 stream.abort(new Error('incorrect file length,'
127167 + ' requested:' + opts.size + ' file was:' + stat.size
128168 + ' for file:' + hash
@@ -143,8 +183,9 @@
143183
144184 size: createTester(size),
145185
146186 has: createTester(has),
187+ meta: meta,
147188
148189 add: function (hash, cb) {
149190 if(!cb) cb = hash, hash = null
150191
@@ -195,19 +236,16 @@
195236
196237 if(!isLive && !isOld)
197238 throw new Error('ls with neither old or new is empty')
198239
199- var long = (opts.size || opts.long)
200-
240+ var long = (opts.size || opts.long || opts.meta)
201241 var old = pull(
202242 glob(path.join(dir, '*', '*', '*')),
203- long
204- ? paramap(function (filename, cb) {
205- fs.stat(filename, function (err, stat) {
206- cb(err, {id: toHash(filename), size: stat.size, ts: +stat.ctime})
207- })
208- }, 32)
209- : pull.map(toHash)
243+ long ? paramap(function (filename, cb) {
244+ stat(filename, function (err, stat) {
245+ cb(err, toMeta(toHash(filename), stat))
246+ })
247+ }, 32) : pull.map(toHash)
210248 )
211249
212250 if(!isLive) return old
213251
@@ -228,11 +266,4 @@
228266 }
229267 }
230268 }
231269
232-
233-
234-
235-
236-
237-
238-

Built with git-ssb-web