filerstore.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package filer2
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/stats"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. type FilerStore interface {
  11. // GetName gets the name to locate the configuration in filer.toml file
  12. GetName() string
  13. // Initialize initializes the file store
  14. Initialize(configuration util.Configuration, prefix string) error
  15. InsertEntry(context.Context, *Entry) error
  16. UpdateEntry(context.Context, *Entry) (err error)
  17. // err == filer2.ErrNotFound if not found
  18. FindEntry(context.Context, FullPath) (entry *Entry, err error)
  19. DeleteEntry(context.Context, FullPath) (err error)
  20. DeleteFolderChildren(context.Context, FullPath) (err error)
  21. ListDirectoryEntries(ctx context.Context, dirPath FullPath, startFileName string, includeStartFile bool, limit int) ([]*Entry, error)
  22. BeginTransaction(ctx context.Context) (context.Context, error)
  23. CommitTransaction(ctx context.Context) error
  24. RollbackTransaction(ctx context.Context) error
  25. }
  26. var ErrNotFound = errors.New("filer: no entry is found in filer store")
  27. type FilerStoreWrapper struct {
  28. actualStore FilerStore
  29. }
  30. func NewFilerStoreWrapper(store FilerStore) *FilerStoreWrapper {
  31. if innerStore, ok := store.(*FilerStoreWrapper); ok {
  32. return innerStore
  33. }
  34. return &FilerStoreWrapper{
  35. actualStore: store,
  36. }
  37. }
  38. func (fsw *FilerStoreWrapper) GetName() string {
  39. return fsw.actualStore.GetName()
  40. }
  41. func (fsw *FilerStoreWrapper) Initialize(configuration util.Configuration, prefix string) error {
  42. return fsw.actualStore.Initialize(configuration, prefix)
  43. }
  44. func (fsw *FilerStoreWrapper) InsertEntry(ctx context.Context, entry *Entry) error {
  45. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "insert").Inc()
  46. start := time.Now()
  47. defer func() {
  48. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "insert").Observe(time.Since(start).Seconds())
  49. }()
  50. filer_pb.BeforeEntrySerialization(entry.Chunks)
  51. return fsw.actualStore.InsertEntry(ctx, entry)
  52. }
  53. func (fsw *FilerStoreWrapper) UpdateEntry(ctx context.Context, entry *Entry) error {
  54. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "update").Inc()
  55. start := time.Now()
  56. defer func() {
  57. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "update").Observe(time.Since(start).Seconds())
  58. }()
  59. filer_pb.BeforeEntrySerialization(entry.Chunks)
  60. return fsw.actualStore.UpdateEntry(ctx, entry)
  61. }
  62. func (fsw *FilerStoreWrapper) FindEntry(ctx context.Context, fp FullPath) (entry *Entry, err error) {
  63. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "find").Inc()
  64. start := time.Now()
  65. defer func() {
  66. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "find").Observe(time.Since(start).Seconds())
  67. }()
  68. entry, err = fsw.actualStore.FindEntry(ctx, fp)
  69. if err != nil {
  70. return nil, err
  71. }
  72. filer_pb.AfterEntryDeserialization(entry.Chunks)
  73. return
  74. }
  75. func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp FullPath) (err error) {
  76. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "delete").Inc()
  77. start := time.Now()
  78. defer func() {
  79. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "delete").Observe(time.Since(start).Seconds())
  80. }()
  81. return fsw.actualStore.DeleteEntry(ctx, fp)
  82. }
  83. func (fsw *FilerStoreWrapper) DeleteFolderChildren(ctx context.Context, fp FullPath) (err error) {
  84. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "deleteFolderChildren").Inc()
  85. start := time.Now()
  86. defer func() {
  87. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "deleteFolderChildren").Observe(time.Since(start).Seconds())
  88. }()
  89. return fsw.actualStore.DeleteFolderChildren(ctx, fp)
  90. }
  91. func (fsw *FilerStoreWrapper) ListDirectoryEntries(ctx context.Context, dirPath FullPath, startFileName string, includeStartFile bool, limit int) ([]*Entry, error) {
  92. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "list").Inc()
  93. start := time.Now()
  94. defer func() {
  95. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "list").Observe(time.Since(start).Seconds())
  96. }()
  97. entries, err := fsw.actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
  98. if err != nil {
  99. return nil, err
  100. }
  101. for _, entry := range entries {
  102. filer_pb.AfterEntryDeserialization(entry.Chunks)
  103. }
  104. return entries, err
  105. }
  106. func (fsw *FilerStoreWrapper) BeginTransaction(ctx context.Context) (context.Context, error) {
  107. return fsw.actualStore.BeginTransaction(ctx)
  108. }
  109. func (fsw *FilerStoreWrapper) CommitTransaction(ctx context.Context) error {
  110. return fsw.actualStore.CommitTransaction(ctx)
  111. }
  112. func (fsw *FilerStoreWrapper) RollbackTransaction(ctx context.Context) error {
  113. return fsw.actualStore.RollbackTransaction(ctx)
  114. }