feat(filestore): support file info methods

This commit is contained in:
hexxa 2021-09-04 17:49:33 +08:00 committed by Hexxa
parent 25e1ba9ba9
commit 0e7f39b8cc
5 changed files with 200 additions and 15 deletions

View file

@ -413,3 +413,22 @@ func (bp *BoltPvd) ListStringsIn(ns string) (map[string]string, error) {
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
}

View file

@ -38,6 +38,7 @@ type IKVStore interface {
GetStringIn(ns, key string) (string, bool)
SetStringIn(ns, key, val string) error
ListStringsIn(ns string) (map[string]string, error)
ListStringsByPrefixIn(prefix, ns string) (map[string]string, error)
TryLock(key string) error
Unlock(key string) error
}

View file

@ -171,6 +171,21 @@ func TestKVStoreProviders(t *testing.T) {
} else if 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)
if err != nil {
t.Errorf("there should be no error %v", err)