value.go
value.go - Overview
This file defines the value log functionality for BadgerDB, including structs and functions for reading, writing, and managing value log files, garbage collection, and value thresholding.
Detailed Documentation
Constants
const (
bitDelete byte = 1 << 0
bitValuePointer byte = 1 << 1
bitDiscardEarlierVersions byte = 1 << 2
bitMergeEntry byte = 1 << 3
bitTxn byte = 1 << 6
bitFinTxn byte = 1 << 7
mi int64 = 1 << 20
vlogHeaderSize = 20
)
- Purpose: Defines constants used within the value log.
bitDelete
: Indicates that the key has been deleted.bitValuePointer
: Indicates that the value is stored in the value log.bitDiscardEarlierVersions
: Indicates that earlier versions can be discarded.bitMergeEntry
: Indicates an entry used by the merge operator.bitTxn
: Indicates that the entry is part of a transaction.bitFinTxn
: Indicates that the entry is the end of a transaction.mi
: Represents 1MB.vlogHeaderSize
: Size of the value log header.
errStop var
var errStop = errors.New("Stop iteration")
- Purpose: Error to stop iteration.
errTruncate var
var errTruncate = errors.New("Do truncate")
- Purpose: Error to indicate truncation.
logEntry type
type logEntry func(e Entry, vp valuePointer) error
- Purpose: Defines the signature for a function that processes a log entry.
safeRead struct
type safeRead struct {
k []byte
v []byte
recordOffset uint32
lf *logFile
}
- Purpose: Helper struct for reading log entries.
hashReader struct
type hashReader struct {
r io.Reader
h hash.Hash32
bytesRead int
}
- Purpose: Reader that also calculates a hash.
newHashReader func
func newHashReader(r io.Reader) *hashReader {
- Purpose: Creates a new
hashReader
. - Parameters:
r
:io.Reader
- The reader to wrap.
- Returns:
*hashReader
- The newhashReader
.
hashReader.Read func
func (t *hashReader) Read(p []byte) (int, error) {
- Purpose: Reads from the underlying reader and updates the hash.
- Parameters:
p
:[]byte
- The buffer to read into.
- Returns:
int
- The number of bytes read.error
- An error, if any.
hashReader.ReadByte func
func (t *hashReader) ReadByte() (byte, error) {
- Purpose: Reads a single byte from the underlying reader and updates the hash.
- Returns:
byte
- The byte read.error
- An error, if any.
hashReader.Sum32 func
func (t *hashReader) Sum32() uint32 {
- Purpose: Returns the sum32 of the underlying hash.
- Returns:
uint32
- The sum32 of the underlying hash.
safeRead.Entry func
func (r *safeRead) Entry(reader io.Reader) (*Entry, error) {
- Purpose: Reads an entry from the provided reader, validates the checksum, and decrypts (if necessary).
- Parameters:
reader
:io.Reader
- The reader to read from.
- Returns:
*Entry
- The entry read.error
- An error, if any.
valueLog.rewrite func
func (vlog *valueLog) rewrite(f *logFile) error {
- Purpose: Rewrites a value log file by iterating over its entries and re-inserting them into the database if they are still valid and point to this file. This is used during garbage collection.
- Parameters:
f
:*logFile
- The log file to rewrite.
- Returns:
error
- An error, if any.
valueLog.incrIteratorCount func
func (vlog *valueLog) incrIteratorCount() {
- Purpose: Increments the number of active iterators.
valueLog.iteratorCount func
func (vlog *valueLog) iteratorCount() int {
- Purpose: Returns the number of active iterators.
- Returns:
int
- The number of active iterators.
valueLog.decrIteratorCount func
func (vlog *valueLog) decrIteratorCount() error {
- Purpose: Decrements the number of active iterators. If the count reaches zero, it deletes any log files that were marked for deletion.
- Returns:
error
- An error, if any.
valueLog.deleteLogFile func
func (vlog *valueLog) deleteLogFile(lf *logFile) error {
- Purpose: Deletes a log file.
- Parameters:
lf
:*logFile
- The log file to delete.
- Returns:
error
- An error, if any.
valueLog.dropAll func
func (vlog *valueLog) dropAll() (int, error) {
- Purpose: Drops all value log files.
- Returns:
int
- The number of files dropped.error
- An error, if any.
DB.valueThreshold func
func (db *DB) valueThreshold() int64 {
- Purpose: Returns the current value threshold.
- Returns:
int64
- The current value threshold.
valueLog struct
type valueLog struct {
dirPath string
filesLock sync.RWMutex
filesMap map[uint32]*logFile
maxFid uint32
filesToBeDeleted []uint32
numActiveIterators atomic.Int32
db *DB
writableLogOffset atomic.Uint32
numEntriesWritten uint32
opt Options
garbageCh chan struct{}
discardStats *discardStats
}
- Purpose: Manages value log files.
dirPath
: Directory where value logs are stored.filesLock
: Read-write mutex for accessing file-related maps and slices.filesMap
: Map of file IDs tologFile
structs.maxFid
: Maximum file ID.filesToBeDeleted
: Slice of file IDs to be deleted.numActiveIterators
: Atomic counter for active iterators.db
: Pointer to theDB
struct.writableLogOffset
: Atomic offset for the writable log file.numEntriesWritten
: Number of entries written to the current log file.opt
: Database options.garbageCh
: Channel for garbage collection.discardStats
: Statistics for discarded entries.
vlogFilePath func
func vlogFilePath(dirPath string, fid uint32) string {
- Purpose: Generates the file path for a value log file.
- Parameters:
dirPath
:string
- The directory path.fid
:uint32
- The file ID.
- Returns:
string
- The file path.
valueLog.fpath func
func (vlog *valueLog) fpath(fid uint32) string {
- Purpose: Returns the file path for a given file ID.
- Parameters:
fid
:uint32
- The file ID.
- Returns:
string
- The file path.
valueLog.populateFilesMap func
func (vlog *valueLog) populateFilesMap() error {
- Purpose: Populates the
filesMap
by reading the value log directory. - Returns:
error
- An error, if any.
valueLog.createVlogFile func
func (vlog *valueLog) createVlogFile() (*logFile, error) {
- Purpose: Creates a new value log file.
- Returns:
*logFile
- The new log file.error
- An error, if any.
errFile func
func errFile(err error, path string, msg string) error {
- Purpose: Formats an error message for file-related errors.
- Parameters:
err
:error
- The error.path
:string
- The file path.msg
:string
- The error message.
- Returns:
error
- The formatted error.
valueLog.init func
func (vlog *valueLog) init(db *DB) {
- Purpose: Initializes the value log struct.
- Parameters:
db
:*DB
- The database instance.
valueLog.open func
func (vlog *valueLog) open(db *DB) error {
- Purpose: Opens the value log files.
- Parameters:
db
:*DB
- The database instance.
- Returns:
error
- An error, if any.
valueLog.Close func
func (vlog *valueLog) Close() error {
- Purpose: Closes all open value log files.
- Returns:
error
- An error, if any.
valueLog.sortedFids func
func (vlog *valueLog) sortedFids() []uint32 {
- Purpose: Returns the sorted list of file IDs.
- Returns:
[]uint32
- Sorted file IDs.
request struct
type request struct {
Entries []*Entry
Ptrs []valuePointer
Wg sync.WaitGroup
Err error
ref atomic.Int32
}
- Purpose: Represents a write request to the value log.
Entries
: Entries to be written.Ptrs
: Value pointers for the written entries.Wg
: Wait group for asynchronous operations.Err
: Error encountered during the write.ref
: Atomic counter for tracking references to the request.
request.reset func
func (req *request) reset() {
- Purpose: Resets the request struct.
request.IncrRef func
func (req *request) IncrRef() {
- Purpose: Increments the reference count.
request.DecrRef func
func (req *request) DecrRef() {
- Purpose: Decrements the reference count.
request.Wait func
func (req *request) Wait() error {
- Purpose: Waits for the request to complete and returns any error.
- Returns:
error
- An error, if any.
requests type
type requests []*request
- Purpose: Represents a slice of requests.
requests.DecrRef func
func (reqs requests) DecrRef() {
- Purpose: Decrements the reference count of all requests.
requests.IncrRef func
func (reqs requests) IncrRef() {
- Purpose: Increments the reference count of all requests.
valueLog.sync func
func (vlog *valueLog) sync() error {
- Purpose: Syncs the current value log file to disk.
- Returns:
error
- An error, if any.
valueLog.woffset func
func (vlog *valueLog) woffset() uint32 {
- Purpose: Returns the current write offset.
- Returns:
uint32
- The current write offset.
valueLog.validateWrites func
func (vlog *valueLog) validateWrites(reqs []*request) error {
- Purpose: Validates that the writes in the given requests can fit into the value log file.
- Parameters:
reqs
:[]*request
- The requests to validate.
- Returns:
error
- An error, if any.
estimateRequestSize func
func estimateRequestSize(req *request) uint64 {
- Purpose: Estimates the size required to write a request to the value log.
- Parameters:
req
:*request
- The request to estimate.
- Returns:
uint64
- The estimated size.
valueLog.write func
func (vlog *valueLog) write(reqs []*request) error {
- Purpose: Writes the given requests to the value log.
- Parameters:
reqs
:[]*request
- The requests to write.
- Returns:
error
- An error, if any.
valueLog.getFileRLocked func
func (vlog *valueLog) getFileRLocked(vp valuePointer) (*logFile, error) {
- Purpose: Gets a
logFile
and acquires a read lock on its mmap. - Parameters:
vp
:valuePointer
- Value pointer.
- Returns:
*logFile
- Log file.error
- An error, if any.
valueLog.Read func
func (vlog *valueLog) Read(vp valuePointer, _ *y.Slice) ([]byte, func(), error) {
- Purpose: Reads the value log at a given location.
- Parameters:
vp
:valuePointer
- The location to read from._
: `*