vector.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (C) 2015 The Protocol Authors.
  2. package protocol
  3. import "time"
  4. // The Vector type represents a version vector. The zero value is a usable
  5. // version vector. The vector has slice semantics and some operations on it
  6. // are "append-like" in that they may return the same vector modified, or v
  7. // new allocated Vector with the modified contents.
  8. // Counter represents a single counter in the version vector.
  9. // Update returns a Vector with the index for the specific ID incremented by
  10. // one. If it is possible, the vector v is updated and returned. If it is not,
  11. // a copy will be created, updated and returned.
  12. func (v Vector) Update(id ShortID) Vector {
  13. now := uint64(time.Now().Unix())
  14. return v.updateWithNow(id, now)
  15. }
  16. func (v Vector) updateWithNow(id ShortID, now uint64) Vector {
  17. for i := range v.Counters {
  18. if v.Counters[i].ID == id {
  19. // Update an existing index
  20. v.Counters[i].Value = max(v.Counters[i].Value+1, now)
  21. return v
  22. } else if v.Counters[i].ID > id {
  23. // Insert a new index
  24. nv := make([]Counter, len(v.Counters)+1)
  25. copy(nv, v.Counters[:i])
  26. nv[i].ID = id
  27. nv[i].Value = max(1, now)
  28. copy(nv[i+1:], v.Counters[i:])
  29. return Vector{Counters: nv}
  30. }
  31. }
  32. // Append a new index
  33. return Vector{Counters: append(v.Counters, Counter{
  34. ID: id,
  35. Value: max(1, now),
  36. })}
  37. }
  38. func max(a, b uint64) uint64 {
  39. if a > b {
  40. return a
  41. }
  42. return b
  43. }
  44. // Merge returns the vector containing the maximum indexes from v and b. If it
  45. // is possible, the vector v is updated and returned. If it is not, a copy
  46. // will be created, updated and returned.
  47. func (v Vector) Merge(b Vector) Vector {
  48. var vi, bi int
  49. for bi < len(b.Counters) {
  50. if vi == len(v.Counters) {
  51. // We've reach the end of v, all that remains are appends
  52. return Vector{Counters: append(v.Counters, b.Counters[bi:]...)}
  53. }
  54. if v.Counters[vi].ID > b.Counters[bi].ID {
  55. // The index from b should be inserted here
  56. n := make([]Counter, len(v.Counters)+1)
  57. copy(n, v.Counters[:vi])
  58. n[vi] = b.Counters[bi]
  59. copy(n[vi+1:], v.Counters[vi:])
  60. v.Counters = n
  61. }
  62. if v.Counters[vi].ID == b.Counters[bi].ID {
  63. if val := b.Counters[bi].Value; val > v.Counters[vi].Value {
  64. v.Counters[vi].Value = val
  65. }
  66. }
  67. if bi < len(b.Counters) && v.Counters[vi].ID == b.Counters[bi].ID {
  68. bi++
  69. }
  70. vi++
  71. }
  72. return v
  73. }
  74. // Copy returns an identical vector that is not shared with v.
  75. func (v Vector) Copy() Vector {
  76. nv := make([]Counter, len(v.Counters))
  77. copy(nv, v.Counters)
  78. return Vector{Counters: nv}
  79. }
  80. // Equal returns true when the two vectors are equivalent.
  81. func (v Vector) Equal(b Vector) bool {
  82. return v.Compare(b) == Equal
  83. }
  84. // LesserEqual returns true when the two vectors are equivalent or v is Lesser
  85. // than b.
  86. func (v Vector) LesserEqual(b Vector) bool {
  87. comp := v.Compare(b)
  88. return comp == Lesser || comp == Equal
  89. }
  90. // GreaterEqual returns true when the two vectors are equivalent or v is Greater
  91. // than b.
  92. func (v Vector) GreaterEqual(b Vector) bool {
  93. comp := v.Compare(b)
  94. return comp == Greater || comp == Equal
  95. }
  96. // Concurrent returns true when the two vectors are concurrent.
  97. func (v Vector) Concurrent(b Vector) bool {
  98. comp := v.Compare(b)
  99. return comp == ConcurrentGreater || comp == ConcurrentLesser
  100. }
  101. // Counter returns the current value of the given counter ID.
  102. func (v Vector) Counter(id ShortID) uint64 {
  103. for _, c := range v.Counters {
  104. if c.ID == id {
  105. return c.Value
  106. }
  107. }
  108. return 0
  109. }
  110. // DropOthers removes all counters, keeping only the one with given id. If there
  111. // is no such counter, an empty Vector is returned.
  112. func (v Vector) DropOthers(id ShortID) Vector {
  113. for i, c := range v.Counters {
  114. if c.ID == id {
  115. v.Counters = v.Counters[i : i+1]
  116. return v
  117. }
  118. }
  119. return Vector{}
  120. }
  121. // Ordering represents the relationship between two Vectors.
  122. type Ordering int
  123. const (
  124. Equal Ordering = iota
  125. Greater
  126. Lesser
  127. ConcurrentLesser
  128. ConcurrentGreater
  129. )
  130. // There's really no such thing as "concurrent lesser" and "concurrent
  131. // greater" in version vectors, just "concurrent". But it's useful to be able
  132. // to get a strict ordering between versions for stable sorts and so on, so we
  133. // return both variants. The convenience method Concurrent() can be used to
  134. // check for either case.
  135. // Compare returns the Ordering that describes a's relation to b.
  136. func (v Vector) Compare(b Vector) Ordering {
  137. var ai, bi int // index into a and b
  138. var av, bv Counter // value at current index
  139. result := Equal
  140. for ai < len(v.Counters) || bi < len(b.Counters) {
  141. var aMissing, bMissing bool
  142. if ai < len(v.Counters) {
  143. av = v.Counters[ai]
  144. } else {
  145. av = Counter{}
  146. aMissing = true
  147. }
  148. if bi < len(b.Counters) {
  149. bv = b.Counters[bi]
  150. } else {
  151. bv = Counter{}
  152. bMissing = true
  153. }
  154. switch {
  155. case av.ID == bv.ID:
  156. // We have a counter value for each side
  157. if av.Value > bv.Value {
  158. if result == Lesser {
  159. return ConcurrentLesser
  160. }
  161. result = Greater
  162. } else if av.Value < bv.Value {
  163. if result == Greater {
  164. return ConcurrentGreater
  165. }
  166. result = Lesser
  167. }
  168. case !aMissing && av.ID < bv.ID || bMissing:
  169. // Value is missing on the b side
  170. if av.Value > 0 {
  171. if result == Lesser {
  172. return ConcurrentLesser
  173. }
  174. result = Greater
  175. }
  176. case !bMissing && bv.ID < av.ID || aMissing:
  177. // Value is missing on the a side
  178. if bv.Value > 0 {
  179. if result == Greater {
  180. return ConcurrentGreater
  181. }
  182. result = Lesser
  183. }
  184. }
  185. if ai < len(v.Counters) && (av.ID <= bv.ID || bMissing) {
  186. ai++
  187. }
  188. if bi < len(b.Counters) && (bv.ID <= av.ID || aMissing) {
  189. bi++
  190. }
  191. }
  192. return result
  193. }