bbgo_origin/pkg/bbgo/persistence.go

176 lines
4.1 KiB
Go
Raw Permalink Normal View History

package bbgo
import (
"fmt"
"reflect"
log "github.com/sirupsen/logrus"
2022-03-14 13:21:04 +00:00
"github.com/c9s/bbgo/pkg/service"
)
2020-12-07 03:43:17 +00:00
type PersistenceSelector struct {
// StoreID is the store you want to use.
StoreID string `json:"store" yaml:"store"`
// Type is the persistence type
Type string `json:"type" yaml:"type"`
}
// Persistence is used for strategy to inject the persistence.
type Persistence struct {
PersistenceSelector *PersistenceSelector `json:"persistence,omitempty" yaml:"persistence,omitempty"`
Facade *service.PersistenceServiceFacade `json:"-" yaml:"-"`
2020-12-07 03:43:17 +00:00
}
2021-03-18 09:20:07 +00:00
func (p *Persistence) backendService(t string) (service.PersistenceService, error) {
2020-12-07 03:43:17 +00:00
switch t {
case "json":
2021-03-18 09:20:07 +00:00
return p.Facade.Json, nil
2020-12-07 03:43:17 +00:00
case "redis":
if p.Facade.Redis == nil {
log.Warn("redis persistence is not available, fallback to memory backend")
return p.Facade.Memory, nil
}
2021-03-18 09:20:07 +00:00
return p.Facade.Redis, nil
2020-12-07 03:43:17 +00:00
2020-12-07 04:03:56 +00:00
case "memory":
2021-03-18 09:20:07 +00:00
return p.Facade.Memory, nil
2020-12-07 04:03:56 +00:00
2020-12-07 03:43:17 +00:00
}
2021-03-18 09:20:07 +00:00
return nil, fmt.Errorf("unsupported persistent type %s", t)
2020-12-07 03:43:17 +00:00
}
func (p *Persistence) Load(val interface{}, subIDs ...string) error {
2021-03-18 09:20:07 +00:00
ps, err := p.backendService(p.PersistenceSelector.Type)
2020-12-07 03:43:17 +00:00
if err != nil {
return err
}
2022-03-14 13:21:04 +00:00
log.Debugf("using persistence store %T for loading", ps)
2020-12-07 03:43:17 +00:00
if p.PersistenceSelector.StoreID == "" {
2021-03-18 09:20:07 +00:00
p.PersistenceSelector.StoreID = "default"
2020-12-07 03:43:17 +00:00
}
2021-03-18 09:20:07 +00:00
store := ps.NewStore(p.PersistenceSelector.StoreID, subIDs...)
2020-12-07 03:43:17 +00:00
return store.Load(val)
}
func (p *Persistence) Save(val interface{}, subIDs ...string) error {
2021-03-18 09:20:07 +00:00
ps, err := p.backendService(p.PersistenceSelector.Type)
2020-12-07 03:43:17 +00:00
if err != nil {
return err
}
2022-03-14 13:21:04 +00:00
log.Debugf("using persistence store %T for storing", ps)
2020-12-07 03:43:17 +00:00
if p.PersistenceSelector.StoreID == "" {
2021-03-18 09:20:07 +00:00
p.PersistenceSelector.StoreID = "default"
2020-12-07 03:43:17 +00:00
}
2021-03-18 09:20:07 +00:00
store := ps.NewStore(p.PersistenceSelector.StoreID, subIDs...)
2020-12-07 03:43:17 +00:00
return store.Save(val)
}
2022-05-05 10:02:08 +00:00
func (p *Persistence) Sync(obj interface{}) error {
id := callID(obj)
if len(id) == 0 {
return nil
}
ps := p.Facade.Get()
return storePersistenceFields(obj, id, ps)
}
2022-05-05 05:06:02 +00:00
type StructFieldIterator func(tag string, ft reflect.StructField, fv reflect.Value) error
func iterateFieldsByTag(obj interface{}, tagName string, cb StructFieldIterator) error {
sv := reflect.ValueOf(obj)
st := reflect.TypeOf(obj)
if st.Kind() != reflect.Ptr {
return fmt.Errorf("f needs to be a pointer of a struct, %s given", st)
}
// solve the reference
st = st.Elem()
sv = sv.Elem()
if st.Kind() != reflect.Struct {
return fmt.Errorf("f needs to be a struct, %s given", st)
}
for i := 0; i < sv.NumField(); i++ {
fv := sv.Field(i)
ft := st.Field(i)
fvt := fv.Type()
_ = fvt
// skip unexported fields
if !st.Field(i).IsExported() {
continue
}
tag, ok := ft.Tag.Lookup(tagName)
if !ok {
continue
}
if err := cb(tag, ft, fv); err != nil {
return err
}
}
return nil
}
// https://github.com/xiaojun207/go-base-utils/blob/master/utils/Clone.go
func newTypeValueInterface(typ reflect.Type) interface{} {
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
dst := reflect.New(typ).Elem()
return dst.Addr().Interface()
}
dst := reflect.New(typ)
return dst.Interface()
}
2022-05-05 05:05:01 +00:00
func loadPersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error {
return iterateFieldsByTag(obj, "persistence", func(tag string, field reflect.StructField, value reflect.Value) error {
newValueInf := newTypeValueInterface(value.Type())
// inf := value.Interface()
2022-05-05 09:56:41 +00:00
store := persistence.NewStore("state", id, tag)
if err := store.Load(&newValueInf); err != nil {
if err == service.ErrPersistenceNotExists {
return nil
}
return err
}
newValue := reflect.ValueOf(newValueInf)
if value.Kind() != reflect.Ptr && newValue.Kind() == reflect.Ptr {
newValue = newValue.Elem()
}
// log.Debugf("%v = %v (%s) -> %v (%s)\n", field, value, value.Type(), newValue, newValue.Type())
value.Set(newValue)
return nil
})
}
2022-05-05 05:05:01 +00:00
func storePersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error {
return iterateFieldsByTag(obj, "persistence", func(tag string, ft reflect.StructField, fv reflect.Value) error {
inf := fv.Interface()
2022-05-05 09:56:41 +00:00
store := persistence.NewStore("state", id, tag)
return store.Save(inf)
})
}