read_tx.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "math"
  18. "sync"
  19. bolt "github.com/coreos/bbolt"
  20. )
  21. // safeRangeBucket is a hack to avoid inadvertently reading duplicate keys;
  22. // overwrites on a bucket should only fetch with limit=1, but safeRangeBucket
  23. // is known to never overwrite any key so range is safe.
  24. var safeRangeBucket = []byte("key")
  25. type ReadTx interface {
  26. Lock()
  27. Unlock()
  28. UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte)
  29. UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error
  30. }
  31. type readTx struct {
  32. // mu protects accesses to the txReadBuffer
  33. mu sync.RWMutex
  34. buf txReadBuffer
  35. // txmu protects accesses to buckets and tx on Range requests.
  36. txmu sync.RWMutex
  37. tx *bolt.Tx
  38. buckets map[string]*bolt.Bucket
  39. }
  40. func (rt *readTx) Lock() { rt.mu.RLock() }
  41. func (rt *readTx) Unlock() { rt.mu.RUnlock() }
  42. func (rt *readTx) UnsafeRange(bucketName, key, endKey []byte, limit int64) ([][]byte, [][]byte) {
  43. if endKey == nil {
  44. // forbid duplicates for single keys
  45. limit = 1
  46. }
  47. if limit <= 0 {
  48. limit = math.MaxInt64
  49. }
  50. if limit > 1 && !bytes.Equal(bucketName, safeRangeBucket) {
  51. panic("do not use unsafeRange on non-keys bucket")
  52. }
  53. keys, vals := rt.buf.Range(bucketName, key, endKey, limit)
  54. if int64(len(keys)) == limit {
  55. return keys, vals
  56. }
  57. // find/cache bucket
  58. bn := string(bucketName)
  59. rt.txmu.RLock()
  60. bucket, ok := rt.buckets[bn]
  61. rt.txmu.RUnlock()
  62. if !ok {
  63. rt.txmu.Lock()
  64. bucket = rt.tx.Bucket(bucketName)
  65. rt.buckets[bn] = bucket
  66. rt.txmu.Unlock()
  67. }
  68. // ignore missing bucket since may have been created in this batch
  69. if bucket == nil {
  70. return keys, vals
  71. }
  72. rt.txmu.Lock()
  73. c := bucket.Cursor()
  74. rt.txmu.Unlock()
  75. k2, v2 := unsafeRange(c, key, endKey, limit-int64(len(keys)))
  76. return append(k2, keys...), append(v2, vals...)
  77. }
  78. func (rt *readTx) UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error {
  79. dups := make(map[string]struct{})
  80. getDups := func(k, v []byte) error {
  81. dups[string(k)] = struct{}{}
  82. return nil
  83. }
  84. visitNoDup := func(k, v []byte) error {
  85. if _, ok := dups[string(k)]; ok {
  86. return nil
  87. }
  88. return visitor(k, v)
  89. }
  90. if err := rt.buf.ForEach(bucketName, getDups); err != nil {
  91. return err
  92. }
  93. rt.txmu.Lock()
  94. err := unsafeForEach(rt.tx, bucketName, visitNoDup)
  95. rt.txmu.Unlock()
  96. if err != nil {
  97. return err
  98. }
  99. return rt.buf.ForEach(bucketName, visitor)
  100. }
  101. func (rt *readTx) reset() {
  102. rt.buf.reset()
  103. rt.buckets = make(map[string]*bolt.Bucket)
  104. rt.tx = nil
  105. }