read_tx.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 the Tx on Range requests
  36. txmu sync.Mutex
  37. tx *bolt.Tx
  38. }
  39. func (rt *readTx) Lock() { rt.mu.RLock() }
  40. func (rt *readTx) Unlock() { rt.mu.RUnlock() }
  41. func (rt *readTx) UnsafeRange(bucketName, key, endKey []byte, limit int64) ([][]byte, [][]byte) {
  42. if endKey == nil {
  43. // forbid duplicates for single keys
  44. limit = 1
  45. }
  46. if limit <= 0 {
  47. limit = math.MaxInt64
  48. }
  49. if limit > 1 && !bytes.Equal(bucketName, safeRangeBucket) {
  50. panic("do not use unsafeRange on non-keys bucket")
  51. }
  52. keys, vals := rt.buf.Range(bucketName, key, endKey, limit)
  53. if int64(len(keys)) == limit {
  54. return keys, vals
  55. }
  56. // ignore error since bucket may have been created in this batch
  57. k2, v2, _ := unsafeRange(rt.tx, bucketName, key, endKey, limit-int64(len(keys)), &rt.txmu)
  58. return append(k2, keys...), append(v2, vals...)
  59. }
  60. func (rt *readTx) UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error {
  61. dups := make(map[string]struct{})
  62. f1 := func(k, v []byte) error {
  63. dups[string(k)] = struct{}{}
  64. return visitor(k, v)
  65. }
  66. f2 := func(k, v []byte) error {
  67. if _, ok := dups[string(k)]; ok {
  68. return nil
  69. }
  70. return visitor(k, v)
  71. }
  72. if err := rt.buf.ForEach(bucketName, f1); err != nil {
  73. return err
  74. }
  75. rt.txmu.Lock()
  76. err := unsafeForEach(rt.tx, bucketName, f2)
  77. rt.txmu.Unlock()
  78. return err
  79. }