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

     1  package decache
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/cache"
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/cache/locations"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    12  )
    13  
    14  func Decache(conn *rpc.Connection, locs []cache.Locator, showProgress bool, cT walk.CacheType) (string, error) {
    15  	itemsSeen := int64(0)
    16  	itemsProcessed := int64(0)
    17  	bytesProcessed := 0
    18  
    19  	cacheName := strings.ToLower(cT.String())
    20  	bar := logger.NewBar(logger.BarOptions{
    21  		Prefix:  "Decaching " + cacheName + " if present",
    22  		Enabled: showProgress,
    23  		Total:   int64(len(locs)),
    24  	})
    25  
    26  	procFunc := func(info *locations.ItemInfo) bool {
    27  		itemsSeen++
    28  		itemsProcessed++
    29  		bytesProcessed += info.Size()
    30  		if itemsSeen%1000 == 0 {
    31  			bar.Tick()
    32  		} else {
    33  			bar.Bump()
    34  		}
    35  		return true
    36  	}
    37  
    38  	skipFunc := func(info *locations.ItemInfo) bool {
    39  		itemsSeen++
    40  		if itemsSeen%1000 == 0 {
    41  			bar.Tick()
    42  		} else {
    43  			bar.Bump()
    44  		}
    45  		return true
    46  	}
    47  
    48  	if err := conn.Store.Decache(locs, procFunc, skipFunc); err != nil {
    49  		bar.Finish(true /* newLine */)
    50  		return "", err
    51  	}
    52  	bar.Finish(true /* newLine */)
    53  	return fmt.Sprintf("%d items totaling %d bytes were removed from the %s cache.", itemsProcessed, bytesProcessed, cacheName), nil
    54  }