forked from cockroachdb/cockroach
/
disk_row_container.go
222 lines (199 loc) · 7.8 KB
/
disk_row_container.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2017 The Cockroach 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 distsqlrun
import (
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/mon"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// diskRowContainer is a sortableRowContainer that stores rows on disk according
// to the ordering specified in diskRowContainer.ordering. The underlying store
// is a SortedDiskMap so the sorting itself is delegated. Use an iterator
// created through NewIterator() to read the rows in sorted order.
type diskRowContainer struct {
diskMap engine.SortedDiskMap
// diskAcc keeps track of disk usage.
diskAcc mon.BoundAccount
// bufferedRows buffers writes to the diskMap.
bufferedRows engine.SortedDiskMapBatchWriter
scratchKey []byte
scratchVal []byte
scratchEncRow sqlbase.EncDatumRow
// rowID is used as a key suffix to prevent duplicate rows from overwriting
// each other.
rowID uint64
// types is the schema of rows in the container.
types []sqlbase.ColumnType
// ordering is the order in which rows should be sorted.
ordering sqlbase.ColumnOrdering
// encodings keeps around the DatumEncoding equivalents of the encoding
// directions in ordering to avoid conversions in hot paths.
encodings []sqlbase.DatumEncoding
// valueIdxs holds the indexes of the columns that we encode as values. The
// columns described by ordering will be encoded as keys. See
// makeDiskRowContainer() for more encoding specifics.
valueIdxs []int
datumAlloc sqlbase.DatumAlloc
}
var _ sortableRowContainer = &diskRowContainer{}
// makeDiskRowContainer creates a diskRowContainer with the given engine as the
// underlying store that rows are stored on.
// Arguments:
// - diskMonitor is used to monitor this diskRowContainer's disk usage.
// - types is the schema of rows that will be added to this container.
// - ordering is the output ordering; the order in which rows should be sorted.
// - e is the underlying store that rows are stored on.
func makeDiskRowContainer(
ctx context.Context,
diskMonitor *mon.BytesMonitor,
types []sqlbase.ColumnType,
ordering sqlbase.ColumnOrdering,
e engine.Engine,
) diskRowContainer {
diskMap := engine.NewRocksDBMap(e)
d := diskRowContainer{
diskMap: diskMap,
diskAcc: diskMonitor.MakeBoundAccount(),
types: types,
ordering: ordering,
scratchEncRow: make(sqlbase.EncDatumRow, len(types)),
}
d.bufferedRows = d.diskMap.NewBatchWriter()
// The ordering is specified for a subset of the columns. These will be
// encoded as a key in the given order according to the given direction so
// that the sorting can be delegated to the underlying SortedDiskMap. To
// avoid converting encoding.Direction to sqlbase.DatumEncoding we do this
// once at initialization and store the conversions in d.encodings.
// We encode the other columns as values. The indexes of these columns are
// kept around in d.valueIdxs to have them ready in hot paths.
// For composite columns that are specified in d.ordering, the Datum is
// encoded both in the key for comparison and in the value for decoding.
orderingIdxs := make(map[int]struct{})
for _, orderInfo := range d.ordering {
orderingIdxs[orderInfo.ColIdx] = struct{}{}
}
d.valueIdxs = make([]int, 0, len(d.types))
for i := range d.types {
// TODO(asubiotto): A datum of a type for with HasCompositeKeyEncoding
// returns true may not necessarily need to be encoded in the value, so
// make this more fine-grained. See IsComposite() methods in
// pkg/sql/parser/datum.go.
if _, ok := orderingIdxs[i]; !ok || sqlbase.HasCompositeKeyEncoding(d.types[i].SemanticType) {
d.valueIdxs = append(d.valueIdxs, i)
}
}
d.encodings = make([]sqlbase.DatumEncoding, len(d.ordering))
for i, orderInfo := range ordering {
d.encodings[i] = sqlbase.EncodingDirToDatumEncoding(orderInfo.Direction)
}
return d
}
func (d *diskRowContainer) AddRow(ctx context.Context, row sqlbase.EncDatumRow) error {
if len(row) != len(d.types) {
log.Fatalf(ctx, "invalid row length %d, expected %d", len(row), len(d.types))
}
for i, orderInfo := range d.ordering {
var err error
d.scratchKey, err = row[orderInfo.ColIdx].Encode(&d.datumAlloc, d.encodings[i], d.scratchKey)
if err != nil {
return err
}
}
for _, i := range d.valueIdxs {
var err error
d.scratchVal, err = row[i].Encode(&d.datumAlloc, sqlbase.DatumEncoding_VALUE, d.scratchVal)
if err != nil {
return err
}
}
// Put a unique row to keep track of duplicates. Note that this will not
// mess with key decoding.
d.scratchKey = encoding.EncodeUvarintAscending(d.scratchKey, d.rowID)
if err := d.diskAcc.Grow(ctx, int64(len(d.scratchKey)+len(d.scratchVal))); err != nil {
return err
}
if err := d.bufferedRows.Put(d.scratchKey, d.scratchVal); err != nil {
return err
}
d.scratchKey = d.scratchKey[:0]
d.scratchVal = d.scratchVal[:0]
d.rowID++
return nil
}
// Sort is a noop because the use of a SortedDiskMap as the underlying store
// keeps the rows in sorted order.
func (d *diskRowContainer) Sort(context.Context) {}
func (d *diskRowContainer) Close(ctx context.Context) {
// We can ignore the error here because the flushed data is immediately cleared
// in the following Close.
_ = d.bufferedRows.Close(ctx)
d.diskMap.Close(ctx)
d.diskAcc.Close(ctx)
}
// keyValToRow decodes a key and a value byte slice stored with AddRow() into
// a sqlbase.EncDatumRow. The returned EncDatumRow is only valid until the next
// call to keyValToRow().
func (d *diskRowContainer) keyValToRow(k []byte, v []byte) (sqlbase.EncDatumRow, error) {
for i, orderInfo := range d.ordering {
// Types with composite key encodings are decoded from the value.
if sqlbase.HasCompositeKeyEncoding(d.types[orderInfo.ColIdx].SemanticType) {
// Skip over the encoded key.
encLen, err := encoding.PeekLength(k)
if err != nil {
return nil, err
}
k = k[encLen:]
continue
}
var err error
d.scratchEncRow[orderInfo.ColIdx], k, err = sqlbase.EncDatumFromBuffer(d.types[orderInfo.ColIdx], d.encodings[i], k)
if err != nil {
return nil, errors.Wrap(err, "unable to decode row")
}
}
for _, i := range d.valueIdxs {
var err error
d.scratchEncRow[i], v, err = sqlbase.EncDatumFromBuffer(d.types[i], sqlbase.DatumEncoding_VALUE, v)
if err != nil {
return nil, errors.Wrap(err, "unable to decode row")
}
}
return d.scratchEncRow, nil
}
// diskRowIterator iterates over the rows in a diskRowContainer.
type diskRowIterator struct {
rowContainer *diskRowContainer
engine.SortedDiskMapIterator
}
var _ rowIterator = diskRowIterator{}
func (d *diskRowContainer) NewIterator(ctx context.Context) rowIterator {
if err := d.bufferedRows.Flush(); err != nil {
log.Fatal(ctx, err)
}
return diskRowIterator{rowContainer: d, SortedDiskMapIterator: d.diskMap.NewIterator()}
}
// Row returns the current row. The returned sqlbase.EncDatumRow is only valid
// until the next call to Row().
func (r diskRowIterator) Row() (sqlbase.EncDatumRow, error) {
if ok, err := r.Valid(); err != nil {
return nil, errors.Wrap(err, "unable to check row validity")
} else if !ok {
return nil, errors.New("invalid row")
}
return r.rowContainer.keyValToRow(r.Key(), r.Value())
}