You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.4 KiB

8 months ago
8 months ago
  1. package filer
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "math"
  7. "strings"
  8. )
  9. var (
  10. _ = FilerStore(&FilerStorePathTranslator{})
  11. )
  12. type FilerStorePathTranslator struct {
  13. actualStore FilerStore
  14. storeRoot string
  15. }
  16. func NewFilerStorePathTranslator(storeRoot string, store FilerStore) *FilerStorePathTranslator {
  17. if innerStore, ok := store.(*FilerStorePathTranslator); ok {
  18. return innerStore
  19. }
  20. if !strings.HasSuffix(storeRoot, "/") {
  21. storeRoot += "/"
  22. }
  23. return &FilerStorePathTranslator{
  24. actualStore: store,
  25. storeRoot: storeRoot,
  26. }
  27. }
  28. func (t *FilerStorePathTranslator) translatePath(fp util.FullPath) (newPath util.FullPath) {
  29. newPath = fp
  30. if t.storeRoot == "/" {
  31. return
  32. }
  33. newPath = fp[len(t.storeRoot)-1:]
  34. if newPath == "" {
  35. newPath = "/"
  36. }
  37. return
  38. }
  39. func (t *FilerStorePathTranslator) changeEntryPath(entry *Entry) (previousPath util.FullPath) {
  40. previousPath = entry.FullPath
  41. if t.storeRoot == "/" {
  42. return
  43. }
  44. entry.FullPath = t.translatePath(previousPath)
  45. return
  46. }
  47. func (t *FilerStorePathTranslator) recoverEntryPath(entry *Entry, previousPath util.FullPath) {
  48. entry.FullPath = previousPath
  49. }
  50. func (t *FilerStorePathTranslator) GetName() string {
  51. return t.actualStore.GetName()
  52. }
  53. func (t *FilerStorePathTranslator) Initialize(configuration util.Configuration, prefix string) error {
  54. return t.actualStore.Initialize(configuration, prefix)
  55. }
  56. func (t *FilerStorePathTranslator) InsertEntry(ctx context.Context, entry *Entry) error {
  57. previousPath := t.changeEntryPath(entry)
  58. defer t.recoverEntryPath(entry, previousPath)
  59. return t.actualStore.InsertEntry(ctx, entry)
  60. }
  61. func (t *FilerStorePathTranslator) UpdateEntry(ctx context.Context, entry *Entry) error {
  62. previousPath := t.changeEntryPath(entry)
  63. defer t.recoverEntryPath(entry, previousPath)
  64. return t.actualStore.UpdateEntry(ctx, entry)
  65. }
  66. func (t *FilerStorePathTranslator) FindEntry(ctx context.Context, fp util.FullPath) (entry *Entry, err error) {
  67. if t.storeRoot == "/" {
  68. return t.actualStore.FindEntry(ctx, fp)
  69. }
  70. newFullPath := t.translatePath(fp)
  71. entry, err = t.actualStore.FindEntry(ctx, newFullPath)
  72. if err == nil {
  73. entry.FullPath = fp[:len(t.storeRoot)-1] + entry.FullPath
  74. }
  75. return
  76. }
  77. func (t *FilerStorePathTranslator) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  78. newFullPath := t.translatePath(fp)
  79. return t.actualStore.DeleteEntry(ctx, newFullPath)
  80. }
  81. func (t *FilerStorePathTranslator) DeleteOneEntry(ctx context.Context, existingEntry *Entry) (err error) {
  82. previousPath := t.changeEntryPath(existingEntry)
  83. defer t.recoverEntryPath(existingEntry, previousPath)
  84. return t.actualStore.DeleteEntry(ctx, existingEntry.FullPath)
  85. }
  86. func (t *FilerStorePathTranslator) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  87. newFullPath := t.translatePath(fp)
  88. return t.actualStore.DeleteFolderChildren(ctx, newFullPath)
  89. }
  90. func (t *FilerStorePathTranslator) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc ListEachEntryFunc) (string, error) {
  91. newFullPath := t.translatePath(dirPath)
  92. return t.actualStore.ListDirectoryEntries(ctx, newFullPath, startFileName, includeStartFile, limit, func(entry *Entry) bool {
  93. entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
  94. return eachEntryFunc(entry)
  95. })
  96. }
  97. func (t *FilerStorePathTranslator) ListRecursivePrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (string, error) {
  98. glog.V(5).Infof("ListRecursivePrefixedEntries dirPath %v", dirPath)
  99. newFullPath := t.translatePath(dirPath)
  100. return t.actualStore.ListRecursivePrefixedEntries(ctx, newFullPath, startFileName, includeStartFile, limit, prefix, func(entry *Entry) bool {
  101. entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
  102. return eachEntryFunc(entry)
  103. })
  104. }
  105. func (t *FilerStorePathTranslator) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (string, error) {
  106. newFullPath := t.translatePath(dirPath)
  107. if limit > math.MaxInt32-1 {
  108. limit = math.MaxInt32 - 1
  109. }
  110. return t.actualStore.ListDirectoryPrefixedEntries(ctx, newFullPath, startFileName, includeStartFile, limit, prefix, func(entry *Entry) bool {
  111. entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
  112. return eachEntryFunc(entry)
  113. })
  114. }
  115. func (t *FilerStorePathTranslator) BeginTransaction(ctx context.Context) (context.Context, error) {
  116. return t.actualStore.BeginTransaction(ctx)
  117. }
  118. func (t *FilerStorePathTranslator) CommitTransaction(ctx context.Context) error {
  119. return t.actualStore.CommitTransaction(ctx)
  120. }
  121. func (t *FilerStorePathTranslator) RollbackTransaction(ctx context.Context) error {
  122. return t.actualStore.RollbackTransaction(ctx)
  123. }
  124. func (t *FilerStorePathTranslator) Shutdown() {
  125. t.actualStore.Shutdown()
  126. }
  127. func (t *FilerStorePathTranslator) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  128. return t.actualStore.KvPut(ctx, key, value)
  129. }
  130. func (t *FilerStorePathTranslator) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  131. return t.actualStore.KvGet(ctx, key)
  132. }
  133. func (t *FilerStorePathTranslator) KvDelete(ctx context.Context, key []byte) (err error) {
  134. return t.actualStore.KvDelete(ctx, key)
  135. }