-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
kv_fetcher.go
97 lines (87 loc) · 2.38 KB
/
kv_fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package row
import (
"context"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
)
// KVFetcher wraps kvBatchFetcher, providing a NextKV interface that returns the
// next kv from its input.
type KVFetcher struct {
kvBatchFetcher
kvs []roachpb.KeyValue
batchResponse []byte
bytesRead int64
Span roachpb.Span
newSpan bool
}
// NewKVFetcher creates a new KVFetcher.
func NewKVFetcher(
txn *kv.Txn,
spans roachpb.Spans,
reverse bool,
useBatchLimit bool,
firstBatchLimit int64,
lockStr sqlbase.ScanLockingStrength,
returnRangeInfo bool,
) (*KVFetcher, error) {
kvBatchFetcher, err := makeKVBatchFetcher(
txn, spans, reverse, useBatchLimit, firstBatchLimit, lockStr, returnRangeInfo,
)
return newKVFetcher(&kvBatchFetcher), err
}
func newKVFetcher(batchFetcher kvBatchFetcher) *KVFetcher {
return &KVFetcher{
kvBatchFetcher: batchFetcher,
}
}
// NextKV returns the next kv from this fetcher. Returns false if there are no
// more kvs to fetch, the kv that was fetched, and any errors that may have
// occurred.
func (f *KVFetcher) NextKV(
ctx context.Context,
) (ok bool, kv roachpb.KeyValue, newSpan bool, err error) {
for {
newSpan = f.newSpan
f.newSpan = false
if len(f.kvs) != 0 {
kv = f.kvs[0]
f.kvs = f.kvs[1:]
return true, kv, newSpan, nil
}
if len(f.batchResponse) > 0 {
var key []byte
var rawBytes []byte
var err error
key, rawBytes, f.batchResponse, err = enginepb.ScanDecodeKeyValueNoTS(f.batchResponse)
if err != nil {
return false, kv, false, err
}
return true, roachpb.KeyValue{
Key: key,
Value: roachpb.Value{
RawBytes: rawBytes,
},
}, newSpan, nil
}
ok, f.kvs, f.batchResponse, f.Span, err = f.nextBatch(ctx)
if err != nil {
return ok, kv, false, err
}
if !ok {
return false, kv, false, nil
}
f.newSpan = true
f.bytesRead += int64(len(f.batchResponse))
}
}