tx_buffer.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2017 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 backend
  15. import (
  16. "bytes"
  17. "sort"
  18. )
  19. // txBuffer handles functionality shared between txWriteBuffer and txReadBuffer.
  20. type txBuffer struct {
  21. buckets map[string]*bucketBuffer
  22. }
  23. func (txb *txBuffer) reset() {
  24. for k, v := range txb.buckets {
  25. if v.used == 0 {
  26. // demote
  27. delete(txb.buckets, k)
  28. }
  29. v.used = 0
  30. }
  31. }
  32. // txWriteBuffer buffers writes of pending updates that have not yet committed.
  33. type txWriteBuffer struct {
  34. txBuffer
  35. seq bool
  36. }
  37. func (txw *txWriteBuffer) put(bucket, k, v []byte) {
  38. txw.seq = false
  39. txw.putSeq(bucket, k, v)
  40. }
  41. func (txw *txWriteBuffer) putSeq(bucket, k, v []byte) {
  42. b, ok := txw.buckets[string(bucket)]
  43. if !ok {
  44. b = newBucketBuffer()
  45. txw.buckets[string(bucket)] = b
  46. }
  47. b.add(k, v)
  48. }
  49. func (txw *txWriteBuffer) writeback(txr *txReadBuffer) {
  50. for k, wb := range txw.buckets {
  51. rb, ok := txr.buckets[k]
  52. if !ok {
  53. delete(txw.buckets, k)
  54. txr.buckets[k] = wb
  55. continue
  56. }
  57. if !txw.seq && wb.used > 1 {
  58. // assume no duplicate keys
  59. sort.Sort(wb)
  60. }
  61. rb.merge(wb)
  62. }
  63. txw.reset()
  64. }
  65. // txReadBuffer accesses buffered updates.
  66. type txReadBuffer struct{ txBuffer }
  67. func (txr *txReadBuffer) Range(bucketName, key, endKey []byte, limit int64) ([][]byte, [][]byte) {
  68. if b := txr.buckets[string(bucketName)]; b != nil {
  69. return b.Range(key, endKey, limit)
  70. }
  71. return nil, nil
  72. }
  73. func (txr *txReadBuffer) ForEach(bucketName []byte, visitor func(k, v []byte) error) error {
  74. if b := txr.buckets[string(bucketName)]; b != nil {
  75. return b.ForEach(visitor)
  76. }
  77. return nil
  78. }
  79. type kv struct {
  80. key []byte
  81. val []byte
  82. }
  83. // bucketBuffer buffers key-value pairs that are pending commit.
  84. type bucketBuffer struct {
  85. buf []kv
  86. // used tracks number of elements in use so buf can be reused without reallocation.
  87. used int
  88. }
  89. func newBucketBuffer() *bucketBuffer {
  90. return &bucketBuffer{buf: make([]kv, 512), used: 0}
  91. }
  92. func (bb *bucketBuffer) Range(key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte) {
  93. f := func(i int) bool { return bytes.Compare(bb.buf[i].key, key) >= 0 }
  94. idx := sort.Search(bb.used, f)
  95. if idx < 0 {
  96. return nil, nil
  97. }
  98. if len(endKey) == 0 {
  99. if bytes.Equal(key, bb.buf[idx].key) {
  100. keys = append(keys, bb.buf[idx].key)
  101. vals = append(vals, bb.buf[idx].val)
  102. }
  103. return keys, vals
  104. }
  105. if bytes.Compare(endKey, bb.buf[idx].key) <= 0 {
  106. return nil, nil
  107. }
  108. for i := idx; i < bb.used && int64(len(keys)) < limit; i++ {
  109. if bytes.Compare(endKey, bb.buf[i].key) <= 0 {
  110. break
  111. }
  112. keys = append(keys, bb.buf[i].key)
  113. vals = append(vals, bb.buf[i].val)
  114. }
  115. return keys, vals
  116. }
  117. func (bb *bucketBuffer) ForEach(visitor func(k, v []byte) error) error {
  118. for i := 0; i < bb.used; i++ {
  119. if err := visitor(bb.buf[i].key, bb.buf[i].val); err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }
  125. func (bb *bucketBuffer) add(k, v []byte) {
  126. bb.buf[bb.used].key, bb.buf[bb.used].val = k, v
  127. bb.used++
  128. if bb.used == len(bb.buf) {
  129. buf := make([]kv, (3*len(bb.buf))/2)
  130. copy(buf, bb.buf)
  131. bb.buf = buf
  132. }
  133. }
  134. // merge merges data from bb into bbsrc.
  135. func (bb *bucketBuffer) merge(bbsrc *bucketBuffer) {
  136. for i := 0; i < bbsrc.used; i++ {
  137. bb.add(bbsrc.buf[i].key, bbsrc.buf[i].val)
  138. }
  139. if bb.used == bbsrc.used {
  140. return
  141. }
  142. if bytes.Compare(bb.buf[(bb.used-bbsrc.used)-1].key, bbsrc.buf[0].key) < 0 {
  143. return
  144. }
  145. sort.Stable(bb)
  146. // remove duplicates, using only newest update
  147. widx := 0
  148. for ridx := 1; ridx < bb.used; ridx++ {
  149. if !bytes.Equal(bb.buf[ridx].key, bb.buf[widx].key) {
  150. widx++
  151. }
  152. bb.buf[widx] = bb.buf[ridx]
  153. }
  154. bb.used = widx + 1
  155. }
  156. func (bb *bucketBuffer) Len() int { return bb.used }
  157. func (bb *bucketBuffer) Less(i, j int) bool {
  158. return bytes.Compare(bb.buf[i].key, bb.buf[j].key) < 0
  159. }
  160. func (bb *bucketBuffer) Swap(i, j int) { bb.buf[i], bb.buf[j] = bb.buf[j], bb.buf[i] }