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.

167 lines
5.3 KiB

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