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.

152 lines
5.0 KiB

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