From 771241b2a217468052c9e48e52225a3077220b3c Mon Sep 17 00:00:00 2001 From: xmdhs Date: Fri, 2 Aug 2024 19:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/bbolt.go | 67 ------------------------------------------------- db/interface.go | 12 --------- 2 files changed, 79 deletions(-) delete mode 100644 db/bbolt.go delete mode 100644 db/interface.go diff --git a/db/bbolt.go b/db/bbolt.go deleted file mode 100644 index b74000c..0000000 --- a/db/bbolt.go +++ /dev/null @@ -1,67 +0,0 @@ -package db - -import ( - "context" - "encoding/json" - "errors" - "fmt" - - "github.com/xmdhs/clash2sfa/model" - "go.etcd.io/bbolt" -) - -type BBolt struct { - db *bbolt.DB -} - -func NewBBolt(path string) (*BBolt, error) { - db, err := bbolt.Open(path, 0666, bbolt.DefaultOptions) - if err != nil { - return nil, fmt.Errorf("NewBBolt: %w", err) - } - err = db.Update(func(tx *bbolt.Tx) error { - _, err := tx.CreateBucketIfNotExists([]byte("arg")) - return err - }) - if err != nil { - return nil, fmt.Errorf("NewBBolt: %w", err) - } - return &BBolt{db: db}, nil -} - -var ErrNotFind = errors.New("没找到") - -func (b *BBolt) GetArg(cxt context.Context, blake3 string) (model.ConvertArg, error) { - m := model.ConvertArg{} - err := b.db.View(func(tx *bbolt.Tx) error { - buc := tx.Bucket([]byte("arg")) - b := buc.Get([]byte(blake3)) - if b == nil { - return ErrNotFind - } - err := json.Unmarshal(b, &m) - if err != nil { - panic(err) - } - return nil - }) - if err != nil { - return m, fmt.Errorf("GetArg: %w", err) - } - return m, nil -} - -func (b *BBolt) PutArg(cxt context.Context, blake3 string, arg model.ConvertArg) error { - rb, err := json.Marshal(arg) - if err != nil { - return fmt.Errorf("PutArg: %w", err) - } - err = b.db.Update(func(tx *bbolt.Tx) error { - buc := tx.Bucket([]byte("arg")) - return buc.Put([]byte(blake3), rb) - }) - if err != nil { - return fmt.Errorf("PutArg: %w", err) - } - return nil -} diff --git a/db/interface.go b/db/interface.go deleted file mode 100644 index 3fa684f..0000000 --- a/db/interface.go +++ /dev/null @@ -1,12 +0,0 @@ -package db - -import ( - "context" - - "github.com/xmdhs/clash2sfa/model" -) - -type DB interface { - GetArg(cxt context.Context, blake3 string) (model.ConvertArg, error) - PutArg(cxt context.Context, blake3 string, arg model.ConvertArg) error -}