vector.go 5.6 KB

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