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.

114 lines
2.1 KiB

5 years ago
5 years ago
  1. package filer2
  2. import (
  3. "context"
  4. "math"
  5. "sync"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. )
  9. type BucketName string
  10. type BucketOption struct {
  11. Name BucketName
  12. Replication string
  13. }
  14. type FilerBuckets struct {
  15. dirBucketsPath string
  16. buckets map[BucketName]*BucketOption
  17. sync.RWMutex
  18. }
  19. func (f *Filer) LoadBuckets(dirBucketsPath string) {
  20. f.buckets = &FilerBuckets{
  21. buckets: make(map[BucketName]*BucketOption),
  22. }
  23. f.DirBucketsPath = dirBucketsPath
  24. limit := math.MaxInt32
  25. entries, err := f.ListDirectoryEntries(context.Background(), util.FullPath(dirBucketsPath), "", false, limit)
  26. if err != nil {
  27. glog.V(1).Infof("no buckets found: %v", err)
  28. return
  29. }
  30. glog.V(1).Infof("buckets found: %d", len(entries))
  31. f.buckets.Lock()
  32. for _, entry := range entries {
  33. f.buckets.buckets[BucketName(entry.Name())] = &BucketOption{
  34. Name: BucketName(entry.Name()),
  35. Replication: entry.Replication,
  36. }
  37. }
  38. f.buckets.Unlock()
  39. }
  40. func (f *Filer) ReadBucketOption(buketName string) (replication string) {
  41. f.buckets.RLock()
  42. defer f.buckets.RUnlock()
  43. option, found := f.buckets.buckets[BucketName(buketName)]
  44. if !found {
  45. return ""
  46. }
  47. return option.Replication
  48. }
  49. func (f *Filer) isBucket(entry *Entry) bool {
  50. if !entry.IsDirectory() {
  51. return false
  52. }
  53. parent, dirName := entry.FullPath.DirAndName()
  54. if parent != f.DirBucketsPath {
  55. return false
  56. }
  57. f.buckets.RLock()
  58. defer f.buckets.RUnlock()
  59. _, found := f.buckets.buckets[BucketName(dirName)]
  60. return found
  61. }
  62. func (f *Filer) maybeAddBucket(entry *Entry) {
  63. if !entry.IsDirectory() {
  64. return
  65. }
  66. parent, dirName := entry.FullPath.DirAndName()
  67. if parent != f.DirBucketsPath {
  68. return
  69. }
  70. f.addBucket(dirName, &BucketOption{
  71. Name: BucketName(dirName),
  72. Replication: entry.Replication,
  73. })
  74. }
  75. func (f *Filer) addBucket(buketName string, bucketOption *BucketOption) {
  76. f.buckets.Lock()
  77. defer f.buckets.Unlock()
  78. f.buckets.buckets[BucketName(buketName)] = bucketOption
  79. }
  80. func (f *Filer) deleteBucket(buketName string) {
  81. f.buckets.Lock()
  82. defer f.buckets.Unlock()
  83. delete(f.buckets.buckets, BucketName(buketName))
  84. }