@@ -7,6 +7,8 @@ const each = require('async/each')
7
7
const assert = require ( 'assert' )
8
8
const path = require ( 'path' )
9
9
const debug = require ( 'debug' )
10
+ const Big = require ( 'big.js' )
11
+ const pull = require ( 'pull-stream' )
10
12
11
13
const backends = require ( './backends' )
12
14
const version = require ( './version' )
@@ -17,6 +19,8 @@ const defaultOptions = require('./default-options')
17
19
18
20
const log = debug ( 'repo' )
19
21
22
+ const noLimit = Number . MAX_SAFE_INTEGER
23
+
20
24
const lockers = {
21
25
memory : require ( './lock-memory' ) ,
22
26
fs : require ( './lock' )
@@ -201,32 +205,73 @@ class IpfsRepo {
201
205
this . version . exists ( callback )
202
206
}
203
207
208
+ /**
209
+ * Get repo status.
210
+ *
211
+ * @param {function(Error, Object) } callback
212
+ * @return {void }
213
+ */
204
214
stat ( callback ) {
205
215
waterfall ( [
206
- ( cb ) => cb ( null , { repoPath : this . path } ) ,
207
- ( stats , cb ) => {
208
- this . version . get ( ( err , version ) => {
209
- stats . version = version
210
- cb ( err , stats )
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 )
211
233
} )
212
- } ,
213
- ( stats , cb ) => {
214
- this . blocks . query ( { keysOnly : true } , ( err , list ) => {
215
- list = list || [ ]
216
- stats . numObjects = list . length
217
- cb ( err , stats )
218
- } )
219
- } ,
220
- ( stats , cb ) => {
221
- // TODO:
222
- stats . repoSize = null
223
- stats . storageMax = null
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
+
224
250
cb ( null , stats )
225
- }
251
+ } )
226
252
] , callback )
227
253
}
228
254
}
229
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
+ )
273
+ }
274
+
230
275
module . exports = IpfsRepo
231
276
module . exports . repoVersion = repoVersion
232
277
0 commit comments