collection.go 1.5 KB

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