sdstore

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

SDStore

Copyright (c) 2022, Toqns Inc.

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package sdstore provides a Simple Disk Store.

Index

Constants

This section is empty.

Variables

View Source
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.

func (CodecEncoder) Decode added in v0.2.0

func (c CodecEncoder) Decode(b []byte, dest any) error

Decode implements the Decoder interface for CodecEncoder.

func (CodecEncoder) Encode added in v0.2.0

func (c CodecEncoder) Encode(data any) ([]byte, error)

Encode implements the Encoder interface for CodecEncoder.

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

func (c *Collection) QueryPaginated(f func(any) bool, page int, rows int) (res []any, pages int, err error)

func (*Collection) Update

func (c *Collection) Update(id string, data any) error

Update stores an updated record to disk.

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 Decoder

type Decoder interface {
	Decode([]byte, any) error
}

Decoder is an interface that store decoders have to implement.

type DecoderFunc

type DecoderFunc func([]byte, any) error

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.

func (DecoderFunc) Decode

func (d DecoderFunc) Decode(b []byte, dest any) error

Decode implements the Decoder interface for DecoderFUnc.

type Encoder

type Encoder interface {
	Encode(any) ([]byte, error)
}

Encoder is an interface that store encoders have to implement.

type EncoderFunc

type EncoderFunc func(any) ([]byte, error)

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.

func (EncoderFunc) Encode

func (e EncoderFunc) Encode(data any) ([]byte, error)

Encode implements the Encoder interface for 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

type SDStore struct {
	Path    string
	Name    string
	Encoder Encoder
	Decoder Decoder
	Perms   os.FileMode
}

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.

Jump to

Keyboard shortcuts

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