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.

158 lines
4.8 KiB

  1. package filer
  2. import (
  3. "context"
  4. "github.com/chrislusf/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) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (string, error) {
  97. newFullPath := t.translatePath(dirPath)
  98. if limit > math.MaxInt32-1 {
  99. limit = math.MaxInt32 - 1
  100. }
  101. return t.actualStore.ListDirectoryPrefixedEntries(ctx, newFullPath, startFileName, includeStartFile, limit, prefix, func(entry *Entry) bool {
  102. entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
  103. return eachEntryFunc(entry)
  104. })
  105. }
  106. func (t *FilerStorePathTranslator) BeginTransaction(ctx context.Context) (context.Context, error) {
  107. return t.actualStore.BeginTransaction(ctx)
  108. }
  109. func (t *FilerStorePathTranslator) CommitTransaction(ctx context.Context) error {
  110. return t.actualStore.CommitTransaction(ctx)
  111. }
  112. func (t *FilerStorePathTranslator) RollbackTransaction(ctx context.Context) error {
  113. return t.actualStore.RollbackTransaction(ctx)
  114. }
  115. func (t *FilerStorePathTranslator) Shutdown() {
  116. t.actualStore.Shutdown()
  117. }
  118. func (t *FilerStorePathTranslator) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  119. return t.actualStore.KvPut(ctx, key, value)
  120. }
  121. func (t *FilerStorePathTranslator) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  122. return t.actualStore.KvGet(ctx, key)
  123. }
  124. func (t *FilerStorePathTranslator) KvDelete(ctx context.Context, key []byte) (err error) {
  125. return t.actualStore.KvDelete(ctx, key)
  126. }