truncindex.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Package truncindex provides a general 'index tree', used by Docker
  2. // in order to be able to reference containers by only a few unambiguous
  3. // characters of their id.
  4. package truncindex
  5. import (
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "sync"
  10. "github.com/tchap/go-patricia/patricia"
  11. )
  12. var (
  13. // ErrEmptyPrefix is an error returned if the prefix was empty.
  14. ErrEmptyPrefix = errors.New("Prefix can't be empty")
  15. // ErrAmbiguousPrefix is returned if the prefix was ambiguous
  16. // (multiple ids for the prefix).
  17. ErrAmbiguousPrefix = errors.New("Multiple IDs found with provided prefix")
  18. // ErrIllegalChar is returned when a space is in the ID
  19. ErrIllegalChar = errors.New("illegal character: ' '")
  20. // ErrNotExist is returned when ID or its prefix not found in index.
  21. ErrNotExist = errors.New("ID does not exist")
  22. )
  23. // TruncIndex allows the retrieval of string identifiers by any of their unique prefixes.
  24. // This is used to retrieve image and container IDs by more convenient shorthand prefixes.
  25. type TruncIndex struct {
  26. sync.RWMutex
  27. trie *patricia.Trie
  28. ids map[string]struct{}
  29. }
  30. // NewTruncIndex creates a new TruncIndex and initializes with a list of IDs.
  31. func NewTruncIndex(ids []string) (idx *TruncIndex) {
  32. idx = &TruncIndex{
  33. ids: make(map[string]struct{}),
  34. // Change patricia max prefix per node length,
  35. // because our len(ID) always 64
  36. trie: patricia.NewTrie(patricia.MaxPrefixPerNode(64)),
  37. }
  38. for _, id := range ids {
  39. idx.addID(id)
  40. }
  41. return
  42. }
  43. func (idx *TruncIndex) addID(id string) error {
  44. if strings.Contains(id, " ") {
  45. return ErrIllegalChar
  46. }
  47. if id == "" {
  48. return ErrEmptyPrefix
  49. }
  50. if _, exists := idx.ids[id]; exists {
  51. return fmt.Errorf("id already exists: '%s'", id)
  52. }
  53. idx.ids[id] = struct{}{}
  54. if inserted := idx.trie.Insert(patricia.Prefix(id), struct{}{}); !inserted {
  55. return fmt.Errorf("failed to insert id: %s", id)
  56. }
  57. return nil
  58. }
  59. // Add adds a new ID to the TruncIndex.
  60. func (idx *TruncIndex) Add(id string) error {
  61. idx.Lock()
  62. defer idx.Unlock()
  63. if err := idx.addID(id); err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. // Delete removes an ID from the TruncIndex. If there are multiple IDs
  69. // with the given prefix, an error is thrown.
  70. func (idx *TruncIndex) Delete(id string) error {
  71. idx.Lock()
  72. defer idx.Unlock()
  73. if _, exists := idx.ids[id]; !exists || id == "" {
  74. return fmt.Errorf("no such id: '%s'", id)
  75. }
  76. delete(idx.ids, id)
  77. if deleted := idx.trie.Delete(patricia.Prefix(id)); !deleted {
  78. return fmt.Errorf("no such id: '%s'", id)
  79. }
  80. return nil
  81. }
  82. // Get retrieves an ID from the TruncIndex. If there are multiple IDs
  83. // with the given prefix, an error is thrown.
  84. func (idx *TruncIndex) Get(s string) (string, error) {
  85. if s == "" {
  86. return "", ErrEmptyPrefix
  87. }
  88. var (
  89. id string
  90. )
  91. subTreeVisitFunc := func(prefix patricia.Prefix, item patricia.Item) error {
  92. if id != "" {
  93. // we haven't found the ID if there are two or more IDs
  94. id = ""
  95. return ErrAmbiguousPrefix
  96. }
  97. id = string(prefix)
  98. return nil
  99. }
  100. idx.RLock()
  101. defer idx.RUnlock()
  102. if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil {
  103. return "", err
  104. }
  105. if id != "" {
  106. return id, nil
  107. }
  108. return "", ErrNotExist
  109. }
  110. // Iterate iterates over all stored IDs, and passes each of them to the given handler.
  111. func (idx *TruncIndex) Iterate(handler func(id string)) {
  112. idx.trie.Visit(func(prefix patricia.Prefix, item patricia.Item) error {
  113. handler(string(prefix))
  114. return nil
  115. })
  116. }