index.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. )
  20. type index interface {
  21. Get(key []byte, atRev int64) (rev, created revision, ver int64, err error)
  22. Range(key, end []byte, atRev int64) ([][]byte, []revision)
  23. Put(key []byte, rev revision)
  24. Restore(key []byte, created, modified revision, ver int64)
  25. Tombstone(key []byte, rev revision) error
  26. RangeSince(key, end []byte, rev int64) []revision
  27. Compact(rev int64) map[revision]struct{}
  28. Equal(b index) bool
  29. }
  30. type treeIndex struct {
  31. sync.RWMutex
  32. tree *btree.BTree
  33. }
  34. func newTreeIndex() index {
  35. return &treeIndex{
  36. tree: btree.New(32),
  37. }
  38. }
  39. func (ti *treeIndex) Put(key []byte, rev revision) {
  40. keyi := &keyIndex{key: key}
  41. ti.Lock()
  42. defer ti.Unlock()
  43. item := ti.tree.Get(keyi)
  44. if item == nil {
  45. keyi.put(rev.main, rev.sub)
  46. ti.tree.ReplaceOrInsert(keyi)
  47. return
  48. }
  49. okeyi := item.(*keyIndex)
  50. okeyi.put(rev.main, rev.sub)
  51. }
  52. func (ti *treeIndex) Restore(key []byte, created, modified revision, ver int64) {
  53. keyi := &keyIndex{key: key}
  54. ti.Lock()
  55. defer ti.Unlock()
  56. item := ti.tree.Get(keyi)
  57. if item == nil {
  58. keyi.restore(created, modified, ver)
  59. ti.tree.ReplaceOrInsert(keyi)
  60. return
  61. }
  62. okeyi := item.(*keyIndex)
  63. okeyi.put(modified.main, modified.sub)
  64. }
  65. func (ti *treeIndex) Get(key []byte, atRev int64) (modified, created revision, ver int64, err error) {
  66. keyi := &keyIndex{key: key}
  67. ti.RLock()
  68. defer ti.RUnlock()
  69. item := ti.tree.Get(keyi)
  70. if item == nil {
  71. return revision{}, revision{}, 0, ErrRevisionNotFound
  72. }
  73. keyi = item.(*keyIndex)
  74. return keyi.get(atRev)
  75. }
  76. func (ti *treeIndex) Range(key, end []byte, atRev int64) (keys [][]byte, revs []revision) {
  77. if end == nil {
  78. rev, _, _, err := ti.Get(key, atRev)
  79. if err != nil {
  80. return nil, nil
  81. }
  82. return [][]byte{key}, []revision{rev}
  83. }
  84. keyi := &keyIndex{key: key}
  85. endi := &keyIndex{key: end}
  86. ti.RLock()
  87. defer ti.RUnlock()
  88. ti.tree.AscendGreaterOrEqual(keyi, func(item btree.Item) bool {
  89. if len(endi.key) > 0 && !item.Less(endi) {
  90. return false
  91. }
  92. curKeyi := item.(*keyIndex)
  93. rev, _, _, err := curKeyi.get(atRev)
  94. if err != nil {
  95. return true
  96. }
  97. revs = append(revs, rev)
  98. keys = append(keys, curKeyi.key)
  99. return true
  100. })
  101. return keys, revs
  102. }
  103. func (ti *treeIndex) Tombstone(key []byte, rev revision) error {
  104. keyi := &keyIndex{key: key}
  105. ti.Lock()
  106. defer ti.Unlock()
  107. item := ti.tree.Get(keyi)
  108. if item == nil {
  109. return ErrRevisionNotFound
  110. }
  111. ki := item.(*keyIndex)
  112. return ki.tombstone(rev.main, rev.sub)
  113. }
  114. // RangeSince returns all revisions from key(including) to end(excluding)
  115. // at or after the given rev. The returned slice is sorted in the order
  116. // of revision.
  117. func (ti *treeIndex) RangeSince(key, end []byte, rev int64) []revision {
  118. ti.RLock()
  119. defer ti.RUnlock()
  120. keyi := &keyIndex{key: key}
  121. if end == nil {
  122. item := ti.tree.Get(keyi)
  123. if item == nil {
  124. return nil
  125. }
  126. keyi = item.(*keyIndex)
  127. return keyi.since(rev)
  128. }
  129. endi := &keyIndex{key: end}
  130. var revs []revision
  131. ti.tree.AscendGreaterOrEqual(keyi, func(item btree.Item) bool {
  132. if len(endi.key) > 0 && !item.Less(endi) {
  133. return false
  134. }
  135. curKeyi := item.(*keyIndex)
  136. revs = append(revs, curKeyi.since(rev)...)
  137. return true
  138. })
  139. sort.Sort(revisions(revs))
  140. return revs
  141. }
  142. func (ti *treeIndex) Compact(rev int64) map[revision]struct{} {
  143. available := make(map[revision]struct{})
  144. var emptyki []*keyIndex
  145. plog.Printf("store.index: compact %d", rev)
  146. // TODO: do not hold the lock for long time?
  147. // This is probably OK. Compacting 10M keys takes O(10ms).
  148. ti.Lock()
  149. defer ti.Unlock()
  150. ti.tree.Ascend(compactIndex(rev, available, &emptyki))
  151. for _, ki := range emptyki {
  152. item := ti.tree.Delete(ki)
  153. if item == nil {
  154. plog.Panic("store.index: unexpected delete failure during compaction")
  155. }
  156. }
  157. return available
  158. }
  159. func compactIndex(rev int64, available map[revision]struct{}, emptyki *[]*keyIndex) func(i btree.Item) bool {
  160. return func(i btree.Item) bool {
  161. keyi := i.(*keyIndex)
  162. keyi.compact(rev, available)
  163. if keyi.isEmpty() {
  164. *emptyki = append(*emptyki, keyi)
  165. }
  166. return true
  167. }
  168. }
  169. func (a *treeIndex) Equal(bi index) bool {
  170. b := bi.(*treeIndex)
  171. if a.tree.Len() != b.tree.Len() {
  172. return false
  173. }
  174. equal := true
  175. a.tree.Ascend(func(item btree.Item) bool {
  176. aki := item.(*keyIndex)
  177. bki := b.tree.Get(item).(*keyIndex)
  178. if !aki.equal(bki) {
  179. equal = false
  180. return false
  181. }
  182. return true
  183. })
  184. return equal
  185. }