wal

package module
v0.0.0-...-12d7164 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2023 License: MIT Imports: 10 Imported by: 1

README

wal

GoDoc

A fast write ahead log for Go, all writes wait for sync and all functions are thread safe.

Getting Started

// open a write ahead log file
w, err := Open("testlog")

// write some records
pos, err = w.Write("first record")
pos, err = w.Write("second record")
pos, err = w.Write("third record")

// read all records from head
r, err = w.NewReader(0)
for {
    pos, data, err = r.Next()
    if pos == -1{
        break
    }
}

// truncate the log from position before 100
err = w.TruncateBefore(100)
// truncate the log from position after 200
err = w.TruncateAfter(200)

// backup
name, err = w.Backup()

// recover the write ahead log from the backup file
err = w.RecoverFromBackup(name)

// close the log
w.Close()

License

wal source code is available under the MIT License.

Documentation

Index

Constants

View Source
const (
	// MaxRecordSize is the maximum size of a record
	MaxRecordSize = 1<<32 - 1
	// SyncConcurrency is the number of concurrent syncs
	SyncConcurrency = 1 << 10
	// RecordHeaderSize is the size of the record header
	RecordHeaderSize = 8
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader represents a reader for the write ahead log, Reader is thread-unsafe

func (*Reader) Close

func (r *Reader) Close()

Close close the reader

func (*Reader) Next

func (r *Reader) Next() (int64, []byte, error)

Next read a record from the write ahead log, return a byte slice contains the entry and the position for the write ahead log, -1 means the last record

type Wal

type Wal struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

WAL represents a write ahead log that provides durability and fault-tolerance for incoming writes

func Open

func Open(name string, filePerm os.FileMode) (*Wal, error)

Open open a new write ahead log

func (*Wal) Backup

func (w *Wal) Backup() (string, error)

Backup backup the write ahead log, return the backup file name this operation is mutually exclusive with all read and write operations

func (*Wal) Close

func (w *Wal) Close() error

Close close the write ahead log

func (*Wal) NewReader

func (w *Wal) NewReader(pos int64) (*Reader, error)

NewReader returns a new reader for the write ahead log, the created reader traverses the current point snapshot to read all the records in the current write ahead log

func (*Wal) RecoverFromBackup

func (w *Wal) RecoverFromBackup(name string) error

RecoverFromBackup recover the write ahead log from the backup this operation is mutually exclusive with all read and write operations

func (*Wal) TruncateAfter

func (w *Wal) TruncateAfter(pos int64) error

TruncateAfter truncates the back of the write ahead log by removing all records after the provided pos this operation is mutually exclusive with all read and write operations

func (*Wal) TruncateBefore

func (w *Wal) TruncateBefore(pos int64) error

TruncateBefore truncates the front of the write ahead log by removing all records that are before the provided pos this operation is mutually exclusive with all read and write operations

func (*Wal) Write

func (w *Wal) Write(p []byte) (int64, error)

write a record to the write ahead log, return the position of the record

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL