index.go 5.0 KB

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