Documentation
¶
Index ¶
- Variables
- func DefaultOpts[V any](s *Skhron[V])
- type Skhron
- func (s *Skhron[V]) CleanUp()
- func (s *Skhron[V]) CreateSnapshot() error
- func (s *Skhron[V]) Delete(key string) error
- func (s *Skhron[V]) Exists(key string) bool
- func (s *Skhron[V]) Get(key string) (V, error)
- func (s *Skhron[V]) GetRegex(mask *regexp.Regexp) []V
- func (s *Skhron[V]) LoadSnapshot() error
- func (s *Skhron[V]) MarshalJSON() ([]byte, error)
- func (s *Skhron[V]) PeriodicCleanup(ctx context.Context, period time.Duration, done chan struct{})
- func (s *Skhron[V]) Put(key string, value V) error
- func (s *Skhron[V]) PutTTL(key string, value V, ttl time.Duration) error
- type StorageOpt
Constants ¶
This section is empty.
Variables ¶
var (
SkhronExtension = ".skh"
)
Functions ¶
func DefaultOpts ¶
Types ¶
type Skhron ¶
type Skhron[V any] struct { // Skhron.Data is a main object. All the data is stored here. // It is shrinking map, which shrinks every "limit" (skhron.WithMapLimit option) deletions. Data *smap.Map[string, V] `json:"data,omitempty"` // Skhron.TTLq is a queue object used to delete expired items in time. // Each put operation the object is either added to the queue or updated in the queue. // The cleaning up process takes an item from the front of the queue and checks, // Whether the item has expired or not. Thus, we avoid scanning the whole map to find expired items. TTLq *expireQueue `json:"ttlq,omitempty"` // A directory where snapshots would be stored SnapshotDir string // A name (WITHOUT EXTENSION) which would be used to store the latest snapshot SnapshotName string // A directory where temporary files would be stored TempSnapshotDir string // contains filtered or unexported fields }
func New ¶
func New[V any](opts ...StorageOpt[V]) *Skhron[V]
Initialize Skhron instance with options.
func (*Skhron[V]) CleanUp ¶
func (s *Skhron[V]) CleanUp()
CleanUp is a function which removes expired items. It is called periodically by the `PeriodicCleanup` function. This function locks mutex for its operations.
func (*Skhron[V]) CreateSnapshot ¶
CreateSnapshot is a function which create snapshot (json dump of struct) in the temporary directory. Then it checks if an older snapshot exists in snapshot directory. If it is, it renames it to format "{snapshot name}_{time stamp}.skh" and then moves new snapshot to the snapshot directory
func (*Skhron[V]) Delete ¶
Delete is a function which deletes a key from the storage. It takes the key as string parameter. It does not delete the key from the queue, since when the key is expired it would be deleted from the queue without side effects. This function locks mutex for its operations.
func (*Skhron[V]) Exists ¶
Exists is a function which check wheater a key is present in the storage. It takes the key as string parameter. This function locks mutex for its operations.
func (*Skhron[V]) Get ¶
Get is a function which fetches a value in the storage under a key. It takes the key as string parameter. If the key is not present, error is returned. This function locks mutex for its operations.
func (*Skhron[V]) GetRegex ¶
GetRegex is a function which fetches values in the storage under keys, which match the mask regex. It takes the regex as parameter. This function locks mutex for its operations.
func (*Skhron[V]) LoadSnapshot ¶
LoadSnapshot is a function, which loads data from the latest snapshot file and writes data to the Skhron object. It looks for file {snapshot dir}/{snapshot file}.skh If load is failed, error is returned.
func (*Skhron[V]) MarshalJSON ¶
JsonMarshal is a function, which converts the struct into JSON-string bytes
func (*Skhron[V]) PeriodicCleanup ¶
`PeriodicCleanup` is a function that periodically calls the `CleanUp` function. It works until `ctx.Done()` signal is sent. It puts into `done` channel when it finishes. It backups current state of the storage into file `./skhron/skhron_{timestamp}.json` on exit. It runs clean up process every `period` time duration.
func (*Skhron[V]) Put ¶
Put is a function which puts a value in the storage under a key. It takes the key as string and the value as V. This function locks mutex for its operations.
func (*Skhron[V]) PutTTL ¶
PutTTL is a function which puts a value in the storage under a key with certain TTL. It takes the key as string, the value as V and ttl as time.Duration. It scans the entire queue to find if the item is already in the queue. If it is, it updates the item and updates the queue (to maintain priority). If it is not, it puts the item into the queue. This function locks mutex for its operations.
type StorageOpt ¶
func WithMapLimit ¶
func WithMapLimit[V any](limit uint64) StorageOpt[V]
func WithSnapshotDir ¶
func WithSnapshotDir[V any](dir string) StorageOpt[V]
func WithSnapshotName ¶
func WithSnapshotName[V any](name string) StorageOpt[V]
func WithTempSnapshotDir ¶
func WithTempSnapshotDir[V any](dir string) StorageOpt[V]