wal

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 8 Imported by: 1

README

wal

PkgGoDev Build Status codecov Go Report Card LICENSE

Package wal implements write-ahead logging.

Feature

  • Low memory usage
  • Segment
  • Batch writes
  • Clean/Truncate/Reset

Get started

Install
go get github.com/hslam/wal
Import
import "github.com/hslam/wal"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/wal"
	"os"
)

func main() {
	path := "wal"
	os.RemoveAll(path)
	w, err := wal.Open(path, &wal.Options{SegmentEntries: 3})
	if err != nil {
		panic(err)
	}
	defer w.Close()
	// Write
	w.Write(1, []byte("Hello World"))
	w.Flush()
	w.Sync()
	// Batch Write
	w.Write(2, []byte("Hello WAL"))
	w.Write(3, []byte("Hello MH"))
	w.Flush()
	w.Sync()
	data, _ := w.Read(1)
	fmt.Println(string(data))
	w.Clean(2)
	w.Truncate(2)
	w.Reset()
}
Output
Hello World
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

wal was written by Meng Huang.

Documentation

Overview

Package wal implements write-ahead logging.

Index

Constants

View Source
const (
	// DefaultSegmentSize is the default segment size.
	DefaultSegmentSize = 1024 * 1024 * 512
	// DefaultSegmentEntries is the default segment entries.
	DefaultSegmentEntries = 1024 * 1024 * 8
	// DefaultWriteBufferSize is the default write buffer size.
	DefaultWriteBufferSize = 1024 * 1024
	// DefaultEncodeBufferSize is the default encode buffer size.
	DefaultEncodeBufferSize = 1024 * 64
	// DefaultBase is the default base.
	DefaultBase = 10
)
View Source
const (
	// DefaultLogSuffix is the default log suffix.
	DefaultLogSuffix = ".log"
	// DefaultIndexSuffix is the default index suffix.
	DefaultIndexSuffix = ".idx"
)

Variables

View Source
var (
	// ErrClosed is returned when the log is closed.
	ErrClosed = errors.New("closed")
	// ErrUnexpectedSize is returned when the number of bytes is unexpected.
	ErrUnexpectedSize = errors.New("unexpected size")
	// ErrOutOfRange is returned when the index is out of range.
	ErrOutOfRange = errors.New("out of range")
	// ErrZeroIndex is returned because the index must be greater than zero.
	ErrZeroIndex = errors.New("index can not be zero")
	// ErrOutOfOrder is returned when the index is out of order. The index must be equal to LastIndex + 1
	ErrOutOfOrder = errors.New("out of order")
	// ErrBase is returned when base < 2 or base > 36
	ErrBase = errors.New("2 <= base <= 36")
)

Functions

This section is empty.

Types

type Options

type Options struct {
	// SegmentSize is the segment size.
	SegmentSize int
	// SegmentEntries is the number of segment entries.
	SegmentEntries int
	// EncodeBufferSize is the encode buffer size.
	EncodeBufferSize int
	// WriteBufferSize is the write buffer size.
	WriteBufferSize int
	// LogSuffix is the log suffix.
	LogSuffix string
	// IndexSuffix is the index suffix.
	IndexSuffix string
	// Base is the base.
	Base int
	// NoSplitSegment is used by the Clean method. When this option is set,
	// do not split the segment. Default is false .
	NoSplitSegment bool
}

Options represents options

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns default options.

type WAL

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

WAL represents a write-ahead log.

func Open

func Open(path string, opts *Options) (w *WAL, err error)

Open opens a write-ahead log with options.

func (*WAL) Clean

func (w *WAL) Clean(index uint64) (err error)

Clean cleans up the old entries before index.

func (*WAL) Close

func (w *WAL) Close() (err error)

Close closes the write-ahead log.

func (*WAL) FirstIndex

func (w *WAL) FirstIndex() (index uint64, err error)

FirstIndex returns the write-ahead log first index.

func (*WAL) Flush

func (w *WAL) Flush() error

Flush writes buffered data to file.

func (*WAL) IsExist

func (w *WAL) IsExist(index uint64) (bool, error)

IsExist returns true when the index is in range.

func (*WAL) LastIndex

func (w *WAL) LastIndex() (index uint64, err error)

LastIndex returns the write-ahead log last index.

func (*WAL) Read

func (w *WAL) Read(index uint64) (data []byte, err error)

Read returns an entry by index.

func (*WAL) Reset

func (w *WAL) Reset() (err error)

Reset discards all entries.

func (*WAL) Sync

func (w *WAL) Sync() error

Sync commits the current contents of the file to stable storage. Typically, this means flushing the file system's in-memory copy of recently written data to disk.

func (*WAL) Truncate

func (w *WAL) Truncate(index uint64) (err error)

Truncate deletes the dirty entries after index.

func (*WAL) Write

func (w *WAL) Write(index uint64, data []byte) (err error)

Write writes an entry to buffer.

Jump to

Keyboard shortcuts

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