links.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "sync"
  4. "github.com/docker/docker/container"
  5. )
  6. // linkIndex stores link relationships between containers, including their specified alias
  7. // The alias is the name the parent uses to reference the child
  8. type linkIndex struct {
  9. // idx maps a parent->alias->child relationship
  10. idx map[*container.Container]map[string]*container.Container
  11. // childIdx maps child->parent->aliases
  12. childIdx map[*container.Container]map[*container.Container]map[string]struct{}
  13. mu sync.Mutex
  14. }
  15. func newLinkIndex() *linkIndex {
  16. return &linkIndex{
  17. idx: make(map[*container.Container]map[string]*container.Container),
  18. childIdx: make(map[*container.Container]map[*container.Container]map[string]struct{}),
  19. }
  20. }
  21. // link adds indexes for the passed in parent/child/alias relationships
  22. func (l *linkIndex) link(parent, child *container.Container, alias string) {
  23. l.mu.Lock()
  24. if l.idx[parent] == nil {
  25. l.idx[parent] = make(map[string]*container.Container)
  26. }
  27. l.idx[parent][alias] = child
  28. if l.childIdx[child] == nil {
  29. l.childIdx[child] = make(map[*container.Container]map[string]struct{})
  30. }
  31. if l.childIdx[child][parent] == nil {
  32. l.childIdx[child][parent] = make(map[string]struct{})
  33. }
  34. l.childIdx[child][parent][alias] = struct{}{}
  35. l.mu.Unlock()
  36. }
  37. // unlink removes the requested alias for the given parent/child
  38. func (l *linkIndex) unlink(alias string, child, parent *container.Container) {
  39. l.mu.Lock()
  40. delete(l.idx[parent], alias)
  41. delete(l.childIdx[child], parent)
  42. l.mu.Unlock()
  43. }
  44. // children maps all the aliases-> children for the passed in parent
  45. // aliases here are the aliases the parent uses to refer to the child
  46. func (l *linkIndex) children(parent *container.Container) map[string]*container.Container {
  47. l.mu.Lock()
  48. children := l.idx[parent]
  49. l.mu.Unlock()
  50. return children
  51. }
  52. // parents maps all the aliases->parent for the passed in child
  53. // aliases here are the aliases the parents use to refer to the child
  54. func (l *linkIndex) parents(child *container.Container) map[string]*container.Container {
  55. l.mu.Lock()
  56. parents := make(map[string]*container.Container)
  57. for parent, aliases := range l.childIdx[child] {
  58. for alias := range aliases {
  59. parents[alias] = parent
  60. }
  61. }
  62. l.mu.Unlock()
  63. return parents
  64. }
  65. // delete deletes all link relationships referencing this container
  66. func (l *linkIndex) delete(container *container.Container) []string {
  67. l.mu.Lock()
  68. var aliases []string
  69. for alias, child := range l.idx[container] {
  70. aliases = append(aliases, alias)
  71. delete(l.childIdx[child], container)
  72. }
  73. delete(l.idx, container)
  74. delete(l.childIdx, container)
  75. l.mu.Unlock()
  76. return aliases
  77. }