key_index.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package storage
  2. import (
  3. "bytes"
  4. "errors"
  5. "log"
  6. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/google/btree"
  7. )
  8. var (
  9. ErrIndexNotFound = errors.New("index: not found")
  10. )
  11. // keyIndex stores the index of an key in the backend.
  12. // Each keyIndex has at least one key generation.
  13. // Each generation might have several key versions.
  14. // Tombstone on a key appends an tombstone version at the end
  15. // of the current generation and creates a new empty generation.
  16. // Each version of a key has an index pointing to the backend.
  17. //
  18. // For example: put(1);put(2);tombstone(3);put(4);tombstone(5) on key "foo"
  19. // generate a keyIndex:
  20. // key: "foo"
  21. // index: 5
  22. // generations:
  23. // {empty}
  24. // {4, 5(t)}
  25. // {1, 2, 3(t)}
  26. //
  27. // Compact a keyIndex removes the versions with smaller or equal to
  28. // index except the largest one. If the generations becomes empty
  29. // during compaction, it will be removed. if all the generations get
  30. // removed, the keyIndex Should be removed.
  31. // For example:
  32. // compact(2) on the previous example
  33. // generations:
  34. // {empty}
  35. // {4, 5(t)}
  36. // {2, 3(t)}
  37. //
  38. // compact(4)
  39. // generations:
  40. // {empty}
  41. // {4, 5(t)}
  42. //
  43. // compact(5):
  44. // generations:
  45. // {empty}
  46. // {5(t)}
  47. //
  48. // compact(6):
  49. // generations:
  50. // {empty} -> key SHOULD be removed.
  51. type keyIndex struct {
  52. key []byte
  53. index uint64
  54. generations []generation
  55. }
  56. // put puts an index to the keyIndex.
  57. func (ki *keyIndex) put(index uint64) {
  58. if index < ki.index {
  59. log.Panicf("store.keyindex: put with unexpected smaller index [%d / %d]", index, ki.index)
  60. }
  61. if len(ki.generations) == 0 {
  62. ki.generations = append(ki.generations, generation{})
  63. }
  64. g := &ki.generations[len(ki.generations)-1]
  65. g.cont = append(g.cont, index)
  66. g.ver++
  67. ki.index = index
  68. }
  69. // tombstone puts an index, pointing to a tombstone, to the keyIndex.
  70. // It also creates a new empty generation in the keyIndex.
  71. func (ki *keyIndex) tombstone(index uint64) {
  72. if ki.isEmpty() {
  73. log.Panicf("store.keyindex: unexpected tombstone on empty keyIndex %s", string(ki.key))
  74. }
  75. ki.put(index)
  76. ki.generations = append(ki.generations, generation{})
  77. }
  78. // get gets the index of thk that satisfies the given atIndex.
  79. // Index must be lower or equal to the given atIndex.
  80. func (ki *keyIndex) get(atIndex uint64) (index uint64, err error) {
  81. if ki.isEmpty() {
  82. log.Panicf("store.keyindex: unexpected get on empty keyIndex %s", string(ki.key))
  83. }
  84. g := ki.findGeneration(atIndex)
  85. if g.isEmpty() {
  86. return 0, ErrIndexNotFound
  87. }
  88. f := func(index, ver uint64) bool {
  89. if index <= atIndex {
  90. return false
  91. }
  92. return true
  93. }
  94. _, n := g.walk(f)
  95. if n != -1 {
  96. return g.cont[n], nil
  97. }
  98. return 0, ErrIndexNotFound
  99. }
  100. // compact compacts a keyIndex by removing the versions with smaller or equal
  101. // index than the given atIndex except the largest one.
  102. // If a generation becomes empty during compaction, it will be removed.
  103. func (ki *keyIndex) compact(atIndex uint64, available map[uint64]struct{}) {
  104. if ki.isEmpty() {
  105. log.Panic("store.keyindex: unexpected compact on empty keyIndex %s", string(ki.key))
  106. }
  107. // walk until reaching the first content that has an index smaller or equal to
  108. // the atIndex.
  109. // add all the reached indexes into available map.
  110. f := func(index, _ uint64) bool {
  111. available[index] = struct{}{}
  112. if index <= atIndex {
  113. return false
  114. }
  115. return true
  116. }
  117. g := ki.findGeneration(atIndex)
  118. i := len(ki.generations) - 1
  119. for i >= 0 {
  120. wg := &ki.generations[i]
  121. if wg == g {
  122. break
  123. }
  124. wg.walk(f)
  125. i--
  126. }
  127. _, n := g.walk(f)
  128. // remove the previous contents.
  129. if n != -1 {
  130. g.cont = g.cont[n:]
  131. }
  132. // remove the previous generations.
  133. ki.generations = ki.generations[i:]
  134. return
  135. }
  136. func (ki *keyIndex) isEmpty() bool {
  137. return len(ki.generations) == 1 && ki.generations[0].isEmpty()
  138. }
  139. // findGeneartion finds out the generation of the keyIndex that the
  140. // given index belongs to.
  141. func (ki *keyIndex) findGeneration(index uint64) *generation {
  142. g, youngerg := len(ki.generations)-1, len(ki.generations)-2
  143. // If the head index of a younger generation is smaller than
  144. // the given index, the index cannot be in the younger
  145. // generation.
  146. for youngerg >= 0 && ki.generations[youngerg].cont != nil {
  147. yg := ki.generations[youngerg]
  148. if yg.cont[len(yg.cont)-1] < index {
  149. break
  150. }
  151. g--
  152. youngerg--
  153. }
  154. if g < 0 {
  155. return nil
  156. }
  157. return &ki.generations[g]
  158. }
  159. func (a *keyIndex) Less(b btree.Item) bool {
  160. return bytes.Compare(a.key, b.(*keyIndex).key) == -1
  161. }
  162. type generation struct {
  163. ver uint64
  164. cont []uint64
  165. }
  166. func (g *generation) isEmpty() bool { return len(g.cont) == 0 }
  167. // walk walks through the (index, version) pairs in the generation in ascending order.
  168. // It passes the (index, version) to the given function.
  169. // walk returns until: 1. it finishs walking all pairs 2. the function returns false.
  170. // walk returns the (index, version) pair at where it stopped. If it stopped after
  171. // finishing walking, (0, -1) will be returned.
  172. func (g *generation) walk(f func(index, ver uint64) bool) (uint64, int) {
  173. ver := g.ver
  174. l := len(g.cont)
  175. for i := range g.cont {
  176. ok := f(g.cont[l-i-1], ver)
  177. if !ok {
  178. return ver, l - i - 1
  179. }
  180. ver--
  181. }
  182. return 0, -1
  183. }