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.

113 lines
2.1 KiB

  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. }