Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 912b849

Browse files
committed
fix and document TestHAMTEnumerationWhenComputingSize
1 parent 156365d commit 912b849

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

hamt/hamt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ type Shard struct {
7171

7272
dserv ipld.DAGService
7373

74+
// FIXME: Remove. We don't actually store "value nodes". This confusing
75+
// abstraction just removes the maxpadlen from the link names to extract
76+
// the actual value link the trie is storing.
7477
// leaf node
7578
key string
7679
val *ipld.Link

io/directory_test.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,12 @@ func TestHAMTEnumerationWhenComputingSize(t *testing.T) {
266266
//DefaultShardWidth = 8
267267

268268
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
271275

272276
ds := mdtest.Mock()
273277
node, err := hamt.CreateFullShard(ds, treeHeight)
@@ -285,16 +289,7 @@ func TestHAMTEnumerationWhenComputingSize(t *testing.T) {
285289
_, timeoutExceeded := hamtDir.sizeBelowThreshold(1, func(ctx context.Context, _ *ipld.Link) {
286290
})
287291
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())
298293
}
299294

300295
// Compare entries in the leftDir against the rightDir and possibly
@@ -441,9 +436,9 @@ func TestDirBuilder(t *testing.T) {
441436
type serialFetchDag struct {
442437
ipld.DAGService
443438

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
447442
}
448443

449444
var _ ipld.DAGService = (*serialFetchDag)(nil)
@@ -460,40 +455,47 @@ func newSerialFetchDag(ds ipld.DAGService) (*serialFetchDag, chan<- struct{}) {
460455
}
461456

462457
func (d *serialFetchDag) ResetCounter() {
463-
d.cidsRequested = make(map[cid.Cid]struct{})
458+
d.cidsFetched = make(map[cid.Cid]struct{})
464459
}
465460

466-
func (d *serialFetchDag) UniqueCidsRequested() int {
461+
func (d *serialFetchDag) UniqueCidsFetched() int {
467462
d.mapLock.Lock()
468463
defer d.mapLock.Unlock()
469-
return len(d.cidsRequested)
464+
return len(d.cidsFetched)
470465
}
471466

472467
// Retrieve only one node at a time.
473468
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+
}
477475

478476
node, err := d.DAGService.Get(ctx, c)
479477
if err != nil {
480478
return node, err
481479
}
482480

481+
d.mapLock.Lock()
482+
d.cidsFetched[c] = struct{}{}
483+
d.mapLock.Unlock()
484+
483485
// Only block for leaf shard nodes.
484486
shard, shardErr := hamt.NewHamtFromDag(nil, node)
485487
// (We can pass a nil, we won't use the dag service.)
486488
if shardErr != nil || !shard.IsValueNode() {
487489
return node, err
488490
}
489491

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")
497499

498500
return node, err
499501
}

0 commit comments

Comments
 (0)