qurl/storage/bolt/bolt.go

44 lines
662 B
Go
Raw Normal View History

2018-11-11 16:56:30 -09:00
package bolt
import (
bolt "go.etcd.io/bbolt"
"net/url"
"time"
2018-11-18 18:28:44 -09:00
"os"
2018-11-11 16:56:30 -09:00
)
type BoltStorage struct {
DB *bolt.DB
}
func (stor *BoltStorage) Shutdown() {
stor.DB.Close()
}
func New(u *url.URL) (*BoltStorage, error) {
path := u.Opaque
if u.Path != "" {
path = u.Path
}
db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 3 * time.Second})
if err != nil {
return nil, err
}
return &BoltStorage{DB: db}, nil
}
2018-11-18 18:28:44 -09:00
func (stor *BoltStorage) Backup(bpath string) error {
tx, err := stor.DB.Begin(false)
if err != nil {
return err
}
defer tx.Rollback()
err = tx.CopyFile(bpath, os.FileMode(0600))
if err != nil {
return err
}
return nil
}