feat: extension support (#78)

This commit is contained in:
Levi authored and GitHub committed 2023-10-16 09:47:31 +08:00
1 parent 82d316af83
commit c073fc0a67
118 files changed
+6971 -780

No files matched your search

+20 -1
View File
@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"reflect"
"sync"
)
type Storage interface {
@@ -47,10 +48,13 @@ func changeValue(p any, v any) {
var memData = make(map[string]map[string]any)
type MemStorage struct {
lock *sync.RWMutex
}
func NewMemStorage() *MemStorage {
return &MemStorage{}
return &MemStorage{
lock: &sync.RWMutex{},
}
}
func (n *MemStorage) Setup(buckets []string) error {
@@ -63,11 +67,15 @@ func (n *MemStorage) Setup(buckets []string) error {
}
func (n *MemStorage) Put(bucket string, key string, v any) error {
n.lock.Lock()
defer n.lock.Unlock()
memData[bucket][key] = v
return nil
}
func (n *MemStorage) Get(bucket string, key string, v any) (bool, error) {
n.lock.RLock()
defer n.lock.RUnlock()
if dv, ok := memData[bucket][key]; ok {
changeValue(v, dv)
return true, nil
@@ -76,6 +84,8 @@ func (n *MemStorage) Get(bucket string, key string, v any) (bool, error) {
}
func (n *MemStorage) List(bucket string, v any) error {
n.lock.RLock()
defer n.lock.RUnlock()
data := memData[bucket]
list := make([]any, 0)
for _, v := range data {
@@ -86,6 +96,8 @@ func (n *MemStorage) List(bucket string, v any) error {
}
func (n *MemStorage) Pop(bucket string, key string, v any) error {
n.lock.Lock()
defer n.lock.Unlock()
data := memData[bucket]
changeValue(v, data[key])
delete(data, key)
@@ -93,6 +105,8 @@ func (n *MemStorage) Pop(bucket string, key string, v any) error {
}
func (n *MemStorage) Delete(bucket string, key string) error {
n.lock.Lock()
defer n.lock.Unlock()
delete(memData[bucket], key)
return nil
}
@@ -102,6 +116,8 @@ func (n *MemStorage) Close() error {
}
func (n *MemStorage) Clear() error {
n.lock.Lock()
defer n.lock.Unlock()
memData = make(map[string]map[string]any)
return nil
}
@@ -116,6 +132,9 @@ type BoltStorage struct {
}
func NewBoltStorage(dir string) *BoltStorage {
if err := os.MkdirAll(dir, 0755); err != nil {
panic(err)
}
path := filepath.Join(dir, dbFile)
db, err := bbolt.Open(path, 0600, nil)
if err != nil {