Documentation ¶
Overview ¶
Package sdstore provides a Simple Disk Store.
Index ¶
- Variables
- type CodecEncoder
- type Collection
- func (c *Collection) Create(id string, data any) error
- func (c *Collection) Delete(id string) error
- func (c *Collection) Get(id string, dest any) error
- func (c *Collection) GetIndexed(field string, v string, dest any) error
- func (c *Collection) Init() (*Collection, error)
- func (c *Collection) Query(f func(any) bool) (res []any, err error)
- func (c *Collection) QueryPaginated(f func(any) bool, page int, rows int) (res []any, pages int, err error)
- func (c *Collection) Update(id string, data any) error
- type CollectionOption
- type Decoder
- type DecoderFunc
- type Encoder
- type EncoderFunc
- type IndexedValueNotUniqueError
- type SDStore
- type StoreOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotInitialized is an error returned when a user attempts to do an action on a Collection that // hasn't been initialized. ErrNotInitialized = errors.New("collection is not initialized (run Init() to initialize)") // ErrAlreadyInitialized is an error returned whn a users attempts to initialize an already initialized // collection. ErrAlreadyInitialized = errors.New("collection is already initialized") // ErrNotIDNotUnique is an error returned when a user attempts to Create a record that already exists. ErrNotIDNotUnique = errors.New("id is not unique") // ErrNotFound is an error returned when a record is not found. ErrNotFound = errors.New("not found") // ErrInvalidRecordType is an error returned when record data isn't of the expected type. ErrInvalidRecordType = errors.New("record should be struct or pointer to struct") )
Functions ¶
This section is empty.
Types ¶
type CodecEncoder ¶ added in v0.2.0
type CodecEncoder struct {
// contains filtered or unexported fields
}
CodecEncoder provides functionality for encoding/decoding Gob encoded data.
func NewBincEncoder ¶ added in v0.2.0
func NewBincEncoder() CodecEncoder
NewBincEncoder returns a CodecEncoder using Binc encoding.
func NewCborEncoder ¶ added in v0.2.0
func NewCborEncoder() CodecEncoder
NewCborEncoder returns a CodecEncoder using CBOR encoding.
func NewMsgpackEncoder ¶ added in v0.2.0
func NewMsgpackEncoder() CodecEncoder
NewMsgpackEncoder returns a CodecEncoder using Msgpack encoding.
type Collection ¶
type Collection struct { Path string Name string Encoder Encoder Decoder Decoder Indexing struct { Fields []string Indexes map[string]string } FilePerm fs.FileMode DirPerm fs.FileMode // contains filtered or unexported fields }
Collection provides functionality to work with records, which are stored as plain files.
func (*Collection) Create ¶
func (c *Collection) Create(id string, data any) error
Create encodes and stores the provided record to disk.
func (*Collection) Delete ¶
func (c *Collection) Delete(id string) error
Delete removes a record from disk and indexes.
func (*Collection) Get ¶
func (c *Collection) Get(id string, dest any) error
Get receives a record from disk by the provided ID and will decode the result to dest.
dest should be a pointer to a struct.
func (*Collection) GetIndexed ¶
func (c *Collection) GetIndexed(field string, v string, dest any) error
GetIndexed receives a record from disk through the index of field/v and will decode the result to dest.
dest should be a pointer to a struct.
func (*Collection) Init ¶
func (c *Collection) Init() (*Collection, error)
Init will initialize a Collection. Initialization consists of ensuring the collections file path exists and loading and processing of the index file if it exists.
func (*Collection) Query ¶
func (c *Collection) Query(f func(any) bool) (res []any, err error)
Query returns a slice of data based on the result of the filter function. The filter function uses the type as set in Init.
func (*Collection) QueryPaginated ¶ added in v0.2.0
type CollectionOption ¶
type CollectionOption func(*Collection)
CollectionOption is an option for the setup of a Collection.
func WithCollectionPerms ¶
func WithCollectionPerms(perms fs.FileMode) CollectionOption
WithCollectionPerms is an option to set the Collection's permissions for the working directorry and record files.
func WithIndexedFields ¶
func WithIndexedFields(fields ...string) CollectionOption
WithIndexedFields is an option to set which struct fields are to be indexed.
type DecoderFunc ¶
DecoderFunc is a function providing decoding functionality.
func DecodeFunc ¶
func DecodeFunc(f func([]byte, any) error) DecoderFunc
DecodeFunc wraps the provided function into an DecoderFunc.
type EncoderFunc ¶
EncoderFunc is a function providing encoding functionality.
func EncodeFunc ¶
func EncodeFunc(f func(any) ([]byte, error)) EncoderFunc
EncodeFunc wraps the provided function into an EncoderFunc.
type IndexedValueNotUniqueError ¶
type IndexedValueNotUniqueError struct {
Field string
}
IndexedValueNotUniqueError is an error indicating that the value of an indexed field is not unique.
func (*IndexedValueNotUniqueError) Error ¶
func (err *IndexedValueNotUniqueError) Error() string
Error implements the Error interface for IndexedValueNotUniqueError
type SDStore ¶
SDStore is a key/value store
func New ¶
func New(name string, path string, opts ...StoreOption) (*SDStore, error)
New returns an initialized store with json encoding as default. name is the store's name, path is the directory path. Additionally one or more options can be provided.
func (*SDStore) Collection ¶
func (s *SDStore) Collection(name string, record any, opts ...CollectionOption) (*Collection, error)
Collection returns an initialized collection for this store.
type StoreOption ¶
type StoreOption func(*SDStore)
StoreOption is an option for the setup of a Store.
func WithBincEncoding ¶ added in v0.2.1
func WithBincEncoding() StoreOption
WithBincEncoding is an option to set Binc encoding of records.
func WithCborEncoding ¶ added in v0.2.0
func WithCborEncoding() StoreOption
WithCborEncoding is an option to set CBOR encoding of records.
func WithEncoding ¶
func WithEncoding(e Encoder, d Decoder) StoreOption
WithEncoding is an option to set store's encoder and decoder.
func WithJSONEncoding ¶
func WithJSONEncoding() StoreOption
WithJSONEncoding is an option to set JSON encoding of records.
func WithMsgpackEncoding ¶ added in v0.2.1
func WithMsgpackEncoding() StoreOption
WithMsgpackEncoding is an option to set Msgpack encoding of records.
func WithPerms ¶
func WithPerms(p os.FileMode) StoreOption
WithPerms is an option to set the store's permissions for the working directorry.