@@ -132,6 +132,20 @@ func GetLinksDirect(serv format.NodeGetter) GetLinks {
132132 }
133133}
134134
135+ // GetLinksDirectWithProgressTracker creates a function as GetLinksDirect, but
136+ // updates the ProgressTracker with the raw block data size of the retrieved node.
137+ func GetLinksDirectWithProgressTracker (serv format.NodeGetter , tracker * ProgressTracker ) GetLinks {
138+ return func (ctx context.Context , c cid.Cid ) ([]* format.Link , error ) {
139+ nd , err := serv .Get (ctx , c )
140+ if err != nil {
141+ return nil , err
142+ }
143+ // We don't use Size() as it returns cumulative size including linked nodes.
144+ tracker .Update (uint64 (len (nd .RawData ())))
145+ return nd .Links (), nil
146+ }
147+ }
148+
135149type sesGetter struct {
136150 bs * bserv.Session
137151 decoder * legacy.Decoder
@@ -208,20 +222,13 @@ func FetchGraphWithDepthLimit(ctx context.Context, root cid.Cid, depthLim int, s
208222 // We default to Concurrent() walk.
209223 opts = append ([]WalkOption {Concurrent ()}, opts ... )
210224
211- // If we have a ProgressTracker, we wrap the visit function to handle it.
225+ // If we have a ProgressTracker, we wrap the get links function to handle it.
212226 v , _ := ctx .Value (progressContextKey ).(* ProgressTracker )
213227 if v == nil {
214228 return WalkDepth (ctx , GetLinksDirect (ng ), root , visit , opts ... )
215229 }
216230
217- visitProgress := func (c cid.Cid , depth int ) bool {
218- if visit (c , depth ) {
219- v .Increment ()
220- return true
221- }
222- return false
223- }
224- return WalkDepth (ctx , GetLinksDirect (ng ), root , visitProgress , opts ... )
231+ return WalkDepth (ctx , GetLinksDirectWithProgressTracker (ng , v ), root , visit , opts ... )
225232}
226233
227234// GetMany gets many nodes from the DAG at once.
@@ -457,10 +464,18 @@ func sequentialWalkDepth(ctx context.Context, getLinks GetLinks, root cid.Cid, d
457464 return nil
458465}
459466
467+ // ProgressStat represents the progress of a fetch operation.
468+ type ProgressStat struct {
469+ // Nodes is the total number of nodes fetched.
470+ Nodes int
471+ // Bytes is the total bytes of raw block data.
472+ Bytes uint64
473+ }
474+
460475// ProgressTracker is used to show progress when fetching nodes.
461476type ProgressTracker struct {
462- Total int
463- lk sync.Mutex
477+ stat ProgressStat
478+ lk sync.Mutex
464479}
465480
466481// DeriveContext returns a new context with value "progress" derived from the
@@ -469,18 +484,26 @@ func (p *ProgressTracker) DeriveContext(ctx context.Context) context.Context {
469484 return context .WithValue (ctx , progressContextKey , p )
470485}
471486
472- // Increment adds one to the total progress .
473- func (p * ProgressTracker ) Increment ( ) {
487+ // Update adds one to the total nodes and updates the total bytes .
488+ func (p * ProgressTracker ) Update ( bytes uint64 ) {
474489 p .lk .Lock ()
475490 defer p .lk .Unlock ()
476- p .Total ++
491+ p .stat .Nodes ++
492+ p .stat .Bytes += bytes
477493}
478494
479495// Value returns the current progress.
480496func (p * ProgressTracker ) Value () int {
481497 p .lk .Lock ()
482498 defer p .lk .Unlock ()
483- return p .Total
499+ return p .stat .Nodes
500+ }
501+
502+ // ProgressStat returns the current progress stat.
503+ func (p * ProgressTracker ) ProgressStat () ProgressStat {
504+ p .lk .Lock ()
505+ defer p .lk .Unlock ()
506+ return p .stat
484507}
485508
486509func parallelWalkDepth (ctx context.Context , getLinks GetLinks , root cid.Cid , visit func (cid.Cid , int ) bool , options * walkOptions ) error {
0 commit comments