2016-02-26 11:05:47 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-07-20 04:19:42 +08:00
|
|
|
|
2020-03-26 06:30:29 +08:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2016-02-26 11:05:47 +08:00
|
|
|
)
|
|
|
|
|
2019-12-27 01:53:50 +08:00
|
|
|
// ErrNoSharedVar is returned by Store.SharedVar when there is no such variable.
|
|
|
|
var ErrNoSharedVar = errors.New("no such shared variable")
|
2016-02-26 11:05:47 +08:00
|
|
|
|
|
|
|
func init() {
|
2019-11-15 05:35:09 +08:00
|
|
|
initDB["initialize shared variable table"] = func(tx *bolt.Tx) error {
|
2019-12-27 01:52:28 +08:00
|
|
|
_, err := tx.CreateBucketIfNotExists([]byte(bucketSharedVar))
|
2019-11-15 05:35:09 +08:00
|
|
|
return err
|
2016-02-26 11:05:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 05:41:36 +08:00
|
|
|
// SharedVar gets the value of a shared variable.
|
2019-12-27 02:05:13 +08:00
|
|
|
func (s *dbStore) SharedVar(n string) (string, error) {
|
2016-02-26 11:05:47 +08:00
|
|
|
var value string
|
2017-07-20 04:19:42 +08:00
|
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
2019-12-27 01:52:28 +08:00
|
|
|
b := tx.Bucket([]byte(bucketSharedVar))
|
2020-08-01 12:12:42 +08:00
|
|
|
v := b.Get([]byte(n))
|
|
|
|
if v == nil {
|
2019-12-27 01:53:50 +08:00
|
|
|
return ErrNoSharedVar
|
2017-07-20 04:19:42 +08:00
|
|
|
}
|
2020-08-01 12:12:42 +08:00
|
|
|
value = string(v)
|
|
|
|
return nil
|
2017-07-20 04:19:42 +08:00
|
|
|
})
|
2016-02-26 11:05:47 +08:00
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// SetSharedVar sets the value of a shared variable.
|
2019-12-27 02:05:13 +08:00
|
|
|
func (s *dbStore) SetSharedVar(n, v string) error {
|
2017-07-20 04:19:42 +08:00
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
2019-12-27 01:52:28 +08:00
|
|
|
b := tx.Bucket([]byte(bucketSharedVar))
|
2017-07-20 04:19:42 +08:00
|
|
|
return b.Put([]byte(n), []byte(v))
|
|
|
|
})
|
2016-02-26 11:05:47 +08:00
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// DelSharedVar deletes a shared variable.
|
2019-12-27 02:05:13 +08:00
|
|
|
func (s *dbStore) DelSharedVar(n string) error {
|
2017-07-20 04:19:42 +08:00
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
2019-12-27 01:52:28 +08:00
|
|
|
b := tx.Bucket([]byte(bucketSharedVar))
|
2017-07-20 04:19:42 +08:00
|
|
|
return b.Delete([]byte(n))
|
|
|
|
})
|
2016-02-26 11:05:47 +08:00
|
|
|
}
|