Skip to content

Commit 423b177

Browse files
committed
feat: implement .stat function
1 parent fc4cfb3 commit 423b177

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,22 @@
5656
"dependencies": {
5757
"async": "^2.6.0",
5858
"base32.js": "~0.1.0",
59+
"big.js": "^5.0.3",
5960
"cids": "~0.5.2",
60-
"interface-datastore": "~0.4.2",
6161
"datastore-core": "~0.4.0",
6262
"datastore-fs": "~0.4.2",
6363
"datastore-level": "~0.7.0",
6464
"debug": "^3.1.0",
65+
"interface-datastore": "~0.4.2",
6566
"ipfs-block": "~0.6.1",
6667
"level-js": "timkuijsten/level.js#idbunwrapper",
6768
"leveldown": "^1.7.2",
6869
"lock-me": "^1.0.3",
6970
"lodash.get": "^4.4.2",
7071
"lodash.has": "^4.5.2",
7172
"lodash.set": "^4.3.2",
72-
"multiaddr": "^3.0.1"
73+
"multiaddr": "^3.0.1",
74+
"pull-stream": "^3.6.1"
7375
},
7476
"license": "MIT",
7577
"contributors": [

src/blockstore.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Block = require('ipfs-block')
88
const setImmediate = require('async/setImmediate')
99
const reject = require('async/reject')
1010
const CID = require('cids')
11+
const pull = require('pull-stream')
1112

1213
/**
1314
* Transform a raw buffer to a base32 encoded key.
@@ -49,6 +50,21 @@ function maybeWithSharding (filestore, options, callback) {
4950

5051
function createBaseStore (store) {
5152
return {
53+
/**
54+
* Query the store.
55+
*
56+
* @param {object} query
57+
* @param {function(Error, Array)} callback
58+
* @return {void}
59+
*/
60+
query (query, callback) {
61+
pull(
62+
store.query(query),
63+
pull.collect((err, list) => {
64+
callback(err, list)
65+
})
66+
)
67+
},
5268
/**
5369
* Get a single block by CID.
5470
*

src/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const each = require('async/each')
77
const assert = require('assert')
88
const path = require('path')
99
const debug = require('debug')
10+
const Big = require('big.js')
11+
const pull = require('pull-stream')
1012

1113
const backends = require('./backends')
1214
const version = require('./version')
@@ -17,6 +19,8 @@ const defaultOptions = require('./default-options')
1719

1820
const log = debug('repo')
1921

22+
const noLimit = Number.MAX_SAFE_INTEGER
23+
2024
const lockers = {
2125
memory: require('./lock-memory'),
2226
fs: require('./lock')
@@ -200,6 +204,72 @@ class IpfsRepo {
200204
exists (callback) {
201205
this.version.exists(callback)
202206
}
207+
208+
/**
209+
* Get repo status.
210+
*
211+
* @param {function(Error, Object)} callback
212+
* @return {void}
213+
*/
214+
stat (callback) {
215+
waterfall([
216+
(cb) => cb(null, {
217+
repoPath: this.path,
218+
repoSize: new Big(0),
219+
storageMax: new Big(noLimit)
220+
}),
221+
(stats, cb) => this.version.get((err, version) => {
222+
stats.version = version
223+
cb(err, stats)
224+
}),
225+
(stats, cb) => this.blocks.query({}, (err, list) => {
226+
list = list || []
227+
stats.numObjects = new Big(list.length)
228+
229+
list.forEach(block => {
230+
stats.repoSize = stats.repoSize
231+
.plus(block.value.byteLength)
232+
.plus(block.key._buf.byteLength)
233+
})
234+
235+
cb(err, stats)
236+
}),
237+
(stats, cb) => getSize(this.datastore, (err, size) => {
238+
stats.repoSize = stats.repoSize.plus(size)
239+
cb(err, stats)
240+
}),
241+
(stats, cb) => getSize(this.keys, (err, size) => {
242+
stats.repoSize = stats.repoSize.plus(size)
243+
cb(err, stats)
244+
}),
245+
(stats, cb) => this.config.get('Datastore.StorageMax', (err, max) => {
246+
if (!err) {
247+
stats.storageMax = new Big(max)
248+
}
249+
250+
cb(null, stats)
251+
})
252+
], callback)
253+
}
254+
}
255+
256+
function getSize (queryFn, callback) {
257+
pull(
258+
queryFn.query({}),
259+
pull.collect((err, list) => {
260+
if (err) return callback(err, 0)
261+
262+
let val = new Big(0)
263+
264+
list.forEach(block => {
265+
val = val
266+
.plus(block.value.byteLength)
267+
.plus(block.key._buf.byteLength)
268+
})
269+
270+
callback(null, val)
271+
})
272+
)
203273
}
204274

205275
module.exports = IpfsRepo

test/repo-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,19 @@ module.exports = (repo) => {
9999
})
100100
})
101101
})
102+
103+
describe('stat', () => {
104+
it('get stats', (done) => {
105+
repo.stat((err, stats) => {
106+
expect(err).to.not.exist()
107+
expect(stats).to.exist()
108+
expect(stats).to.have.property('numObjects')
109+
expect(stats).to.have.property('version')
110+
expect(stats).to.have.property('repoPath')
111+
expect(stats).to.have.property('repoSize')
112+
expect(stats).to.have.property('storageMax')
113+
done()
114+
})
115+
})
116+
})
102117
}

0 commit comments

Comments
 (0)