wal

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: Apache-2.0 Imports: 15 Imported by: 1

README

wal

Write ahead log for Go.

Features

  • transaction
    • when transaction was disabled, then Commit func is disabled.
    • when transaction was enabled and level is ReadCommitted, get any uncommitted entry will return not found error.
  • snapshot
  • batch writer
  • support key

Install

go get github.com/aacfactory/wal

Usage

// create
logs, createErr := wal.New[uint64](`file path`, wal.Unit64KeyEncoder(), wal.EnableTransaction(wal.ReadUncommitted))
// write, it will return the index of log
index, writeErr := logs.Write(1, []byte("some content"))
// read 
key, content, state, readErr := logs.Read(index)
// commit
commitErr := logs.Commit(index)
// key
index, content, state, readErr := logs.Key(1)
// close
logs.Close()

Batch mode, write multiple at one time, or cancel at one time.

batch := logs.Batch()
indexes := make([]uint64, 0, 1)
for i := 0; i < 3; i++ {
    index, err := batch.Write(uint64(i), []byte(time.Now().Format(time.RFC3339)))
    if err != nil {
        // handle err
        return
    }
    indexes = append(indexes, index)
}
fmt.Println(batch.Flush())
fmt.Println(logs.Commit(indexes...))

Documentation

Index

Constants

View Source
const (
	SOT64B = SOT(1 << (6 + iota))
	SOT128B
	SOT256B
	SOT512B
	SOT1K
	SOT2K
	SOT4K
	SOT8K
)
View Source
const (
	UncommittedState = State(iota + 1)
	CommittedState
	DiscardedState
)
View Source
const (
	ReadCommitted = TransactionLevel(iota + 1)
	ReadUncommitted
)

Variables

View Source
var (
	ErrInvalidEntry         = fmt.Errorf("invalid entry")
	ErrClosed               = fmt.Errorf("wal was closed")
	ErrNoFirstIndex         = fmt.Errorf("wal has no first Index")
	ErrNoLastIndex          = fmt.Errorf("wal has no last Index")
	ErrNotFound             = fmt.Errorf("not found")
	ErrCommittedOrDiscarded = fmt.Errorf("entry was Committed or Discarded")
)

Functions

func ExistFile added in v1.1.0

func ExistFile(filePath string) (ok bool)

Types

type Batch

type Batch[K ordered] struct {
	// contains filtered or unexported fields
}

func (*Batch[K]) Cancel

func (batch *Batch[K]) Cancel()

func (*Batch[K]) Close

func (batch *Batch[K]) Close()

func (*Batch[K]) Flush

func (batch *Batch[K]) Flush() (err error)

func (*Batch[K]) Write

func (batch *Batch[K]) Write(key K, p []byte) (index uint64, err error)

type Entry

type Entry []byte

func DecodeEntries

func DecodeEntries(sot SOT, p []byte) (entries []Entry)

func NewEntry

func NewEntry(sot SOT, index uint64, key []byte, p []byte) (entry Entry)

func (Entry) Commit

func (entry Entry) Commit()

func (Entry) Data

func (entry Entry) Data() (p []byte)

func (Entry) Discard added in v1.2.0

func (entry Entry) Discard()

func (Entry) Header added in v1.4.0

func (entry Entry) Header() (header EntryHeader)

func (Entry) Key added in v1.2.0

func (entry Entry) Key() (key []byte)

func (Entry) Remove added in v1.3.0

func (entry Entry) Remove()

func (Entry) TEUsLen added in v1.4.0

func (entry Entry) TEUsLen() (n uint16)

func (Entry) Validate

func (entry Entry) Validate() (ok bool)

type EntryHeader added in v1.4.0

type EntryHeader []byte

func (EntryHeader) Committed added in v1.4.0

func (header EntryHeader) Committed() (ok bool)

func (EntryHeader) Discarded added in v1.4.0

func (header EntryHeader) Discarded() (ok bool)

func (EntryHeader) HashCode added in v1.4.0

func (header EntryHeader) HashCode() (code uint64)

func (EntryHeader) Index added in v1.4.0

func (header EntryHeader) Index() (index uint64)

func (EntryHeader) Removed added in v1.4.0

func (header EntryHeader) Removed() (ok bool)

func (EntryHeader) State added in v1.4.0

func (header EntryHeader) State() (state State)

func (EntryHeader) StateFinished added in v1.4.0

func (header EntryHeader) StateFinished() (ok bool)

func (EntryHeader) String added in v1.4.0

func (header EntryHeader) String() (s string)

type File

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

func OpenFile

func OpenFile(path string) (file *File, err error)

func (*File) Close

func (f *File) Close()

func (*File) Path added in v1.1.0

func (f *File) Path() (path string)

func (*File) ReadAt

func (f *File) ReadAt(p []byte, start uint64) (err error)

func (*File) Reopen added in v1.3.3

func (f *File) Reopen() (err error)

func (*File) Size

func (f *File) Size() (n uint64)

func (*File) Truncate added in v1.1.0

func (f *File) Truncate(end uint64) (err error)

func (*File) WriteAt

func (f *File) WriteAt(p []byte, start uint64) (err error)

type KeyEncoder added in v1.3.0

type KeyEncoder[K ordered] interface {
	Encode(key K) (p []byte, err error)
	Decode(p []byte) (key K, err error)
}

func Int64KeyEncoder added in v1.3.0

func Int64KeyEncoder() KeyEncoder[int64]

func StringKeyEncoder added in v1.3.0

func StringKeyEncoder() KeyEncoder[string]

func Unit64KeyEncoder added in v1.3.0

func Unit64KeyEncoder() KeyEncoder[uint64]

type Option added in v1.3.2

type Option func(options *Options)

func EnableTransaction added in v1.3.2

func EnableTransaction(level TransactionLevel) Option

func UseSOT added in v1.4.0

func UseSOT(sot SOT) Option

func WithCacheSize added in v1.3.2

func WithCacheSize(size int) Option

type Options added in v1.3.2

type Options struct {
	SOT                SOT
	CacheSize          int
	TransactionEnabled bool
	TransactionLevel   TransactionLevel
}

type SOT added in v1.4.0

type SOT uint16

func (SOT) String added in v1.4.0

func (sot SOT) String() string

type State added in v1.2.0

type State uint16

func (State) String added in v1.2.0

func (state State) String() string

type TEU added in v1.4.0

type TEU []byte

func NewTEU added in v1.4.0

func NewTEU(size SOT) (teu TEU)

type TEUs added in v1.4.0

type TEUs []byte

func NewTEUs added in v1.4.0

func NewTEUs(size SOT, p []byte) (b TEUs)

func (TEUs) Data added in v1.4.0

func (s TEUs) Data() (p []byte)

func (TEUs) FirstDataCut added in v1.4.0

func (s TEUs) FirstDataCut(low int, high int) (p []byte)

func (TEUs) Len added in v1.4.0

func (s TEUs) Len() (p uint16)

func (TEUs) SOT added in v1.4.0

func (s TEUs) SOT() (sot SOT)

type TransactionLevel added in v1.3.2

type TransactionLevel int

func (TransactionLevel) String added in v1.3.2

func (level TransactionLevel) String() string

type WAL

type WAL[K ordered] struct {
	// contains filtered or unexported fields
}

func New

func New[K ordered](path string, keyEncoder KeyEncoder[K], options ...Option) (wal *WAL[K], err error)

func (*WAL[K]) Batch

func (wal *WAL[K]) Batch() (batch *Batch[K])

func (*WAL[K]) Close

func (wal *WAL[K]) Close()

func (*WAL[K]) Commit

func (wal *WAL[K]) Commit(indexes ...uint64) (err error)

func (*WAL[K]) CommitKey added in v1.3.0

func (wal *WAL[K]) CommitKey(keys ...K) (err error)

func (*WAL[K]) CreateSnapshot

func (wal *WAL[K]) CreateSnapshot(endIndex uint64, sink io.Writer) (err error)

CreateSnapshot contains end Index

func (*WAL[K]) Discard added in v1.2.0

func (wal *WAL[K]) Discard(indexes ...uint64) (err error)

func (*WAL[K]) DiscardKey added in v1.3.0

func (wal *WAL[K]) DiscardKey(keys ...K) (err error)

func (*WAL[K]) FirstIndex

func (wal *WAL[K]) FirstIndex() (idx uint64, err error)

func (*WAL[K]) FirstKey added in v1.3.0

func (wal *WAL[K]) FirstKey() (k K, err error)

func (*WAL[K]) Key added in v1.2.0

func (wal *WAL[K]) Key(key K) (index uint64, p []byte, state State, err error)

func (*WAL[K]) LastIndex

func (wal *WAL[K]) LastIndex() (idx uint64, err error)

func (*WAL[K]) LastKey added in v1.3.0

func (wal *WAL[K]) LastKey() (k K, err error)

func (*WAL[K]) Len added in v1.1.0

func (wal *WAL[K]) Len() (n uint64)

func (*WAL[K]) OldestUncommitted

func (wal *WAL[K]) OldestUncommitted() (index uint64, has bool)

func (*WAL[K]) OldestUncommittedKey added in v1.3.2

func (wal *WAL[K]) OldestUncommittedKey() (key K, has bool)

func (*WAL[K]) Read

func (wal *WAL[K]) Read(index uint64) (key K, p []byte, state State, err error)

func (*WAL[K]) Remove added in v1.3.0

func (wal *WAL[K]) Remove(index uint64) (err error)

func (*WAL[K]) RemoveKey added in v1.3.0

func (wal *WAL[K]) RemoveKey(key K) (err error)

func (*WAL[K]) TruncateBack added in v1.1.0

func (wal *WAL[K]) TruncateBack(startIndex uint64) (err error)

TruncateBack truncate logs after and contains startIndex

func (*WAL[K]) TruncateFront added in v1.1.0

func (wal *WAL[K]) TruncateFront(endIndex uint64) (err error)

TruncateFront truncate logs before and contains endIndex

func (*WAL[K]) Uncommitted

func (wal *WAL[K]) Uncommitted(index uint64) (ok bool)

func (*WAL[K]) UncommittedKey added in v1.3.1

func (wal *WAL[K]) UncommittedKey(key K) (ok bool)

func (*WAL[K]) UncommittedSize added in v1.2.0

func (wal *WAL[K]) UncommittedSize() (n uint64)

func (*WAL[K]) Write

func (wal *WAL[K]) Write(key K, p []byte) (index uint64, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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