set.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2020, 2020 OCI Contributors
  2. // Copyright 2017 Docker, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package digestset
  16. import (
  17. "errors"
  18. "sort"
  19. "strings"
  20. "sync"
  21. digest "github.com/opencontainers/go-digest"
  22. )
  23. var (
  24. // ErrDigestNotFound is used when a matching digest
  25. // could not be found in a set.
  26. ErrDigestNotFound = errors.New("digest not found")
  27. // ErrDigestAmbiguous is used when multiple digests
  28. // are found in a set. None of the matching digests
  29. // should be considered valid matches.
  30. ErrDigestAmbiguous = errors.New("ambiguous digest string")
  31. )
  32. // Set is used to hold a unique set of digests which
  33. // may be easily referenced by easily referenced by a string
  34. // representation of the digest as well as short representation.
  35. // The uniqueness of the short representation is based on other
  36. // digests in the set. If digests are omitted from this set,
  37. // collisions in a larger set may not be detected, therefore it
  38. // is important to always do short representation lookups on
  39. // the complete set of digests. To mitigate collisions, an
  40. // appropriately long short code should be used.
  41. type Set struct {
  42. mutex sync.RWMutex
  43. entries digestEntries
  44. }
  45. // NewSet creates an empty set of digests
  46. // which may have digests added.
  47. func NewSet() *Set {
  48. return &Set{
  49. entries: digestEntries{},
  50. }
  51. }
  52. // checkShortMatch checks whether two digests match as either whole
  53. // values or short values. This function does not test equality,
  54. // rather whether the second value could match against the first
  55. // value.
  56. func checkShortMatch(alg digest.Algorithm, hex, shortAlg, shortHex string) bool {
  57. if len(hex) == len(shortHex) {
  58. if hex != shortHex {
  59. return false
  60. }
  61. if len(shortAlg) > 0 && string(alg) != shortAlg {
  62. return false
  63. }
  64. } else if !strings.HasPrefix(hex, shortHex) {
  65. return false
  66. } else if len(shortAlg) > 0 && string(alg) != shortAlg {
  67. return false
  68. }
  69. return true
  70. }
  71. // Lookup looks for a digest matching the given string representation.
  72. // If no digests could be found ErrDigestNotFound will be returned
  73. // with an empty digest value. If multiple matches are found
  74. // ErrDigestAmbiguous will be returned with an empty digest value.
  75. func (dst *Set) Lookup(d string) (digest.Digest, error) {
  76. dst.mutex.RLock()
  77. defer dst.mutex.RUnlock()
  78. if len(dst.entries) == 0 {
  79. return "", ErrDigestNotFound
  80. }
  81. var (
  82. searchFunc func(int) bool
  83. alg digest.Algorithm
  84. hex string
  85. )
  86. dgst, err := digest.Parse(d)
  87. if err == digest.ErrDigestInvalidFormat {
  88. hex = d
  89. searchFunc = func(i int) bool {
  90. return dst.entries[i].val >= d
  91. }
  92. } else {
  93. hex = dgst.Hex()
  94. alg = dgst.Algorithm()
  95. searchFunc = func(i int) bool {
  96. if dst.entries[i].val == hex {
  97. return dst.entries[i].alg >= alg
  98. }
  99. return dst.entries[i].val >= hex
  100. }
  101. }
  102. idx := sort.Search(len(dst.entries), searchFunc)
  103. if idx == len(dst.entries) || !checkShortMatch(dst.entries[idx].alg, dst.entries[idx].val, string(alg), hex) {
  104. return "", ErrDigestNotFound
  105. }
  106. if dst.entries[idx].alg == alg && dst.entries[idx].val == hex {
  107. return dst.entries[idx].digest, nil
  108. }
  109. if idx+1 < len(dst.entries) && checkShortMatch(dst.entries[idx+1].alg, dst.entries[idx+1].val, string(alg), hex) {
  110. return "", ErrDigestAmbiguous
  111. }
  112. return dst.entries[idx].digest, nil
  113. }
  114. // Add adds the given digest to the set. An error will be returned
  115. // if the given digest is invalid. If the digest already exists in the
  116. // set, this operation will be a no-op.
  117. func (dst *Set) Add(d digest.Digest) error {
  118. if err := d.Validate(); err != nil {
  119. return err
  120. }
  121. dst.mutex.Lock()
  122. defer dst.mutex.Unlock()
  123. entry := &digestEntry{alg: d.Algorithm(), val: d.Hex(), digest: d}
  124. searchFunc := func(i int) bool {
  125. if dst.entries[i].val == entry.val {
  126. return dst.entries[i].alg >= entry.alg
  127. }
  128. return dst.entries[i].val >= entry.val
  129. }
  130. idx := sort.Search(len(dst.entries), searchFunc)
  131. if idx == len(dst.entries) {
  132. dst.entries = append(dst.entries, entry)
  133. return nil
  134. } else if dst.entries[idx].digest == d {
  135. return nil
  136. }
  137. entries := append(dst.entries, nil)
  138. copy(entries[idx+1:], entries[idx:len(entries)-1])
  139. entries[idx] = entry
  140. dst.entries = entries
  141. return nil
  142. }
  143. // Remove removes the given digest from the set. An err will be
  144. // returned if the given digest is invalid. If the digest does
  145. // not exist in the set, this operation will be a no-op.
  146. func (dst *Set) Remove(d digest.Digest) error {
  147. if err := d.Validate(); err != nil {
  148. return err
  149. }
  150. dst.mutex.Lock()
  151. defer dst.mutex.Unlock()
  152. entry := &digestEntry{alg: d.Algorithm(), val: d.Hex(), digest: d}
  153. searchFunc := func(i int) bool {
  154. if dst.entries[i].val == entry.val {
  155. return dst.entries[i].alg >= entry.alg
  156. }
  157. return dst.entries[i].val >= entry.val
  158. }
  159. idx := sort.Search(len(dst.entries), searchFunc)
  160. // Not found if idx is after or value at idx is not digest
  161. if idx == len(dst.entries) || dst.entries[idx].digest != d {
  162. return nil
  163. }
  164. entries := dst.entries
  165. copy(entries[idx:], entries[idx+1:])
  166. entries = entries[:len(entries)-1]
  167. dst.entries = entries
  168. return nil
  169. }
  170. // All returns all the digests in the set
  171. func (dst *Set) All() []digest.Digest {
  172. dst.mutex.RLock()
  173. defer dst.mutex.RUnlock()
  174. retValues := make([]digest.Digest, len(dst.entries))
  175. for i := range dst.entries {
  176. retValues[i] = dst.entries[i].digest
  177. }
  178. return retValues
  179. }
  180. // ShortCodeTable returns a map of Digest to unique short codes. The
  181. // length represents the minimum value, the maximum length may be the
  182. // entire value of digest if uniqueness cannot be achieved without the
  183. // full value. This function will attempt to make short codes as short
  184. // as possible to be unique.
  185. func ShortCodeTable(dst *Set, length int) map[digest.Digest]string {
  186. dst.mutex.RLock()
  187. defer dst.mutex.RUnlock()
  188. m := make(map[digest.Digest]string, len(dst.entries))
  189. l := length
  190. resetIdx := 0
  191. for i := 0; i < len(dst.entries); i++ {
  192. var short string
  193. extended := true
  194. for extended {
  195. extended = false
  196. if len(dst.entries[i].val) <= l {
  197. short = dst.entries[i].digest.String()
  198. } else {
  199. short = dst.entries[i].val[:l]
  200. for j := i + 1; j < len(dst.entries); j++ {
  201. if checkShortMatch(dst.entries[j].alg, dst.entries[j].val, "", short) {
  202. if j > resetIdx {
  203. resetIdx = j
  204. }
  205. extended = true
  206. } else {
  207. break
  208. }
  209. }
  210. if extended {
  211. l++
  212. }
  213. }
  214. }
  215. m[dst.entries[i].digest] = short
  216. if i >= resetIdx {
  217. l = length
  218. }
  219. }
  220. return m
  221. }
  222. type digestEntry struct {
  223. alg digest.Algorithm
  224. val string
  225. digest digest.Digest
  226. }
  227. type digestEntries []*digestEntry
  228. func (d digestEntries) Len() int {
  229. return len(d)
  230. }
  231. func (d digestEntries) Less(i, j int) bool {
  232. if d[i].val != d[j].val {
  233. return d[i].val < d[j].val
  234. }
  235. return d[i].alg < d[j].alg
  236. }
  237. func (d digestEntries) Swap(i, j int) {
  238. d[i], d[j] = d[j], d[i]
  239. }