fix(db): aggregate store pkgs under db dir
This commit is contained in:
parent
8e7dd50efd
commit
4d8a8999a5
15 changed files with 13 additions and 13 deletions
199
src/db/fileinfostore/file_info_store.go
Normal file
199
src/db/fileinfostore/file_info_store.go
Normal file
|
@ -0,0 +1,199 @@
|
|||
package fileinfostore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore"
|
||||
)
|
||||
|
||||
const (
|
||||
InitNs = "Init"
|
||||
InfoNs = "sharing"
|
||||
InitTimeKey = "initTime"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("file info not found")
|
||||
)
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
return err == ErrNotFound
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
IsDir bool `json:"isDir"`
|
||||
Shared bool `json:"shared"`
|
||||
Sha1 string `json:"sha1"`
|
||||
}
|
||||
|
||||
type IFileInfoStore interface {
|
||||
AddSharing(dirPath string) error
|
||||
DelSharing(dirPath string) error
|
||||
GetSharing(dirPath string) (bool, bool)
|
||||
ListSharings(prefix string) (map[string]bool, error)
|
||||
GetInfo(itemPath string) (*FileInfo, error)
|
||||
SetInfo(itemPath string, info *FileInfo) error
|
||||
DelInfo(itemPath string) error
|
||||
SetSha1(itemPath, sign string) error
|
||||
GetInfos(itemPaths []string) (map[string]*FileInfo, error)
|
||||
}
|
||||
|
||||
type FileInfoStore struct {
|
||||
mtx *sync.RWMutex
|
||||
store kvstore.IKVStore
|
||||
}
|
||||
|
||||
func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
|
||||
_, ok := store.GetStringIn(InitNs, InitTimeKey)
|
||||
if !ok {
|
||||
var err error
|
||||
for _, nsName := range []string{
|
||||
InitNs,
|
||||
InfoNs,
|
||||
} {
|
||||
if err = store.AddNamespace(nsName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := store.SetStringIn(InitNs, InitTimeKey, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FileInfoStore{
|
||||
store: store,
|
||||
mtx: &sync.RWMutex{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) AddSharing(dirPath string) error {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(dirPath)
|
||||
if err != nil {
|
||||
if !IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
info = &FileInfo{
|
||||
IsDir: true,
|
||||
}
|
||||
}
|
||||
info.Shared = true
|
||||
return fi.SetInfo(dirPath, info)
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) DelSharing(dirPath string) error {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(dirPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info.Shared = false
|
||||
return fi.SetInfo(dirPath, info)
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) GetSharing(dirPath string) (bool, bool) {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(dirPath)
|
||||
if err != nil {
|
||||
return false, false
|
||||
}
|
||||
return info.IsDir && info.Shared, true
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) ListSharings(prefix string) (map[string]bool, error) {
|
||||
infoStrs, err := fi.store.ListStringsByPrefixIn(prefix, InfoNs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := &FileInfo{}
|
||||
sharings := map[string]bool{}
|
||||
for itemPath, infoStr := range infoStrs {
|
||||
err = json.Unmarshal([]byte(infoStr), info)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list sharing error: %w", err)
|
||||
}
|
||||
if info.IsDir && info.Shared {
|
||||
sharings[itemPath] = true
|
||||
}
|
||||
}
|
||||
|
||||
return sharings, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) GetInfo(itemPath string) (*FileInfo, error) {
|
||||
infoStr, ok := fi.store.GetStringIn(InfoNs, itemPath)
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
info := &FileInfo{}
|
||||
err := json.Unmarshal([]byte(infoStr), info)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get file info: %w", err)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) GetInfos(itemPaths []string) (map[string]*FileInfo, error) {
|
||||
infos := map[string]*FileInfo{}
|
||||
for _, itemPath := range itemPaths {
|
||||
info, err := fi.GetInfo(itemPath)
|
||||
if err != nil {
|
||||
if !IsNotFound(err) {
|
||||
return nil, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
infos[itemPath] = info
|
||||
}
|
||||
|
||||
return infos, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) SetInfo(itemPath string, info *FileInfo) error {
|
||||
infoStr, err := json.Marshal(info)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set file info: %w", err)
|
||||
}
|
||||
|
||||
err = fi.store.SetStringIn(InfoNs, itemPath, string(infoStr))
|
||||
if err != nil {
|
||||
return fmt.Errorf("set file info: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) DelInfo(itemPath string) error {
|
||||
return fi.store.DelStringIn(InfoNs, itemPath)
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) SetSha1(itemPath, sign string) error {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(itemPath)
|
||||
if err != nil {
|
||||
if !IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
info = &FileInfo{
|
||||
IsDir: false,
|
||||
Shared: false,
|
||||
}
|
||||
}
|
||||
info.Sha1 = sign
|
||||
return fi.SetInfo(itemPath, info)
|
||||
}
|
133
src/db/fileinfostore/file_info_store_test.go
Normal file
133
src/db/fileinfostore/file_info_store_test.go
Normal file
|
@ -0,0 +1,133 @@
|
|||
package fileinfostore
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
|
||||
)
|
||||
|
||||
func TestUserStores(t *testing.T) {
|
||||
|
||||
testSharingMethods := func(t *testing.T, store IFileInfoStore) {
|
||||
dirPaths := []string{"admin/path1", "admin/path1/path2"}
|
||||
var err error
|
||||
|
||||
// add sharings
|
||||
for _, dirPath := range dirPaths {
|
||||
err = store.AddSharing(dirPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// list sharings
|
||||
prefix := "admin"
|
||||
sharingMap, err := store.ListSharings(prefix)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sharingDir := range dirPaths {
|
||||
if !sharingMap[sharingDir] {
|
||||
t.Fatalf("sharing(%s) not found", sharingDir)
|
||||
}
|
||||
mustTrue, exist := store.GetSharing(sharingDir)
|
||||
if !mustTrue || !exist {
|
||||
t.Fatalf("get sharing(%t %t) should exist", mustTrue, exist)
|
||||
}
|
||||
}
|
||||
|
||||
// del sharings
|
||||
for _, dirPath := range dirPaths {
|
||||
err = store.DelSharing(dirPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
sharingMap, err = store.ListSharings(prefix)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, dirPath := range dirPaths {
|
||||
if _, ok := sharingMap[dirPath]; ok {
|
||||
t.Fatalf("sharing(%s) should not exist", dirPath)
|
||||
}
|
||||
shared, exist := store.GetSharing(dirPath)
|
||||
if shared {
|
||||
t.Fatalf("get sharing(%t, %t) should not shared but exist", shared, exist)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testInfoMethods := func(t *testing.T, store IFileInfoStore) {
|
||||
pathInfos := map[string]*FileInfo{
|
||||
"admin/item": &FileInfo{
|
||||
Shared: false,
|
||||
IsDir: false,
|
||||
Sha1: "file",
|
||||
},
|
||||
"admin/dir": &FileInfo{
|
||||
Shared: true,
|
||||
IsDir: true,
|
||||
Sha1: "dir",
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
// add infos
|
||||
for itemPath, info := range pathInfos {
|
||||
err = store.SetInfo(itemPath, info)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// get infos
|
||||
for itemPath, expected := range pathInfos {
|
||||
info, err := store.GetInfo(itemPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Shared != expected.Shared || info.IsDir != expected.IsDir || info.Sha1 != expected.Sha1 {
|
||||
t.Fatalf("info not equaled (%v) (%v)", expected, info)
|
||||
}
|
||||
}
|
||||
|
||||
// del sharings
|
||||
for itemPath := range pathInfos {
|
||||
err = store.DelInfo(itemPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// get infos
|
||||
for itemPath := range pathInfos {
|
||||
_, err := store.GetInfo(itemPath)
|
||||
if !IsNotFound(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("testing FileInfoStore", func(t *testing.T) {
|
||||
rootPath, err := ioutil.TempDir("./", "quickshare_userstore_test_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(rootPath)
|
||||
|
||||
kvstore := boltdbpvd.New(rootPath, 1024)
|
||||
defer kvstore.Close()
|
||||
|
||||
store, err := NewFileInfoStore(kvstore)
|
||||
if err != nil {
|
||||
t.Fatal("fail to new kvstore", err)
|
||||
}
|
||||
|
||||
testSharingMethods(t, store)
|
||||
testInfoMethods(t, store)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue