2018-08-15 15:01:38 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2019-03-16 06:55:34 +08:00
|
|
|
"context"
|
2018-08-15 15:01:38 +08:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-01-09 10:48:19 +08:00
|
|
|
"time"
|
2020-03-06 02:35:21 +08:00
|
|
|
|
2021-01-11 18:30:19 +08:00
|
|
|
"github.com/go-redis/redis/v8"
|
2020-03-06 02:35:21 +08:00
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2020-03-06 02:35:21 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-08 09:01:39 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-23 15:01:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-08-15 15:01:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DIR_LIST_MARKER = "\x00"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UniversalRedisStore struct {
|
|
|
|
Client redis.UniversalClient
|
|
|
|
}
|
|
|
|
|
2019-03-31 14:09:16 +08:00
|
|
|
func (store *UniversalRedisStore) BeginTransaction(ctx context.Context) (context.Context, error) {
|
2019-03-31 14:08:29 +08:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2019-03-31 14:09:16 +08:00
|
|
|
func (store *UniversalRedisStore) CommitTransaction(ctx context.Context) error {
|
2019-03-31 14:08:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-31 14:09:16 +08:00
|
|
|
func (store *UniversalRedisStore) RollbackTransaction(ctx context.Context) error {
|
2019-03-31 14:08:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *UniversalRedisStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-08-15 15:01:38 +08:00
|
|
|
|
|
|
|
value, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 02:00:20 +08:00
|
|
|
if len(entry.Chunks) > 50 {
|
|
|
|
value = util.MaybeGzipData(value)
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:30:19 +08:00
|
|
|
_, err = store.Client.Set(ctx, string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Result()
|
2018-08-15 15:01:38 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, name := entry.FullPath.DirAndName()
|
|
|
|
if name != "" {
|
2021-01-11 18:30:19 +08:00
|
|
|
_, err = store.Client.SAdd(ctx, genDirectoryListKey(dir), name).Result()
|
2018-08-15 15:01:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *UniversalRedisStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-08-15 15:01:38 +08:00
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
return store.InsertEntry(ctx, entry)
|
2018-08-15 15:01:38 +08:00
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *UniversalRedisStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
|
2018-08-15 15:01:38 +08:00
|
|
|
|
2021-01-11 18:30:19 +08:00
|
|
|
data, err := store.Client.Get(ctx, string(fullpath)).Result()
|
2018-08-15 15:01:38 +08:00
|
|
|
if err == redis.Nil {
|
2020-03-08 09:01:39 +08:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2018-08-15 15:01:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-08-23 09:19:26 +08:00
|
|
|
return nil, fmt.Errorf("get %s : %v", fullpath, err)
|
2018-08-15 15:01:38 +08:00
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
entry = &filer.Entry{
|
2018-08-15 15:01:38 +08:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
2020-09-04 02:00:20 +08:00
|
|
|
err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData([]byte(data)))
|
2018-08-15 15:01:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
func (store *UniversalRedisStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
|
2018-08-15 15:01:38 +08:00
|
|
|
|
2021-01-11 18:30:19 +08:00
|
|
|
_, err = store.Client.Del(ctx, string(fullpath)).Result()
|
2018-08-15 15:01:38 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
if name != "" {
|
2021-01-11 18:30:19 +08:00
|
|
|
_, err = store.Client.SRem(ctx, genDirectoryListKey(dir), name).Result()
|
2018-08-15 15:01:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 23:23:20 +08:00
|
|
|
func (store *UniversalRedisStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
|
2019-12-13 16:23:05 +08:00
|
|
|
|
2021-01-11 18:30:19 +08:00
|
|
|
members, err := store.Client.SMembers(ctx, genDirectoryListKey(string(fullpath))).Result()
|
2019-12-13 16:23:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete folder %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileName := range members {
|
2020-03-23 15:01:34 +08:00
|
|
|
path := util.NewFullPath(string(fullpath), fileName)
|
2021-01-11 18:30:19 +08:00
|
|
|
_, err = store.Client.Del(ctx, string(path)).Result()
|
2019-12-13 16:23:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
|
|
|
|
}
|
2021-09-19 05:05:16 +08:00
|
|
|
// not efficient, but need to remove if it is a directory
|
|
|
|
store.Client.Del(ctx, genDirectoryListKey(string(path)))
|
2019-12-13 16:23:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
func (store *UniversalRedisStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
|
|
|
return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
|
2020-08-06 01:19:16 +08:00
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
2018-08-15 15:01:38 +08:00
|
|
|
|
2021-01-15 15:10:37 +08:00
|
|
|
dirListKey := genDirectoryListKey(string(dirPath))
|
2021-01-11 18:30:19 +08:00
|
|
|
members, err := store.Client.SMembers(ctx, dirListKey).Result()
|
2018-08-15 15:01:38 +08:00
|
|
|
if err != nil {
|
2021-01-16 15:56:24 +08:00
|
|
|
return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
|
2018-08-15 15:01:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip
|
|
|
|
if startFileName != "" {
|
|
|
|
var t []string
|
|
|
|
for _, m := range members {
|
|
|
|
if strings.Compare(m, startFileName) >= 0 {
|
|
|
|
if m == startFileName {
|
2021-01-15 14:21:31 +08:00
|
|
|
if includeStartFile {
|
2018-08-15 15:01:38 +08:00
|
|
|
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
|
2021-01-16 15:56:24 +08:00
|
|
|
if limit < int64(len(members)) {
|
2018-08-15 15:01:38 +08:00
|
|
|
members = members[:limit]
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch entry meta
|
|
|
|
for _, fileName := range members {
|
2021-01-15 15:10:37 +08:00
|
|
|
path := util.NewFullPath(string(dirPath), fileName)
|
2019-03-16 06:55:34 +08:00
|
|
|
entry, err := store.FindEntry(ctx, path)
|
2021-01-16 15:56:24 +08:00
|
|
|
lastFileName = fileName
|
2018-08-15 15:01:38 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("list %s : %v", path, err)
|
2021-01-15 11:56:14 +08:00
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-15 15:01:38 +08:00
|
|
|
} else {
|
2020-03-06 02:35:21 +08:00
|
|
|
if entry.TtlSec > 0 {
|
|
|
|
if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
|
2021-01-11 18:30:19 +08:00
|
|
|
store.Client.Del(ctx, string(path)).Result()
|
|
|
|
store.Client.SRem(ctx, dirListKey, fileName).Result()
|
2020-03-06 02:35:21 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 15:56:24 +08:00
|
|
|
if !eachEntryFunc(entry) {
|
|
|
|
break
|
|
|
|
}
|
2018-08-15 15:01:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
return lastFileName, err
|
2018-08-15 15:01:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func genDirectoryListKey(dir string) (dirList string) {
|
|
|
|
return dir + DIR_LIST_MARKER
|
|
|
|
}
|
2020-03-15 11:30:26 +08:00
|
|
|
|
|
|
|
func (store *UniversalRedisStore) Shutdown() {
|
|
|
|
store.Client.Close()
|
|
|
|
}
|