优化KV相关错误提示/可以从路径直接加载数据库

This commit is contained in:
GoEdgeLab
2024-04-23 11:56:00 +08:00
parent 1f9f3d64e5
commit e18bd2a6e1
3 changed files with 57 additions and 8 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/iwind/TeaGo/Tea"
"io"
"os"
"path/filepath"
"strings"
"sync"
)
@@ -52,6 +53,31 @@ func NewStore(storeName string) (*Store, error) {
}, nil
}
// NewStoreWithPath create store with path
func NewStoreWithPath(path string) (*Store, error) {
if !strings.HasSuffix(path, ".store") {
return nil, errors.New("store path must contains a '.store' suffix")
}
_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
_ = os.MkdirAll(path, 0777)
}
var storeName = filepath.Base(path)
storeName = strings.TrimSuffix(storeName, ".store")
if !IsValidName(storeName) {
return nil, errors.New("invalid store name '" + storeName + "'")
}
return &Store{
name: storeName,
path: path,
locker: fsutils.NewLocker(path + "/.fs"),
}, nil
}
func OpenStore(storeName string) (*Store, error) {
store, err := NewStore(storeName)
if err != nil {
@@ -117,6 +143,10 @@ func DefaultStore() (*Store, error) {
return defaultSore, resultErr
}
func (this *Store) Path() string {
return this.path
}
func (this *Store) Open() error {
err := this.locker.Lock()
if err != nil {