-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
storage.go
270 lines (228 loc) · 6.6 KB
/
storage.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2021 PingCAP, Inc.
//
// 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 cteutil
import (
"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/disk"
"github.com/pingcap/tidb/pkg/util/memory"
"github.com/pingcap/tidb/pkg/util/syncutil"
)
var _ Storage = &StorageRC{}
// Storage is a temporary storage to store the intermidate data of CTE.
//
// Common usage as follows:
//
// storage.Lock()
// if !storage.Done() {
// fill all data into storage
// }
// storage.UnLock()
// read data from storage
type Storage interface {
// If is first called, will open underlying storage. Otherwise will add ref count by one.
OpenAndRef() error
// Minus ref count by one, if ref count is zero, close underlying storage.
DerefAndClose() (err error)
// SwapData swaps data of two storage.
// Other metainfo is not touched, such ref count/done flag etc.
SwapData(other Storage) error
// Reopen reset storage and related info.
// So the status of Storage is like a new created one.
Reopen() error
// Add chunk into underlying storage.
// Should return directly if chk is empty.
Add(chk *chunk.Chunk) error
// Get Chunk by index.
GetChunk(chkIdx int) (*chunk.Chunk, error)
// Get row by RowPtr.
GetRow(ptr chunk.RowPtr) (chunk.Row, error)
// NumChunks return chunk number of the underlying storage.
NumChunks() int
// NumRows return row number of the underlying storage.
NumRows() int
// Storage is not thread-safe.
// By using Lock(), users can achieve the purpose of ensuring thread safety.
Lock()
Unlock()
// Usually, Storage is filled first, then user can read it.
// User can check whether Storage is filled first, if not, they can fill it.
Done() bool
SetDone()
// Store error message, so we can return directly.
Error() error
SetError(err error)
// Readers use iter information to determine
// whether they need to read data from the beginning.
SetIter(iter int)
GetIter() int
GetMemTracker() *memory.Tracker
GetDiskTracker() *disk.Tracker
ActionSpill() *chunk.SpillDiskAction
}
// StorageRC implements Storage interface using RowContainer.
type StorageRC struct {
err error
rc *chunk.RowContainer
tp []*types.FieldType
refCnt int
chkSize int
iter int
mu syncutil.Mutex
done bool
}
// NewStorageRowContainer create a new StorageRC.
func NewStorageRowContainer(tp []*types.FieldType, chkSize int) *StorageRC {
return &StorageRC{tp: tp, chkSize: chkSize}
}
// OpenAndRef impls Storage OpenAndRef interface.
func (s *StorageRC) OpenAndRef() (err error) {
if !s.valid() {
s.rc = chunk.NewRowContainer(s.tp, s.chkSize)
s.refCnt = 1
s.iter = 0
} else {
s.refCnt++
}
return nil
}
// DerefAndClose impls Storage DerefAndClose interface.
func (s *StorageRC) DerefAndClose() (err error) {
if !s.valid() {
return errors.New("Storage not opend yet")
}
s.refCnt--
if s.refCnt < 0 {
return errors.New("Storage ref count is less than zero")
} else if s.refCnt == 0 {
s.refCnt = -1
s.done = false
s.err = nil
s.iter = 0
if err = s.rc.Close(); err != nil {
return err
}
s.rc = nil
}
return nil
}
// SwapData impls Storage Swap interface.
func (s *StorageRC) SwapData(other Storage) (err error) {
otherRC, ok := other.(*StorageRC)
if !ok {
return errors.New("cannot swap if underlying storages are different")
}
s.tp, otherRC.tp = otherRC.tp, s.tp
s.chkSize, otherRC.chkSize = otherRC.chkSize, s.chkSize
s.rc, otherRC.rc = otherRC.rc, s.rc
return nil
}
// Reopen impls Storage Reopen interface.
func (s *StorageRC) Reopen() (err error) {
if err = s.rc.Close(); err != nil {
return err
}
s.iter = 0
s.done = false
s.err = nil
// Create a new RowContainer.
// Because some meta infos in old RowContainer are not resetted.
// Such as memTracker/actionSpill etc. So we just use a new one.
s.rc = chunk.NewRowContainer(s.tp, s.chkSize)
return nil
}
// Add impls Storage Add interface.
func (s *StorageRC) Add(chk *chunk.Chunk) (err error) {
if !s.valid() {
return errors.New("Storage is not valid")
}
if chk.NumRows() == 0 {
return nil
}
return s.rc.Add(chk)
}
// GetChunk impls Storage GetChunk interface.
func (s *StorageRC) GetChunk(chkIdx int) (*chunk.Chunk, error) {
if !s.valid() {
return nil, errors.New("Storage is not valid")
}
return s.rc.GetChunk(chkIdx)
}
// GetRow impls Storage GetRow interface.
func (s *StorageRC) GetRow(ptr chunk.RowPtr) (chunk.Row, error) {
if !s.valid() {
return chunk.Row{}, errors.New("Storage is not valid")
}
return s.rc.GetRow(ptr)
}
// NumChunks impls Storage NumChunks interface.
func (s *StorageRC) NumChunks() int {
return s.rc.NumChunks()
}
// NumRows impls Storage NumRows interface.
func (s *StorageRC) NumRows() int {
return s.rc.NumRow()
}
// Lock impls Storage Lock interface.
func (s *StorageRC) Lock() {
s.mu.Lock()
}
// Unlock impls Storage Unlock interface.
func (s *StorageRC) Unlock() {
s.mu.Unlock()
}
// Done impls Storage Done interface.
func (s *StorageRC) Done() bool {
return s.done
}
// SetDone impls Storage SetDone interface.
func (s *StorageRC) SetDone() {
s.done = true
}
// Error impls Storage Error interface.
func (s *StorageRC) Error() error {
return s.err
}
// SetError impls Storage SetError interface.
func (s *StorageRC) SetError(err error) {
s.err = err
}
// SetIter impls Storage SetIter interface.
func (s *StorageRC) SetIter(iter int) {
s.iter = iter
}
// GetIter impls Storage GetIter interface.
func (s *StorageRC) GetIter() int {
return s.iter
}
// GetMemTracker impls Storage GetMemTracker interface.
func (s *StorageRC) GetMemTracker() *memory.Tracker {
return s.rc.GetMemTracker()
}
// GetDiskTracker impls Storage GetDiskTracker interface.
func (s *StorageRC) GetDiskTracker() *memory.Tracker {
return s.rc.GetDiskTracker()
}
// ActionSpill impls Storage ActionSpill interface.
func (s *StorageRC) ActionSpill() *chunk.SpillDiskAction {
return s.rc.ActionSpill()
}
// ActionSpillForTest is for test.
func (s *StorageRC) ActionSpillForTest() *chunk.SpillDiskAction {
return s.rc.ActionSpillForTest()
}
func (s *StorageRC) valid() bool {
return s.refCnt > 0 && s.rc != nil
}