db

package
v0.22.3 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDBPath       = ".e-dnevnik.db"  // default BadgerDB folder
	DefaultTTL          = time.Hour * 9000 // a bit more than 1 year TTL
	DefaultDiscardRatio = 0.5              // recommended discard ratio from Badger docs
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Edb

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

Edb holds e-dnevnik structure including Bardger struct.

func New

func New(filePath string) (*Edb, error)

New opens a new database, flagging if the database already preexisting.

func (*Edb) CheckAndFlagTTL added in v0.20.0

func (db *Edb) CheckAndFlagTTL(bucket, subBucket string, target []string) (bool, error)

CheckAndFlagTTL checks if a key already exists in the database and marks it with a flag if it doesn't exist. The flag is set with a TTL of 1+ year.

The key is created by hashing a concatenation of the bucket, subBucket and target strings using SHA-256.

If the key already exists, the function returns (true, nil). If the key doesn't exist, the function marks the key and returns (false, nil) on success or (false, error) on error.

func (*Edb) Close

func (db *Edb) Close() error

Close closes database, optionally running GC (removing state data from value log file).

func (*Edb) Existing

func (db *Edb) Existing() bool

Existing returns if the database was freshly initialized.

func (*Edb) FetchAndStore added in v0.20.0

func (db *Edb) FetchAndStore(key []byte, f func(old []byte) ([]byte, error)) error

FetchAndStore fetches a value by key, applies a given function to the value and stores the result.

It does the following steps:

1. Finds the key in the database. 2. Copies the associated value. 3. Calls the given function with the copied value as argument and stores the result. 4. Stores the result in the database with the same key and a TTL of 1+ year.

If any of the steps fail, it will return an error.

Jump to

Keyboard shortcuts

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