mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-25 14:36:34 +08:00
优化KV相关错误提示/可以从路径直接加载数据库
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user