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

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package blocksPkg
     6  
     7  import (
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/cache"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/decache"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    14  )
    15  
    16  func (opts *BlocksOptions) HandleDecache(rCtx *output.RenderCtx) error {
    17  	itemsToRemove, err := opts.getItemsToRemove()
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    23  		showProgress := opts.Globals.ShowProgress()
    24  		if msg, err := decache.Decache(opts.Conn, itemsToRemove, showProgress, opts.getCacheType()); err != nil {
    25  			errorChan <- err
    26  		} else {
    27  			s := types.Message{
    28  				Msg: msg,
    29  			}
    30  			modelChan <- &s
    31  		}
    32  	}
    33  
    34  	opts.Globals.NoHeader = true
    35  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts())
    36  }
    37  
    38  func (opts *BlocksOptions) getCacheType() walk.CacheType {
    39  	if opts.Logs {
    40  		return walk.Cache_Logs
    41  	} else if opts.Traces {
    42  		return walk.Cache_Traces
    43  	} else {
    44  		return walk.Cache_Blocks
    45  	}
    46  }
    47  
    48  func (opts *BlocksOptions) getItemsToRemove() ([]cache.Locator, error) {
    49  	cT := opts.getCacheType()
    50  	switch cT {
    51  	case walk.Cache_Logs:
    52  		itemsToRemove, err := decache.LocationsFromLogs(opts.Conn, opts.BlockIds)
    53  		return itemsToRemove, err
    54  	case walk.Cache_Traces:
    55  		itemsToRemove, err := decache.LocationsFromTraces(opts.Conn, opts.BlockIds)
    56  		return itemsToRemove, err
    57  	case walk.Cache_Blocks:
    58  		itemsToRemove, err := decache.LocationsFromBlocks(opts.Conn, opts.BlockIds)
    59  		return itemsToRemove, err
    60  	default:
    61  		logger.Fatal("Unknown cache type. Should not happen.")
    62  		return []cache.Locator{}, nil
    63  	}
    64  }