filer_buckets.go 2.1 KB

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