-
Notifications
You must be signed in to change notification settings - Fork 3
/
plan.go
195 lines (169 loc) · 3.93 KB
/
plan.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
package kvql
import (
"fmt"
"os"
)
var (
PlanBatchSize = 32
EnableFieldCache = true
)
func init() {
if dfc := os.Getenv("DISABLE_FCACHE"); dfc == "1" {
EnableFieldCache = false
}
}
type ExecuteCtx struct {
Hit int
EnableCache bool
FieldCaches map[string]any
FieldChunkKeyCaches map[string][]any
FieldChunkCaches map[string][]any
}
func NewExecuteCtx() *ExecuteCtx {
return &ExecuteCtx{
Hit: 0,
EnableCache: EnableFieldCache,
FieldCaches: make(map[string]any),
FieldChunkKeyCaches: make(map[string][]any),
FieldChunkCaches: make(map[string][]any),
}
}
func (c *ExecuteCtx) GetFieldResult(name string) (any, bool) {
if !c.EnableCache {
return nil, false
}
if val, have := c.FieldCaches[name]; have {
return val, true
}
return nil, false
}
func (c *ExecuteCtx) SetFieldResult(name string, value any) {
if !c.EnableCache {
return
}
c.FieldCaches[name] = value
}
func (c *ExecuteCtx) GetChunkFieldResult(name string, key []byte) ([]any, bool) {
if !c.EnableCache {
return nil, false
}
ckey := fmt.Sprintf("%s-%s", name, string(key))
if chunk, have := c.FieldChunkKeyCaches[ckey]; have {
return chunk, true
}
return nil, false
}
func (c *ExecuteCtx) AppendChunkFieldResult(name string, chunk []any) {
if !c.EnableCache {
return
}
cdata, have := c.FieldChunkCaches[name]
if have {
cdata = append(cdata, chunk...)
c.FieldChunkCaches[name] = cdata
} else {
cchunk := make([]any, len(chunk))
copy(cchunk, chunk)
c.FieldChunkCaches[name] = cchunk
}
}
func (c *ExecuteCtx) SetChunkFieldResult(name string, key []byte, chunk []any) {
if !c.EnableCache {
return
}
ckey := fmt.Sprintf("%s-%s", name, string(key))
if _, have := c.FieldChunkKeyCaches[ckey]; have {
return
}
c.FieldChunkKeyCaches[ckey] = chunk
c.AppendChunkFieldResult(name, chunk)
}
func (c *ExecuteCtx) GetChunkFieldFinalResult(name string) ([]any, bool) {
if !c.EnableCache {
return nil, false
}
val, have := c.FieldChunkCaches[name]
return val, have
}
func (c *ExecuteCtx) UpdateHit() {
c.Hit++
}
func (c *ExecuteCtx) Clear() {
if !c.EnableCache {
return
}
clear(c.FieldCaches)
clear(c.FieldChunkCaches)
clear(c.FieldChunkKeyCaches)
}
func (c *ExecuteCtx) AdjustChunkCache(chooseIdxes []int) {
if !c.EnableCache {
return
}
cidxes := make(map[int]struct{})
for _, idx := range chooseIdxes {
cidxes[idx] = struct{}{}
}
for k, v := range c.FieldChunkCaches {
nv := make([]any, 0, len(chooseIdxes))
for i, item := range v {
if _, have := cidxes[i]; have {
nv = append(nv, item)
}
}
c.FieldChunkCaches[k] = nv
}
}
type FinalPlan interface {
String() string
Explain() []string
Init() error
Next(ctx *ExecuteCtx) ([]Column, error)
Batch(ctx *ExecuteCtx) ([][]Column, error)
FieldNameList() []string
FieldTypeList() []Type
}
type Plan interface {
String() string
Explain() []string
Init() error
Next(ctx *ExecuteCtx) (key []byte, value []byte, err error)
Batch(ctx *ExecuteCtx) (rows []KVPair, err error)
}
var (
_ Plan = (*FullScanPlan)(nil)
_ Plan = (*EmptyResultPlan)(nil)
_ Plan = (*RangeScanPlan)(nil)
_ Plan = (*PrefixScanPlan)(nil)
_ Plan = (*MultiGetPlan)(nil)
_ Plan = (*LimitPlan)(nil)
_ FinalPlan = (*ProjectionPlan)(nil)
_ FinalPlan = (*AggregatePlan)(nil)
_ FinalPlan = (*FinalOrderPlan)(nil)
_ FinalPlan = (*FinalLimitPlan)(nil)
_ FinalPlan = (*PutPlan)(nil)
)
type Column any
type EmptyResultPlan struct {
Storage Storage
}
func NewEmptyResultPlan(s Storage, f *FilterExec) Plan {
return &EmptyResultPlan{
Storage: s,
}
}
func (p *EmptyResultPlan) Init() error {
return nil
}
func (p *EmptyResultPlan) Next(ctx *ExecuteCtx) ([]byte, []byte, error) {
return nil, nil, nil
}
func (p *EmptyResultPlan) String() string {
return "EmptyResultPlan"
}
func (p *EmptyResultPlan) Explain() []string {
return []string{p.String()}
}
func (p *EmptyResultPlan) Batch(ctx *ExecuteCtx) ([]KVPair, error) {
return nil, nil
}