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

     1  package index
     2  
     3  import (
     4  	"encoding/binary"
     5  	"io"
     6  	"os"
     7  	"unsafe"
     8  
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    11  )
    12  
    13  // updateTag updates both the index and the bloom filter headers for a chunk.
    14  // This is a non-recoverable operation. The caller must take care of making a backup of
    15  // the file before we start if desired.
    16  func (idx *Index) updateTag(tag, fileName string) error {
    17  	var err error
    18  	if idx.File, err = os.OpenFile(fileName, os.O_RDWR, 0644); err != nil {
    19  		return err
    20  
    21  	} else {
    22  		defer func() {
    23  			_ = idx.File.Sync() // probably redundant
    24  			_ = idx.File.Close()
    25  			idx.File = nil
    26  		}()
    27  
    28  		// don't love this, but it saves us from having to read in and preserve the header
    29  		_, _ = idx.File.Seek(int64(unsafe.Sizeof(idx.Header.Magic)), io.SeekStart)
    30  		if err = binary.Write(idx.File, binary.LittleEndian, base.BytesToHash(config.HeaderHash(tag))); err != nil {
    31  			return err
    32  		}
    33  	}
    34  
    35  	return nil
    36  }