package bolt import ( "errors" "github.com/gisle/stufflog/config" "github.com/gisle/stufflog/database/repositories" "go.etcd.io/bbolt" "time" "unsafe" ) // A Database is a database.Database implementation using a bolt backend. type Database struct { users repositories.UserRepository userSessions repositories.UserSessionRepository activities repositories.ActivityRepository periods repositories.PeriodRepository } func (database *Database) Users() repositories.UserRepository { return database.users } func (database *Database) UserSessions() repositories.UserSessionRepository { return database.userSessions } func (database *Database) Activities() repositories.ActivityRepository { return database.activities } func (database *Database) Periods() repositories.PeriodRepository { return database.periods } func Init(cfg config.Database) (*Database, error) { opts := *bbolt.DefaultOptions opts.Timeout = time.Second * 5 db, err := bbolt.Open(cfg.Path, 0700, &opts) if err != nil { return nil, err } users, err := newUserRepository(db) if err != nil { return nil, err } userSessions, err := newUserSessionRepository(db) if err != nil { return nil, err } periods, err := newPeriodRepository(db) if err != nil { return nil, err } activities, err := newActivityRepository(db) if err != nil { return nil, err } database := &Database{ users: users, userSessions: userSessions, periods: periods, activities: activities, } return database, nil } // unsafeStringToBytes makes a byte array, mutation is punishable by segfault. func unsafeStringToBytes(s string) []byte { return *(*[]byte)(unsafe.Pointer(&s)) } var errUnchanged = errors.New("database/bolt: unchanged")