keystore
--
import "vimagination.zapto.org/keystore"
Package keystore is a simple key-value storage system with file and memory
backing
Usage
var (
ErrUnknownKey = errors.New("key not found")
ErrKeyExists = errors.New("key already exists")
ErrInvalidKey = errors.New("key contains invalid characters")
)
Errors
type FileBackedMemStore
type FileBackedMemStore struct {
FileStore
}
FileBackedMemStore combines both a FileStore and a MemStore
func NewFileBackedMemStore
func NewFileBackedMemStore(baseDir, tmpDir string, mangler Mangler) (*FileBackedMemStore, error)
NewFileBackedMemStore create a new Store which uses the filesystem for permanent
storage, but uses memory for caching
func NewFileBackedMemStoreFromFileStore
func NewFileBackedMemStoreFromFileStore(filestore *FileStore) *FileBackedMemStore
NewFileBackedMemStoreFromFileStore uses an existing FileStore to create a new
File Backed Memory Store
func (*FileBackedMemStore) Clear
func (fs *FileBackedMemStore) Clear(keys ...string)
Clear removes keys from the memory cache. Specifying no keys removes all data
func (*FileBackedMemStore) Get
func (fs *FileBackedMemStore) Get(key string, r io.ReaderFrom) error
Get retrieves a key from the Store, first looking in the memcache and then going
to the filesystem
func (*FileBackedMemStore) Remove
func (fs *FileBackedMemStore) Remove(key string) error
Remove deletes a key from both the memcache and the filesystem
func (*FileBackedMemStore) Rename
func (fs *FileBackedMemStore) Rename(oldkey, newkey string) error
Rename moves data from an existing key to a new, unused key
func (*FileBackedMemStore) Set
func (fs *FileBackedMemStore) Set(key string, w io.WriterTo) error
Set stores the key in both the memcache and the filesystem
type FileStore
type FileStore struct {
}
FileStore implements the Store interface and provides a file backed keystore
func NewFileStore
func NewFileStore(baseDir, tmpDir string, mangler Mangler) (*FileStore, error)
NewFileStore creates a file backed key-value store
func (*FileStore) Exists
func (fs *FileStore) Exists(key string) bool
Exists returns true when the key exists within the store
func (*FileStore) Get
func (fs *FileStore) Get(key string, r io.ReaderFrom) error
Get retrieves the key data from the filesystem
func (*FileStore) Keys
func (fs *FileStore) Keys() []string
Keys returns a sorted slice of all of the keys
func (*FileStore) Remove
func (fs *FileStore) Remove(key string) error
Remove deletes the key data from the filesystem
func (*FileStore) Rename
func (fs *FileStore) Rename(oldkey, newkey string) error
Rename moves data from an existing key to a new, unused key
func (*FileStore) Set
func (fs *FileStore) Set(key string, w io.WriterTo) error
Set stores the key data on the filesystem
func (*FileStore) Stat
func (fs *FileStore) Stat(key string) (os.FileInfo, error)
Stat returns the FileInfo of the file relatining to the given key
type Float32
type Float32 float32
Float32 is a float32 that implements io.ReaderFrom and io.WriterTo
func (*Float32) ReadFrom
func (t *Float32) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the float32 from the Reader
func (Float32) WriteTo
func (t Float32) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the float32 to the Writer
type Float64
type Float64 float64
Float64 is a float64 that implements io.ReaderFrom and io.WriterTo
func (*Float64) ReadFrom
func (t *Float64) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the float64 from the Reader
func (Float64) WriteTo
func (t Float64) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the float64 to the Writer
type Int
type Int int
Int is a int that implements io.ReaderFrom and io.WriterTo
func (*Int) ReadFrom
func (t *Int) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the int from the Reader
func (Int) WriteTo
func (t Int) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the int to the Writer
type Int16
type Int16 int16
Int16 is a int16 that implements io.ReaderFrom and io.WriterTo
func (*Int16) ReadFrom
func (t *Int16) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the int16 from the Reader
func (Int16) WriteTo
func (t Int16) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the int16 to the Writer
type Int32
type Int32 int32
Int32 is a int32 that implements io.ReaderFrom and io.WriterTo
func (*Int32) ReadFrom
func (t *Int32) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the int32 from the Reader
func (Int32) WriteTo
func (t Int32) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the int32 to the Writer
type Int64
type Int64 int64
Int64 is a int64 that implements io.ReaderFrom and io.WriterTo
func (*Int64) ReadFrom
func (t *Int64) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the int64 from the Reader
func (Int64) WriteTo
func (t Int64) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the int64 to the Writer
type Int8
type Int8 int8
Int8 is a int8 that implements io.ReaderFrom and io.WriterTo
func (*Int8) ReadFrom
func (t *Int8) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the int8 from the Reader
func (Int8) WriteTo
func (t Int8) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the int8 to the Writer
type Mangler
type Mangler interface {
Encode(string) []string
Decode([]string) (string, error)
}
Mangler is an interface for the methods required to un/mangle a key
var Base64Mangler Mangler = base64Mangler{}
Base64Mangler represents the default Mangler that simple base64 encodes the key
var NoMangle Mangler = noMangle{}
NoMangle is a mangler that performs no mangling. This should only be used when
you are certain that there are no filesystem special characters in the key name
type MemStore
type MemStore struct {
}
MemStore implements Store and does so entirely in memory
func NewMemStore
func NewMemStore() *MemStore
NewMemStore creates a new memory-backed key-value store
func (*MemStore) Exists
func (ms *MemStore) Exists(key string) bool
Exists returns true when the key exists within the store
func (*MemStore) Get
func (ms *MemStore) Get(key string, r io.ReaderFrom) error
Get retrieves the key data from memory
func (*MemStore) GetAll
func (ms *MemStore) GetAll(data map[string]io.ReaderFrom) error
GetAll retrieves data for all of the keys given. Useful to reduce locking.
Unknown Key errors are not returned, only errors from the ReaderFrom's
func (*MemStore) Keys
func (ms *MemStore) Keys() []string
Keys returns a sorted slice of all of the keys
func (*MemStore) ReadFrom
func (ms *MemStore) ReadFrom(r io.Reader) (int64, error)
ReadFrom implements the io.ReaderFrom interface allowing a MemStore to be be
retrieved in another Store
func (*MemStore) Remove
func (ms *MemStore) Remove(key string) error
Remove deletes the key data from memory
func (*MemStore) RemoveAll
func (ms *MemStore) RemoveAll(keys ...string)
RemoveAll will attempt to remove all keys given. It does not return an error if
a key doesn't exist
func (*MemStore) Rename
func (ms *MemStore) Rename(oldkey, newkey string) error
Rename moves data from an existing key to a new, unused key
func (*MemStore) Set
func (ms *MemStore) Set(key string, w io.WriterTo) error
Set stores the key data in memory
func (*MemStore) SetAll
func (ms *MemStore) SetAll(data map[string]io.WriterTo) error
SetAll set data for all of the keys given. Useful to reduce locking. Will return
the first error found, so may not set all data.
func (*MemStore) WriteTo
func (ms *MemStore) WriteTo(w io.Writer) (int64, error)
WriteTo implements the io.WriterTo interface allowing a MemStore to be be stored
in another Store
type Store
type Store interface {
Get(string, io.ReaderFrom) error
Set(string, io.WriterTo) error
Remove(string) error
Keys() []string
Rename(string, string) error
}
Store represents the methods required for a Keystore
type String
type String string
String is a string that implements io.ReaderFrom and io.WriterTo
func (*String) ReadFrom
func (t *String) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the string from the Reader
func (String) WriteTo
func (t String) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the string to the Writer
type Uint
type Uint uint
Uint is a uint that implements io.ReaderFrom and io.WriterTo
func (*Uint) ReadFrom
func (t *Uint) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the uint from the Reader
func (Uint) WriteTo
func (t Uint) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the uint to the Writer
type Uint16
type Uint16 uint16
Uint16 is a uint16 that implements io.ReaderFrom and io.WriterTo
func (*Uint16) ReadFrom
func (t *Uint16) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the uint16 from the Reader
func (Uint16) WriteTo
func (t Uint16) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the uint16 to the Writer
type Uint32
type Uint32 uint32
Uint32 is a uint32 that implements io.ReaderFrom and io.WriterTo
func (*Uint32) ReadFrom
func (t *Uint32) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the uint32 from the Reader
func (Uint32) WriteTo
func (t Uint32) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the uint32 to the Writer
type Uint64
type Uint64 uint64
Uint64 is a uint64 that implements io.ReaderFrom and io.WriterTo
func (*Uint64) ReadFrom
func (t *Uint64) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the uint64 from the Reader
func (Uint64) WriteTo
func (t Uint64) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the uint64 to the Writer
type Uint8
type Uint8 uint8
Uint8 is a uint8 that implements io.ReaderFrom and io.WriterTo
func (*Uint8) ReadFrom
func (t *Uint8) ReadFrom(r io.Reader) (int64, error)
ReadFrom decodes the uint8 from the Reader
func (Uint8) WriteTo
func (t Uint8) WriteTo(w io.Writer) (int64, error)
WriteTo encodes the uint8 to the Writer