wal

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 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.0"
)

Variables

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

Functions

This section is empty.

Types

type Index

type Index codec.ID

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) Read

func (l *Log) Read(idx Index) ([]byte, 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

type OptionFunc

type OptionFunc func(*logOpt)

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