2020-06-09 13:04:44 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
firedBucket = []byte("fired")
|
2021-02-22 18:27:49 +00:00
|
|
|
unbanBucket = []byte("unban")
|
2022-02-24 04:59:41 +00:00
|
|
|
trackBucket = []byte("track")
|
2020-06-09 13:04:44 +00:00
|
|
|
|
|
|
|
buckets = [][]byte{
|
|
|
|
firedBucket,
|
2021-02-22 18:27:49 +00:00
|
|
|
unbanBucket,
|
2022-02-24 04:59:41 +00:00
|
|
|
trackBucket,
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
db *bbolt.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load(dbPath string) (*Database, error) {
|
2022-01-19 19:51:12 +00:00
|
|
|
db, err := bbolt.Open(dbPath, 0o600, bbolt.DefaultOptions)
|
2020-06-09 13:04:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, b := range buckets {
|
|
|
|
if err := db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(b)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Database{
|
|
|
|
db: db,
|
|
|
|
}, nil
|
|
|
|
}
|