index.go 5.7 KB

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