Documentation
¶
Index ¶
- Constants
- Variables
- type Edb
- func (db *Edb) CheckAndFlagTTL(ctx context.Context, bucket, subBucket string, target []string) (bool, error)
- func (db *Edb) Close() error
- func (db *Edb) Existing() bool
- func (db *Edb) FetchAndStore(ctx context.Context, key []byte, f func(old []byte) ([]byte, error)) error
- func (db *Edb) ImportFromBadger(ctx context.Context, badgerPath string) error
Constants ¶
const ( DefaultDBPath = ".e-dnevnik.db" DefaultEntryTTL = time.Hour * 9000 // a bit more than 1 year TTL DefaultDBOptions = "?_pragma=journal_mode(WAL)&_pragma=synchronous(NORMAL)&_pragma=busy_timeout(8000)" )
Variables ¶
var ( ErrSqliteOpen = errors.New("could not open Sqlite database") ErrSqliteCreateTable = errors.New("could not create table") ErrSqlitePrepareCheck = errors.New("failed to prepare check key statement") ErrSqlitePrepareInsert = errors.New("failed to prepare insert key statement") ErrDeleteBadgerDB = errors.New("could not remove old BadgerDB directory, please delete manually") )
var ( ErrBadgerDBNotFound = errors.New("BadgerDB not found") ErrBadgerDBOpen = errors.New("failed to open BadgerDB") ErrSqliteTx = errors.New("failed to begin sqlite transaction") ErrSqlitePrepare = errors.New("failed to prepare statement") ErrSqliteImport = errors.New("import failed") ErrSqliteCommit = errors.New("failed to commit transaction") )
Functions ¶
This section is empty.
Types ¶
type Edb ¶
type Edb struct {
// contains filtered or unexported fields
}
Edb holds e-dnevnik structure including sql.DB struct.
func (*Edb) CheckAndFlagTTL ¶
func (db *Edb) CheckAndFlagTTL(ctx context.Context, 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) FetchAndStore ¶
func (db *Edb) FetchAndStore(ctx context.Context, 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.