tx_buffer.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // unsafeCopy returns a copy of txReadBuffer, caller should acquire backend.readTx.RLock()
  80. func (txr *txReadBuffer) unsafeCopy() txReadBuffer {
  81. txrCopy := txReadBuffer{
  82. txBuffer: txBuffer{
  83. buckets: make(map[string]*bucketBuffer, len(txr.txBuffer.buckets)),
  84. },
  85. }
  86. for bucketName, bucket := range txr.txBuffer.buckets {
  87. txrCopy.txBuffer.buckets[bucketName] = bucket.Copy()
  88. }
  89. return txrCopy
  90. }
  91. type kv struct {
  92. key []byte
  93. val []byte
  94. }
  95. // bucketBuffer buffers key-value pairs that are pending commit.
  96. type bucketBuffer struct {
  97. buf []kv
  98. // used tracks number of elements in use so buf can be reused without reallocation.
  99. used int
  100. }
  101. func newBucketBuffer() *bucketBuffer {
  102. return &bucketBuffer{buf: make([]kv, 512), used: 0}
  103. }
  104. func (bb *bucketBuffer) Range(key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte) {
  105. f := func(i int) bool { return bytes.Compare(bb.buf[i].key, key) >= 0 }
  106. idx := sort.Search(bb.used, f)
  107. if idx < 0 {
  108. return nil, nil
  109. }
  110. if len(endKey) == 0 {
  111. if bytes.Equal(key, bb.buf[idx].key) {
  112. keys = append(keys, bb.buf[idx].key)
  113. vals = append(vals, bb.buf[idx].val)
  114. }
  115. return keys, vals
  116. }
  117. if bytes.Compare(endKey, bb.buf[idx].key) <= 0 {
  118. return nil, nil
  119. }
  120. for i := idx; i < bb.used && int64(len(keys)) < limit; i++ {
  121. if bytes.Compare(endKey, bb.buf[i].key) <= 0 {
  122. break
  123. }
  124. keys = append(keys, bb.buf[i].key)
  125. vals = append(vals, bb.buf[i].val)
  126. }
  127. return keys, vals
  128. }
  129. func (bb *bucketBuffer) ForEach(visitor func(k, v []byte) error) error {
  130. for i := 0; i < bb.used; i++ {
  131. if err := visitor(bb.buf[i].key, bb.buf[i].val); err != nil {
  132. return err
  133. }
  134. }
  135. return nil
  136. }
  137. func (bb *bucketBuffer) add(k, v []byte) {
  138. bb.buf[bb.used].key, bb.buf[bb.used].val = k, v
  139. bb.used++
  140. if bb.used == len(bb.buf) {
  141. buf := make([]kv, (3*len(bb.buf))/2)
  142. copy(buf, bb.buf)
  143. bb.buf = buf
  144. }
  145. }
  146. // merge merges data from bbsrc into bb.
  147. func (bb *bucketBuffer) merge(bbsrc *bucketBuffer) {
  148. for i := 0; i < bbsrc.used; i++ {
  149. bb.add(bbsrc.buf[i].key, bbsrc.buf[i].val)
  150. }
  151. if bb.used == bbsrc.used {
  152. return
  153. }
  154. if bytes.Compare(bb.buf[(bb.used-bbsrc.used)-1].key, bbsrc.buf[0].key) < 0 {
  155. return
  156. }
  157. sort.Stable(bb)
  158. // remove duplicates, using only newest update
  159. widx := 0
  160. for ridx := 1; ridx < bb.used; ridx++ {
  161. if !bytes.Equal(bb.buf[ridx].key, bb.buf[widx].key) {
  162. widx++
  163. }
  164. bb.buf[widx] = bb.buf[ridx]
  165. }
  166. bb.used = widx + 1
  167. }
  168. func (bb *bucketBuffer) Len() int { return bb.used }
  169. func (bb *bucketBuffer) Less(i, j int) bool {
  170. return bytes.Compare(bb.buf[i].key, bb.buf[j].key) < 0
  171. }
  172. func (bb *bucketBuffer) Swap(i, j int) { bb.buf[i], bb.buf[j] = bb.buf[j], bb.buf[i] }
  173. func (bb *bucketBuffer) Copy() *bucketBuffer {
  174. bbCopy := bucketBuffer{
  175. buf: make([]kv, len(bb.buf)),
  176. used: bb.used,
  177. }
  178. copy(bbCopy.buf, bb.buf)
  179. return &bbCopy
  180. }