Add Debug variable to control verbose output.
This commit is contained in:
parent
20c89c6e56
commit
814c6ffa20
42
persist.go
42
persist.go
|
@ -17,6 +17,8 @@ import (
|
|||
bolt "github.com/coreos/bbolt"
|
||||
)
|
||||
|
||||
var Debug bool
|
||||
|
||||
// allow persistant storage and reloading of variables to bolt database.
|
||||
// everything is time stamped.
|
||||
// bucket is the variable name
|
||||
|
@ -101,13 +103,15 @@ func New(name string, x interface{},ps ...bool) *Var {
|
|||
fmt.Println("Load error:",err)
|
||||
wchan <- encode(ret)
|
||||
}
|
||||
fmt.Println("New(): ",ret.name)
|
||||
if Debug { fmt.Println("New(): ",ret.name) }
|
||||
mux.Lock()
|
||||
if loaded == nil {
|
||||
loaded = make(chan *Var,conf.MaxVars) // buffered channel
|
||||
}
|
||||
mux.Unlock()
|
||||
if Debug { fmt.Println("New(): loaded<- ret") }
|
||||
loaded<- ret
|
||||
if Debug { fmt.Println("New(): returning") }
|
||||
return ret
|
||||
}
|
||||
|
||||
|
@ -155,7 +159,7 @@ func (p *Var) Load(ts ...time.Time) error {
|
|||
p.Time = t
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X))
|
||||
if Debug { fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X)) }
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@ -194,7 +198,7 @@ func Checkpoints() {
|
|||
}
|
||||
|
||||
func Undo() {
|
||||
fmt.Println("Undo()")
|
||||
if Debug { fmt.Println("Undo()") }
|
||||
mux.Lock()
|
||||
__checkpoints.DeleteSync(lastCheckpoint)
|
||||
h,_ := __checkpoints.History()
|
||||
|
@ -303,7 +307,7 @@ func start() {
|
|||
var err error
|
||||
dbdir := path.Dir(conf.DBPath)
|
||||
if _, err = os.Stat(dbdir); os.IsNotExist(err) {
|
||||
log.Print("creating db dir")
|
||||
if Debug { log.Print("creating db dir") }
|
||||
err = os.MkdirAll(dbdir,0700)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -327,13 +331,13 @@ func start() {
|
|||
h, err := __checkpoints.History()
|
||||
if len(h) > 0 {
|
||||
lastCheckpoint = h[len(h)-1].Time
|
||||
fmt.Println("lastCheckpoint = ",lastCheckpoint)
|
||||
if Debug { fmt.Println("lastCheckpoint = ",lastCheckpoint) }
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
|
||||
func writer() {
|
||||
fmt.Println("launching writer thread")
|
||||
if Debug { fmt.Println("launching writer thread") }
|
||||
runtime.UnlockOSThread()
|
||||
for p := range wchan {
|
||||
wmux.Lock()
|
||||
|
@ -344,7 +348,7 @@ func writer() {
|
|||
if err != nil {
|
||||
log.Fatal("Cannot create bucket for ",string(p.name))
|
||||
}
|
||||
fmt.Println("writer(): created bucket for ",string(p.name))
|
||||
if Debug { fmt.Println("writer(): created bucket for ",string(p.name)) }
|
||||
b = tx.Bucket(p.name)
|
||||
if b == nil {
|
||||
log.Fatal("Bucket error")
|
||||
|
@ -361,7 +365,7 @@ func writer() {
|
|||
case []byte:
|
||||
t := time.Now()
|
||||
if string(p.name) == "__checkpoints" {
|
||||
fmt.Println("saving: ",string(p.name),t)
|
||||
if Debug { fmt.Println("saving: ",string(p.name),t) }
|
||||
lastCheckpoint = t
|
||||
}
|
||||
err = b.Put(tencode(t),v)
|
||||
|
@ -379,9 +383,9 @@ func writer() {
|
|||
close(p.donech)
|
||||
}
|
||||
}
|
||||
fmt.Println("leaving writer thread")
|
||||
if Debug { fmt.Println("leaving writer thread") }
|
||||
db.Close()
|
||||
fmt.Println("DB is closed")
|
||||
if Debug { fmt.Println("DB is closed") }
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
|
@ -463,7 +467,7 @@ func ExpireAfterFunc(exp time.Duration) TidyFunc {
|
|||
if exp > 0 {
|
||||
exp = -1 * exp
|
||||
}
|
||||
fmt.Println("ExpireAfter(",exp,")")
|
||||
if Debug { fmt.Println("ExpireAfter(",exp,")") }
|
||||
return func(b *bolt.Bucket) int {
|
||||
stime := time.Now()
|
||||
etime := stime.Add(exp)
|
||||
|
@ -485,10 +489,10 @@ func ExpireAfterFunc(exp time.Duration) TidyFunc {
|
|||
}
|
||||
|
||||
func tidyThread() {
|
||||
fmt.Println("tidyThread() starting")
|
||||
if Debug { fmt.Println("tidyThread() starting") }
|
||||
lchan := make(chan string)
|
||||
go func() {
|
||||
fmt.Println("starting loaded reading thread")
|
||||
if Debug { fmt.Println("starting loaded reading thread") }
|
||||
if loaded == nil {
|
||||
loaded = make(chan *Var,conf.MaxVars) // buffered channel
|
||||
}
|
||||
|
@ -499,21 +503,21 @@ func tidyThread() {
|
|||
loaded<- p // put it back in the queue
|
||||
}
|
||||
}
|
||||
fmt.Println("closing lchan")
|
||||
if Debug { fmt.Println("closing lchan") }
|
||||
close(lchan)
|
||||
}
|
||||
}()
|
||||
tick := time.NewTicker(conf.Tidy.Interval)
|
||||
for { select {
|
||||
case <-tchan:
|
||||
fmt.Println("closing loaded channel")
|
||||
if Debug { fmt.Println("closing loaded channel") }
|
||||
close(loaded)
|
||||
tg.Done()
|
||||
fmt.Println("tidy(): returning")
|
||||
if Debug { fmt.Println("tidy(): returning") }
|
||||
return
|
||||
case <-tick.C:
|
||||
name := <-lchan
|
||||
fmt.Println("tidyThread(): tidying ", name)
|
||||
if Debug { fmt.Println("tidyThread(): tidying ", name) }
|
||||
Tidy(name)
|
||||
} }
|
||||
}
|
||||
|
@ -534,8 +538,8 @@ func Tidy(name string) {
|
|||
})
|
||||
mux.Unlock()
|
||||
if i > 0 {
|
||||
fmt.Println("tidy(): deleted ",i," entries in ",time.Since(stime))
|
||||
if Debug { fmt.Println("tidy(): deleted ",i," entries in ",time.Since(stime)) }
|
||||
}
|
||||
fmt.Println("tidy(): done tidying",name)
|
||||
if Debug { fmt.Println("tidy(): done tidying",name) }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user