raftsqlite

package module
v0.0.0-...-016fc82 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

raft-sqlite

A SQLite-backed implementation of the LogStore and StableStore interfaces from hashicorp/raft, using the pure-Go modernc.org/sqlite driver.

Usage

store, err := raftsqlite.NewSQLiteStore("/path/to/raft.db")
if err != nil {
    log.Fatal(err)
}
defer store.Close()

// Use as both LogStore and StableStore
raft.NewRaft(config, fsm, store, store, snapshots, transport)

For more control, use raftsqlite.New with Options:

store, err := raftsqlite.New(raftsqlite.Options{
	Path: "/path/to/raft.db",
	Logf: log.Printf,
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound indicates a given key does not exist.
	ErrKeyNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type Options

type Options struct {
	// Path is the file path to the SQLite database.
	//
	// Either Path or TxFactory must be set, but not both.
	Path string

	// TxFactory is an optional function that can be used to provide custom
	// transaction handling. If set, the store will use this function to create
	// transactions instead of opening a database at Path.
	//
	// Either TxFactory or Path must be set, but not both.
	TxFactory func(context.Context, *sql.TxOptions) (*sql.Tx, error)

	// Logf is an optional log function for diagnostic messages.
	// If nil, log messages are discarded.
	Logf func(string, ...any)
}

Options contains all the configuration used to open the SQLite store.

type SQLiteStore

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

SQLiteStore provides access to a SQLite database for Raft to store and retrieve log entries. It also provides key/value storage, and can be used as a LogStore and StableStore.

func New

func New(options Options) (*SQLiteStore, error)

New uses the supplied options to open the SQLite database and prepare it for use as a raft backend.

func NewSQLiteStore

func NewSQLiteStore(path string) (*SQLiteStore, error)

NewSQLiteStore takes a file path and returns a connected Raft backend.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close cancels background goroutines and closes the database connection.

func (*SQLiteStore) DeleteRange

func (s *SQLiteStore) DeleteRange(min, max uint64) error

DeleteRange deletes a range of log entries. The range is inclusive.

func (*SQLiteStore) FirstIndex

func (s *SQLiteStore) FirstIndex() (uint64, error)

FirstIndex returns the first known index from the Raft log.

func (*SQLiteStore) Get

func (s *SQLiteStore) Get(key []byte) ([]byte, error)

Get is used to retrieve a value from the k/v store by key.

func (*SQLiteStore) GetLog

func (s *SQLiteStore) GetLog(index uint64, log *raft.Log) error

GetLog gets a log entry at a given index.

func (*SQLiteStore) GetUint64

func (s *SQLiteStore) GetUint64(key []byte) (uint64, error)

GetUint64 is like Get, but handles uint64 values.

func (*SQLiteStore) LastIndex

func (s *SQLiteStore) LastIndex() (uint64, error)

LastIndex returns the last known index from the Raft log.

func (*SQLiteStore) Set

func (s *SQLiteStore) Set(key []byte, val []byte) error

Set is used to set a key/value pair outside of the raft log.

func (*SQLiteStore) SetUint64

func (s *SQLiteStore) SetUint64(key []byte, val uint64) error

SetUint64 is like Set, but handles uint64 values.

func (*SQLiteStore) StoreLog

func (s *SQLiteStore) StoreLog(log *raft.Log) error

StoreLog stores a single raft log entry.

func (*SQLiteStore) StoreLogs

func (s *SQLiteStore) StoreLogs(logs []*raft.Log) error

StoreLogs stores multiple raft log entries.

type SnapshotStore

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

SnapshotStore is an implementation of raft.SnapshotStore that uses SQLite as the backend.

func NewSnapshotStore

func NewSnapshotStore(options SnapshotStoreOptions) (*SnapshotStore, error)

NewSnapshotStore uses the supplied options to open the SQLite database and prepare it for use as a raft snapshot store.

func (*SnapshotStore) Close

func (s *SnapshotStore) Close() error

Close cancels background goroutines and closes the database connection.

func (*SnapshotStore) Create

func (s *SnapshotStore) Create(version raft.SnapshotVersion, index, term uint64, configuration raft.Configuration, configurationIndex uint64, trans raft.Transport) (raft.SnapshotSink, error)

Create is used to begin a snapshot at a given index and term, and with the given committed configuration. The version parameter controls which snapshot version to create.

func (*SnapshotStore) List

func (s *SnapshotStore) List() ([]*raft.SnapshotMeta, error)

List is used to list the available snapshots in the store. It returns them in descending order, with the highest index first.

func (*SnapshotStore) Open

Open takes a snapshot ID and provides a ReadCloser. Once close is called it is assumed the snapshot is no longer needed.

type SnapshotStoreOptions

type SnapshotStoreOptions struct {
	// Path is the file path to the SQLite database.
	//
	// Either Path or TxFactory must be set, but not both.
	Path string

	// TxFactory is an optional function that can be used to provide custom
	// transaction handling. If set, the store will use this function to create
	// transactions instead of opening a database at Path.
	//
	// Either TxFactory or Path must be set, but not both.
	TxFactory func(context.Context, *sql.TxOptions) (*sql.Tx, error)

	// Logf is an optional log function for diagnostic messages.
	// If nil, log messages are discarded.
	Logf func(string, ...any)

	// Retain is the number of snapshots to retain. Must be >= 1.
	Retain int
}

SnapshotStoreOptions contains all the configuration used to open the snapshot store.

Jump to

Keyboard shortcuts

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