wal

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 10 Imported by: 0

README

wal

MIT License GoDoc Go Report Card Releases

simple/small write ahead log.

Installation

$ go get github.com/octu0/wal

Example

import "github.com/octu0/wal"

func main() {
	log, err := Open("/path/to/dir", wal.WithSync(true))
	if err != nil {
		panic(err)
	}
	defer log.Close()

	i1, err := log.Write([]byte("data1"))
	i2, err := log.Write([]byte("data2"))
	err := log.WriteAt(Index(100), []byte("data3"))

	data1, _ := log.Read(i1)
	println(string(data1)) // => "data1"

	data3, _ := log.Read(Index(100))
	println(string(data3)) // => "data3"

	// delete logs on memory
	if err := log.Delete(i1, i2); err != nil {
		panic(err)
	}

	// compaction of deleted logs to free disk space
	if err := log.Compact(); err != nil {
		panic(err)
	}
}

License

MIT, see LICENSE file for details.

Documentation

Overview

Example
dir, err := os.MkdirTemp("", "testdir")
if err != nil {
	panic(err)
}

log, err := Open(dir)
if err != nil {
	panic(err)
}

i1, _ := log.Write([]byte("data1"))
i2, _ := log.Write([]byte("data2"))
_ = log.WriteAt(Index(100), []byte("data3"))

data1, _ := log.Read(i1)
fmt.Println(string(data1))

data3, _ := log.Read(Index(100))
fmt.Println(string(data3))

// delete logs on memory
if err := log.Delete(i1, i2); err != nil {
	panic(err)
}

// compaction of deleted logs to free disk space
if err := log.Compact(); err != nil {
	panic(err)
}
Output:

data1
data3

Index

Examples

Constants

View Source
const (
	AppName string = "wal"
	Version string = "1.0.5"
)

Variables

View Source
var (
	ErrClosed         = errors.New("closed")
	ErrCompactRunning = errors.New("compat already in progress")
	ErrNotFound       = errors.New("not found")
)
View Source
var (
	ErrLocked = errors.Errorf("locked")
)

Functions

This section is empty.

Types

type DataLoadFunc added in v1.0.4

type DataLoadFunc func(Index)

type FileLocker added in v1.0.2

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

func NewFileLocker added in v1.0.2

func NewFileLocker(path string) *FileLocker

func (*FileLocker) TryLock added in v1.0.2

func (lock *FileLocker) TryLock() error

func (*FileLocker) Unlock added in v1.0.2

func (lock *FileLocker) Unlock() error

type Index

type Index codec.ID

type Locker added in v1.0.2

type Locker interface {
	TryLock() error
	Unlock() error
}

type Log

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

func Open

func Open(dir string, funcs ...OptionFunc) (*Log, error)

func (*Log) Close

func (l *Log) Close() error

func (*Log) Compact

func (l *Log) Compact() error

func (*Log) Delete

func (l *Log) Delete(idxs ...Index) error

func (*Log) LastIndex

func (l *Log) LastIndex() Index

func (*Log) Len

func (l *Log) Len() int

func (*Log) NeedCompaction added in v1.0.2

func (l *Log) NeedCompaction() bool

func (*Log) Read

func (l *Log) Read(idx Index) ([]byte, error)

func (*Log) ReadFrom added in v1.0.3

func (l *Log) ReadFrom(r io.Reader) (int64, error)

func (*Log) ReclaimableSpace

func (l *Log) ReclaimableSpace() uint64

func (*Log) Sync

func (l *Log) Sync() error

func (*Log) Write

func (l *Log) Write(data []byte) (Index, error)

func (*Log) WriteAt

func (l *Log) WriteAt(idx Index, data []byte) error

func (*Log) WriteTo added in v1.0.3

func (l *Log) WriteTo(w io.Writer) (int64, error)

type OptionFunc

type OptionFunc func(*logOpt)

func WithCloseCompaction added in v1.0.2

func WithCloseCompaction(enable bool) OptionFunc

func WithDataLoadFunc added in v1.0.4

func WithDataLoadFunc(fn DataLoadFunc) OptionFunc

func WithSync

func WithSync(enable bool) OptionFunc

func WithWriteBufferSize

func WithWriteBufferSize(size int) OptionFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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