storage

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package storage is the flat-file JSON store. Per-resource locks coordinate goroutines while advisory file locks make read-modify-write safe across processes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadAs

func ReadAs[T any](s *Store, key []string) (T, error)

ReadAs decodes a resource into a T.

func UpdateAs

func UpdateAs[T any](s *Store, key []string, mutate func(*T)) (T, error)

UpdateAs is a typed update helper. It preserves struct field ordering on the rewrite, while Update preserves arbitrary parsed-object ordering. mutate has the same non-reentrancy requirement as Update.

Types

type NotFoundError

type NotFoundError struct {
	Message string `json:"message"`
}

NotFoundError is the model-visible storage miss.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type Option

type Option func(*Store)

Option configures a Store.

type Store

type Store struct {
	Dir string
	// contains filtered or unexported fields
}

Store is a JSON store rooted at Dir.

func New

func New(dir string, options ...Option) *Store

New constructs a store at the exact storage directory passed by the caller.

func NewFromDataDir

func NewFromDataDir(dataDir string, options ...Option) *Store

NewFromDataDir roots the store at dataDir/storage.

func (*Store) CreateExclusive

func (s *Store) CreateExclusive(key []string, content any) (bool, error)

CreateExclusive writes a resource only when no claimant has created it. The store-wide advisory lock makes the check/atomic-rename indivisible across cooperating processes; the returned boolean reports the winner.

func (*Store) List

func (s *Store) List(prefix []string) ([][]string, error)

List returns descendant resource keys in sorted order.

func (*Store) Read

func (s *Store) Read(key []string) (any, error)

Read decodes a resource into a generic JSON value (maps, slices, float64).

func (*Store) ReadInto

func (s *Store) ReadInto(key []string, dst any) error

ReadInto decodes a resource into dst using encoding/json.

func (*Store) Remove

func (s *Store) Remove(key []string) error

Remove deletes a stored resource. Missing resources are ignored.

func (*Store) Update

func (s *Store) Update(key []string, mutate func(any)) (any, error)

Update holds the resource mutex and cross-process flock across read, mutation, and rewrite. mutate must not call a Store method for the same key: the callback is deliberately non-reentrant and doing so will deadlock.

func (*Store) Write

func (s *Store) Write(key []string, content any) error

Write persists content as two-space-indented JSON without a trailing newline.

func (*Store) WriteBatch

func (s *Store) WriteBatch(items []WriteItem) error

WriteBatch serializes a related group under ordered per-resource locks. Callers control item order; prompt persistence writes parts before the message that makes the turn visible. Files are synced individually, then each touched directory is synced once after all renames.

type WriteItem

type WriteItem struct {
	Key     []string
	Content any
}

WriteItem is one resource in a WriteBatch operation.

Jump to

Keyboard shortcuts

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