Skip to content

Commit

Permalink
refactor(dice): 重构插件存储和一些代码优化
Browse files Browse the repository at this point in the history
- 修改 BuntDBPluginStorage 初始化逻辑,使用插件名称创建独立数据库文件
- 优化 ExtInfo.StorageSet 和 StorageGet 方法,修复潜在的空指针问题
-重构 buntdb 存储实现,移除未使用的编码器和解码器
- 优化 gorm 存储实现,改进记录未找到时的错误处理
- 添加日志记录,提高代码可读性和调试方便性
  • Loading branch information
PaienNate committed Jan 14, 2025
1 parent 971ddfa commit 1fec296
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
1 change: 1 addition & 0 deletions dice/dice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion dice/dice_jsvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions dice/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -194,5 +195,5 @@ func (i *ExtInfo) StorageGet(k string) (string, error) {
if !found {
return "", nil
}
return *v, nil
return v, nil
}
7 changes: 5 additions & 2 deletions dice/plugin_store/bunt_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Check failure on line 47 in dice/plugin_store/bunt_storage.go

View workflow job for this annotation

GitHub Actions / Test & Lint

ineffectual assignment to value (ineffassign)
err := v.Close()
if err != nil {
log.Errorf("close buntdb error: %v", err)
return true
Expand Down
44 changes: 16 additions & 28 deletions utils/gokv/bunt/sealdice_bunt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -46,20 +38,25 @@ 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
}
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.
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -157,7 +146,6 @@ func NewStore(options Options) (Store, error) {
}

result.db = db
result.codec = options.Codec

return result, nil
}
4 changes: 2 additions & 2 deletions utils/gokv/gormkv/sealdice_gorm_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1fec296

Please sign in to comment.