Documentation ¶
Overview ¶
Package persist provides a basic type-safe map that persists to disk.
Example (Map) ¶
package main import ( "fmt" "log" "libdb.so/persist" "libdb.so/persist/driver/badgerdb" ) func main() { type User struct { ID int Name string } m, err := persist.NewMustMap[string, User](badgerdb.Open, ":memory:") if err != nil { log.Fatalln("cannot create badgerdb-backed map:", err) } defer m.Close() m.Store("foo", User{ID: 1, Name: "foo"}) m.Store("bar", User{ID: 2, Name: "bar"}) u, ok := m.Load("foo") fmt.Println(u, ok) iter := m.All() iter(func(k string, u User) bool { fmt.Printf("%s: %v\n", k, u) return true }) }
Output: {1 foo} true bar: {2 bar} foo: {1 foo}
Example (Value) ¶
package main import ( "fmt" "log" "libdb.so/persist" "libdb.so/persist/driver/badgerdb" ) func main() { type Config struct { EnableBananas bool } c, err := persist.NewMustValue[Config](badgerdb.Open, ":memory:") if err != nil { log.Fatalln("cannot create badgerdb-backed map:", err) } defer c.Close() c.Store(Config{EnableBananas: true}) u, _ := c.Load() fmt.Printf("%+v\n", u) }
Output: {EnableBananas:true}
Index ¶
- type Driver
- type DriverOpenFunc
- type DriverReadOnlyTx
- type DriverReadWriteTx
- type Encoder
- type EncoderPair
- type Map
- func (m Map[K, V]) All() Seq2[K, V]
- func (m Map[K, V]) Close() error
- func (m Map[K, V]) Delete(k K) error
- func (m Map[K, V]) Encoder() EncoderPair[K, V]
- func (m Map[K, V]) Keys() Seq[K]
- func (m Map[K, V]) Load(k K) (V, bool, error)
- func (m Map[K, V]) LoadAndDelete(k K) (v V, loaded bool, err error)
- func (m Map[K, V]) LoadOrStore(k K, v V) (value V, loaded bool, err error)
- func (m Map[K, V]) Store(k K, v V) error
- type MustMap
- type MustValue
- type Seq
- type Seq2
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Driver ¶
type Driver interface { io.Closer AcquireRO(func(DriverReadOnlyTx) error) error AcquireRW(func(DriverReadWriteTx) error) error }
Driver is a driver for a persistent map. It exposes the ability to acquire read-only and read-write transactions. Note that transactions are assumed to have the properties of a database transaction, i.e. they are atomic and isolated.
type DriverOpenFunc ¶
DriverOpenFunc is a function that opens a driver. It assumes that drivers already have a sane default configuration, so the user is not provided with any configuration options.
There is one exception: if the path exactly matches the string ":memory:", then the driver must be non-persistent. If the driver is unable to satisfy this requirement, it must return an error.
var CBORDriver DriverOpenFunc = openCBORDriver
CBORDriver is a driver that stores data in a CBOR file.
var DefaultDriver DriverOpenFunc = CBORDriver
DefaultDriver is the default driver. Nothing uses it, but it's here for convenience.
type DriverReadOnlyTx ¶
type DriverReadOnlyTx interface { Get(k []byte) ([]byte, bool, error) Each(func(k, v []byte) error) error EachKey(func(k []byte) error) error }
DriverReadOnlyTx is a read-only transaction.
type DriverReadWriteTx ¶
type DriverReadWriteTx interface { DriverReadOnlyTx Set(k, v []byte) error Delete(k []byte) error }
DriverReadWriteTx is a read-write transaction.
type Encoder ¶
type Encoder[T any] interface { // Encode encodes a value to a byte slice. // A byte slice is passed to allow reusing the same slice // for multiple encodings. Encode(T, []byte) ([]byte, error) // Decode decodes a value from a byte slice. The byte slice // must not be modified or stored. Decode([]byte) (T, error) }
Encoder is a type that can encode a value to a byte slice. Such value must also be decodable from the same byte slice.
func BytesEncoder ¶
BytesEncoder returns an Encoder that encodes values literally as byte slices. Use this for fast key formatting.
func CBOREncoder ¶
CBOREncoder returns an Encoder that encodes values using the CBOR format.
func StringEncoder ¶
StringEncoder returns an Encoder that encodes values literally as strings. Use this for fast key formatting.
type EncoderPair ¶
EncoderPair is a pair of encoders.
type Map ¶
type Map[K, V any] struct { // contains filtered or unexported fields }
Map is a type-safe map that persists to disk.
func NewMap ¶
func NewMap[K, V any](driverOpener DriverOpenFunc, path string) (Map[K, V], error)
NewMap returns a new Map using the default CBOR encoder and a provided driver with sane defaults.
func NewMapFromEncoders ¶
func NewMapFromEncoders[K, V any](driver Driver, encs EncoderPair[K, V]) *Map[K, V]
NewMapFromEncoders returns a new Map from a pair of encoders.
func (Map[K, V]) Close ¶
Close closes the map. The user must call this function to ensure that the map is properly closed.
func (Map[K, V]) Encoder ¶
func (m Map[K, V]) Encoder() EncoderPair[K, V]
Encoder returns the encoder pair used by the map.
func (Map[K, V]) LoadAndDelete ¶
LoadAndDelete gets a value by key, or deletes the key if it is not found.
func (Map[K, V]) LoadOrStore ¶
LoadOrStore gets a value by key, or stores a value if the key is not found.
type MustMap ¶
MustMap wraps a map and guarantees that no errors will be returned from the map's methods, with the exception of Get, which now returns a bool.
func NewMustMap ¶
func NewMustMap[K, V any](driverOpener DriverOpenFunc, path string) (MustMap[K, V], error)
NewMustMap returns a new MustMap. It has the same exact signature as NewMap, and the user must still handle errors as they would with NewMap.
func WrapMustMap ¶
WrapMustMap wraps a Map in a MustMap.
func (MustMap[K, V]) Delete ¶
func (m MustMap[K, V]) Delete(key K)
Delete deletes the key-value pair. If an error occurs, the function panics.
func (MustMap[K, V]) Load ¶
Load returns the value associated with the key, or false if the key is not found.
func (MustMap[K, V]) LoadAndDelete ¶
LoadAndDelete returns the value associated with the key, or false if the key is not found. If an error occurs, the function panics.
func (MustMap[K, V]) LoadOrStore ¶
LoadOrStore returns the existing value associated with the key if one exists, or stores and returns the given value. If an error occurs, the function panics.
type MustValue ¶
MustValue wraps a value and guarantees that no errors will be returned from the value's methods.
func NewMustValue ¶
func NewMustValue[V any](driverOpener DriverOpenFunc, path string) (MustValue[V], error)
NewMustValue returns a new MustValue. It has the same exact signature as NewValue, and the user must still handle errors as they would with NewValue.
func NewMustValueWithDefault ¶
func NewMustValueWithDefault[V any](driverOpener DriverOpenFunc, path string, def V) (MustValue[V], error)
NewMustValueWithDefault returns a new MustValue. It has the same exact signature as NewValueWithDefault, and the user must still handle errors as they would with NewValueWithDefault.
func WrapMustValue ¶
WrapMustValue wraps a Value in a MustValue.
func (MustValue[V]) LoadAndDelete ¶
func (MustValue[V]) LoadOrStore ¶
type Seq ¶
Seq is an iterator over a map that yields values. It is inspired by https://github.com/golang/go/issues/61897.
type Seq2 ¶
Seq2 is an iterator over a map that yields key-value pairs. It is inspired by https://github.com/golang/go/issues/61897.
type Value ¶
type Value[V any] interface { // Store sets the value. Store(value V) error // Load gets the value. Load() (V, bool, error) // LoadOrStore gets the value, or stores the value if it doesn't exist. LoadOrStore(value V) (actual V, loaded bool, err error) // LoadAndDelete gets the value and deletes it. LoadAndDelete() (V, bool, error) // Delete deletes the value. Delete() error // Close closes the value. Close() error }
Value is a type-safe value that persists to disk.
func NewMappedValue ¶
NewMappedValue returns a new Value using the provided map and key.
func NewValue ¶
func NewValue[V any](driverOpener DriverOpenFunc, path string) (Value[V], error)
NewValue returns a new Value using the default CBOR encoder and a provided driver with sane defaults.
func NewValueWithDefault ¶
func NewValueWithDefault[V any](driverOpener DriverOpenFunc, path string, def V) (Value[V], error)
NewValueWithDefault returns a new Value using the default CBOR encoder and the provided driver with sane defaults. If the value doesn't exist, it will be set to the provided default.