@@ -266,8 +266,12 @@ func TestHAMTEnumerationWhenComputingSize(t *testing.T) {
266
266
//DefaultShardWidth = 8
267
267
268
268
treeHeight := 2
269
- //HAMTShardingSize = int(math.Pow(float64(DefaultShardWidth), float64(treeHeight)))
270
- HAMTShardingSize = DefaultShardWidth
269
+ thresholdToWidthRatio := 4
270
+ HAMTShardingSize = DefaultShardWidth * thresholdToWidthRatio
271
+ // We will fetch enough nodes to get to the leaf shards (`treeHeight - 1`)
272
+ // and then enough shards (each with DefaultShardWidth value links) to
273
+ // reach the HAMTShardingSize.
274
+ nodesToFetch := treeHeight - 1 + thresholdToWidthRatio
271
275
272
276
ds := mdtest .Mock ()
273
277
node , err := hamt .CreateFullShard (ds , treeHeight )
@@ -285,16 +289,7 @@ func TestHAMTEnumerationWhenComputingSize(t *testing.T) {
285
289
_ , timeoutExceeded := hamtDir .sizeBelowThreshold (1 , func (ctx context.Context , _ * ipld.Link ) {
286
290
})
287
291
assert .False (t , timeoutExceeded )
288
- // Fetched nodes: One child (internal) shard. First child of the root
289
- // (key of 0).
290
- // THen: how many children from async? 256 internal children?
291
- // FIXME: This is currently fetching *all* children. Not working.
292
- nodesFetched := 2 // Main and one child with all the values.
293
- // FIXME: Values are encoded in a different shard or just as links?
294
- /// Seems like the second one but get confirmation on this.
295
- assert .Equal (t , nodesFetched , sequentialDagService .UniqueCidsRequested ())
296
- // FIXME: how much? This needs to be deduced
297
- // from the DefaultShardWidth
292
+ assert .Equal (t , nodesToFetch , sequentialDagService .UniqueCidsFetched ())
298
293
}
299
294
300
295
// Compare entries in the leftDir against the rightDir and possibly
@@ -441,9 +436,9 @@ func TestDirBuilder(t *testing.T) {
441
436
type serialFetchDag struct {
442
437
ipld.DAGService
443
438
444
- nextNode <- chan struct {}
445
- cidsRequested map [cid.Cid ]struct {}
446
- mapLock sync.Mutex
439
+ nextNode <- chan struct {}
440
+ cidsFetched map [cid.Cid ]struct {}
441
+ mapLock sync.Mutex
447
442
}
448
443
449
444
var _ ipld.DAGService = (* serialFetchDag )(nil )
@@ -460,40 +455,47 @@ func newSerialFetchDag(ds ipld.DAGService) (*serialFetchDag, chan<- struct{}) {
460
455
}
461
456
462
457
func (d * serialFetchDag ) ResetCounter () {
463
- d .cidsRequested = make (map [cid.Cid ]struct {})
458
+ d .cidsFetched = make (map [cid.Cid ]struct {})
464
459
}
465
460
466
- func (d * serialFetchDag ) UniqueCidsRequested () int {
461
+ func (d * serialFetchDag ) UniqueCidsFetched () int {
467
462
d .mapLock .Lock ()
468
463
defer d .mapLock .Unlock ()
469
- return len (d .cidsRequested )
464
+ return len (d .cidsFetched )
470
465
}
471
466
472
467
// Retrieve only one node at a time.
473
468
func (d * serialFetchDag ) Get (ctx context.Context , c cid.Cid ) (ipld.Node , error ) {
474
- d .mapLock .Lock ()
475
- d .cidsRequested [c ] = struct {}{}
476
- d .mapLock .Unlock ()
469
+ // Simulate a context cancel during a fetch operation (over the network).
470
+ select {
471
+ case <- ctx .Done ():
472
+ return nil , ctx .Err ()
473
+ default :
474
+ }
477
475
478
476
node , err := d .DAGService .Get (ctx , c )
479
477
if err != nil {
480
478
return node , err
481
479
}
482
480
481
+ d .mapLock .Lock ()
482
+ d .cidsFetched [c ] = struct {}{}
483
+ d .mapLock .Unlock ()
484
+
483
485
// Only block for leaf shard nodes.
484
486
shard , shardErr := hamt .NewHamtFromDag (nil , node )
485
487
// (We can pass a nil, we won't use the dag service.)
486
488
if shardErr != nil || ! shard .IsValueNode () {
487
489
return node , err
488
490
}
489
491
490
- fmt .Printf ("looking at channel\n " )
491
- select {
492
- case <- d .nextNode :
493
- case <- ctx .Done ():
494
- return nil , ctx .Err ()
495
- }
496
- fmt .Printf ("green light\n " )
492
+ // fmt.Printf("looking at channel\n")
493
+ // select {
494
+ // case <-d.nextNode:
495
+ // case <-ctx.Done():
496
+ // return nil, ctx.Err()
497
+ // }
498
+ // fmt.Printf("green light\n")
497
499
498
500
return node , err
499
501
}
0 commit comments