index.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package storage
  2. import (
  3. "log"
  4. "sync"
  5. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/google/btree"
  6. )
  7. type index interface {
  8. Get(key []byte, atRev int64) (rev, created reversion, ver int64, err error)
  9. Range(key, end []byte, atRev int64) ([][]byte, []reversion)
  10. Put(key []byte, rev reversion)
  11. Restore(key []byte, created, modified reversion, ver int64)
  12. Tombstone(key []byte, rev reversion) error
  13. Compact(rev int64) map[reversion]struct{}
  14. Equal(b index) bool
  15. }
  16. type treeIndex struct {
  17. sync.RWMutex
  18. tree *btree.BTree
  19. }
  20. func newTreeIndex() index {
  21. return &treeIndex{
  22. tree: btree.New(32),
  23. }
  24. }
  25. func (ti *treeIndex) Put(key []byte, rev reversion) {
  26. keyi := &keyIndex{key: key}
  27. ti.Lock()
  28. defer ti.Unlock()
  29. item := ti.tree.Get(keyi)
  30. if item == nil {
  31. keyi.put(rev.main, rev.sub)
  32. ti.tree.ReplaceOrInsert(keyi)
  33. return
  34. }
  35. okeyi := item.(*keyIndex)
  36. okeyi.put(rev.main, rev.sub)
  37. }
  38. func (ti *treeIndex) Restore(key []byte, created, modified reversion, ver int64) {
  39. keyi := &keyIndex{key: key}
  40. ti.Lock()
  41. defer ti.Unlock()
  42. item := ti.tree.Get(keyi)
  43. if item == nil {
  44. keyi.restore(created, modified, ver)
  45. ti.tree.ReplaceOrInsert(keyi)
  46. return
  47. }
  48. okeyi := item.(*keyIndex)
  49. okeyi.put(modified.main, modified.sub)
  50. }
  51. func (ti *treeIndex) Get(key []byte, atRev int64) (modified, created reversion, ver int64, err error) {
  52. keyi := &keyIndex{key: key}
  53. ti.RLock()
  54. defer ti.RUnlock()
  55. item := ti.tree.Get(keyi)
  56. if item == nil {
  57. return reversion{}, reversion{}, 0, ErrReversionNotFound
  58. }
  59. keyi = item.(*keyIndex)
  60. return keyi.get(atRev)
  61. }
  62. func (ti *treeIndex) Range(key, end []byte, atRev int64) (keys [][]byte, revs []reversion) {
  63. if end == nil {
  64. rev, _, _, err := ti.Get(key, atRev)
  65. if err != nil {
  66. return nil, nil
  67. }
  68. return [][]byte{key}, []reversion{rev}
  69. }
  70. keyi := &keyIndex{key: key}
  71. endi := &keyIndex{key: end}
  72. ti.RLock()
  73. defer ti.RUnlock()
  74. ti.tree.AscendGreaterOrEqual(keyi, func(item btree.Item) bool {
  75. if !item.Less(endi) {
  76. return false
  77. }
  78. curKeyi := item.(*keyIndex)
  79. rev, _, _, err := curKeyi.get(atRev)
  80. if err != nil {
  81. return true
  82. }
  83. revs = append(revs, rev)
  84. keys = append(keys, curKeyi.key)
  85. return true
  86. })
  87. return keys, revs
  88. }
  89. func (ti *treeIndex) Tombstone(key []byte, rev reversion) error {
  90. keyi := &keyIndex{key: key}
  91. ti.Lock()
  92. defer ti.Unlock()
  93. item := ti.tree.Get(keyi)
  94. if item == nil {
  95. return ErrReversionNotFound
  96. }
  97. ki := item.(*keyIndex)
  98. ki.tombstone(rev.main, rev.sub)
  99. return nil
  100. }
  101. func (ti *treeIndex) Compact(rev int64) map[reversion]struct{} {
  102. available := make(map[reversion]struct{})
  103. emptyki := make([]*keyIndex, 0)
  104. log.Printf("store.index: compact %d", rev)
  105. // TODO: do not hold the lock for long time?
  106. // This is probably OK. Compacting 10M keys takes O(10ms).
  107. ti.Lock()
  108. defer ti.Unlock()
  109. ti.tree.Ascend(compactIndex(rev, available, &emptyki))
  110. for _, ki := range emptyki {
  111. item := ti.tree.Delete(ki)
  112. if item == nil {
  113. log.Panic("store.index: unexpected delete failure during compaction")
  114. }
  115. }
  116. return available
  117. }
  118. func compactIndex(rev int64, available map[reversion]struct{}, emptyki *[]*keyIndex) func(i btree.Item) bool {
  119. return func(i btree.Item) bool {
  120. keyi := i.(*keyIndex)
  121. keyi.compact(rev, available)
  122. if keyi.isEmpty() {
  123. *emptyki = append(*emptyki, keyi)
  124. }
  125. return true
  126. }
  127. }
  128. func (a *treeIndex) Equal(bi index) bool {
  129. b := bi.(*treeIndex)
  130. if a.tree.Len() != b.tree.Len() {
  131. return false
  132. }
  133. equal := true
  134. a.tree.Ascend(func(item btree.Item) bool {
  135. aki := item.(*keyIndex)
  136. bki := b.tree.Get(item).(*keyIndex)
  137. if !aki.equal(bki) {
  138. equal = false
  139. return false
  140. }
  141. return true
  142. })
  143. return equal
  144. }