store

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultDbFile string = "dump.scdb"

DefaultDbFile is the default name of the database file that contains all the key-value pairs

Variables

Functions

This section is empty.

Types

type Op

type Op struct {
	Type     OpType
	Key      []byte
	Value    []byte
	Ttl      *uint64
	RespChan chan OpResult
}

Op is an Operation that is to be done on a Store

type OpResult

type OpResult struct {
	Err   error
	Value []byte
	Store *Store
}

OpResult is the result of an Op done on the Store It is usually pushed to the Op.RespChan so that the sender of the Op can receive the result and act upon it

type OpType

type OpType uint

OpType is a type of Op that can be done on Store

const (
	// SetOp for setting key-value pairs
	SetOp OpType = iota
	// GetOp for getting key-value pairs
	GetOp
	// DeleteOp for deleting key-value pairs
	DeleteOp
	// ClearOp for clearing the Store
	ClearOp
	// CompactOp for compacting the Store
	CompactOp
	// CloseOp for closing the Store
	CloseOp
)

type Store

type Store struct {
	BufferPool         *buffers.BufferPool
	Header             *entries.DbFileHeader
	CompactionInterval time.Duration
	C                  chan Op
}

Store is the actual store of the key-value pairs It contains the BufferPool which in turn interfaces with both the memory and the disk to keep, retrieve and delete key-value pairs

func NewStore

func NewStore(path string, maxKeys *uint64, redundantBlocks *uint16, poolCapacity *uint64, compactionInterval *uint32) (*Store, error)

NewStore creates a new Store at the given path, with the given `compactionInterval`

func (*Store) Clear

func (s *Store) Clear() error

Clear removes all data in the store

func (*Store) Close

func (s *Store) Close() error

Close frees up any resources occupied by store. After this, the store is unusable. You have to re-instantiate it or just run into some crazy errors

func (*Store) Compact

func (s *Store) Compact() error

Compact manually removes dangling key-value pairs in the database file

Dangling keys result from either getting expired or being deleted. When a Store.Delete operation is done, the actual key-value pair is just marked as `deleted` but is not removed.

Something similar happens when a key-value is updated. A new key-value pair is created and the old one is left un-indexed. Compaction is important because it reclaims this space and reduces the size of the database file.

This is done automatically for you at the set `compactionInterval` but you may wish to do it manually for some reason.

This is a very expensive operation so use it sparingly.

func (*Store) Delete

func (s *Store) Delete(k []byte) error

Delete removes the key-value for the given key

func (*Store) Get

func (s *Store) Get(k []byte) ([]byte, error)

Get returns the value corresponding to the given key

func (*Store) Open

func (s *Store) Open()

Open opens the Store and starts receiving an Op's as sent on the Store.C channel. It also starts the background compaction task that runs every Store.CompactionInterval Duration

func (*Store) Set

func (s *Store) Set(k []byte, v []byte, ttl *uint64) error

Set sets the given key value in the store This is used to insert or update any key-value pair in the store

Jump to

Keyboard shortcuts

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