storage

package
v0.0.0-...-c05b5d5 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadgerDB

type BadgerDB struct {
	// contains filtered or unexported fields
}

BadgerDB implements KeyValue and represents the application's connection to BadgerDB.

func NewBadgerDB

func NewBadgerDB(sd string, ttl time.Duration) (*BadgerDB, error)

NewBadgerDB initializes the BadgerDB embedded database given the provided storage directory path sd and TTL for keys. It is up to the caller to close the database with Close().

func (*BadgerDB) Cleanup

func (db *BadgerDB) Cleanup() error

Cleanup performs BadgerDB's garbage collection routine with the recommended discardRatio.

See: https://pkg.go.dev/github.com/ipsn/go-ipfs/gxlibs/github.com/dgraph-io/badger#DB.RunValueLogGC

This is the only time old records are actually removed, so make sure you're setting TTLs for records!

func (*BadgerDB) Close

func (db *BadgerDB) Close()

Close tears down the database connection. You should defer this.

func (*BadgerDB) Put

func (db *BadgerDB) Put(entry KVEntry) error

Put upserts an entry

func (*BadgerDB) Read

func (db *BadgerDB) Read(key []byte) (KVEntry, error)

Read returns an entry by key.

type KVEntry

type KVEntry struct {
	Key   []byte
	Value []byte
}

KVEntry is what we'll write to and read from the KV store

type KeyValue

type KeyValue interface {
	// Replace the value of a Set or create a new one if it doesn't exist
	Put(KVEntry) error
	// Return a Set given its key
	Read(key []byte) (KVEntry, error)
	// Cleanup performs routine deletion of old records. We assign
	// TTLs to KV pairs and delete them periodically.
	Cleanup() error
	// Drain/tear down the connection, or something analogous for an
	// embedded database. Implementations should handle retries or drain
	// connections internally and panic on failure, since there is nothing
	// the program can do if it can't close the database.
	Close()
}

KeyValue exposes a common interface for performing CRUD operations on an underlying storage layer. Assumes some kind of persistent KV store for linksrc.Sets.

Implentations need to include connection logic in code to initialize a Store.

type NoOpDB

type NoOpDB struct{}

NoOpDB is used when we need to avoid touching the storage layer while still preserving our interactions with an abstract database. The strategy is to return whatever value will prevent the calling context from further interacting with the storage layer.

For get and put operations, we always return an error, so the caller knows that no actual data has been read or written.

For database-wide operations, such as cleaning up or closing the database, we always return a nil error. This is because, since there is nothing to close or clean up, the operation is always successful.

func (*NoOpDB) Cleanup

func (n *NoOpDB) Cleanup() error

Cleanup always returns nil in order to prevent retries or panics, since we want to keep the program humming along without touching the storage layer.

func (*NoOpDB) Close

func (n *NoOpDB) Close()

Close is no-op

func (*NoOpDB) Put

func (n *NoOpDB) Put(KVEntry) error

Put always returns an error so callers don't assume a new key has been written.

func (*NoOpDB) Read

func (n *NoOpDB) Read(key []byte) (KVEntry, error)

Read always returns an error so callers don't assume a key has been read.

Jump to

Keyboard shortcuts

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