2020-12-22 14:57:13 +08:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2021-10-08 12:12:57 +08:00
|
|
|
"math"
|
2020-12-22 14:57:13 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-05-25 16:10:49 +08:00
|
|
|
_ = FilerStore(&FilerStorePathTranslator{})
|
2020-12-22 14:57:13 +08:00
|
|
|
)
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
type FilerStorePathTranslator struct {
|
2020-12-22 14:57:13 +08:00
|
|
|
actualStore FilerStore
|
|
|
|
storeRoot string
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func NewFilerStorePathTranslator(storeRoot string, store FilerStore) *FilerStorePathTranslator {
|
|
|
|
if innerStore, ok := store.(*FilerStorePathTranslator); ok {
|
2020-12-22 14:57:13 +08:00
|
|
|
return innerStore
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(storeRoot, "/") {
|
|
|
|
storeRoot += "/"
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
return &FilerStorePathTranslator{
|
2020-12-22 14:57:13 +08:00
|
|
|
actualStore: store,
|
|
|
|
storeRoot: storeRoot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) translatePath(fp util.FullPath) (newPath util.FullPath) {
|
2020-12-22 15:37:43 +08:00
|
|
|
newPath = fp
|
|
|
|
if t.storeRoot == "/" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newPath = fp[len(t.storeRoot)-1:]
|
|
|
|
if newPath == "" {
|
|
|
|
newPath = "/"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) changeEntryPath(entry *Entry) (previousPath util.FullPath) {
|
2020-12-22 14:57:13 +08:00
|
|
|
previousPath = entry.FullPath
|
|
|
|
if t.storeRoot == "/" {
|
|
|
|
return
|
|
|
|
}
|
2020-12-22 15:37:43 +08:00
|
|
|
entry.FullPath = t.translatePath(previousPath)
|
2020-12-22 14:57:13 +08:00
|
|
|
return
|
|
|
|
}
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) recoverEntryPath(entry *Entry, previousPath util.FullPath) {
|
2020-12-22 14:57:13 +08:00
|
|
|
entry.FullPath = previousPath
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) GetName() string {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.GetName()
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) Initialize(configuration util.Configuration, prefix string) error {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.Initialize(configuration, prefix)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) InsertEntry(ctx context.Context, entry *Entry) error {
|
2020-12-22 14:57:13 +08:00
|
|
|
previousPath := t.changeEntryPath(entry)
|
|
|
|
defer t.recoverEntryPath(entry, previousPath)
|
|
|
|
|
|
|
|
return t.actualStore.InsertEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) UpdateEntry(ctx context.Context, entry *Entry) error {
|
2020-12-22 14:57:13 +08:00
|
|
|
previousPath := t.changeEntryPath(entry)
|
|
|
|
defer t.recoverEntryPath(entry, previousPath)
|
|
|
|
|
|
|
|
return t.actualStore.UpdateEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) FindEntry(ctx context.Context, fp util.FullPath) (entry *Entry, err error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
if t.storeRoot == "/" {
|
|
|
|
return t.actualStore.FindEntry(ctx, fp)
|
|
|
|
}
|
2020-12-22 15:37:43 +08:00
|
|
|
newFullPath := t.translatePath(fp)
|
2020-12-22 14:57:13 +08:00
|
|
|
entry, err = t.actualStore.FindEntry(ctx, newFullPath)
|
|
|
|
if err == nil {
|
|
|
|
entry.FullPath = fp[:len(t.storeRoot)-1] + entry.FullPath
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
|
2020-12-22 15:37:43 +08:00
|
|
|
newFullPath := t.translatePath(fp)
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.DeleteEntry(ctx, newFullPath)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) DeleteOneEntry(ctx context.Context, existingEntry *Entry) (err error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
|
|
|
|
previousPath := t.changeEntryPath(existingEntry)
|
|
|
|
defer t.recoverEntryPath(existingEntry, previousPath)
|
|
|
|
|
|
|
|
return t.actualStore.DeleteEntry(ctx, existingEntry.FullPath)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
|
2020-12-22 15:37:43 +08:00
|
|
|
newFullPath := t.translatePath(fp)
|
2020-12-22 14:57:13 +08:00
|
|
|
|
2021-07-22 23:23:20 +08:00
|
|
|
return t.actualStore.DeleteFolderChildren(ctx, newFullPath)
|
2020-12-22 14:57:13 +08:00
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc ListEachEntryFunc) (string, error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
|
2020-12-22 15:37:43 +08:00
|
|
|
newFullPath := t.translatePath(dirPath)
|
2020-12-22 14:57:13 +08:00
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
return t.actualStore.ListDirectoryEntries(ctx, newFullPath, startFileName, includeStartFile, limit, func(entry *Entry) bool {
|
2020-12-22 14:57:13 +08:00
|
|
|
entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
|
2021-01-16 15:56:24 +08:00
|
|
|
return eachEntryFunc(entry)
|
|
|
|
})
|
2020-12-22 14:57:13 +08:00
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (string, error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
|
2020-12-22 15:37:43 +08:00
|
|
|
newFullPath := t.translatePath(dirPath)
|
2020-12-22 14:57:13 +08:00
|
|
|
|
2021-10-08 12:12:57 +08:00
|
|
|
if limit > math.MaxInt32-1 {
|
|
|
|
limit = math.MaxInt32 - 1
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
return t.actualStore.ListDirectoryPrefixedEntries(ctx, newFullPath, startFileName, includeStartFile, limit, prefix, func(entry *Entry) bool {
|
2020-12-22 14:57:13 +08:00
|
|
|
entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
|
2021-01-16 15:56:24 +08:00
|
|
|
return eachEntryFunc(entry)
|
|
|
|
})
|
2020-12-22 14:57:13 +08:00
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) BeginTransaction(ctx context.Context) (context.Context, error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.BeginTransaction(ctx)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) CommitTransaction(ctx context.Context) error {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.CommitTransaction(ctx)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) RollbackTransaction(ctx context.Context) error {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.RollbackTransaction(ctx)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) Shutdown() {
|
2020-12-22 14:57:13 +08:00
|
|
|
t.actualStore.Shutdown()
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.KvPut(ctx, key, value)
|
|
|
|
}
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.KvGet(ctx, key)
|
|
|
|
}
|
2022-05-25 16:10:49 +08:00
|
|
|
func (t *FilerStorePathTranslator) KvDelete(ctx context.Context, key []byte) (err error) {
|
2020-12-22 14:57:13 +08:00
|
|
|
return t.actualStore.KvDelete(ctx, key)
|
|
|
|
}
|