seaweedfs/weed/filer/redis3/kv_directory_children.go

139 lines
3.3 KiB
Go
Raw Normal View History

2021-09-18 21:04:30 +00:00
package redis3
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/seaweedfs/seaweedfs/weed/glog"
2021-09-18 21:04:30 +00:00
)
2021-10-07 01:18:24 +00:00
const maxNameBatchSizeLimit = 1000000
2021-10-04 08:01:31 +00:00
2021-10-06 07:03:54 +00:00
func insertChild(ctx context.Context, redisStore *UniversalRedis3Store, key string, name string) error {
// lock and unlock
2021-10-08 04:13:31 +00:00
mutex := redisStore.redsync.NewMutex(key + "lock")
2021-10-06 07:03:54 +00:00
if err := mutex.Lock(); err != nil {
return fmt.Errorf("lock %s: %v", key, err)
}
defer func() {
mutex.Unlock()
}()
client := redisStore.Client
2021-09-18 21:04:30 +00:00
data, err := client.Get(ctx, key).Result()
if err != nil {
if err != redis.Nil {
return fmt.Errorf("read %s: %v", key, err)
}
}
2021-10-04 08:01:31 +00:00
store := newSkipListElementStore(key, client)
2021-10-07 01:18:24 +00:00
nameList := LoadItemList([]byte(data), key, client, store, maxNameBatchSizeLimit)
2021-10-04 08:01:31 +00:00
if err := nameList.WriteName(name); err != nil {
glog.Errorf("add %s %s: %v", key, name, err)
return err
}
2021-10-06 07:03:54 +00:00
2021-10-04 08:01:31 +00:00
if !nameList.HasChanges() {
return nil
}
if err := client.Set(ctx, key, nameList.ToBytes(), 0).Err(); err != nil {
return err
2021-09-18 21:04:30 +00:00
}
2021-10-04 08:01:31 +00:00
2021-09-18 21:04:30 +00:00
return nil
}
2021-10-06 07:03:54 +00:00
func removeChild(ctx context.Context, redisStore *UniversalRedis3Store, key string, name string) error {
// lock and unlock
2021-10-08 04:13:31 +00:00
mutex := redisStore.redsync.NewMutex(key + "lock")
2021-10-06 07:03:54 +00:00
if err := mutex.Lock(); err != nil {
return fmt.Errorf("lock %s: %v", key, err)
}
defer mutex.Unlock()
client := redisStore.Client
2021-09-18 21:04:30 +00:00
data, err := client.Get(ctx, key).Result()
if err != nil {
if err != redis.Nil {
return fmt.Errorf("read %s: %v", key, err)
}
}
2021-10-04 08:01:31 +00:00
store := newSkipListElementStore(key, client)
2021-10-07 01:18:24 +00:00
nameList := LoadItemList([]byte(data), key, client, store, maxNameBatchSizeLimit)
2021-10-04 08:01:31 +00:00
if err := nameList.DeleteName(name); err != nil {
return err
}
if !nameList.HasChanges() {
return nil
}
if err := client.Set(ctx, key, nameList.ToBytes(), 0).Err(); err != nil {
return err
2021-09-18 21:04:30 +00:00
}
2021-10-04 08:01:31 +00:00
2021-09-18 21:04:30 +00:00
return nil
}
2021-10-06 07:03:54 +00:00
func removeChildren(ctx context.Context, redisStore *UniversalRedis3Store, key string, onDeleteFn func(name string) error) error {
// lock and unlock
2021-10-08 04:13:31 +00:00
mutex := redisStore.redsync.NewMutex(key + "lock")
2021-10-06 07:03:54 +00:00
if err := mutex.Lock(); err != nil {
return fmt.Errorf("lock %s: %v", key, err)
}
defer mutex.Unlock()
2021-10-04 08:01:31 +00:00
2021-10-06 07:03:54 +00:00
client := redisStore.Client
2021-10-04 08:01:31 +00:00
data, err := client.Get(ctx, key).Result()
if err != nil {
if err != redis.Nil {
return fmt.Errorf("read %s: %v", key, err)
}
}
store := newSkipListElementStore(key, client)
2021-10-07 01:18:24 +00:00
nameList := LoadItemList([]byte(data), key, client, store, maxNameBatchSizeLimit)
2021-10-04 08:01:31 +00:00
if err = nameList.ListNames("", func(name string) bool {
if err := onDeleteFn(name); err != nil {
glog.Errorf("delete %s child %s: %v", key, name, err)
return false
}
return true
}); err != nil {
return err
}
if err = nameList.RemoteAllListElement(); err != nil {
return err
}
2021-10-08 04:13:31 +00:00
2021-09-18 21:04:30 +00:00
return nil
2021-10-04 08:01:31 +00:00
2021-09-18 21:04:30 +00:00
}
2021-10-06 07:03:54 +00:00
func listChildren(ctx context.Context, redisStore *UniversalRedis3Store, key string, startFileName string, eachFn func(name string) bool) error {
client := redisStore.Client
2021-10-04 08:01:31 +00:00
data, err := client.Get(ctx, key).Result()
if err != nil {
if err != redis.Nil {
return fmt.Errorf("read %s: %v", key, err)
}
}
store := newSkipListElementStore(key, client)
2021-10-07 01:18:24 +00:00
nameList := LoadItemList([]byte(data), key, client, store, maxNameBatchSizeLimit)
2021-10-04 08:01:31 +00:00
if err = nameList.ListNames(startFileName, func(name string) bool {
return eachFn(name)
}); err != nil {
return err
}
2021-09-18 21:04:30 +00:00
return nil
2021-10-04 08:01:31 +00:00
2021-09-18 21:04:30 +00:00
}