persist/persist.go

546 lines
10 KiB
Go
Raw Normal View History

2018-08-03 15:08:30 -04:00
package persist
import (
"bytes"
"encoding/binary"
"encoding/gob"
"errors"
"fmt"
"log"
"os"
"path"
"reflect"
"runtime"
"sync"
"time"
bolt "github.com/coreos/bbolt"
)
var Debug bool
2018-08-03 15:08:30 -04:00
// allow persistant storage and reloading of variables to bolt database.
// everything is time stamped.
// bucket is the variable name
type Var struct {
X interface{}
Time time.Time
2018-08-03 15:08:30 -04:00
name string
2018-08-30 09:23:21 -04:00
permanent bool
2018-08-03 15:08:30 -04:00
}
2018-08-09 09:58:40 -04:00
type encVar struct {
X interface{}
}
2018-08-30 09:23:21 -04:00
type Option func(*Config)
2018-08-03 15:08:30 -04:00
type Config struct {
configured bool
MaxVars int
DBPath string
Tidy TidyConfig
2018-08-03 15:08:30 -04:00
}
2018-08-30 09:23:21 -04:00
func MaxVars(i int) Option {
return func(c *Config) {
c.MaxVars = i
2018-08-03 15:08:30 -04:00
}
2018-08-30 09:23:21 -04:00
}
func DBPath(s string) Option {
return func(c *Config) {
c.DBPath = s
2018-08-03 15:08:30 -04:00
}
2018-08-30 09:23:21 -04:00
}
func TidyInterval(d time.Duration) Option {
return func(c *Config) {
c.Tidy.Interval = d
2018-08-03 15:08:30 -04:00
}
2018-08-30 09:23:21 -04:00
}
2018-08-30 09:23:21 -04:00
func Init(confs ...Option) {
mux.Lock()
tc := TidyConfig {
Interval: 10 * time.Second,
}
conf = Config { // default config
configured: true,
MaxVars: 64,
DBPath: "db",
Tidy: tc,
}
2018-08-30 09:23:21 -04:00
for _,f := range(confs) {
f(&conf)
}
2018-08-30 09:23:21 -04:00
if conf.Tidy.Func == nil {
conf.Tidy.Func = ExpireAfterFunc(24 * time.Hour)
}
2018-08-03 15:08:30 -04:00
mux.Unlock()
}
2018-08-30 09:23:21 -04:00
func New(name string, x interface{},ps ...bool) *Var {
2018-08-03 15:08:30 -04:00
start()
mux.Lock()
if vars == nil {
vars = make(map[string]*Var)
}
ret := vars[name]
if ret == nil {
ret = &Var{X: x, name: name}
vars[name] = ret
}
2018-08-30 09:23:21 -04:00
if len(ps) > 1 {
ret.permanent = ps[0]
2018-08-03 15:08:30 -04:00
}
mux.Unlock()
err := ret.Load()
if err != nil { // save default value if loading failed
fmt.Println("Load error:",err)
wchan <- encode(ret)
}
if Debug { fmt.Println("New(): ",ret.name) }
2018-08-03 15:08:30 -04:00
mux.Lock()
if loaded == nil {
loaded = make(chan *Var,conf.MaxVars) // buffered channel
}
mux.Unlock()
if Debug { fmt.Println("New(): loaded<- ret") }
2018-08-03 15:08:30 -04:00
loaded<- ret
if Debug { fmt.Println("New(): returning") }
2018-08-03 15:08:30 -04:00
return ret
}
func (p *Var) Set(x interface{}) error {
if reflect.TypeOf(x) == reflect.TypeOf(p.X) {
p.X = x
p.Save()
return nil
} else {
return errors.New("Set(): Type mismatch")
}
}
func (p *Var) SaveSync() {
donech := make(chan struct{})
wchan <- encode(p,donech)
<-donech
}
func (p *Var) Save(sync ...bool) {
dirty = true
wchan <- encode(p)
}
func (p *Var) Load(ts ...time.Time) error {
var err error
var v []byte
var t time.Time
2018-08-03 15:08:30 -04:00
if len(ts) == 0 {
v, t, err = retrieve(p.name,lastCheckpoint)
2018-08-03 15:08:30 -04:00
} else {
v, t, err = retrieve(p.name,ts[0])
2018-08-03 15:08:30 -04:00
}
if err == nil {
var p2 Var
dec := gob.NewDecoder(bytes.NewBuffer(v))
err := dec.Decode(&p2)
if reflect.TypeOf(p2.X) != reflect.TypeOf(p.X) {
err = errors.New("Load(): Type mismatch")
}
if err != nil {
fmt.Println("Load(): ",err)
} else {
p.X = p2.X
p.Time = t
2018-08-03 15:08:30 -04:00
}
} else {
if Debug { fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X)) }
2018-08-03 15:08:30 -04:00
}
return err
}
func (p *Var) Get(ts ...time.Time) interface{} {
if len(ts) == 0 {
return p.X
} else {
p2 := *p
p2.Load(ts[0])
return p2.X
}
}
func (p *Var) Delete(t time.Time) {
wchan<- dencode(p,t)
}
func (p *Var) DeleteSync(t time.Time) {
donech := make(chan struct{})
wchan<- dencode(p,t,donech)
<-donech
}
func Commit() {
mux.Lock()
if dirty {
dirty = false
__checkpoints.SaveSync()
}
mux.Unlock()
}
func Checkpoints() {
fmt.Println(__checkpoints.History())
}
func Undo() {
if Debug { fmt.Println("Undo()") }
2018-08-03 15:08:30 -04:00
mux.Lock()
__checkpoints.DeleteSync(lastCheckpoint)
h,_ := __checkpoints.History()
var t time.Time
if len(h) > 0 {
t = h[len(h)-1].Time
} else {
return
}
lastCheckpoint = t
for _,v := range vars {
v.Load(t)
}
dirty = false
mux.Unlock()
}
func Shutdown() {
close(tchan)
tg.Wait()
close(wchan)
wg.Wait()
mux.Lock()
launched = false
vars = make(map[string]*Var)
mux.Unlock()
}
var (
launched bool
wchan chan encoded
tchan chan struct{}
mux sync.Mutex
wmux sync.Mutex
wg sync.WaitGroup
tg sync.WaitGroup
db *bolt.DB
loaded chan *Var
conf Config
__checkpoints *Var
lastCheckpoint time.Time
vars map[string]*Var
dirty bool
)
type encoded struct {
name []byte
value interface{}
donech chan struct{}
}
func encode(p *Var,chs ...chan struct{}) encoded {
ret := encoded{name:[]byte(p.name)}
2018-08-09 09:58:40 -04:00
ep := encVar{ p.X }
2018-08-03 15:08:30 -04:00
if len(chs) > 0 {
ret.donech = chs[0]
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
2018-08-09 09:58:40 -04:00
err := enc.Encode(ep)
2018-08-03 15:08:30 -04:00
if err != nil {
fmt.Println("encode(): ",err)
}
ret.value = buf.Bytes()
return ret
}
func dencode(p *Var,t time.Time,chs ...chan struct{}) encoded {
ret := encoded{name:[]byte(p.name),value:t}
if len(chs) > 0 {
ret.donech = chs[0]
}
return ret
}
// tencode creates a DB key based on the time provided
func tencode(t time.Time) []byte {
buf := make([]byte, 8)
ns := t.UnixNano()
binary.BigEndian.PutUint64(buf, uint64(ns))
return buf
}
func tdecode(x []byte) time.Time {
ns := int64(binary.BigEndian.Uint64(x))
return time.Unix(0,ns)
}
func start() {
mux.Lock()
if launched {
mux.Unlock()
return
}
launched = true
if conf.configured == false {
mux.Unlock()
2018-08-03 15:08:30 -04:00
Init()
mux.Lock()
2018-08-03 15:08:30 -04:00
}
var err error
dbdir := path.Dir(conf.DBPath)
if _, err = os.Stat(dbdir); os.IsNotExist(err) {
if Debug { log.Print("creating db dir") }
2018-08-03 15:08:30 -04:00
err = os.MkdirAll(dbdir,0700)
if err != nil {
log.Fatal(err)
}
}
db, err = bolt.Open(conf.DBPath, 0600, &bolt.Options{Timeout:5 * time.Second})
if err != nil {
log.Fatal("persist.start() bolt.Open(): ", err)
}
tchan = make(chan struct{})
tg.Add(1)
go tidyThread()
wchan = make(chan encoded)
wg.Add(1)
go writer()
__checkpoints = &Var{name:"__checkpoints", X: nil}
h, err := __checkpoints.History()
if len(h) > 0 {
lastCheckpoint = h[len(h)-1].Time
if Debug { fmt.Println("lastCheckpoint = ",lastCheckpoint) }
2018-08-03 15:08:30 -04:00
}
mux.Unlock()
}
func writer() {
if Debug { fmt.Println("launching writer thread") }
2018-08-03 15:08:30 -04:00
runtime.UnlockOSThread()
for p := range wchan {
wmux.Lock()
err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(p.name)
if b == nil {
_, err := tx.CreateBucket(p.name)
if err != nil {
log.Fatal("Cannot create bucket for ",string(p.name))
}
if Debug { fmt.Println("writer(): created bucket for ",string(p.name)) }
2018-08-03 15:08:30 -04:00
b = tx.Bucket(p.name)
if b == nil {
log.Fatal("Bucket error")
}
}
var err error
switch v := p.value.(type) {
case time.Time:
vv := b.Get(tencode(v))
if len(vv) == 0 {
return errors.New("writer() error: can't delete")
}
err = b.Delete(tencode(v))
case []byte:
t := time.Now()
if string(p.name) == "__checkpoints" {
if Debug { fmt.Println("saving: ",string(p.name),t) }
2018-08-03 15:08:30 -04:00
lastCheckpoint = t
}
err = b.Put(tencode(t),v)
}
if err != nil {
log.Fatal("writer error", err)
}
return nil
})
wmux.Unlock()
if err != nil {
fmt.Println("writer() error: ",err)
}
if p.donech != nil { // close out synchronous writes
close(p.donech)
}
}
if Debug { fmt.Println("leaving writer thread") }
2018-08-03 15:08:30 -04:00
db.Close()
if Debug { fmt.Println("DB is closed") }
2018-08-03 15:08:30 -04:00
wg.Done()
}
func (p *Var) History() ([]Var, error) {
ret := make([]Var,0)
2018-08-03 15:08:30 -04:00
wmux.Lock()
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(p.name))
if b == nil {
return errors.New("DB entry not found")
}
c := b.Cursor()
for k, x := c.First(); x != nil; k, x = c.Next() {
t := tdecode(k)
p2 := Var{}
dec := gob.NewDecoder(bytes.NewBuffer(x))
err := dec.Decode(&p2)
if err != nil {
return err
}
if reflect.TypeOf(p2.X) != reflect.TypeOf(p.X) {
return errors.New("History(): Type mismatch")
}
p3 := *p
p3.X = p2.X
p3.Time = t
ret = append(ret,p3)
2018-08-03 15:08:30 -04:00
}
return nil
})
wmux.Unlock()
return ret, err
}
// retrieve a variable from the database. If ts is provided, return the first version
// that is not after ts[0].
func retrieve(name string, ts ...time.Time) ([]byte, time.Time, error) {
2018-08-03 15:08:30 -04:00
var ret []byte
var t time.Time
2018-08-03 15:08:30 -04:00
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b == nil {
return errors.New("DB entry not found")
}
c := b.Cursor()
if len(ts) == 0 {
var k []byte
k, ret = c.Last()
t = tdecode(k)
2018-08-03 15:08:30 -04:00
} else {
var k []byte
for k, ret = c.Last(); ret != nil; k, ret = c.Prev() {
t = tdecode(k)
if ts[0].After(t) {
2018-08-03 15:08:30 -04:00
return nil
}
}
return errors.New("Not found")
}
return nil
})
return ret, t, err
2018-08-03 15:08:30 -04:00
}
type TidyFunc func(*bolt.Bucket) int
type TidyConfig struct {
Interval time.Duration
Func TidyFunc
}
2018-08-30 09:23:21 -04:00
func ExpireAfter(exp time.Duration) Option {
return func(c *Config) {
c.Tidy.Func = ExpireAfterFunc(exp)
}
}
func ExpireAfterFunc(exp time.Duration) TidyFunc {
if exp > 0 {
exp = -1 * exp
}
if Debug { fmt.Println("ExpireAfter(",exp,")") }
return func(b *bolt.Bucket) int {
stime := time.Now()
etime := stime.Add(exp)
i := 0
c := b.Cursor()
end,_ := c.Last() // always keep the most recent on
for k,_ := c.First(); time.Since(stime) < 10 * time.Millisecond; k,_ = c.Next() {
if tdecode(k).After(etime) {
return i // entry not expired
}
if reflect.DeepEqual(k,end) {
return i // always keep the most recent entry
}
b.Delete(k)
i++
}
return i
}
}
2018-08-03 15:08:30 -04:00
func tidyThread() {
if Debug { fmt.Println("tidyThread() starting") }
2018-08-03 15:08:30 -04:00
lchan := make(chan string)
go func() {
if Debug { fmt.Println("starting loaded reading thread") }
2018-08-03 15:08:30 -04:00
if loaded == nil {
loaded = make(chan *Var,conf.MaxVars) // buffered channel
}
for {
for p := range loaded {
2018-08-30 09:23:21 -04:00
if p.permanent == false {
2018-08-03 15:08:30 -04:00
lchan<- p.name
loaded<- p // put it back in the queue
}
}
if Debug { fmt.Println("closing lchan") }
2018-08-03 15:08:30 -04:00
close(lchan)
}
}()
tick := time.NewTicker(conf.Tidy.Interval)
2018-08-03 15:08:30 -04:00
for { select {
case <-tchan:
if Debug { fmt.Println("closing loaded channel") }
2018-08-03 15:08:30 -04:00
close(loaded)
tg.Done()
if Debug { fmt.Println("tidy(): returning") }
2018-08-03 15:08:30 -04:00
return
case <-tick.C:
name := <-lchan
if Debug { fmt.Println("tidyThread(): tidying ", name) }
2018-08-03 15:08:30 -04:00
Tidy(name)
} }
}
// discard entries if they are too old.
func Tidy(name string) {
stime := time.Now()
mux.Lock()
i := 0
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b == nil {
fmt.Println("Can't open bucket for ",name)
return nil
}
i = conf.Tidy.Func(b)
2018-08-03 15:08:30 -04:00
return nil
})
mux.Unlock()
if i > 0 {
if Debug { fmt.Println("tidy(): deleted ",i," entries in ",time.Since(stime)) }
2018-08-03 15:08:30 -04:00
}
if Debug { fmt.Println("tidy(): done tidying",name) }
2018-08-03 15:08:30 -04:00
}