2 Star 2 Fork 1

cockroachdb/cockroach

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mvcc.go 5.98 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2016 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/LICENSE
package engineccl
import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/pkg/errors"
)
// MVCCIncrementalIterator iterates over the diff of the key range
// [startKey,endKey) and time range (startTime,endTime]. If a key was added or
// modified between startTime and endTime, the iterator will position at the
// most recent version (before or at endTime) of that key. If the key was most
// recently deleted, this is signalled with an empty value.
//
// Note: The endTime is inclusive to be consistent with the non-incremental
// iterator, where reads at a given timestamp return writes at that
// timestamp. The startTime is then made exclusive so that iterating time 1 to
// 2 and then 2 to 3 will only return values with time 2 once. An exclusive
// start time would normally make it difficult to scan timestamp 0, but
// CockroachDB uses that as a sentinel for key metadata anyway.
//
// Expected usage:
// iter := NewMVCCIncrementalIterator(e, startTime, endTime)
// defer iter.Close()
// for iter.Seek(startKey); ; iter.Next() {
// ok, err := iter.Valid()
// if !ok { ... }
// [code using iter.Key() and iter.Value()]
// }
// if err := iter.Error(); err != nil {
// ...
// }
type MVCCIncrementalIterator struct {
// TODO(dan): Move all this logic into c++ and make this a thin wrapper.
iter engine.Iterator
startTime hlc.Timestamp
endTime hlc.Timestamp
err error
valid bool
nextkey bool
// For allocation avoidance.
meta enginepb.MVCCMetadata
}
var _ engine.SimpleIterator = &MVCCIncrementalIterator{}
// NewMVCCIncrementalIterator creates an MVCCIncrementalIterator with the
// specified engine and time range.
func NewMVCCIncrementalIterator(
e engine.Reader, startTime, endTime hlc.Timestamp,
) *MVCCIncrementalIterator {
return &MVCCIncrementalIterator{
iter: e.NewTimeBoundIterator(startTime, endTime),
startTime: startTime,
endTime: endTime,
}
}
// Seek advances the iterator to the first key in the engine which is >= the
// provided key.
func (i *MVCCIncrementalIterator) Seek(startKey engine.MVCCKey) {
i.iter.Seek(startKey)
i.err = nil
i.valid = true
i.nextkey = false
i.NextKey()
}
// Close frees up resources held by the iterator.
func (i *MVCCIncrementalIterator) Close() {
i.iter.Close()
}
// Next advances the iterator to the next key/value in the iteration. After this
// call, Valid() will be true if the iterator was not positioned at the last
// key.
func (i *MVCCIncrementalIterator) Next() {
// TODO(dan): Implement Next. We'll need this for `RESTORE ... AS OF SYSTEM
// TIME`.
panic("unimplemented")
}
// NextKey advances the iterator to the next MVCC key. This operation is
// distinct from Next which advances to the next version of the current key or
// the next key if the iterator is currently located at the last version for a
// key.
func (i *MVCCIncrementalIterator) NextKey() {
for {
if !i.valid {
return
}
if ok, err := i.iter.Valid(); !ok {
i.err = err
i.valid = false
return
}
if i.nextkey {
i.nextkey = false
i.iter.NextKey()
continue
}
unsafeMetaKey := i.iter.UnsafeKey()
if unsafeMetaKey.IsValue() {
i.meta.Reset()
i.meta.Timestamp = unsafeMetaKey.Timestamp
} else {
if i.err = i.iter.ValueProto(&i.meta); i.err != nil {
i.valid = false
return
}
}
if i.meta.IsInline() {
// Inline values are only used in non-user data. They're not needed
// for backup, so they're not handled by this method. If one shows
// up, throw an error so it's obvious something is wrong.
i.valid = false
i.err = errors.Errorf("inline values are unsupported by MVCCIncrementalIterator: %s",
unsafeMetaKey.Key)
return
}
if i.meta.Txn != nil {
if !i.endTime.Less(i.meta.Timestamp) {
i.err = &roachpb.WriteIntentError{
Intents: []roachpb.Intent{{
Span: roachpb.Span{Key: i.iter.Key().Key},
Status: roachpb.PENDING,
Txn: *i.meta.Txn,
}},
}
i.valid = false
return
}
i.iter.Next()
continue
}
if i.endTime.Less(i.meta.Timestamp) {
i.iter.Next()
continue
}
if !i.startTime.Less(i.meta.Timestamp) {
i.iter.NextKey()
continue
}
// Skip tombstone (len=0) records when startTime is zero (non-incremental).
if (i.startTime == hlc.Timestamp{}) && len(i.iter.UnsafeValue()) == 0 {
i.iter.NextKey()
continue
}
i.nextkey = true
break
}
}
// Valid must be called after any call to Reset(), Next(), or similar methods.
// It returns (true, nil) if the iterator points to a valid key (it is undefined
// to call Key(), Value(), or similar methods unless Valid() has returned (true,
// nil)). It returns (false, nil) if the iterator has moved past the end of the
// valid range, or (false, err) if an error has occurred. Valid() will never
// return true with a non-nil error.
func (i *MVCCIncrementalIterator) Valid() (bool, error) {
return i.valid, i.err
}
// Key returns the current key.
func (i *MVCCIncrementalIterator) Key() engine.MVCCKey {
return i.iter.Key()
}
// Value returns the current value as a byte slice.
func (i *MVCCIncrementalIterator) Value() []byte {
return i.iter.Value()
}
// UnsafeKey returns the same key as Key, but the memory is invalidated on the
// next call to {Next,Reset,Close}.
func (i *MVCCIncrementalIterator) UnsafeKey() engine.MVCCKey {
return i.iter.UnsafeKey()
}
// UnsafeValue returns the same value as Value, but the memory is invalidated on
// the next call to {Next,Reset,Close}.
func (i *MVCCIncrementalIterator) UnsafeValue() []byte {
return i.iter.UnsafeValue()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_cockroachdb/cockroach.git
git@gitee.com:mirrors_cockroachdb/cockroach.git
mirrors_cockroachdb
cockroach
cockroach
v1.1.2

搜索帮助

Cb406eda 1850385 E526c682 1850385