index.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mvcc
  15. import (
  16. "sort"
  17. "sync"
  18. "github.com/google/btree"
  19. "go.uber.org/zap"
  20. )
  21. type index interface {
  22. Get(key []byte, atRev int64) (rev, created revision, ver int64, err error)
  23. Range(key, end []byte, atRev int64) ([][]byte, []revision)
  24. Revisions(key, end []byte, atRev int64) []revision
  25. Put(key []byte, rev revision)
  26. Tombstone(key []byte, rev revision) error
  27. RangeSince(key, end []byte, rev int64) []revision
  28. Compact(rev int64) map[revision]struct{}
  29. Keep(rev int64) map[revision]struct{}
  30. Equal(b index) bool
  31. Insert(ki *keyIndex)
  32. KeyIndex(ki *keyIndex) *keyIndex
  33. }
  34. type treeIndex struct {
  35. sync.RWMutex
  36. tree *btree.BTree
  37. lg *zap.Logger
  38. }
  39. func newTreeIndex(lg *zap.Logger) index {
  40. return &treeIndex{
  41. tree: btree.New(32),
  42. lg: lg,
  43. }
  44. }
  45. func (ti *treeIndex) Put(key []byte, rev revision) {
  46. keyi := &keyIndex{key: key}
  47. ti.Lock()
  48. defer ti.Unlock()
  49. item := ti.tree.Get(keyi)
  50. if item == nil {
  51. keyi.put(ti.lg, rev.main, rev.sub)
  52. ti.tree.ReplaceOrInsert(keyi)
  53. return
  54. }
  55. okeyi := item.(*keyIndex)
  56. okeyi.put(ti.lg, rev.main, rev.sub)
  57. }
  58. func (ti *treeIndex) Get(key []byte, atRev int64) (modified, created revision, ver int64, err error) {
  59. keyi := &keyIndex{key: key}
  60. ti.RLock()
  61. defer ti.RUnlock()
  62. if keyi = ti.keyIndex(keyi); keyi == nil {
  63. return revision{}, revision{}, 0, ErrRevisionNotFound
  64. }
  65. return keyi.get(ti.lg, atRev)
  66. }
  67. func (ti *treeIndex) KeyIndex(keyi *keyIndex) *keyIndex {
  68. ti.RLock()
  69. defer ti.RUnlock()
  70. return ti.keyIndex(keyi)
  71. }
  72. func (ti *treeIndex) keyIndex(keyi *keyIndex) *keyIndex {
  73. if item := ti.tree.Get(keyi); item != nil {
  74. return item.(*keyIndex)
  75. }
  76. return nil
  77. }
  78. func (ti *treeIndex) visit(key, end []byte, f func(ki *keyIndex)) {
  79. keyi, endi := &keyIndex{key: key}, &keyIndex{key: end}
  80. ti.RLock()
  81. defer ti.RUnlock()
  82. ti.tree.AscendGreaterOrEqual(keyi, func(item btree.Item) bool {
  83. if len(endi.key) > 0 && !item.Less(endi) {
  84. return false
  85. }
  86. f(item.(*keyIndex))
  87. return true
  88. })
  89. }
  90. func (ti *treeIndex) Revisions(key, end []byte, atRev int64) (revs []revision) {
  91. if end == nil {
  92. rev, _, _, err := ti.Get(key, atRev)
  93. if err != nil {
  94. return nil
  95. }
  96. return []revision{rev}
  97. }
  98. ti.visit(key, end, func(ki *keyIndex) {
  99. if rev, _, _, err := ki.get(ti.lg, atRev); err == nil {
  100. revs = append(revs, rev)
  101. }
  102. })
  103. return revs
  104. }
  105. func (ti *treeIndex) Range(key, end []byte, atRev int64) (keys [][]byte, revs []revision) {
  106. if end == nil {
  107. rev, _, _, err := ti.Get(key, atRev)
  108. if err != nil {
  109. return nil, nil
  110. }
  111. return [][]byte{key}, []revision{rev}
  112. }
  113. ti.visit(key, end, func(ki *keyIndex) {
  114. if rev, _, _, err := ki.get(ti.lg, atRev); err == nil {
  115. revs = append(revs, rev)
  116. keys = append(keys, ki.key)
  117. }
  118. })
  119. return keys, revs
  120. }
  121. func (ti *treeIndex) Tombstone(key []byte, rev revision) error {
  122. keyi := &keyIndex{key: key}
  123. ti.Lock()
  124. defer ti.Unlock()
  125. item := ti.tree.Get(keyi)
  126. if item == nil {
  127. return ErrRevisionNotFound
  128. }
  129. ki := item.(*keyIndex)
  130. return ki.tombstone(ti.lg, rev.main, rev.sub)
  131. }
  132. // RangeSince returns all revisions from key(including) to end(excluding)
  133. // at or after the given rev. The returned slice is sorted in the order
  134. // of revision.
  135. func (ti *treeIndex) RangeSince(key, end []byte, rev int64) []revision {
  136. keyi := &keyIndex{key: key}
  137. ti.RLock()
  138. defer ti.RUnlock()
  139. if end == nil {
  140. item := ti.tree.Get(keyi)
  141. if item == nil {
  142. return nil
  143. }
  144. keyi = item.(*keyIndex)
  145. return keyi.since(ti.lg, rev)
  146. }
  147. endi := &keyIndex{key: end}
  148. var revs []revision
  149. ti.tree.AscendGreaterOrEqual(keyi, func(item btree.Item) bool {
  150. if len(endi.key) > 0 && !item.Less(endi) {
  151. return false
  152. }
  153. curKeyi := item.(*keyIndex)
  154. revs = append(revs, curKeyi.since(ti.lg, rev)...)
  155. return true
  156. })
  157. sort.Sort(revisions(revs))
  158. return revs
  159. }
  160. func (ti *treeIndex) Compact(rev int64) map[revision]struct{} {
  161. available := make(map[revision]struct{})
  162. if ti.lg != nil {
  163. ti.lg.Info("compact tree index", zap.Int64("revision", rev))
  164. } else {
  165. plog.Printf("store.index: compact %d", rev)
  166. }
  167. ti.Lock()
  168. clone := ti.tree.Clone()
  169. ti.Unlock()
  170. clone.Ascend(func(item btree.Item) bool {
  171. keyi := item.(*keyIndex)
  172. //Lock is needed here to prevent modification to the keyIndex while
  173. //compaction is going on or revision added to empty before deletion
  174. ti.Lock()
  175. keyi.compact(ti.lg, rev, available)
  176. if keyi.isEmpty() {
  177. item := ti.tree.Delete(keyi)
  178. if item == nil {
  179. if ti.lg != nil {
  180. ti.lg.Panic("failed to delete during compaction")
  181. } else {
  182. plog.Panic("store.index: unexpected delete failure during compaction")
  183. }
  184. }
  185. }
  186. ti.Unlock()
  187. return true
  188. })
  189. return available
  190. }
  191. // Keep finds all revisions to be kept for a Compaction at the given rev.
  192. func (ti *treeIndex) Keep(rev int64) map[revision]struct{} {
  193. available := make(map[revision]struct{})
  194. ti.RLock()
  195. defer ti.RUnlock()
  196. ti.tree.Ascend(func(i btree.Item) bool {
  197. keyi := i.(*keyIndex)
  198. keyi.keep(rev, available)
  199. return true
  200. })
  201. return available
  202. }
  203. func (ti *treeIndex) Equal(bi index) bool {
  204. b := bi.(*treeIndex)
  205. if ti.tree.Len() != b.tree.Len() {
  206. return false
  207. }
  208. equal := true
  209. ti.tree.Ascend(func(item btree.Item) bool {
  210. aki := item.(*keyIndex)
  211. bki := b.tree.Get(item).(*keyIndex)
  212. if !aki.equal(bki) {
  213. equal = false
  214. return false
  215. }
  216. return true
  217. })
  218. return equal
  219. }
  220. func (ti *treeIndex) Insert(ki *keyIndex) {
  221. ti.Lock()
  222. defer ti.Unlock()
  223. ti.tree.ReplaceOrInsert(ki)
  224. }