key_index.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package storage
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/google/btree"
  8. )
  9. var (
  10. ErrReversionNotFound = errors.New("stroage: reversion not found")
  11. )
  12. // keyIndex stores the reversion of an key in the backend.
  13. // Each keyIndex has at least one key generation.
  14. // Each generation might have several key versions.
  15. // Tombstone on a key appends an tombstone version at the end
  16. // of the current generation and creates a new empty generation.
  17. // Each version of a key has an index pointing to the backend.
  18. //
  19. // For example: put(1.0);put(2.0);tombstone(3.0);put(4.0);tombstone(5.0) on key "foo"
  20. // generate a keyIndex:
  21. // key: "foo"
  22. // rev: 5
  23. // generations:
  24. // {empty}
  25. // {4.0, 5.0(t)}
  26. // {1.0, 2.0, 3.0(t)}
  27. //
  28. // Compact a keyIndex removes the versions with smaller or equal to
  29. // rev except the largest one. If the generations becomes empty
  30. // during compaction, it will be removed. if all the generations get
  31. // removed, the keyIndex Should be removed.
  32. // For example:
  33. // compact(2) on the previous example
  34. // generations:
  35. // {empty}
  36. // {4.0, 5.0(t)}
  37. // {2.0, 3.0(t)}
  38. //
  39. // compact(4)
  40. // generations:
  41. // {empty}
  42. // {4.0, 5.0(t)}
  43. //
  44. // compact(5):
  45. // generations:
  46. // {empty} -> key SHOULD be removed.
  47. //
  48. // compact(6):
  49. // generations:
  50. // {empty} -> key SHOULD be removed.
  51. type keyIndex struct {
  52. key []byte
  53. modified reversion // the main rev of the last modification
  54. generations []generation
  55. }
  56. // put puts a reversion to the keyIndex.
  57. func (ki *keyIndex) put(main int64, sub int64) {
  58. rev := reversion{main: main, sub: sub}
  59. if !rev.GreaterThan(ki.modified) {
  60. log.Panicf("store.keyindex: put with unexpected smaller reversion [%v / %v]", rev, ki.modified)
  61. }
  62. if len(ki.generations) == 0 {
  63. ki.generations = append(ki.generations, generation{})
  64. }
  65. g := &ki.generations[len(ki.generations)-1]
  66. if len(g.revs) == 0 {
  67. g.created = rev
  68. }
  69. g.revs = append(g.revs, rev)
  70. g.ver++
  71. ki.modified = rev
  72. }
  73. func (ki *keyIndex) restore(created, modified reversion, ver int64) {
  74. if len(ki.generations) != 0 {
  75. log.Panicf("store.keyindex: cannot restore non-empty keyIndex")
  76. }
  77. ki.modified = modified
  78. g := generation{created: created, ver: ver, revs: []reversion{modified}}
  79. ki.generations = append(ki.generations, g)
  80. }
  81. // tombstone puts a reversion, pointing to a tombstone, to the keyIndex.
  82. // It also creates a new empty generation in the keyIndex.
  83. func (ki *keyIndex) tombstone(main int64, sub int64) {
  84. if ki.isEmpty() {
  85. log.Panicf("store.keyindex: unexpected tombstone on empty keyIndex %s", string(ki.key))
  86. }
  87. ki.put(main, sub)
  88. ki.generations = append(ki.generations, generation{})
  89. }
  90. // get gets the modified, created reversion and version of the key that satisfies the given atRev.
  91. // Rev must be higher than or equal to the given atRev.
  92. func (ki *keyIndex) get(atRev int64) (modified, created reversion, ver int64, err error) {
  93. if ki.isEmpty() {
  94. log.Panicf("store.keyindex: unexpected get on empty keyIndex %s", string(ki.key))
  95. }
  96. g := ki.findGeneration(atRev)
  97. if g.isEmpty() {
  98. return reversion{}, reversion{}, 0, ErrReversionNotFound
  99. }
  100. f := func(rev reversion) bool {
  101. if rev.main <= atRev {
  102. return false
  103. }
  104. return true
  105. }
  106. n := g.walk(f)
  107. if n != -1 {
  108. return g.revs[n], g.created, g.ver - int64(len(g.revs)-n-1), nil
  109. }
  110. return reversion{}, reversion{}, 0, ErrReversionNotFound
  111. }
  112. // compact compacts a keyIndex by removing the versions with smaller or equal
  113. // reversion than the given atRev except the largest one (If the largest one is
  114. // a tombstone, it will not be kept).
  115. // If a generation becomes empty during compaction, it will be removed.
  116. func (ki *keyIndex) compact(atRev int64, available map[reversion]struct{}) {
  117. if ki.isEmpty() {
  118. log.Panic("store.keyindex: unexpected compact on empty keyIndex %s", string(ki.key))
  119. }
  120. // walk until reaching the first reversion that has an reversion smaller or equal to
  121. // the atReversion.
  122. // add it to the available map
  123. f := func(rev reversion) bool {
  124. if rev.main <= atRev {
  125. available[rev] = struct{}{}
  126. return false
  127. }
  128. return true
  129. }
  130. g := ki.findGeneration(atRev)
  131. if g == nil {
  132. return
  133. }
  134. i := 0
  135. for i <= len(ki.generations)-1 {
  136. wg := &ki.generations[i]
  137. if wg == g {
  138. break
  139. }
  140. i++
  141. }
  142. if !g.isEmpty() {
  143. n := g.walk(f)
  144. // remove the previous contents.
  145. if n != -1 {
  146. g.revs = g.revs[n:]
  147. }
  148. // remove any tombstone
  149. if len(g.revs) == 1 && i != len(ki.generations)-1 {
  150. delete(available, g.revs[0])
  151. i++
  152. }
  153. }
  154. // remove the previous generations.
  155. ki.generations = ki.generations[i:]
  156. return
  157. }
  158. func (ki *keyIndex) isEmpty() bool {
  159. return len(ki.generations) == 1 && ki.generations[0].isEmpty()
  160. }
  161. // findGeneartion finds out the generation of the keyIndex that the
  162. // given index belongs to.
  163. func (ki *keyIndex) findGeneration(rev int64) *generation {
  164. cg := len(ki.generations) - 1
  165. for cg >= 0 {
  166. if len(ki.generations[cg].revs) == 0 {
  167. cg--
  168. continue
  169. }
  170. g := ki.generations[cg]
  171. if g.revs[0].main <= rev {
  172. return &ki.generations[cg]
  173. }
  174. cg--
  175. }
  176. return nil
  177. }
  178. func (a *keyIndex) Less(b btree.Item) bool {
  179. return bytes.Compare(a.key, b.(*keyIndex).key) == -1
  180. }
  181. func (a *keyIndex) equal(b *keyIndex) bool {
  182. if !bytes.Equal(a.key, b.key) {
  183. return false
  184. }
  185. if a.modified != b.modified {
  186. return false
  187. }
  188. if len(a.generations) != len(b.generations) {
  189. return false
  190. }
  191. for i := range a.generations {
  192. ag, bg := a.generations[i], b.generations[i]
  193. if !ag.equal(bg) {
  194. return false
  195. }
  196. }
  197. return true
  198. }
  199. func (ki *keyIndex) String() string {
  200. var s string
  201. for _, g := range ki.generations {
  202. s += g.String()
  203. }
  204. return s
  205. }
  206. type generation struct {
  207. ver int64
  208. created reversion // when the generation is created (put in first reversion).
  209. revs []reversion
  210. }
  211. func (g *generation) isEmpty() bool { return g == nil || len(g.revs) == 0 }
  212. // walk walks through the reversions in the generation in ascending order.
  213. // It passes the revision to the given function.
  214. // walk returns until: 1. it finishs walking all pairs 2. the function returns false.
  215. // walk returns the position at where it stopped. If it stopped after
  216. // finishing walking, -1 will be returned.
  217. func (g *generation) walk(f func(rev reversion) bool) int {
  218. l := len(g.revs)
  219. for i := range g.revs {
  220. ok := f(g.revs[l-i-1])
  221. if !ok {
  222. return l - i - 1
  223. }
  224. }
  225. return -1
  226. }
  227. func (g *generation) String() string {
  228. return fmt.Sprintf("g: created[%d] ver[%d], revs %#v\n", g.created, g.ver, g.revs)
  229. }
  230. func (a generation) equal(b generation) bool {
  231. if a.ver != b.ver {
  232. return false
  233. }
  234. if len(a.revs) != len(b.revs) {
  235. return false
  236. }
  237. for i := range a.revs {
  238. ar, br := a.revs[i], b.revs[i]
  239. if ar != br {
  240. return false
  241. }
  242. }
  243. return true
  244. }