2017-01-05 13:07:50 +03:00
|
|
|
// Copyright 2017 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
|
2017-07-06 00:32:13 +03:00
|
|
|
bolt "github.com/coreos/bbolt"
|
2017-01-05 13:07:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// safeRangeBucket is a hack to avoid inadvertently reading duplicate keys;
|
|
|
|
// overwrites on a bucket should only fetch with limit=1, but safeRangeBucket
|
|
|
|
// is known to never overwrite any key so range is safe.
|
|
|
|
var safeRangeBucket = []byte("key")
|
|
|
|
|
|
|
|
type ReadTx interface {
|
|
|
|
Lock()
|
|
|
|
Unlock()
|
|
|
|
|
|
|
|
UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte)
|
|
|
|
UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type readTx struct {
|
|
|
|
// mu protects accesses to the txReadBuffer
|
|
|
|
mu sync.RWMutex
|
|
|
|
buf txReadBuffer
|
|
|
|
|
|
|
|
// txmu protects accesses to the Tx on Range requests
|
|
|
|
txmu sync.Mutex
|
|
|
|
tx *bolt.Tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *readTx) Lock() { rt.mu.RLock() }
|
|
|
|
func (rt *readTx) Unlock() { rt.mu.RUnlock() }
|
|
|
|
|
|
|
|
func (rt *readTx) UnsafeRange(bucketName, key, endKey []byte, limit int64) ([][]byte, [][]byte) {
|
|
|
|
if endKey == nil {
|
|
|
|
// forbid duplicates for single keys
|
|
|
|
limit = 1
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
limit = math.MaxInt64
|
|
|
|
}
|
|
|
|
if limit > 1 && !bytes.Equal(bucketName, safeRangeBucket) {
|
|
|
|
panic("do not use unsafeRange on non-keys bucket")
|
|
|
|
}
|
|
|
|
keys, vals := rt.buf.Range(bucketName, key, endKey, limit)
|
|
|
|
if int64(len(keys)) == limit {
|
|
|
|
return keys, vals
|
|
|
|
}
|
|
|
|
// ignore error since bucket may have been created in this batch
|
2017-05-03 19:59:19 +03:00
|
|
|
k2, v2, _ := unsafeRange(rt.tx, bucketName, key, endKey, limit-int64(len(keys)), &rt.txmu)
|
2017-01-05 13:07:50 +03:00
|
|
|
return append(k2, keys...), append(v2, vals...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *readTx) UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error {
|
|
|
|
dups := make(map[string]struct{})
|
2017-07-28 03:42:19 +03:00
|
|
|
getDups := func(k, v []byte) error {
|
2017-01-05 13:07:50 +03:00
|
|
|
dups[string(k)] = struct{}{}
|
2017-07-28 03:42:19 +03:00
|
|
|
return nil
|
2017-01-05 13:07:50 +03:00
|
|
|
}
|
2017-07-28 03:42:19 +03:00
|
|
|
visitNoDup := func(k, v []byte) error {
|
2017-01-05 13:07:50 +03:00
|
|
|
if _, ok := dups[string(k)]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return visitor(k, v)
|
|
|
|
}
|
2017-07-28 03:42:19 +03:00
|
|
|
if err := rt.buf.ForEach(bucketName, getDups); err != nil {
|
2017-01-05 13:07:50 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
rt.txmu.Lock()
|
2017-07-28 03:42:19 +03:00
|
|
|
err := unsafeForEach(rt.tx, bucketName, visitNoDup)
|
2017-01-05 13:07:50 +03:00
|
|
|
rt.txmu.Unlock()
|
2017-07-28 03:42:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rt.buf.ForEach(bucketName, visitor)
|
2017-01-05 13:07:50 +03:00
|
|
|
}
|