vector.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // IsEmpty returns true when there are no counters.
  111. func (v Vector) IsEmpty() bool {
  112. return len(v.Counters) == 0
  113. }
  114. // DropOthers removes all counters, keeping only the one with given id. If there
  115. // is no such counter, an empty Vector is returned.
  116. func (v Vector) DropOthers(id ShortID) Vector {
  117. for i, c := range v.Counters {
  118. if c.ID == id {
  119. v.Counters = v.Counters[i : i+1]
  120. return v
  121. }
  122. }
  123. return Vector{}
  124. }
  125. // Ordering represents the relationship between two Vectors.
  126. type Ordering int
  127. const (
  128. Equal Ordering = iota
  129. Greater
  130. Lesser
  131. ConcurrentLesser
  132. ConcurrentGreater
  133. )
  134. // There's really no such thing as "concurrent lesser" and "concurrent
  135. // greater" in version vectors, just "concurrent". But it's useful to be able
  136. // to get a strict ordering between versions for stable sorts and so on, so we
  137. // return both variants. The convenience method Concurrent() can be used to
  138. // check for either case.
  139. // Compare returns the Ordering that describes a's relation to b.
  140. func (v Vector) Compare(b Vector) Ordering {
  141. var ai, bi int // index into a and b
  142. var av, bv Counter // value at current index
  143. result := Equal
  144. for ai < len(v.Counters) || bi < len(b.Counters) {
  145. var aMissing, bMissing bool
  146. if ai < len(v.Counters) {
  147. av = v.Counters[ai]
  148. } else {
  149. av = Counter{}
  150. aMissing = true
  151. }
  152. if bi < len(b.Counters) {
  153. bv = b.Counters[bi]
  154. } else {
  155. bv = Counter{}
  156. bMissing = true
  157. }
  158. switch {
  159. case av.ID == bv.ID:
  160. // We have a counter value for each side
  161. if av.Value > bv.Value {
  162. if result == Lesser {
  163. return ConcurrentLesser
  164. }
  165. result = Greater
  166. } else if av.Value < bv.Value {
  167. if result == Greater {
  168. return ConcurrentGreater
  169. }
  170. result = Lesser
  171. }
  172. case !aMissing && av.ID < bv.ID || bMissing:
  173. // Value is missing on the b side
  174. if av.Value > 0 {
  175. if result == Lesser {
  176. return ConcurrentLesser
  177. }
  178. result = Greater
  179. }
  180. case !bMissing && bv.ID < av.ID || aMissing:
  181. // Value is missing on the a side
  182. if bv.Value > 0 {
  183. if result == Greater {
  184. return ConcurrentGreater
  185. }
  186. result = Lesser
  187. }
  188. }
  189. if ai < len(v.Counters) && (av.ID <= bv.ID || bMissing) {
  190. ai++
  191. }
  192. if bi < len(b.Counters) && (bv.ID <= av.ID || aMissing) {
  193. bi++
  194. }
  195. }
  196. return result
  197. }