aof.go
aof.go - Overview
This file implements the Append-Only File (AOF) functionality for a database, providing persistence by writing all operations to a file.
Detailed Documentation
AOF
type AOF struct {
file *os.File
writer *bufio.Writer
mutex sync.Mutex
path string
}
- Purpose: Represents an Append-Only File. It holds the file, writer, mutex for concurrent access, and the file path.
- Fields:
file
: A pointer to the underlyingos.File
.writer
: A pointer to abufio.Writer
for buffered writing to the file.mutex
: Async.Mutex
to protect concurrent access to the file.path
: The path to the AOF file.
FileMode
const (
FileMode int = 0644
)
- Purpose: Defines the file mode for creating the AOF file (0644).
NewAOF
func NewAOF(path string) (*AOF, error) {
- Purpose: Creates a new AOF instance. Opens the file in append mode, creating it if it doesn't exist.
- Parameters:
path
: The path to the AOF file.
- Returns:
- A pointer to the new
AOF
instance. - An
error
if opening the file fails.
- A pointer to the new
(a *AOF) Write
func (a *AOF) Write(operation string) error {
- Purpose: Writes an operation string to the AOF file. It locks the mutex, writes the operation with a newline, flushes the buffer, and syncs the file to disk.
- Parameters:
operation
: The string representing the operation to be written.
- Returns: An
error
if writing or syncing fails.
(a *AOF) Close
func (a *AOF) Close() error {
- Purpose: Closes the AOF file. Flushes the buffer before closing.
- Returns: An
error
if flushing or closing fails.
(a *AOF) Load
func (a *AOF) Load() ([]string, error) {
- Purpose: Loads the operations from the AOF file. Reads each line as a separate operation.
- Returns:
- A slice of strings, where each string is an operation.
- An
error
if opening, scanning, or closing the file fails.