collection.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package topology
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/storage"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. )
  7. type Collection struct {
  8. Name string
  9. volumeSizeLimit uint64
  10. storageType2VolumeLayout *util.ConcurrentReadMap
  11. }
  12. func NewCollection(name string, volumeSizeLimit uint64) *Collection {
  13. c := &Collection{Name: name, volumeSizeLimit: volumeSizeLimit}
  14. c.storageType2VolumeLayout = util.NewConcurrentReadMap()
  15. return c
  16. }
  17. func (c *Collection) String() string {
  18. return fmt.Sprintf("Name:%s, volumeSizeLimit:%d, storageType2VolumeLayout:%v", c.Name, c.volumeSizeLimit, c.storageType2VolumeLayout)
  19. }
  20. func (c *Collection) GetOrCreateVolumeLayout(rp *storage.ReplicaPlacement, ttl *storage.TTL) *VolumeLayout {
  21. keyString := rp.String()
  22. if ttl != nil {
  23. keyString += ttl.String()
  24. }
  25. vl := c.storageType2VolumeLayout.Get(keyString, func() interface{} {
  26. return NewVolumeLayout(rp, ttl, c.volumeSizeLimit)
  27. })
  28. return vl.(*VolumeLayout)
  29. }
  30. func (c *Collection) Lookup(vid storage.VolumeId) []*DataNode {
  31. for _, vl := range c.storageType2VolumeLayout.Items() {
  32. if vl != nil {
  33. if list := vl.(*VolumeLayout).Lookup(vid); list != nil {
  34. return list
  35. }
  36. }
  37. }
  38. return nil
  39. }
  40. func (c *Collection) ListVolumeServers() (nodes []*DataNode) {
  41. for _, vl := range c.storageType2VolumeLayout.Items() {
  42. if vl != nil {
  43. if list := vl.(*VolumeLayout).ListVolumeServers(); list != nil {
  44. nodes = append(nodes, list...)
  45. }
  46. }
  47. }
  48. return
  49. }