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.

150 lines
4.5 KiB

  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) changeEntryPath(entry *Entry) (previousPath util.FullPath){
  27. previousPath = entry.FullPath
  28. if t.storeRoot == "/" {
  29. return
  30. }
  31. entry.FullPath = previousPath[len(t.storeRoot)-1:]
  32. return
  33. }
  34. func (t *FilerStorePathTranlator) recoverEntryPath(entry *Entry, previousPath util.FullPath) {
  35. entry.FullPath = previousPath
  36. }
  37. func (t *FilerStorePathTranlator) GetName() string {
  38. return t.actualStore.GetName()
  39. }
  40. func (t *FilerStorePathTranlator) Initialize(configuration util.Configuration, prefix string) error {
  41. return t.actualStore.Initialize(configuration, prefix)
  42. }
  43. func (t *FilerStorePathTranlator) InsertEntry(ctx context.Context, entry *Entry) error {
  44. previousPath := t.changeEntryPath(entry)
  45. defer t.recoverEntryPath(entry, previousPath)
  46. return t.actualStore.InsertEntry(ctx, entry)
  47. }
  48. func (t *FilerStorePathTranlator) UpdateEntry(ctx context.Context, entry *Entry) error {
  49. previousPath := t.changeEntryPath(entry)
  50. defer t.recoverEntryPath(entry, previousPath)
  51. return t.actualStore.UpdateEntry(ctx, entry)
  52. }
  53. func (t *FilerStorePathTranlator) FindEntry(ctx context.Context, fp util.FullPath) (entry *Entry, err error) {
  54. if t.storeRoot == "/" {
  55. return t.actualStore.FindEntry(ctx, fp)
  56. }
  57. newFullPath := fp[len(t.storeRoot)-1:]
  58. entry, err = t.actualStore.FindEntry(ctx, newFullPath)
  59. if err == nil {
  60. entry.FullPath = fp[:len(t.storeRoot)-1] + entry.FullPath
  61. }
  62. return
  63. }
  64. func (t *FilerStorePathTranlator) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  65. newFullPath := fp[len(t.storeRoot)-1:]
  66. return t.actualStore.DeleteEntry(ctx, newFullPath)
  67. }
  68. func (t *FilerStorePathTranlator) DeleteOneEntry(ctx context.Context, existingEntry *Entry) (err error) {
  69. previousPath := t.changeEntryPath(existingEntry)
  70. defer t.recoverEntryPath(existingEntry, previousPath)
  71. return t.actualStore.DeleteEntry(ctx, existingEntry.FullPath)
  72. }
  73. func (t *FilerStorePathTranlator) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  74. newFullPath := fp[len(t.storeRoot)-1:]
  75. return t.actualStore.DeleteFolderChildren(ctx, newFullPath)
  76. }
  77. func (t *FilerStorePathTranlator) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*Entry, error) {
  78. newFullPath := dirPath[len(t.storeRoot)-1:]
  79. entries, err := t.actualStore.ListDirectoryEntries(ctx, newFullPath, startFileName, includeStartFile, limit)
  80. if err != nil {
  81. return nil, err
  82. }
  83. for _, entry := range entries {
  84. entry.FullPath = dirPath[:len(t.storeRoot)-1] + entry.FullPath
  85. }
  86. return entries, err
  87. }
  88. func (t *FilerStorePathTranlator) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int, prefix string) ([]*Entry, error) {
  89. newFullPath := dirPath[len(t.storeRoot)-1:]
  90. entries, err := t.actualStore.ListDirectoryPrefixedEntries(ctx, newFullPath, startFileName, includeStartFile, limit, prefix)
  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, nil
  98. }
  99. func (t *FilerStorePathTranlator) BeginTransaction(ctx context.Context) (context.Context, error) {
  100. return t.actualStore.BeginTransaction(ctx)
  101. }
  102. func (t *FilerStorePathTranlator) CommitTransaction(ctx context.Context) error {
  103. return t.actualStore.CommitTransaction(ctx)
  104. }
  105. func (t *FilerStorePathTranlator) RollbackTransaction(ctx context.Context) error {
  106. return t.actualStore.RollbackTransaction(ctx)
  107. }
  108. func (t *FilerStorePathTranlator) Shutdown() {
  109. t.actualStore.Shutdown()
  110. }
  111. func (t *FilerStorePathTranlator) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  112. return t.actualStore.KvPut(ctx, key, value)
  113. }
  114. func (t *FilerStorePathTranlator) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  115. return t.actualStore.KvGet(ctx, key)
  116. }
  117. func (t *FilerStorePathTranlator) KvDelete(ctx context.Context, key []byte) (err error) {
  118. return t.actualStore.KvDelete(ctx, key)
  119. }