2018-08-15 07:01:38 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2019-03-15 22:55:34 +00:00
|
|
|
"context"
|
2018-08-15 07:01:38 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-01-09 02:48:19 +00:00
|
|
|
"time"
|
2020-03-05 18:35:21 +00:00
|
|
|
|
|
|
|
"github.com/go-redis/redis"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-08 01:01:39 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-23 07:01:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-08-15 07:01:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DIR_LIST_MARKER = "\x00"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UniversalRedisStore struct {
|
|
|
|
Client redis.UniversalClient
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *UniversalRedisStore) BeginTransaction(ctx context.Context) (context.Context, error) {
|
2019-03-31 06:08:29 +00:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *UniversalRedisStore) CommitTransaction(ctx context.Context) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *UniversalRedisStore) RollbackTransaction(ctx context.Context) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:55:34 +00:00
|
|
|
func (store *UniversalRedisStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
|
2018-08-15 07:01:38 +00:00
|
|
|
|
|
|
|
value, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
|
|
|
|
}
|
|
|
|
|
2019-01-17 01:17:19 +00:00
|
|
|
_, err = store.Client.Set(string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Result()
|
2018-08-15 07:01:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, name := entry.FullPath.DirAndName()
|
|
|
|
if name != "" {
|
|
|
|
_, err = store.Client.SAdd(genDirectoryListKey(dir), name).Result()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:55:34 +00:00
|
|
|
func (store *UniversalRedisStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
|
2018-08-15 07:01:38 +00:00
|
|
|
|
2019-03-15 22:55:34 +00:00
|
|
|
return store.InsertEntry(ctx, entry)
|
2018-08-15 07:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *UniversalRedisStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer2.Entry, err error) {
|
2018-08-15 07:01:38 +00:00
|
|
|
|
|
|
|
data, err := store.Client.Get(string(fullpath)).Result()
|
|
|
|
if err == redis.Nil {
|
2020-03-08 01:01:39 +00:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2018-08-15 07:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-08-23 01:19:26 +00:00
|
|
|
return nil, fmt.Errorf("get %s : %v", fullpath, err)
|
2018-08-15 07:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry = &filer2.Entry{
|
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
|
|
|
err = entry.DecodeAttributesAndChunks([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *UniversalRedisStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
|
2018-08-15 07:01:38 +00:00
|
|
|
|
|
|
|
_, err = store.Client.Del(string(fullpath)).Result()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
if name != "" {
|
|
|
|
_, err = store.Client.SRem(genDirectoryListKey(dir), name).Result()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *UniversalRedisStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
|
2019-12-13 08:23:05 +00:00
|
|
|
|
|
|
|
members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).Result()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete folder %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileName := range members {
|
2020-03-23 07:01:34 +00:00
|
|
|
path := util.NewFullPath(string(fullpath), fileName)
|
2019-12-13 08:23:05 +00:00
|
|
|
_, err = store.Client.Del(string(path)).Result()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
|
2018-08-15 07:01:38 +00:00
|
|
|
limit int) (entries []*filer2.Entry, err error) {
|
|
|
|
|
2020-03-05 18:35:21 +00:00
|
|
|
dirListKey := genDirectoryListKey(string(fullpath))
|
|
|
|
members, err := store.Client.SMembers(dirListKey).Result()
|
2018-08-15 07:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("list %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip
|
|
|
|
if startFileName != "" {
|
|
|
|
var t []string
|
|
|
|
for _, m := range members {
|
|
|
|
if strings.Compare(m, startFileName) >= 0 {
|
|
|
|
if m == startFileName {
|
|
|
|
if inclusive {
|
|
|
|
t = append(t, m)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t = append(t, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
members = t
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort
|
|
|
|
sort.Slice(members, func(i, j int) bool {
|
|
|
|
return strings.Compare(members[i], members[j]) < 0
|
|
|
|
})
|
|
|
|
|
|
|
|
// limit
|
|
|
|
if limit < len(members) {
|
|
|
|
members = members[:limit]
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch entry meta
|
|
|
|
for _, fileName := range members {
|
2020-03-23 07:01:34 +00:00
|
|
|
path := util.NewFullPath(string(fullpath), fileName)
|
2019-03-15 22:55:34 +00:00
|
|
|
entry, err := store.FindEntry(ctx, path)
|
2018-08-15 07:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("list %s : %v", path, err)
|
|
|
|
} else {
|
2020-03-05 18:35:21 +00:00
|
|
|
if entry.TtlSec > 0 {
|
|
|
|
if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
|
|
|
|
store.Client.Del(string(path)).Result()
|
|
|
|
store.Client.SRem(dirListKey, fileName).Result()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 07:01:38 +00:00
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func genDirectoryListKey(dir string) (dirList string) {
|
|
|
|
return dir + DIR_LIST_MARKER
|
|
|
|
}
|
2020-03-15 03:30:26 +00:00
|
|
|
|
|
|
|
func (store *UniversalRedisStore) Shutdown() {
|
|
|
|
store.Client.Close()
|
|
|
|
}
|