github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/index/download_one_chunk.go (about)

     1  package index
     2  
     3  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     4  // Use of this source code is governed by a license that can
     5  // be found in the LICENSE file.
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/manifest"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/progress"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    16  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    17  )
    18  
    19  // DownloadOneChunk a filename to an index portion, finds the correspoding CID (hash)
    20  // entry in the manifest, and downloads the index chunk to the local drive
    21  func DownloadOneChunk(chain string, man *manifest.Manifest, fileRange base.FileRange) error {
    22  	// Find bloom filter's CID
    23  	matchedPin := man.ChunkMap[fileRange.String()]
    24  	if matchedPin == nil || matchedPin.Range == "" {
    25  		return fmt.Errorf("filename path missing in chunks: %s", fileRange)
    26  	}
    27  
    28  	logger.Info("Bloom filter hit, downloading index portion", (colors.Yellow + fileRange.String() + colors.Off), "from IPFS.")
    29  
    30  	// Start downloading the filter
    31  	matchedPin.BloomHash = "" // we want to download only the index chunk
    32  	chunks := []types.ChunkRecord{*matchedPin}
    33  	progressChannel := make(chan *progress.ProgressMsg)
    34  
    35  	go func() {
    36  		DownloadChunks(chain, chunks, walk.Index_Final, 4 /* poolSize */, progressChannel)
    37  		close(progressChannel)
    38  	}()
    39  
    40  	for event := range progressChannel {
    41  		switch event.Event {
    42  		case progress.AllDone:
    43  			return nil
    44  		case progress.Cancelled:
    45  			return nil
    46  		case progress.Error:
    47  			return fmt.Errorf("error while downloading: %s", event.Message)
    48  		}
    49  	}
    50  	return nil
    51  }