key_index.go 7.1 KB

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