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") 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.
The check-then-insert pair is wrapped in a SQLite BEGIN IMMEDIATE transaction so that two concurrent callers cannot both observe "not found" and each insert the same key. A dedicated *sql.Conn is used because database/sql's BeginTx() issues BEGIN DEFERRED, which acquires the write lock lazily on first write and leaves the SELECT above racing with other writers.
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.
The fetch/modify/store sequence is wrapped in a SQLite BEGIN IMMEDIATE transaction so two concurrent callers (for the same queue key) cannot each read the same snapshot, apply their own transformation, and then race to overwrite the other's result — which would silently drop queue entries. A dedicated *sql.Conn is used because database/sql's BeginTx() issues BEGIN DEFERRED, which acquires the write lock lazily on first write and leaves the initial SELECT racing with other writers.
If any of the steps fail, it will return an error.