2020-04-12 09:50:41 +00:00
|
|
|
package redis2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-01-11 10:30:19 +00:00
|
|
|
"github.com/go-redis/redis/v8"
|
2020-04-12 09:50:41 +00:00
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2020-04-12 09:50:41 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DIR_LIST_MARKER = "\x00"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UniversalRedis2Store struct {
|
2020-12-22 10:34:08 +00:00
|
|
|
Client redis.UniversalClient
|
2020-12-22 10:33:31 +00:00
|
|
|
superLargeDirectoryHash map[string]bool
|
2020-12-22 10:26:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:33:31 +00:00
|
|
|
func (store *UniversalRedis2Store) isSuperLargeDirectory(dir string) (isSuperLargeDirectory bool) {
|
|
|
|
_, isSuperLargeDirectory = store.superLargeDirectoryHash[dir]
|
2020-12-22 10:26:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *UniversalRedis2Store) loadSuperLargeDirectories(superLargeDirectories []string) {
|
|
|
|
// set directory hash
|
2020-12-22 10:33:31 +00:00
|
|
|
store.superLargeDirectoryHash = make(map[string]bool)
|
2020-12-22 10:26:05 +00:00
|
|
|
for _, dir := range superLargeDirectories {
|
2020-12-22 10:33:31 +00:00
|
|
|
store.superLargeDirectoryHash[dir] = true
|
2020-12-22 10:26:05 +00:00
|
|
|
}
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *UniversalRedis2Store) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
func (store *UniversalRedis2Store) CommitTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (store *UniversalRedis2Store) RollbackTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *UniversalRedis2Store) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2020-04-12 09:50:41 +00:00
|
|
|
|
|
|
|
value, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
|
|
|
|
}
|
|
|
|
|
2022-05-01 17:28:55 +00:00
|
|
|
if len(entry.Chunks) > filer.CountEntryChunksForGzip {
|
2020-09-03 18:00:20 +00:00
|
|
|
value = util.MaybeGzipData(value)
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:30:19 +00:00
|
|
|
if err = store.Client.Set(ctx, string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Err(); err != nil {
|
2020-04-12 09:50:41 +00:00
|
|
|
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, name := entry.FullPath.DirAndName()
|
2020-12-22 10:33:31 +00:00
|
|
|
if store.isSuperLargeDirectory(dir) {
|
2020-12-22 10:26:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-12 09:50:41 +00:00
|
|
|
if name != "" {
|
2021-01-11 10:30:19 +00:00
|
|
|
if err = store.Client.ZAddNX(ctx, genDirectoryListKey(dir), &redis.Z{Score: 0, Member: name}).Err(); err != nil {
|
2020-04-12 09:50:41 +00:00
|
|
|
return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *UniversalRedis2Store) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2020-04-12 09:50:41 +00:00
|
|
|
|
|
|
|
return store.InsertEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *UniversalRedis2Store) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
|
2020-04-12 09:50:41 +00:00
|
|
|
|
2021-01-11 10:30:19 +00:00
|
|
|
data, err := store.Client.Get(ctx, string(fullpath)).Result()
|
2020-04-12 09:50:41 +00:00
|
|
|
if err == redis.Nil {
|
|
|
|
return nil, filer_pb.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
entry = &filer.Entry{
|
2020-04-12 09:50:41 +00:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData([]byte(data)))
|
2020-04-12 09:50:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *UniversalRedis2Store) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
|
|
|
|
|
2021-01-11 10:30:19 +00:00
|
|
|
_, err = store.Client.Del(ctx, genDirectoryListKey(string(fullpath))).Result()
|
2020-09-04 02:17:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete dir list %s : %v", fullpath, err)
|
|
|
|
}
|
2020-04-12 09:50:41 +00:00
|
|
|
|
2021-01-11 10:30:19 +00:00
|
|
|
_, err = store.Client.Del(ctx, string(fullpath)).Result()
|
2020-04-12 09:50:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
2020-12-22 10:33:31 +00:00
|
|
|
if store.isSuperLargeDirectory(dir) {
|
2020-12-22 10:26:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-12 09:50:41 +00:00
|
|
|
if name != "" {
|
2021-01-11 10:30:19 +00:00
|
|
|
_, err = store.Client.ZRem(ctx, genDirectoryListKey(dir), name).Result()
|
2020-04-12 09:50:41 +00:00
|
|
|
if err != nil {
|
2020-09-04 02:17:39 +00:00
|
|
|
return fmt.Errorf("DeleteEntry %s in parent dir: %v", fullpath, err)
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:23:20 +00:00
|
|
|
func (store *UniversalRedis2Store) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
|
2020-04-12 09:50:41 +00:00
|
|
|
|
2020-12-22 10:33:31 +00:00
|
|
|
if store.isSuperLargeDirectory(string(fullpath)) {
|
2020-12-22 10:26:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-28 06:46:25 +00:00
|
|
|
members, err := store.Client.ZRangeByLex(ctx, genDirectoryListKey(string(fullpath)), &redis.ZRangeBy{
|
2021-11-03 06:39:16 +00:00
|
|
|
Min: "-",
|
|
|
|
Max: "+",
|
2021-10-28 06:46:25 +00:00
|
|
|
}).Result()
|
2020-04-12 09:50:41 +00:00
|
|
|
if err != nil {
|
2020-09-04 02:17:39 +00:00
|
|
|
return fmt.Errorf("DeleteFolderChildren %s : %v", fullpath, err)
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileName := range members {
|
|
|
|
path := util.NewFullPath(string(fullpath), fileName)
|
2021-01-11 10:30:19 +00:00
|
|
|
_, err = store.Client.Del(ctx, string(path)).Result()
|
2020-04-12 09:50:41 +00:00
|
|
|
if err != nil {
|
2020-09-04 02:17:39 +00:00
|
|
|
return fmt.Errorf("DeleteFolderChildren %s in parent dir: %v", fullpath, err)
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
2021-09-18 21:05:16 +00:00
|
|
|
// not efficient, but need to remove if it is a directory
|
|
|
|
store.Client.Del(ctx, genDirectoryListKey(string(path)))
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
func (store *UniversalRedis2Store) 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-05 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
func (store *UniversalRedis2Store) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
2020-04-12 09:50:41 +00:00
|
|
|
|
2021-01-15 06:21:31 +00:00
|
|
|
dirListKey := genDirectoryListKey(string(dirPath))
|
2021-10-28 06:46:25 +00:00
|
|
|
|
|
|
|
min := "-"
|
2020-04-12 09:50:41 +00:00
|
|
|
if startFileName != "" {
|
2021-10-28 06:46:25 +00:00
|
|
|
if includeStartFile {
|
|
|
|
min = "[" + startFileName
|
|
|
|
} else {
|
|
|
|
min = "(" + startFileName
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 06:46:25 +00:00
|
|
|
|
|
|
|
members, err := store.Client.ZRangeByLex(ctx, dirListKey, &redis.ZRangeBy{
|
|
|
|
Min: min,
|
|
|
|
Max: "+",
|
|
|
|
Offset: 0,
|
|
|
|
Count: limit,
|
|
|
|
}).Result()
|
2020-04-12 09:50:41 +00:00
|
|
|
if err != nil {
|
2021-01-16 07:56:24 +00:00
|
|
|
return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch entry meta
|
|
|
|
for _, fileName := range members {
|
2021-01-15 06:21:31 +00:00
|
|
|
path := util.NewFullPath(string(dirPath), fileName)
|
2020-04-12 09:50:41 +00:00
|
|
|
entry, err := store.FindEntry(ctx, path)
|
2021-01-16 07:56:24 +00:00
|
|
|
lastFileName = fileName
|
2020-04-12 09:50:41 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("list %s : %v", path, err)
|
2021-01-15 03:56:14 +00:00
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-12 09:50:41 +00:00
|
|
|
} else {
|
|
|
|
if entry.TtlSec > 0 {
|
|
|
|
if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
|
2021-01-11 10:30:19 +00:00
|
|
|
store.Client.Del(ctx, string(path)).Result()
|
|
|
|
store.Client.ZRem(ctx, dirListKey, fileName).Result()
|
2020-04-12 09:50:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 07:56:24 +00:00
|
|
|
if !eachEntryFunc(entry) {
|
|
|
|
break
|
|
|
|
}
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
return lastFileName, err
|
2020-04-12 09:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genDirectoryListKey(dir string) (dirList string) {
|
|
|
|
return dir + DIR_LIST_MARKER
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *UniversalRedis2Store) Shutdown() {
|
|
|
|
store.Client.Close()
|
|
|
|
}
|