consistency

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package consistency implements transactional log management with `Event` and `EventType` abstractions, and supports file-based persistence using `JsonFileLogger` for reliable data storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event[K, V any] struct {
	Sequence  uint64    `json:"sequence"`   // The sequence number of the event, ensuring order.
	EventType EventType `json:"event_type"` // The type of event (e.g., Put or Delete).
	Key       K         `json:"key"`        // The key associated with the event.
	Value     V         `json:"value"`      // The value associated with the event (only for Put).
}

Event represents an entry in the transactional log.

type EventType

type EventType byte

EventType represents the type of an event in the transactional log.

const (
	// EventTypeDelete indicates a delete operation.
	EventTypeDelete EventType = iota
	// EventTypePut indicates a put (write) operation.
	EventTypePut
)

type JsonFileLogger

type JsonFileLogger[K, V any] struct {
	// contains filtered or unexported fields
}

JsonFileLogger is a file-based implementation of the Logger interface. It writes events to a JSON-formatted file for persistence.

func NewJsonFileLogger

func NewJsonFileLogger[K, V any](file string) *JsonFileLogger[K, V]

NewJsonFileLogger initializes a new JsonFileLogger for the given file path.

func (*JsonFileLogger[K, V]) Close

func (a *JsonFileLogger[K, V]) Close() error

Close shuts down the logger, ensuring all pending events are written.

func (*JsonFileLogger[K, V]) Error

func (a *JsonFileLogger[K, V]) Error() <-chan error

Error returns a read-only channel for retrieving errors.

func (*JsonFileLogger[K, V]) ReadEvents

func (a *JsonFileLogger[K, V]) ReadEvents() (<-chan Event[K, V], <-chan error)

ReadEvents reads events from the log file and returns two channels. The method uses a goroutine to read events asynchronously, allowing the caller to process events and handle errors as they are received.

func (*JsonFileLogger[K, V]) WriteDelete

func (a *JsonFileLogger[K, V]) WriteDelete(key K)

WriteDelete writes a delete event to the log.

func (*JsonFileLogger[K, V]) WritePut

func (a *JsonFileLogger[K, V]) WritePut(key K, value V)

WritePut writes a put event to the log.

type Logger

type Logger[K, V any] interface {
	// Close closes the logger and ensures all pending events are processed.
	Close() error
	// WriteDelete writes a delete event to the log.
	WriteDelete(key K)
	// WritePut writes a put event to the log.
	WritePut(key K, value V)
	// ReadEvents reads events from the log in a streaming manner.
	ReadEvents() (<-chan Event[K, V], <-chan error)
}

Logger is an interface that defines the operations for a transactional log.

Jump to

Keyboard shortcuts

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