The Key-Value Store (KV) is the main interface for interacting with BlockchainDB. It provides a simple key-value database implementation optimized for blockchain applications.
The KV component combines several underlying components to provide efficient storage and retrieval:
- Uses
BFilefor value storage - Uses
KFilefor key management - Optionally uses
HistoryFilefor maintaining historical data
type KV struct {
Directory string
vFile *BFile
kFile *KFile
HistoryFile *HistoryFile
UseHistory bool
}- Fixed-size 32-byte keys
- Variable-length values
- Optional history tracking
- Efficient storage with separate files for keys and values
// Create a new KV store with history enabled
kv, err := blockchainDB.NewKV(
true, // Enable history
"/path/to/db", // Directory path
1024, // Number of offset entries
10000, // Key limit before history push
100, // Max cached blocks
)// Open an existing KV store
kv, err := blockchainDB.OpenKV("/path/to/db")// Create a 32-byte key
var key [32]byte
// Fill key with data...
// Store a value
err := kv.Put(key, []byte("value data"))// Retrieve a value using its key
value, err := kv.Get(key)// Close the KV store
err := kv.Close()The KV store separates keys and values into different files:
- Keys are stored in a structured
KFilethat enables efficient lookups - Values are stored in a
BFilewith their offsets referenced by the keys - When history tracking is enabled, older versions of keys can be preserved
- The separation of keys and values optimizes for blockchain use cases where keys are frequently accessed
- The buffered file implementation reduces disk I/O operations
- The
Compress()method can be used to reclaim space from deleted or updated values