filerstore_translate_path.go 4.7 KB

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