Documentation
¶
Index ¶
- Variables
- func UnsafeRecover(dir, segmentPrefix string) ([]string, error)
- type Config
- type Record
- type Wal
- func (c *Wal) Close() error
- func (c *Wal) CurrentIndex() uint64
- func (c *Wal) Get(index uint64) (string, []byte, error)
- func (c *Wal) Iterator() iter.Seq[msg]
- func (c *Wal) PullIterator() (next func() (msg, bool), stop func())
- func (c *Wal) Write(index uint64, key string, value []byte) error
- func (c *Wal) WriteBatch(batch []Record) error
- func (c *Wal) WriteTombstone(index uint64) error
Constants ¶
This section is empty.
Variables ¶
var ErrExists = errors.New("msg with such index already exists")
Functions ¶
func UnsafeRecover ¶
UnsafeRecover recovers the WAL from the given directory. It is unsafe because it removes all the segment and checksum files that are corrupted (checksums do not match). It returns the list of segment and checksum files that were removed.
Types ¶
type Config ¶
type Config struct {
// Dir is the directory where the log files will be stored.
Dir string
// Prefix is the prefix for the segment files.
Prefix string
// SegmentThreshold is the number of records after which a new segment is created.
SegmentThreshold int
// MaxSegments is the maximum number of segments allowed before the oldest segment is deleted.
MaxSegments int
// IsInSyncDiskMode indicates whether the log should be synced to disk after each write.
IsInSyncDiskMode bool
}
Config represents the configuration for the WAL (Write-Ahead Log).
type Record ¶ added in v1.0.2
type Record struct {
// Index is the unique index of the record for fast search.
Index uint64
// Key is the unique key of the record.
Key string
// Value is the value of the record.
Value []byte
}
Record is a struct that represents a record in the log.
type Wal ¶
type Wal struct {
// contains filtered or unexported fields
}
Wal is a write-ahead log that stores key-value pairs.
Wal is append-only log, so we can't delete records from it, but log is divided into segments, which are rotated (oldest deleted) when segments number threshold is reached.
Index stored in memory and loaded from disk on Wal init.
func (*Wal) CurrentIndex ¶
CurrentIndex returns current index of the log.
func (*Wal) Iterator ¶
Iterator returns push-based iterator for the WAL messages. Messages are returned from the oldest to the newest.
Should be used like this:
for msg := range wal.Iterator() {
...
func (*Wal) PullIterator ¶
PullIterator returns pull-based iterator for the WAL messages. Messages are returned from the oldest to the newest.
Should be used like this:
next, stop := wal.PullIterator() defer stop() ...
func (*Wal) WriteBatch ¶ added in v1.0.2
WriteBatch appends a batch of records to the WAL in a single operation.
func (*Wal) WriteTombstone ¶
WriteTombstone writes a tombstone record for the given index. If no record exists for the index, returns nil (no-op). If a record exists, overwrites it with a tombstone.