// Evaluate directory size and a future sizeChange and check if it will be below
// HAMTShardingSize threshold (to trigger a transition to a BasicDirectory).
// Instead of enumerating the entire tree we eagerly call EnumLinksAsync
// until we either reach a value above the threshold (in that case no need
// to keep counting) or an error occurs (like the context being canceled
// if we take too much time fetching the necessary shards).
Evaluate directory size and a future sizeChange and check if it will be below
HAMTShardingSize threshold (to trigger a transition to a BasicDirectory).
Instead of enumerating the entire tree we eagerly call EnumLinksAsync
until we either reach a value above the threshold (in that case no need
to keep counting) or an error occurs (like the context being canceled
if we take too much time fetching the necessary shards).
// Evaluate directory size and a future sizeChange and check if it will be below // HAMTShardingSize threshold ( to trigger a transition to a BasicDirectory ) . // Instead of enumerating the entire tree we eagerly call EnumLinksAsync // until we either reach a value above the threshold ( in that case no need // to keep counting ) or an error occurs ( like the context being canceled // if we take too much time fetching the necessary shards ) .
func (d *HAMTDirectory) sizeBelowThreshold(ctx context.Context, sizeChange int) (below bool, err error) {
	if HAMTShardingSize == 0 {
		panic("asked to compute HAMT size with HAMTShardingSize option off (0)")
	}
	partialSize := 0
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	for linkResult := range d.EnumLinksAsync(ctx) {
		if linkResult.Err != nil {
			return false, linkResult.Err
		}
		partialSize += linksize.LinkSizeFunction(linkResult.Link.Name, linkResult.Link.Cid)
		if partialSize+sizeChange >= HAMTShardingSize {
			return false, nil
		}
	}
	return true, nil
}
func ( d * HAMTDirectory ) sizeBelowThreshold ( ctx context . Context , sizeChange int ) ( below bool , err error ) { if HAMTShardingSize == 0 { panic ( " " ) } partialSize := 0 ctx , cancel := context . WithCancel ( ctx ) defer cancel ( ) for linkResult := range d . EnumLinksAsync ( ctx ) { if linkResult . Err != nil { return false , linkResult . Err } partialSize += linksize . LinkSizeFunction ( linkResult . Link . Name , linkResult . Link . Cid ) if partialSize + sizeChange >= HAMTShardingSize { return false , nil } } return true , nil }
Evaluate directory size and a future sizeChange and check if it will be below
HAMTShardingSize threshold (to trigger a transition to a BasicDirectory).
Evaluate directory size and a future sizeChange and check if it will be below HAMTShardingSize threshold ( to trigger a transition to a BasicDirectory ) .
// We don't necessarily compute the full size of *all* shards as we might // end early if we already know we're above the threshold or run out of time. // We stop the enumeration once we have enough information and exit this function. // We have already fetched enough shards to assert we are //  above the threshold, so no need to keep fetching. // We enumerated *all* links in all shards and didn't reach the threshold.