diff --git a/generate/template.go b/generate/template.go index 5cb991d..b5fdf55 100644 --- a/generate/template.go +++ b/generate/template.go @@ -29,7 +29,7 @@ func (v *Var_N) Get(ts ...time.Time) _T { return ptr.Get(ts...).(_T) } -func (v *Var_N) History() ([]persist.TVar, error) { +func (v *Var_N) History() ([]persist.Var, error) { ptr := (*persist.Var)(unsafe.Pointer(v)) return ptr.History() } diff --git a/persist.go b/persist.go index 26d5922..2b0bca1 100644 --- a/persist.go +++ b/persist.go @@ -23,6 +23,7 @@ import ( type Var struct { X interface{} + Time time.Time name string opt Option } @@ -109,10 +110,11 @@ func (p *Var) Save(sync ...bool) { func (p *Var) Load(ts ...time.Time) error { var err error var v []byte + var t time.Time if len(ts) == 0 { - v, err = retrieve(p.name,lastCheckpoint) + v, t, err = retrieve(p.name,lastCheckpoint) } else { - v, err = retrieve(p.name,ts[0]) + v, t, err = retrieve(p.name,ts[0]) } if err == nil { var p2 Var @@ -125,6 +127,7 @@ func (p *Var) Load(ts ...time.Time) error { fmt.Println("Load(): ",err) } else { p.X = p2.X + p.Time = t } } else { fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X)) @@ -354,13 +357,8 @@ func writer() { wg.Done() } -type TVar struct { - Time time.Time - Value Var -} - -func (p *Var) History() ([]TVar, error) { - ret := make([]TVar,0) +func (p *Var) History() ([]Var, error) { + ret := make([]Var,0) wmux.Lock() err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(p.name)) @@ -381,7 +379,8 @@ func (p *Var) History() ([]TVar, error) { } p3 := *p p3.X = p2.X - ret = append(ret,TVar{t,p3}) + p3.Time = t + ret = append(ret,p3) } return nil }) @@ -391,8 +390,9 @@ func (p *Var) History() ([]TVar, error) { // 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, error) { +func retrieve(name string, ts ...time.Time) ([]byte, time.Time, error) { var ret []byte + var t time.Time err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(name)) if b == nil { @@ -400,11 +400,14 @@ func retrieve(name string, ts ...time.Time) ([]byte, error) { } c := b.Cursor() if len(ts) == 0 { - _, ret = c.Last() + var k []byte + k, ret = c.Last() + t = tdecode(k) } else { var k []byte for k, ret = c.Last(); ret != nil; k, ret = c.Prev() { - if ts[0].After(tdecode(k)) { + t = tdecode(k) + if ts[0].After(t) { return nil } } @@ -412,7 +415,7 @@ func retrieve(name string, ts ...time.Time) ([]byte, error) { } return nil }) - return ret, err + return ret, t, err } func tidyThread() {