diff --git a/server/mvcc/hash.go b/server/mvcc/hash.go new file mode 100644 index 000000000..ecefd9af0 --- /dev/null +++ b/server/mvcc/hash.go @@ -0,0 +1,45 @@ +// Copyright 2022 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 mvcc + +import ( + "hash/crc32" + + "go.etcd.io/etcd/server/v3/mvcc/backend" + "go.etcd.io/etcd/server/v3/mvcc/buckets" +) + +func unsafeHashByRev(tx backend.ReadTx, lower, upper revision, keep map[revision]struct{}) (uint32, error) { + h := crc32.New(crc32.MakeTable(crc32.Castagnoli)) + + h.Write(buckets.Key.Name()) + err := tx.UnsafeForEach(buckets.Key, func(k, v []byte) error { + kr := bytesToRev(k) + if !upper.GreaterThan(kr) { + return nil + } + // skip revisions that are scheduled for deletion + // due to compacting; don't skip if there isn't one. + if lower.GreaterThan(kr) && len(keep) > 0 { + if _, ok := keep[kr]; !ok { + return nil + } + } + h.Write(k) + h.Write(v) + return nil + }) + return h.Sum32(), err +} diff --git a/server/mvcc/kvstore.go b/server/mvcc/kvstore.go index ba5e87f98..7601ad14b 100644 --- a/server/mvcc/kvstore.go +++ b/server/mvcc/kvstore.go @@ -18,7 +18,6 @@ import ( "context" "errors" "fmt" - "hash/crc32" "math" "sync" "time" @@ -196,29 +195,6 @@ func (s *store) HashByRev(rev int64) (hash uint32, currentRev int64, compactRev return hash, currentRev, compactRev, err } -func unsafeHashByRev(tx backend.ReadTx, lower, upper revision, keep map[revision]struct{}) (uint32, error) { - h := crc32.New(crc32.MakeTable(crc32.Castagnoli)) - - h.Write(buckets.Key.Name()) - err := tx.UnsafeForEach(buckets.Key, func(k, v []byte) error { - kr := bytesToRev(k) - if !upper.GreaterThan(kr) { - return nil - } - // skip revisions that are scheduled for deletion - // due to compacting; don't skip if there isn't one. - if lower.GreaterThan(kr) && len(keep) > 0 { - if _, ok := keep[kr]; !ok { - return nil - } - } - h.Write(k) - h.Write(v) - return nil - }) - return h.Sum32(), err -} - func (s *store) updateCompactRev(rev int64) (<-chan struct{}, error) { s.revMu.Lock() if rev <= s.compactMainRev {