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.

141 lines
4.8 KiB

7 years ago
7 years ago
5 years ago
7 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
  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, util.FullPath) (entry *Entry, err error)
  18. DeleteEntry(context.Context, util.FullPath) (err error)
  19. DeleteFolderChildren(context.Context, util.FullPath) (err error)
  20. ListDirectoryEntries(ctx context.Context, dirPath util.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. Shutdown()
  25. }
  26. type FilerStoreWrapper struct {
  27. actualStore FilerStore
  28. }
  29. func NewFilerStoreWrapper(store FilerStore) *FilerStoreWrapper {
  30. if innerStore, ok := store.(*FilerStoreWrapper); ok {
  31. return innerStore
  32. }
  33. return &FilerStoreWrapper{
  34. actualStore: store,
  35. }
  36. }
  37. func (fsw *FilerStoreWrapper) GetName() string {
  38. return fsw.actualStore.GetName()
  39. }
  40. func (fsw *FilerStoreWrapper) Initialize(configuration util.Configuration, prefix string) error {
  41. return fsw.actualStore.Initialize(configuration, prefix)
  42. }
  43. func (fsw *FilerStoreWrapper) InsertEntry(ctx context.Context, entry *Entry) error {
  44. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "insert").Inc()
  45. start := time.Now()
  46. defer func() {
  47. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "insert").Observe(time.Since(start).Seconds())
  48. }()
  49. filer_pb.BeforeEntrySerialization(entry.Chunks)
  50. return fsw.actualStore.InsertEntry(ctx, entry)
  51. }
  52. func (fsw *FilerStoreWrapper) UpdateEntry(ctx context.Context, entry *Entry) error {
  53. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "update").Inc()
  54. start := time.Now()
  55. defer func() {
  56. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "update").Observe(time.Since(start).Seconds())
  57. }()
  58. filer_pb.BeforeEntrySerialization(entry.Chunks)
  59. return fsw.actualStore.UpdateEntry(ctx, entry)
  60. }
  61. func (fsw *FilerStoreWrapper) FindEntry(ctx context.Context, fp util.FullPath) (entry *Entry, err error) {
  62. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "find").Inc()
  63. start := time.Now()
  64. defer func() {
  65. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "find").Observe(time.Since(start).Seconds())
  66. }()
  67. entry, err = fsw.actualStore.FindEntry(ctx, fp)
  68. if err != nil {
  69. return nil, err
  70. }
  71. filer_pb.AfterEntryDeserialization(entry.Chunks)
  72. return
  73. }
  74. func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  75. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "delete").Inc()
  76. start := time.Now()
  77. defer func() {
  78. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "delete").Observe(time.Since(start).Seconds())
  79. }()
  80. return fsw.actualStore.DeleteEntry(ctx, fp)
  81. }
  82. func (fsw *FilerStoreWrapper) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  83. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "deleteFolderChildren").Inc()
  84. start := time.Now()
  85. defer func() {
  86. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "deleteFolderChildren").Observe(time.Since(start).Seconds())
  87. }()
  88. return fsw.actualStore.DeleteFolderChildren(ctx, fp)
  89. }
  90. func (fsw *FilerStoreWrapper) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*Entry, error) {
  91. stats.FilerStoreCounter.WithLabelValues(fsw.actualStore.GetName(), "list").Inc()
  92. start := time.Now()
  93. defer func() {
  94. stats.FilerStoreHistogram.WithLabelValues(fsw.actualStore.GetName(), "list").Observe(time.Since(start).Seconds())
  95. }()
  96. entries, err := fsw.actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
  97. if err != nil {
  98. return nil, err
  99. }
  100. for _, entry := range entries {
  101. filer_pb.AfterEntryDeserialization(entry.Chunks)
  102. }
  103. return entries, err
  104. }
  105. func (fsw *FilerStoreWrapper) BeginTransaction(ctx context.Context) (context.Context, error) {
  106. return fsw.actualStore.BeginTransaction(ctx)
  107. }
  108. func (fsw *FilerStoreWrapper) CommitTransaction(ctx context.Context) error {
  109. return fsw.actualStore.CommitTransaction(ctx)
  110. }
  111. func (fsw *FilerStoreWrapper) RollbackTransaction(ctx context.Context) error {
  112. return fsw.actualStore.RollbackTransaction(ctx)
  113. }
  114. func (fsw *FilerStoreWrapper) Shutdown() {
  115. fsw.actualStore.Shutdown()
  116. }