index

package
v0.0.0-...-2de43c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2024 License: GPL-3.0 Imports: 30 Imported by: 1

Documentation

Overview

Package index handles reading, writing, and searching the Unchained Index include both the chunk data and the Bloom filters

Index

Constants

View Source
const (
	// The number of bytes in a single BloomByte structure
	BLOOM_WIDTH_IN_BYTES = (BLOOM_WIDTH_IN_BITS / 8)
	// The number of bits in a single BloomByte structure
	BLOOM_WIDTH_IN_BITS = (1048576)
	// The maximum number of addresses to add to a bloomBytes before creating a new one
	MAX_ADDRS_IN_BLOOM = 50000
)
View Source
const (
	// AddrRecordWidth - size of Address Record
	AddrRecordWidth = 28
)
View Source
const (
	// AppRecordWidth - size of Appearance Record
	AppRecordWidth = 8
)
View Source
const (
	// HeaderWidth - size of Header Record
	HeaderWidth = 44
)

Variables

View Source
var ErrDownloadError = errors.New("download error")
View Source
var ErrFailedLocalFileRemoval = errors.New("failed to remove local file")
View Source
var ErrIncorrectHash = errors.New("incorrect header hash")
View Source
var ErrIncorrectMagic = errors.New("incorrect magic number")
View Source
var ErrNotInitialized = errors.New("index not initialized")
View Source
var ErrUserHitControlC = errors.New("user hit control + c")
View Source
var ErrWriteToDiscError = errors.New("write to disc error")

Functions

func ChunkCid

func ChunkCid(path string) (chunkCid string, err error)

ChunkCid returns IPFS CID for the chunk without uploading it

func DownloadChunks

func DownloadChunks(chain string, chunksToDownload []types.ChunkRecord, chunkType walk.CacheType, poolSize int, progressChannel progressChan)

DownloadChunks downloads, unzips and saves the chunk of type indicated by chunkType for each chunk in chunks. ProgressMsg is reported to progressChannel.

func DownloadOneChunk

func DownloadOneChunk(chain string, man *manifest.Manifest, fileRange base.FileRange) error

DownloadOneChunk a filename to an index portion, finds the correspoding CID (hash) entry in the manifest, and downloads the index chunk to the local drive

func IsInitialized

func IsInitialized(chain, required string) error

IsInitialized returns an error if the version in the header is not as requested

func ToBloomPath

func ToBloomPath(pathIn string) string

ToBloomPath returns a path pointing to the bloom filter given either a path to itself or its associated index data

func ToIndexPath

func ToIndexPath(pathIn string) string

ToIndexPath returns a path pointing to the index portion

func ToStagingPath

func ToStagingPath(pathIn string) string

ToStagingPath returns a path pointing to the staging folder given either a neighboring path

Types

type AppearanceResult

type AppearanceResult struct {
	Address    base.Address
	Range      base.FileRange
	AppRecords *[]types.AppRecord
	Err        error
}

AppearanceResult carries the appearances found in a single Index for the given address.

type Bloom

type Bloom struct {
	File       *os.File
	SizeOnDisc int64
	Range      base.FileRange
	HeaderSize int64
	Header     bloomHeader
	Count      uint32 // Do not change the size of this field, it's stored on disc
	Blooms     []bloomBytes
}

Bloom structures contain an array of bloomBytes each BLOOM_WIDTH_IN_BYTES wide. A new bloomBytes is added to the Bloom when around MAX_ADDRS_IN_BLOOM addresses has been added. These Adaptive Bloom Filters allow us to maintain a near-constant false-positive rate at the expense of slightly larger bloom filters than might be expected.

func OpenBloom

func OpenBloom(path string, check bool) (Bloom, error)

OpenBloom returns a newly initialized bloom filter. The bloom filter's file pointer is open (if there have been no errors) and its header data has been read into memory. The array has been created with enough space for Count blooms but has not been read from disc. The file remains open for reading (if there is no error) and is positioned at the start of the file.

func (*Bloom) Close

func (bl *Bloom) Close()

Close closes the file if it's opened

func (*Bloom) InsertAddress

func (bl *Bloom) InsertAddress(addr base.Address)

InsertAddress adds an address to the bloom filter.

func (*Bloom) IsMember

func (bl *Bloom) IsMember(addr base.Address) bool

func (*Bloom) Read

func (bl *Bloom) Read(fileName string) (err error)

Read reads the entire contents of the bloom filter

type Chunk

type Chunk struct {
	Range base.FileRange
	Index Index
	Bloom Bloom
}

The Chunk data structure consists of three parts. A FileRange, a Index structure, and a Bloom that carries set membership information for the Index.

func OpenChunk

func OpenChunk(path string, check bool) (chunk Chunk, err error)

OpenChunk returns a fully initialized index chunk. The path argument may point to either a bloom filter file or the index data file. Either will work. The bloom filter file must exist and will be opened for reading and its header will be read into memory, but the filter itself is not. The index data file need not exist (it will be downloaded later if the bloom indicates that its needed). If the index file does exist, however, it will be opened for reading and its header will be read into memory, but the index data itself will not be.

func (*Chunk) Close

func (chunk *Chunk) Close()

Close closes both the bloom filter file pointer and the index data file pointer (if they are open)

func (*Chunk) Tag

func (chunk *Chunk) Tag(tag, fileName string) (err error)

Tag updates the manifest version in the chunk's header

func (*Chunk) Write

func (chunk *Chunk) Write(chain string, publisher base.Address, fileName string, addrAppearanceMap map[string][]types.AppRecord, nApps int) (*writeReport, error)

type Index

type Index struct {
	File           *os.File
	Header         indexHeader
	Range          base.FileRange
	AddrTableStart int64
	AppTableStart  int64
}

Index is one part of the two part Chunk (the other part is the Bloom)

Each Index contains a HeaderRecord followed by two tables: the AddressTable and a related AppearanceTable.

The HeaderRecord (44 bytes long) contains a four-byte magic number (`0xdeadbeef` -- to indicate we're reading a file of the correct type), a 32-byte hash representing the file's version, and two 4-byte integers representing the number of records in the AddressTable (nAddresses) and the number of records in the AppearanceTable (nAppearances) respectively.

The AddressTable has nAddresses records, one for each address that appears in any block between Start to End inclusive. These addresses are found using the TrueBlocks Appearance Finder algorithm. Each AddressRecord consists of a 20-byte address followed by two 4-byte integers representing the Offset into the AppearanceTable where this address's list of appearances starts followed by the Count of appearance records for the given address.

The AppearanceTable contains nAppeeances pairs of <blockNumber.transactionId> pairs arranged by the Offset and Count pairs found in the corresponding AddressTable records.

func OpenIndex

func OpenIndex(fileName string, check bool) (Index, error)

OpenIndex returns an Index with an opened file pointer to the given fileName. The HeaderRecord for the chunk has been populated and the file position to the two tables are ready for use.

func (*Index) Close

func (chunk *Index) Close() error

Close closes the Index's associated File pointer (if opened)

func (*Index) ReadAppearances

func (chunk *Index) ReadAppearances(address base.Address) *AppearanceResult

ReadAppearances searches an already-opened Index for the given address. Returns a AppearanceResult or nil

func (*Index) ReadAppearancesAndReset

func (chunk *Index) ReadAppearancesAndReset(addrRecord *types.AddrRecord) (apps []types.AppRecord, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL