statemate

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2024 License: MIT Imports: 8 Imported by: 2

README

StateMate Go Library

Overview

StateMate is a Go library designed for storing state as key-value pairs with efficient I/O operations and concurrency support.

Features

  • Memory-mapped files for efficient I/O.
  • Thread-safe via read-write mutexes.
  • Generics support for custom unsigned integer key types.
  • Dynamic resizing of data and index files.
  • Optional index gap allowance.

Requirements

  • Go 1.18 or higher for generics support.

Installation

Install the package using:

go get -u github.com/draganm/statemate

Usage

Initialize a StateMate instance
options := statemate.Options{ AllowGaps: true }
sm, err := statemate.Open[uint64]("datafile", options)
if err != nil {
    // Handle error
}
Append Data
err := sm.Append(1, []byte("some data"))
if err != nil {
    // Handle error
}
Read Data
err := sm.Read(1, func(data []byte) error {
    // Process data
    return nil
})
if err != nil {
    // Handle error
}
Check if Empty
isEmpty := sm.IsEmpty()
Get Last Index
lastIndex := sm.LastIndex()

Errors

  • ErrIndexMustBeIncreasing: The provided index must be greater than the last index.
  • ErrIndexGapsAreNotAllowed: If AllowGaps is false, indexes must be consecutive.
  • ErrNotFound: The requested index was not found.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIndexGapsAreNotAllowed = errors.New("index gaps are not allowed")
View Source
var ErrIndexMustBeIncreasing = errors.New("index must be increasing")
View Source
var ErrNotEnoughSpace = errors.New("not enough space")
View Source
var ErrNotFound = errors.New("not found")

Functions

This section is empty.

Types

type Options

type Options struct {
	AllowGaps bool
	MaxSize   uint64
}

func (Options) GetMaxSize added in v0.0.6

func (o Options) GetMaxSize() uint64

type StateMate

type StateMate[T ~uint64] struct {
	// contains filtered or unexported fields
}

func Open

func Open[T ~uint64](dataFileName string, options Options) (*StateMate[T], error)

func (*StateMate[T]) Append

func (sm *StateMate[T]) Append(index T, data []byte) error

func (*StateMate[T]) Close

func (sm *StateMate[T]) Close() error

func (*StateMate[T]) Count added in v0.0.4

func (sm *StateMate[T]) Count() uint64

func (*StateMate[T]) GetFirstIndex added in v0.0.4

func (sm *StateMate[T]) GetFirstIndex() T

func (*StateMate[T]) GetLastIndex added in v0.0.4

func (sm *StateMate[T]) GetLastIndex() T

func (*StateMate[T]) IsEmpty added in v0.0.2

func (sm *StateMate[T]) IsEmpty() bool

func (*StateMate[T]) Read

func (sm *StateMate[T]) Read(index T, fn func(data []byte) error) error

func (*StateMate[T]) StorageStats added in v0.0.8

func (sm *StateMate[T]) StorageStats() StorageStats

StorageStats returns the size of the data and index files. This method is useful for debugging and monitoring. The size of the data file is the number of bytes from the beginning of the first to the end of last data entry. The physical size of the data file may be larger than the data size due to pre-allocation. The size of the index file is the number of bytes from the beginning of the file to the end of the last index entry. The physical size of the index file may be larger than the index size due to pre-allocation.

func (*StateMate[T]) Truncate added in v0.0.5

func (sm *StateMate[T]) Truncate() error

Truncate removes all the padding at the end of the data and index files. This method should be called before the state should be archived.

type StorageStats added in v0.0.8

type StorageStats struct {
	DataSize      uint64
	IndexSize     uint64
	DataFileSize  uint64
	IndexFileSize uint64
}

Directories

Path Synopsis
cmd
statemate command

Jump to

Keyboard shortcuts

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