feat(filestore): support file info methods
This commit is contained in:
parent
25e1ba9ba9
commit
0e7f39b8cc
5 changed files with 200 additions and 15 deletions
|
@ -1,20 +1,42 @@
|
||||||
package fileinfostore
|
package fileinfostore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ihexxa/quickshare/src/kvstore"
|
"github.com/ihexxa/quickshare/src/kvstore"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
InitNs = "Init"
|
InitNs = "Init"
|
||||||
SharingNs = "sharing"
|
InfoNs = "sharing"
|
||||||
InitTimeKey = "initTime"
|
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 {
|
type IFileInfoStore interface {
|
||||||
AddSharing(dirPath string) error
|
AddSharing(dirPath string) error
|
||||||
DelSharing(dirPath string) error
|
DelSharing(dirPath string) error
|
||||||
GetSharing(dirPath string) (bool, bool)
|
GetSharing(dirPath string) (bool, bool)
|
||||||
ListSharings(prefix string) (map[string]bool, error)
|
ListSharings(prefix string) (map[string]bool, error)
|
||||||
|
GetInfo(itemPath string) (*FileInfo, error)
|
||||||
|
SetInfo(itemPath string, info *FileInfo) error
|
||||||
|
DelInfo(itemPath string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileInfoStore struct {
|
type FileInfoStore struct {
|
||||||
|
@ -27,7 +49,7 @@ func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
|
||||||
var err error
|
var err error
|
||||||
for _, nsName := range []string{
|
for _, nsName := range []string{
|
||||||
InitNs,
|
InitNs,
|
||||||
SharingNs,
|
InfoNs,
|
||||||
} {
|
} {
|
||||||
if err = store.AddNamespace(nsName); err != nil {
|
if err = store.AddNamespace(nsName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -35,23 +57,96 @@ func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := store.SetStringIn(InitNs, InitTimeKey, fmt.Sprintf("%d", time.Now().Unix()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &FileInfoStore{
|
return &FileInfoStore{
|
||||||
store: store,
|
store: store,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *FileInfoStore) AddSharing(dirPath string) error {
|
func (fi *FileInfoStore) AddSharing(dirPath string) error {
|
||||||
return us.store.SetBoolIn(SharingNs, dirPath, true)
|
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 (us *FileInfoStore) DelSharing(dirPath string) error {
|
func (fi *FileInfoStore) DelSharing(dirPath string) error {
|
||||||
return us.store.DelBoolIn(SharingNs, dirPath)
|
info, err := fi.GetInfo(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
info.Shared = false
|
||||||
|
return fi.SetInfo(dirPath, info)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *FileInfoStore) GetSharing(dirPath string) (bool, bool) {
|
func (fi *FileInfoStore) GetSharing(dirPath string) (bool, bool) {
|
||||||
return us.store.GetBoolIn(SharingNs, dirPath)
|
info, err := fi.GetInfo(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
// TODO: error is ignored
|
||||||
|
return false, false
|
||||||
|
}
|
||||||
|
return info.IsDir && info.Shared, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *FileInfoStore) ListSharings(prefix string) (map[string]bool, error) {
|
func (fi *FileInfoStore) ListSharings(prefix string) (map[string]bool, error) {
|
||||||
return us.store.ListBoolsByPrefixIn(prefix, SharingNs)
|
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) 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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,8 @@ func TestUserStores(t *testing.T) {
|
||||||
testSharingMethods := func(t *testing.T, store IFileInfoStore) {
|
testSharingMethods := func(t *testing.T, store IFileInfoStore) {
|
||||||
dirPaths := []string{"admin/path1", "admin/path1/path2"}
|
dirPaths := []string{"admin/path1", "admin/path1/path2"}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
// add sharings
|
||||||
for _, dirPath := range dirPaths {
|
for _, dirPath := range dirPaths {
|
||||||
err = store.AddSharing(dirPath)
|
err = store.AddSharing(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -20,12 +22,12 @@ func TestUserStores(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// list sharings
|
||||||
prefix := "admin"
|
prefix := "admin"
|
||||||
sharingMap, err := store.ListSharings(prefix)
|
sharingMap, err := store.ListSharings(prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sharingDir := range dirPaths {
|
for _, sharingDir := range dirPaths {
|
||||||
if !sharingMap[sharingDir] {
|
if !sharingMap[sharingDir] {
|
||||||
t.Fatalf("sharing(%s) not found", sharingDir)
|
t.Fatalf("sharing(%s) not found", sharingDir)
|
||||||
|
@ -36,6 +38,7 @@ func TestUserStores(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// del sharings
|
||||||
for _, dirPath := range dirPaths {
|
for _, dirPath := range dirPaths {
|
||||||
err = store.DelSharing(dirPath)
|
err = store.DelSharing(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -48,12 +51,63 @@ func TestUserStores(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, dirPath := range dirPaths {
|
for _, dirPath := range dirPaths {
|
||||||
if sharingMap[dirPath] {
|
if _, ok := sharingMap[dirPath]; ok {
|
||||||
t.Fatalf("sharing(%s) should not exist", dirPath)
|
t.Fatalf("sharing(%s) should not exist", dirPath)
|
||||||
}
|
}
|
||||||
_, exist := store.GetSharing(dirPath)
|
shared, exist := store.GetSharing(dirPath)
|
||||||
if exist {
|
if shared {
|
||||||
t.Fatalf("get sharing(%t) should not exit", exist)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,5 +128,6 @@ func TestUserStores(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
testSharingMethods(t, store)
|
testSharingMethods(t, store)
|
||||||
|
testInfoMethods(t, store)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -413,3 +413,22 @@ func (bp *BoltPvd) ListStringsIn(ns string) (map[string]string, error) {
|
||||||
|
|
||||||
return kv, err
|
return kv, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bp *BoltPvd) ListStringsByPrefixIn(prefix, ns string) (map[string]string, error) {
|
||||||
|
results := map[string]string{}
|
||||||
|
|
||||||
|
err := bp.db.View(func(tx *bolt.Tx) error {
|
||||||
|
b := tx.Bucket([]byte(ns)).Cursor()
|
||||||
|
if b == nil {
|
||||||
|
return ErrBucketNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
prefixBytes := []byte(prefix)
|
||||||
|
for k, v := b.Seek(prefixBytes); k != nil && bytes.HasPrefix(k, prefixBytes); k, v = b.Next() {
|
||||||
|
results[string(k)] = string(v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return results, err
|
||||||
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ type IKVStore interface {
|
||||||
GetStringIn(ns, key string) (string, bool)
|
GetStringIn(ns, key string) (string, bool)
|
||||||
SetStringIn(ns, key, val string) error
|
SetStringIn(ns, key, val string) error
|
||||||
ListStringsIn(ns string) (map[string]string, error)
|
ListStringsIn(ns string) (map[string]string, error)
|
||||||
|
ListStringsByPrefixIn(prefix, ns string) (map[string]string, error)
|
||||||
TryLock(key string) error
|
TryLock(key string) error
|
||||||
Unlock(key string) error
|
Unlock(key string) error
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,6 +171,21 @@ func TestKVStoreProviders(t *testing.T) {
|
||||||
} else if stringVGot != stringV {
|
} else if stringVGot != stringV {
|
||||||
t.Error(fmt.Sprintln("value not equal", stringVGot, stringV))
|
t.Error(fmt.Sprintln("value not equal", stringVGot, stringV))
|
||||||
}
|
}
|
||||||
|
err = store.SetStringIn(ns, key2, stringV)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("there should be no error %v", err)
|
||||||
|
}
|
||||||
|
kvs, err := store.ListStringsByPrefixIn("key", ns)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("there should be no error %v", err)
|
||||||
|
}
|
||||||
|
if kvs[key] != stringV {
|
||||||
|
t.Errorf("list str key not found")
|
||||||
|
}
|
||||||
|
if kvs[key2] != stringV {
|
||||||
|
t.Errorf("list str key not found")
|
||||||
|
}
|
||||||
|
|
||||||
err = store.DelStringIn(ns, key)
|
err = store.DelStringIn(ns, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("there should be no error %v", err)
|
t.Errorf("there should be no error %v", err)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue