Files
realy/ratel/init.go
mleku bbebbe2b02 Add tracing with lol.Tracer in multiple functions.
Introduced `lol.Tracer` for function entry/exit logging across various packages. This improves traceability and debugging of function executions while preserving existing behavior. Removed unused files `doc.go` and `nothing.go` to clean up the repository.
2025-06-29 07:32:24 +01:00

101 lines
2.8 KiB
Go

package ratel
import (
"encoding/binary"
"errors"
"fmt"
"github.com/dgraph-io/badger/v4"
"realy.lol/chk"
"realy.lol/log"
"realy.lol/lol"
"realy.lol/ratel/prefixes"
"realy.lol/units"
)
// Init sets up the database with the loaded configuration.
func (r *T) Init(path string) (err error) {
lol.Tracer("Init", path)
defer func() { lol.Tracer("end Init", err) }()
r.dataDir = path
log.I.Ln("opening ratel event store at", r.Path())
opts := badger.DefaultOptions(r.dataDir)
opts.BlockCacheSize = int64(r.BlockCacheSize)
opts.BlockSize = units.Gb
opts.CompactL0OnClose = true
opts.LmaxCompaction = true
r.Logger = NewLogger(r.InitLogLevel, r.dataDir)
opts.Logger = r.Logger
if r.DB, err = badger.Open(opts); chk.E(err) {
return err
}
log.T.Ln("getting event store sequence index", r.dataDir)
if r.seq, err = r.DB.GetSequence([]byte("events"), 1000); chk.E(err) {
return err
}
log.T.Ln("running migrations", r.dataDir)
if err = r.runMigrations(); chk.E(err) {
return log.E.Err("error running migrations: %w; %s", err, r.dataDir)
}
return nil
}
const Version = 1
func (r *T) runMigrations() (err error) {
lol.Tracer("runMigrations")
defer func() { lol.Tracer("end runMigrations", err) }()
return r.Update(func(txn *badger.Txn) (err error) {
var version uint16
var item *badger.Item
item, err = txn.Get(prefixes.Version.Key())
if errors.Is(err, badger.ErrKeyNotFound) {
version = 0
} else if chk.E(err) {
return err
} else {
chk.E(item.Value(func(val []byte) (err error) {
version = binary.BigEndian.Uint16(val)
return
}))
}
// do the migrations in increasing steps (there is no rollback)
if version < Version {
// if there is any data in the relay we will stop and notify the user, otherwise we
// just set version to 1 and proceed
prefix := prefixes.Id.Key()
it := txn.NewIterator(badger.IteratorOptions{
PrefetchValues: true,
PrefetchSize: 100,
Prefix: prefix,
})
defer it.Close()
hasAnyEntries := false
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
hasAnyEntries = true
break
}
if hasAnyEntries {
return fmt.Errorf("your database is at version %d, but in order to migrate up "+
"to version 1 you must manually export all the events and then import "+
"again:\n"+
"run an old version of this software, export the data, then delete the "+
"database files, run the new version, import the data back it", version)
}
chk.E(r.bumpVersion(txn, Version))
}
return nil
})
}
func (r *T) bumpVersion(txn *badger.Txn, version uint16) (err error) {
lol.Tracer("bumpVersion", version)
defer func() { lol.Tracer("end bumpVersion", err) }()
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, version)
err = txn.Set(prefixes.Version.Key(), buf)
return
}