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.

161 lines
4.7 KiB

4 years ago
4 years ago
  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) (err error) {
  85. newFullPath := t.translatePath(fp)
  86. return t.actualStore.DeleteFolderChildren(ctx, newFullPath)
  87. }
  88. func (t *FilerStorePathTranlator) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*Entry, error) {
  89. newFullPath := t.translatePath(dirPath)
  90. entries, err := t.actualStore.ListDirectoryEntries(ctx, newFullPath, startFileName, includeStartFile, limit)
  91. if err != nil {
  92. return nil, err
  93. }
  94. for _, entry := range entries {
  95. entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
  96. }
  97. return entries, err
  98. }
  99. func (t *FilerStorePathTranlator) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int, prefix string) ([]*Entry, error) {
  100. newFullPath := t.translatePath(dirPath)
  101. entries, err := t.actualStore.ListDirectoryPrefixedEntries(ctx, newFullPath, startFileName, includeStartFile, limit, prefix)
  102. if err != nil {
  103. return nil, err
  104. }
  105. for _, entry := range entries {
  106. entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
  107. }
  108. return entries, nil
  109. }
  110. func (t *FilerStorePathTranlator) BeginTransaction(ctx context.Context) (context.Context, error) {
  111. return t.actualStore.BeginTransaction(ctx)
  112. }
  113. func (t *FilerStorePathTranlator) CommitTransaction(ctx context.Context) error {
  114. return t.actualStore.CommitTransaction(ctx)
  115. }
  116. func (t *FilerStorePathTranlator) RollbackTransaction(ctx context.Context) error {
  117. return t.actualStore.RollbackTransaction(ctx)
  118. }
  119. func (t *FilerStorePathTranlator) Shutdown() {
  120. t.actualStore.Shutdown()
  121. }
  122. func (t *FilerStorePathTranlator) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  123. return t.actualStore.KvPut(ctx, key, value)
  124. }
  125. func (t *FilerStorePathTranlator) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  126. return t.actualStore.KvGet(ctx, key)
  127. }
  128. func (t *FilerStorePathTranlator) KvDelete(ctx context.Context, key []byte) (err error) {
  129. return t.actualStore.KvDelete(ctx, key)
  130. }