read_tx.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "github.com/boltdb/bolt"
  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. rt.txmu.Lock()
  57. // ignore error since bucket may have been created in this batch
  58. k2, v2, _ := unsafeRange(rt.tx, bucketName, key, endKey, limit-int64(len(keys)))
  59. rt.txmu.Unlock()
  60. return append(k2, keys...), append(v2, vals...)
  61. }
  62. func (rt *readTx) UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error {
  63. dups := make(map[string]struct{})
  64. f1 := func(k, v []byte) error {
  65. dups[string(k)] = struct{}{}
  66. return visitor(k, v)
  67. }
  68. f2 := func(k, v []byte) error {
  69. if _, ok := dups[string(k)]; ok {
  70. return nil
  71. }
  72. return visitor(k, v)
  73. }
  74. if err := rt.buf.ForEach(bucketName, f1); err != nil {
  75. return err
  76. }
  77. rt.txmu.Lock()
  78. err := unsafeForEach(rt.tx, bucketName, f2)
  79. rt.txmu.Unlock()
  80. return err
  81. }