2
2
'use strict'
3
3
4
4
const promisify = require ( 'promisify-es6' )
5
- const { DAGNode, DAGLink, util } = require ( 'ipld-dag-pb' )
5
+ const { DAGNode, DAGLink } = require ( 'ipld-dag-pb' )
6
6
const CID = require ( 'cids' )
7
7
const map = require ( 'async/map' )
8
8
const mapSeries = require ( 'async/mapSeries' )
@@ -12,6 +12,7 @@ const eachLimit = require('async/eachLimit')
12
12
const waterfall = require ( 'async/waterfall' )
13
13
const detectLimit = require ( 'async/detectLimit' )
14
14
const setImmediate = require ( 'async/setImmediate' )
15
+ const queue = require ( 'async/queue' )
15
16
const { Key } = require ( 'interface-datastore' )
16
17
const errCode = require ( 'err-code' )
17
18
const multibase = require ( 'multibase' )
@@ -52,30 +53,49 @@ module.exports = (self) => {
52
53
const recursiveKeys = ( ) =>
53
54
Array . from ( recursivePins ) . map ( key => new CID ( key ) . buffer )
54
55
55
- function getIndirectKeys ( callback ) {
56
- const indirectKeys = new Set ( )
57
- eachLimit ( recursiveKeys ( ) , concurrencyLimit , ( multihash , cb ) => {
58
- dag . _getRecursive ( multihash , ( err , nodes ) => {
56
+ function walkDag ( { cid, onCid = ( ) => { } } , cb ) {
57
+ const q = queue ( function ( { cid } , done ) {
58
+ dag . get ( cid , { preload : false } , function ( err , result ) {
59
59
if ( err ) {
60
- return cb ( err )
60
+ return done ( err )
61
61
}
62
62
63
- map ( nodes , ( node , cb ) => util . cid ( util . serialize ( node ) , {
64
- cidVersion : 0
65
- } ) . then ( cid => cb ( null , cid ) , cb ) , ( err , cids ) => {
66
- if ( err ) {
67
- return cb ( err )
68
- }
63
+ onCid ( cid )
69
64
70
- cids
71
- . map ( cid => cid . toString ( ) )
72
- // recursive pins pre-empt indirect pins
73
- . filter ( key => ! recursivePins . has ( key ) )
74
- . forEach ( key => indirectKeys . add ( key ) )
65
+ if ( result . value . Links ) {
66
+ q . push ( result . value . Links . map ( link => ( {
67
+ cid : link . Hash
68
+ } ) ) )
69
+ }
75
70
76
- cb ( )
77
- } )
71
+ done ( )
78
72
} )
73
+ } , concurrencyLimit )
74
+ q . drain = ( ) => {
75
+ cb ( )
76
+ }
77
+ q . error = ( err ) => {
78
+ q . kill ( )
79
+ cb ( err )
80
+ }
81
+ q . push ( { cid } )
82
+ }
83
+
84
+ function getIndirectKeys ( callback ) {
85
+ const indirectKeys = new Set ( )
86
+ eachLimit ( recursiveKeys ( ) , concurrencyLimit , ( multihash , cb ) => {
87
+ // load every hash in the graph
88
+ walkDag ( {
89
+ cid : new CID ( multihash ) ,
90
+ onCid : ( cid ) => {
91
+ cid = cid . toString ( )
92
+
93
+ // recursive pins pre-empt indirect pins
94
+ if ( ! recursivePins . has ( cid ) ) {
95
+ indirectKeys . add ( cid )
96
+ }
97
+ }
98
+ } , cb )
79
99
} , ( err ) => {
80
100
if ( err ) { return callback ( err ) }
81
101
callback ( null , Array . from ( indirectKeys ) )
@@ -184,7 +204,9 @@ module.exports = (self) => {
184
204
185
205
// verify that each hash can be pinned
186
206
map ( mhs , ( multihash , cb ) => {
187
- const key = toB58String ( multihash )
207
+ const cid = new CID ( multihash )
208
+ const key = cid . toBaseEncodedString ( )
209
+
188
210
if ( recursive ) {
189
211
if ( recursivePins . has ( key ) ) {
190
212
// it's already pinned recursively
@@ -193,11 +215,10 @@ module.exports = (self) => {
193
215
194
216
// entire graph of nested links should be pinned,
195
217
// so make sure we have all the objects
196
- dag . _getRecursive ( key , { preload : options . preload } , ( err ) => {
197
- if ( err ) { return cb ( err ) }
198
- // found all objects, we can add the pin
199
- return cb ( null , key )
200
- } )
218
+ walkDag ( {
219
+ dag,
220
+ cid
221
+ } , ( err ) => cb ( err , key ) )
201
222
} else {
202
223
if ( recursivePins . has ( key ) ) {
203
224
// recursive supersedes direct, can't have both
@@ -209,8 +230,10 @@ module.exports = (self) => {
209
230
}
210
231
211
232
// make sure we have the object
212
- dag . get ( new CID ( multihash ) , { preload : options . preload } , ( err ) => {
213
- if ( err ) { return cb ( err ) }
233
+ dag . get ( cid , ( err ) => {
234
+ if ( err ) {
235
+ return cb ( err )
236
+ }
214
237
// found the object, we can add the pin
215
238
return cb ( null , key )
216
239
} )
0 commit comments