Put the Time field into persist.Var. persist.TVar is no longer needed,

History() now returns []Var.
This commit is contained in:
Greg 2018-08-09 09:29:59 -04:00
parent 384de4d02c
commit ee4657a0ed
2 changed files with 18 additions and 15 deletions

View File

@ -29,7 +29,7 @@ func (v *Var_N) Get(ts ...time.Time) _T {
return ptr.Get(ts...).(_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)) ptr := (*persist.Var)(unsafe.Pointer(v))
return ptr.History() return ptr.History()
} }

View File

@ -23,6 +23,7 @@ import (
type Var struct { type Var struct {
X interface{} X interface{}
Time time.Time
name string name string
opt Option opt Option
} }
@ -109,10 +110,11 @@ func (p *Var) Save(sync ...bool) {
func (p *Var) Load(ts ...time.Time) error { func (p *Var) Load(ts ...time.Time) error {
var err error var err error
var v []byte var v []byte
var t time.Time
if len(ts) == 0 { if len(ts) == 0 {
v, err = retrieve(p.name,lastCheckpoint) v, t, err = retrieve(p.name,lastCheckpoint)
} else { } else {
v, err = retrieve(p.name,ts[0]) v, t, err = retrieve(p.name,ts[0])
} }
if err == nil { if err == nil {
var p2 Var var p2 Var
@ -125,6 +127,7 @@ func (p *Var) Load(ts ...time.Time) error {
fmt.Println("Load(): ",err) fmt.Println("Load(): ",err)
} else { } else {
p.X = p2.X p.X = p2.X
p.Time = t
} }
} else { } else {
fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X)) fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X))
@ -354,13 +357,8 @@ func writer() {
wg.Done() wg.Done()
} }
type TVar struct { func (p *Var) History() ([]Var, error) {
Time time.Time ret := make([]Var,0)
Value Var
}
func (p *Var) History() ([]TVar, error) {
ret := make([]TVar,0)
wmux.Lock() wmux.Lock()
err := db.View(func(tx *bolt.Tx) error { err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(p.name)) b := tx.Bucket([]byte(p.name))
@ -381,7 +379,8 @@ func (p *Var) History() ([]TVar, error) {
} }
p3 := *p p3 := *p
p3.X = p2.X p3.X = p2.X
ret = append(ret,TVar{t,p3}) p3.Time = t
ret = append(ret,p3)
} }
return nil 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 // retrieve a variable from the database. If ts is provided, return the first version
// that is not after ts[0]. // 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 ret []byte
var t time.Time
err := db.View(func(tx *bolt.Tx) error { err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name)) b := tx.Bucket([]byte(name))
if b == nil { if b == nil {
@ -400,11 +400,14 @@ func retrieve(name string, ts ...time.Time) ([]byte, error) {
} }
c := b.Cursor() c := b.Cursor()
if len(ts) == 0 { if len(ts) == 0 {
_, ret = c.Last() var k []byte
k, ret = c.Last()
t = tdecode(k)
} else { } else {
var k []byte var k []byte
for k, ret = c.Last(); ret != nil; k, ret = c.Prev() { 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 return nil
} }
} }
@ -412,7 +415,7 @@ func retrieve(name string, ts ...time.Time) ([]byte, error) {
} }
return nil return nil
}) })
return ret, err return ret, t, err
} }
func tidyThread() { func tidyThread() {