filerstore.go 4.6 KB

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