index.go 4.6 KB

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