diff --git a/dice/dice.go b/dice/dice.go index b7ded3a5..213b1b51 100644 --- a/dice/dice.go +++ b/dice/dice.go @@ -551,6 +551,7 @@ func (d *Dice) ExtAliasToName(s string) string { return s } +// TODO: 8是,这函数根本没有被正确使用啊?! func (d *Dice) ExtRemove(ei *ExtInfo) bool { // Pinenutn: Range模板 ServiceAtNew重构代码 d.ImSession.ServiceAtNew.Range(func(key string, groupInfo *GroupInfo) bool { diff --git a/dice/dice_jsvm.go b/dice/dice_jsvm.go index 87439955..dea8618f 100644 --- a/dice/dice_jsvm.go +++ b/dice/dice_jsvm.go @@ -623,7 +623,7 @@ func (d *Dice) JsShutdown() { func (d *Dice) jsClear() { // 清理js扩展 - prepareRemove := []*ExtInfo{} + var prepareRemove []*ExtInfo for _, i := range d.ExtList { if i.IsJsExt { prepareRemove = append(prepareRemove, i) diff --git a/dice/ext.go b/dice/ext.go index e44178a6..c4eeed09 100644 --- a/dice/ext.go +++ b/dice/ext.go @@ -177,15 +177,16 @@ func (i *ExtInfo) StorageSet(k, v string) error { return err } db := i.Storage - return db.Set(k, v) + err := db.Set(k, v) + return err } func (i *ExtInfo) StorageGet(k string) (string, error) { if err := i.StorageInit(); err != nil { return "", err } - var v *string - found, err := i.Storage.Get(k, v) + var v string + found, err := i.Storage.Get(k, &v) // 有错误先报错 if err != nil { return "", err @@ -194,5 +195,5 @@ func (i *ExtInfo) StorageGet(k string) (string, error) { if !found { return "", nil } - return *v, nil + return v, nil } diff --git a/dice/plugin_store/bunt_storage.go b/dice/plugin_store/bunt_storage.go index 9bddb63b..e8ddda56 100644 --- a/dice/plugin_store/bunt_storage.go +++ b/dice/plugin_store/bunt_storage.go @@ -29,7 +29,7 @@ func (b *BuntDBPluginStorage) GetStorageInstance(pluginName string) (sealkv.Seal } // 尝试进行初始化 options := bunt.Options{ - Path: path.Join(b.Path, "storage.db"), + Path: path.Join(b.Path, pluginName, "storage.db"), } store, err := bunt.NewStore(options) if err != nil { @@ -41,8 +41,11 @@ func (b *BuntDBPluginStorage) GetStorageInstance(pluginName string) (sealkv.Seal } func (b *BuntDBPluginStorage) StorageClose() error { + log.Infof("closing buntdb plugin storage") b.KVMap.Range(func(key string, value *bunt.Store) bool { - err := value.Close() + v := value + value = nil + err := v.Close() if err != nil { log.Errorf("close buntdb error: %v", err) return true diff --git a/utils/gokv/bunt/sealdice_bunt_store.go b/utils/gokv/bunt/sealdice_bunt_store.go index 3b661d62..bbdc960f 100644 --- a/utils/gokv/bunt/sealdice_bunt_store.go +++ b/utils/gokv/bunt/sealdice_bunt_store.go @@ -2,17 +2,15 @@ package bunt import ( "errors" + "fmt" - "github.com/philippgille/gokv/encoding" "github.com/philippgille/gokv/util" "github.com/tidwall/buntdb" ) -// Store is a gokv.Store implementation for bbolt (formerly known as Bolt / Bolt DB). +// Store 注: 由于原本的存储方式是纯KV,只能抛弃序列化设计,直接存储原本的值。 type Store struct { - db *buntdb.DB - bucketName string - codec encoding.Codec + db *buntdb.DB } // Set stores the given value for the given key. @@ -22,15 +20,9 @@ func (s Store) Set(k string, v any) error { if err := util.CheckKeyAndValue(k, v); err != nil { return err } - - // First turn the passed object into something that bbolt can handle - data, err := s.codec.Marshal(v) - if err != nil { - return err - } - - err = s.db.Update(func(tx *buntdb.Tx) error { - _, _, err2 := tx.Set(k, string(data), nil) + res := fmt.Sprintf("%v", v) + err := s.db.Update(func(tx *buntdb.Tx) error { + _, _, err2 := tx.Set(k, res, nil) return err2 }) return err @@ -46,12 +38,17 @@ func (s Store) Get(k string, v any) (found bool, err error) { if err = util.CheckKeyAndValue(k, v); err != nil { return false, err } - - var rawData string //[]byte + var res string err = s.db.View(func(tx *buntdb.Tx) error { - rawData, err = tx.Get(k) + res, err = tx.Get(k) return err }) + // 一些抽象的断言赋值 + if strPtr, ok := v.(*string); ok { + *strPtr = res + } else { + return false, fmt.Errorf("v must be a *string, got %T", v) + } // 特判找不到的情况 if err != nil && errors.Is(err, buntdb.ErrNotFound) { return false, nil @@ -59,7 +56,7 @@ func (s Store) Get(k string, v any) (found bool, err error) { if err != nil { return false, err } - return true, s.codec.Unmarshal([]byte(rawData), v) + return true, nil } // Delete deletes the stored value for the given key. @@ -124,16 +121,11 @@ type Options struct { // Path of the DB file. Must have // Optional ("bbolt.db" by default). Path string - // Encoding format. - // Optional (encoding.JSON by default). - Codec encoding.Codec } // DefaultOptions is an Options object with default values. // BucketName: "default", Path: "bbolt.db", Codec: encoding.JSON -var DefaultOptions = Options{ - Codec: encoding.JSON, -} +var DefaultOptions = Options{} // NewStore creates a new bbolt store. // Note: bbolt uses an exclusive write lock on the database file so it cannot be shared by multiple processes. @@ -146,9 +138,6 @@ func NewStore(options Options) (Store, error) { if options.Path == "" { return result, errors.New("path is nil, you must write it before") } - if options.Codec == nil { - options.Codec = DefaultOptions.Codec - } // Open DB db, err := buntdb.Open(options.Path) @@ -157,7 +146,6 @@ func NewStore(options Options) (Store, error) { } result.db = db - result.codec = options.Codec return result, nil } diff --git a/utils/gokv/gormkv/sealdice_gorm_store.go b/utils/gokv/gormkv/sealdice_gorm_store.go index 3311f9c5..820804d6 100644 --- a/utils/gokv/gormkv/sealdice_gorm_store.go +++ b/utils/gokv/gormkv/sealdice_gorm_store.go @@ -104,9 +104,9 @@ func (s Store) Get(k string, v any) (bool, error) { if result.Error != nil { return false, result.Error } - // 如果查询不到 报错 + // 如果查询不到 返回查询不到就行了 if result.RowsAffected == 0 { - return false, errors.New("record not found") + return false, nil } // 否则返回 return true, s.Codec.Unmarshal(record.Value, v)