key_index.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. ErrRevisionNotFound = errors.New("stroage: revision not found")
  11. )
  12. // keyIndex stores the revision 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 revision // the main rev of the last modification
  54. generations []generation
  55. }
  56. // put puts a revision to the keyIndex.
  57. func (ki *keyIndex) put(main int64, sub int64) {
  58. rev := revision{main: main, sub: sub}
  59. if !rev.GreaterThan(ki.modified) {
  60. log.Panicf("store.keyindex: put with unexpected smaller revision [%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 revision, 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: []revision{modified}}
  79. ki.generations = append(ki.generations, g)
  80. }
  81. // tombstone puts a revision, 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 revision 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 revision, 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 revision{}, revision{}, 0, ErrRevisionNotFound
  99. }
  100. f := func(rev revision) 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 revision{}, revision{}, 0, ErrRevisionNotFound
  111. }
  112. // compact compacts a keyIndex by removing the versions with smaller or equal
  113. // revision 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[revision]struct{}) {
  117. if ki.isEmpty() {
  118. log.Panicf("store.keyindex: unexpected compact on empty keyIndex %s", string(ki.key))
  119. }
  120. // walk until reaching the first revision that has an revision smaller or equal to
  121. // the atRevision.
  122. // add it to the available map
  123. f := func(rev revision) bool {
  124. if rev.main <= atRev {
  125. available[rev] = struct{}{}
  126. return false
  127. }
  128. return true
  129. }
  130. i, g := 0, &ki.generations[0]
  131. // find first generation includes atRev or created after atRev
  132. for i < len(ki.generations)-1 {
  133. if tomb := g.revs[len(g.revs)-1].main; tomb > atRev {
  134. break
  135. }
  136. i++
  137. g = &ki.generations[i]
  138. }
  139. if !g.isEmpty() {
  140. n := g.walk(f)
  141. // remove the previous contents.
  142. if n != -1 {
  143. g.revs = g.revs[n:]
  144. }
  145. // remove any tombstone
  146. if len(g.revs) == 1 && i != len(ki.generations)-1 {
  147. delete(available, g.revs[0])
  148. i++
  149. }
  150. }
  151. // remove the previous generations.
  152. ki.generations = ki.generations[i:]
  153. return
  154. }
  155. func (ki *keyIndex) isEmpty() bool {
  156. return len(ki.generations) == 1 && ki.generations[0].isEmpty()
  157. }
  158. // findGeneartion finds out the generation of the keyIndex that the
  159. // given rev belongs to. If the given rev is at the gap of two generations,
  160. // which means that the key does not exist at the given rev, it returns nil.
  161. func (ki *keyIndex) findGeneration(rev int64) *generation {
  162. lastg := len(ki.generations) - 1
  163. cg := lastg
  164. for cg >= 0 {
  165. if len(ki.generations[cg].revs) == 0 {
  166. cg--
  167. continue
  168. }
  169. g := ki.generations[cg]
  170. if cg != lastg {
  171. if tomb := g.revs[len(g.revs)-1].main; tomb <= rev {
  172. return nil
  173. }
  174. }
  175. if g.revs[0].main <= rev {
  176. return &ki.generations[cg]
  177. }
  178. cg--
  179. }
  180. return nil
  181. }
  182. func (a *keyIndex) Less(b btree.Item) bool {
  183. return bytes.Compare(a.key, b.(*keyIndex).key) == -1
  184. }
  185. func (a *keyIndex) equal(b *keyIndex) bool {
  186. if !bytes.Equal(a.key, b.key) {
  187. return false
  188. }
  189. if a.modified != b.modified {
  190. return false
  191. }
  192. if len(a.generations) != len(b.generations) {
  193. return false
  194. }
  195. for i := range a.generations {
  196. ag, bg := a.generations[i], b.generations[i]
  197. if !ag.equal(bg) {
  198. return false
  199. }
  200. }
  201. return true
  202. }
  203. func (ki *keyIndex) String() string {
  204. var s string
  205. for _, g := range ki.generations {
  206. s += g.String()
  207. }
  208. return s
  209. }
  210. type generation struct {
  211. ver int64
  212. created revision // when the generation is created (put in first revision).
  213. revs []revision
  214. }
  215. func (g *generation) isEmpty() bool { return g == nil || len(g.revs) == 0 }
  216. // walk walks through the revisions in the generation in ascending order.
  217. // It passes the revision to the given function.
  218. // walk returns until: 1. it finishs walking all pairs 2. the function returns false.
  219. // walk returns the position at where it stopped. If it stopped after
  220. // finishing walking, -1 will be returned.
  221. func (g *generation) walk(f func(rev revision) bool) int {
  222. l := len(g.revs)
  223. for i := range g.revs {
  224. ok := f(g.revs[l-i-1])
  225. if !ok {
  226. return l - i - 1
  227. }
  228. }
  229. return -1
  230. }
  231. func (g *generation) String() string {
  232. return fmt.Sprintf("g: created[%d] ver[%d], revs %#v\n", g.created, g.ver, g.revs)
  233. }
  234. func (a generation) equal(b generation) bool {
  235. if a.ver != b.ver {
  236. return false
  237. }
  238. if len(a.revs) != len(b.revs) {
  239. return false
  240. }
  241. for i := range a.revs {
  242. ar, br := a.revs[i], b.revs[i]
  243. if ar != br {
  244. return false
  245. }
  246. }
  247. return true
  248. }