This section provides detailed API documentation for the main components of BlockchainDB.
type KV struct {
Directory string
vFile *BFile
kFile *KFile
HistoryFile *HistoryFile
UseHistory bool
}func NewKV(history bool, directory string, offsetsCnt, KeyLimit uint64, MaxCachedBlocks int) (kv *KV, err error)Creates a new Key-Value store.
Parameters:
history- Whether to enable history trackingdirectory- Directory path for the databaseoffsetsCnt- Number of offset entriesKeyLimit- Key limit before history pushMaxCachedBlocks- Maximum number of blocks to cache
Returns:
kv- The new KV instanceerr- Error, if any
func OpenKV(directory string) (kv *KV, err error)Opens an existing Key-Value store.
Parameters:
directory- Directory path for the database
Returns:
kv- The opened KV instanceerr- Error, if any
func (k *KV) Put(key [32]byte, value []byte) (err error)Stores a value with the given key.
Parameters:
key- 32-byte keyvalue- Value data to store
Returns:
err- Error, if any
func (k *KV) Get(key [32]byte) (value []byte, err error)Retrieves a value using its key.
Parameters:
key- 32-byte key
Returns:
value- Retrieved valueerr- Error, if any
func (k *KV) Close() (err error)Closes the KV store.
Returns:
err- Error, if any
func (k *KV) Open() (err error)Opens the KV store.
Returns:
err- Error, if any
func (k *KV) Compress() (err error)Re-writes the values file to remove trash values.
Returns:
err- Error, if any
type BFile struct {
File *os.File
Filename string
Buffer [BufferSize]byte
EOB uint64
EOD uint64
}func NewBFile(filename string) (file *BFile, err error)Creates a new BFile.
Parameters:
filename- Path to the file
Returns:
file- The new BFile instanceerr- Error, if any
func OpenBFile(filename string) (bFile *BFile, err error)Opens an existing BFile.
Parameters:
filename- Path to the file
Returns:
bFile- The opened BFile instanceerr- Error, if any
func (b *BFile) Write(Data []byte) (update bool, err error)Writes data to the file.
Parameters:
Data- Data to write
Returns:
update- Whether an actual file update occurrederr- Error, if any
func (b *BFile) ReadAt(offset uint64, data []byte) (err error)Reads data from a specific offset.
Parameters:
offset- Offset to read fromdata- Buffer to read into
Returns:
err- Error, if any
func (b *BFile) Flush() (err error)Flushes the buffer to disk.
Returns:
err- Error, if any
func (b *BFile) Close() (err error)Closes the file.
Returns:
err- Error, if any
func (b *BFile) Offset() (offset uint64, err error)Returns the current real size of the BFile.
Returns:
offset- Current offseterr- Error, if any
type KFile struct {
Header
Directory string
File *BFile
History *HistoryFile
Cache map[[32]byte]*DBBKey
BlocksCached int
HistoryMutex sync.Mutex
KeyCnt uint64
TotalCnt uint64
OffsetCnt uint64
KeyLimit uint64
MaxCachedBlocks int
HistoryOffsets int
}func NewKFile(history bool, directory string, offsetCnt uint64, keyLimit uint64, maxCachedBlocks int) (kFile *KFile, err error)Creates a new KFile.
Parameters:
history- Whether to enable historydirectory- Directory pathoffsetCnt- Number of offsetskeyLimit- Key limitmaxCachedBlocks- Maximum cached blocks
Returns:
kFile- The new KFile instanceerr- Error, if any
func OpenKFile(directory string) (kFile *KFile, err error)Opens an existing KFile.
Parameters:
directory- Directory path
Returns:
kFile- The opened KFile instanceerr- Error, if any
func (k *KFile) Put(Key [32]byte, Value *DBBKey) (err error)Stores a key with its associated DBBKey.
Parameters:
Key- 32-byte keyValue- DBBKey to store
Returns:
err- Error, if any
func (k *KFile) Get(Key [32]byte) (dbBKey *DBBKey, err error)Retrieves a DBBKey using its key.
Parameters:
Key- 32-byte key
Returns:
dbBKey- Retrieved DBBKeyerr- Error, if any
func (k *KFile) PushHistory() (err error)Pushes current keys to history and resets.
Returns:
err- Error, if any
type HistoryFile struct {
Directory string
File *BFile
Mutex sync.Mutex
OffsetCnt uint64
Offsets []uint64
}func NewHistoryFile(offsetCnt uint64, directory string) (historyFile *HistoryFile, err error)Creates a new HistoryFile.
Parameters:
offsetCnt- Number of offsetsdirectory- Directory path
Returns:
historyFile- The new HistoryFile instanceerr- Error, if any
func (h *HistoryFile) AddKeys(keyData []byte) (err error)Adds keys to history.
Parameters:
keyData- Key data to add
Returns:
err- Error, if any
func (h *HistoryFile) Get(Key [32]byte) (dbBKey *DBBKey, err error)Gets historical key data.
Parameters:
Key- 32-byte key
Returns:
dbBKey- Retrieved DBBKeyerr- Error, if any
type BloomFilter struct {
Filter []byte
K int
}func NewBloomFilter(size uint64, k int) *BloomFilterCreates a new BloomFilter.
Parameters:
size- Size of the filterk- Number of hash functions
Returns:
- BloomFilter instance
func (b *BloomFilter) Add(data []byte)Adds an element to the filter.
Parameters:
data- Data to add
func (b *BloomFilter) Test(data []byte) boolTests if an element might be in the set.
Parameters:
data- Data to test
Returns:
- Whether the element might be in the set