-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfirst_level_cache.go
More file actions
351 lines (325 loc) · 9.63 KB
/
first_level_cache.go
File metadata and controls
351 lines (325 loc) · 9.63 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package rapidash
import (
"database/sql"
"fmt"
"sort"
"strings"
"sync"
"github.com/blastrain/vitess-sqlparser/sqlparser"
"golang.org/x/xerrors"
)
type FirstLevelCacheMap struct {
*sync.Map
}
func (c *FirstLevelCacheMap) set(tableName string, cache *FirstLevelCache) {
c.Store(tableName, cache)
}
func (c *FirstLevelCacheMap) get(tableName string) (*FirstLevelCache, bool) {
cache, exists := c.Load(tableName)
if !exists {
return nil, false
}
return cache.(*FirstLevelCache), exists
}
func NewFirstLevelCacheMap() *FirstLevelCacheMap {
return &FirstLevelCacheMap{&sync.Map{}}
}
type FirstLevelCache struct {
typ *Struct
indexTrees map[string]*BTree
findAllValue *StructSliceValue
primaryKey string
valueFactory *ValueFactory
}
func NewFirstLevelCache(s *Struct) *FirstLevelCache {
return &FirstLevelCache{
typ: s,
indexTrees: map[string]*BTree{},
valueFactory: NewValueFactory(),
}
}
func (c *FirstLevelCache) WarmUp(conn *sql.DB) (e error) {
ddl, err := c.showCreateTable(conn)
if err != nil {
return xerrors.Errorf("failed to 'show create table': %w", err)
}
stmt, err := sqlparser.Parse(ddl)
if err != nil {
return xerrors.Errorf("cannot parse ddl %s: %w", ddl, err)
}
rows, err := c.loadAll(conn)
if err != nil {
return xerrors.Errorf("failed to load all records: %w", err)
}
defer func() {
if err := rows.Close(); err != nil {
e = xerrors.Errorf("failed to close rows: %w", err)
}
}()
allLeaf, err := c.setupAllLeaf(rows)
if err != nil {
return xerrors.Errorf("cannot setup all leaf: %w", err)
}
for _, constraint := range (stmt.(*sqlparser.CreateTable)).Constraints {
switch constraint.Type {
case sqlparser.ConstraintPrimaryKey:
c.setupPrimaryKey(constraint, allLeaf)
case sqlparser.ConstraintUniq, sqlparser.ConstraintUniqKey, sqlparser.ConstraintUniqIndex:
c.setupUniqKey(constraint, allLeaf)
case sqlparser.ConstraintKey, sqlparser.ConstraintIndex:
c.setupKey(constraint, allLeaf)
}
}
tree := c.indexTrees[c.primaryKey]
if tree != nil {
c.findAllValue = c.flatten(tree.all())
}
return nil
}
func (c *FirstLevelCache) showCreateTable(conn *sql.DB) (string, error) {
var (
tbl string
ddl string
)
if err := conn.QueryRow(fmt.Sprintf("SHOW CREATE TABLE `%s`", c.typ.tableName)).Scan(&tbl, &ddl); err != nil {
return "", xerrors.Errorf("failed to execute 'SHOW CREATE TABLE `%s`': %w", c.typ.tableName, err)
}
return ddl, nil
}
func (c *FirstLevelCache) loadAll(conn *sql.DB) (*sql.Rows, error) {
columns := c.typ.Columns()
escapedColumns := make([]string, len(columns))
for idx, column := range columns {
escapedColumns[idx] = fmt.Sprintf("`%s`", column)
}
query := fmt.Sprintf("SELECT %s FROM %s", strings.Join(escapedColumns, ","), c.typ.tableName)
rows, err := conn.Query(query)
if err != nil {
return nil, xerrors.Errorf("failed to query %s: %w", query, err)
}
return rows, nil
}
func (c *FirstLevelCache) setupAllLeaf(rows *sql.Rows) (*StructSliceValue, error) {
values := NewStructSliceValue()
for rows.Next() {
scanValues := c.typ.ScanValues(c.valueFactory)
if err := rows.Scan(scanValues...); err != nil {
return nil, xerrors.Errorf("cannot scan from rows: %w", err)
}
values.Append(c.typ.StructValue(scanValues))
}
if err := rows.Err(); err != nil {
return nil, xerrors.Errorf("rows has error while scanning: %w", err)
}
return values, nil
}
func (c *FirstLevelCache) setupPrimaryKey(constraint *sqlparser.Constraint, allLeaf *StructSliceValue) {
indexColumn := constraint.Keys[0].String()
c.indexTrees[indexColumn] = c.makeBTree(allLeaf, indexColumn)
c.primaryKey = indexColumn
}
func (c *FirstLevelCache) setupUniqKey(constraint *sqlparser.Constraint, allLeaf *StructSliceValue) {
for idx := range constraint.Keys {
subKeys := constraint.Keys[:idx+1]
if len(subKeys) == 0 {
continue
}
indexColumns := []string{}
for _, key := range subKeys {
indexColumns = append(indexColumns, key.String())
}
tree := c.makeBTree(allLeaf, indexColumns...)
indexKey := strings.Join(indexColumns, ":")
c.indexTrees[indexKey] = tree
}
}
func (c *FirstLevelCache) setupKey(constraint *sqlparser.Constraint, allLeaf *StructSliceValue) {
for idx := range constraint.Keys {
subKeys := constraint.Keys[:idx+1]
if len(subKeys) == 0 {
continue
}
indexColumns := []string{}
for _, key := range subKeys {
indexColumns = append(indexColumns, key.String())
}
tree := c.makeBTree(allLeaf, indexColumns...)
indexKey := strings.Join(indexColumns, ":")
c.indexTrees[indexKey] = tree
}
}
func (c *FirstLevelCache) indexesFromMap(leafMap map[interface{}]*StructSliceValue) []*Value {
indexes := []*Value{}
for index := range leafMap {
indexes = append(indexes, c.valueFactory.CreateValue(index))
}
sort.Slice(indexes, func(i, j int) bool {
return indexes[i].LT(indexes[j])
})
return indexes
}
func (c *FirstLevelCache) makeBTree(allLeaf *StructSliceValue, indexColumns ...string) *BTree {
indexColumn := indexColumns[0]
leafMap := map[interface{}]*StructSliceValue{}
for _, v := range allLeaf.values {
index := v.ValueByColumn(indexColumn)
if leafMap[index.RawValue()] == nil {
leafMap[index.RawValue()] = NewStructSliceValue()
}
leafMap[index.RawValue()].Append(v)
}
indexes := c.indexesFromMap(leafMap)
tree := NewBTree()
for _, index := range indexes {
if len(indexColumns) > 1 {
leafs := leafMap[index.RawValue()]
subtree := c.makeBTree(leafs, indexColumns[1:]...)
tree.insert(index, subtree)
} else {
value := leafMap[index.RawValue()]
tree.insert(index, value)
}
}
return tree
}
func (c *FirstLevelCache) FindByPrimaryKey(key *Value, unmarshaler Unmarshaler) error {
tree := c.indexTrees[c.primaryKey]
if tree.root.isWithoutBranchAndLeaf() {
return nil
}
leaf := tree.searchEq(key)
if leaf != nil {
values, ok := leaf.(*StructSliceValue)
if !ok || values.Len() == 0 {
return ErrRecordNotFoundByPrimaryKey
}
if values.Len() != 1 {
return xerrors.Errorf("found duplicate entries ( %d entries ) by primary key", values.Len())
}
if err := unmarshaler.DecodeRapidash(values); err != nil {
return xerrors.Errorf("failed to decode values: %w", err)
}
}
return nil
}
func (c *FirstLevelCache) findIndexTreeByQueryBuilder(builder *QueryBuilder) *BTree {
indexes := builder.indexes()
for _, index := range indexes {
for k, tree := range c.indexTrees {
if k == index {
return tree
}
}
}
return nil
}
func (c *FirstLevelCache) searchByTree(tree *BTree, conditions *Conditions) (*StructSliceValue, error) {
totalValues := NewStructSliceValue()
leafsOrTrees := conditions.Current().Search(tree)
for _, leafsOrTree := range leafsOrTrees {
values, ok := leafsOrTree.(*StructSliceValue)
if ok {
if values == nil {
return nil, nil
}
subConditions := conditions.Next()
for ; subConditions != nil; subConditions = subConditions.Next() {
values = values.Filter(subConditions.Current())
}
totalValues.AppendSlice(values)
continue
}
tree, ok := leafsOrTree.(*BTree)
if ok {
values, err := c.searchByTree(tree, conditions)
if err != nil {
return nil, xerrors.Errorf("failed to search btree: %w", err)
}
totalValues.AppendSlice(values)
continue
}
return nil, ErrInvalidLeafs
}
return totalValues, nil
}
func (c *FirstLevelCache) findByQueryBuilder(builder *QueryBuilder) (*StructSliceValue, error) {
if builder.conditions.Len() == 0 {
return c.findAll(), nil
}
conditions := builder.conditions
defer conditions.Reset()
var indexTree *BTree
if builder.AvailableIndex() {
indexTree = c.findIndexTreeByQueryBuilder(builder)
}
if indexTree == nil {
log.Warn(fmt.Sprintf("not found index for [select * from %s where %s]. exec full scan", c.typ.tableName, builder.Query()))
values := c.findAll()
if values == nil {
return nil, nil
}
subConditions := conditions.Next()
for ; subConditions != nil; subConditions = subConditions.Next() {
values = values.Filter(subConditions.Current())
}
values.Sort(builder.orderConditions)
return values, nil
}
if indexTree.root.isWithoutBranchAndLeaf() {
return NewStructSliceValue(), nil
}
totalValues, err := c.searchByTree(indexTree, conditions)
if err != nil {
return nil, xerrors.Errorf("failed to search btree: %w", err)
}
totalValues.Sort(builder.orderConditions)
return totalValues, nil
}
func (c *FirstLevelCache) FindByQueryBuilder(builder *QueryBuilder, unmarshaler Unmarshaler) error {
builder.Build(c.valueFactory)
defer builder.Release()
values, err := c.findByQueryBuilder(builder)
if err != nil {
return xerrors.Errorf("failed to findByQueryBuilder: %w", err)
}
if values != nil && values.Len() > 0 {
if err := unmarshaler.DecodeRapidash(values); err != nil {
return xerrors.Errorf("failed to decode values: %w", err)
}
}
return nil
}
func (c *FirstLevelCache) CountByQueryBuilder(builder *QueryBuilder) (uint64, error) {
builder.Build(c.valueFactory)
defer builder.Release()
values, err := c.findByQueryBuilder(builder)
if err != nil {
return 0, xerrors.Errorf("failed to findByQueryBuilder: %w", err)
}
if values == nil {
return 0, nil
}
return uint64(values.Len()), nil
}
func (c *FirstLevelCache) findAll() *StructSliceValue {
return c.findAllValue
}
func (c *FirstLevelCache) FindAll(unmarshaler Unmarshaler) error {
values := c.findAll()
if values != nil && values.Len() > 0 {
if err := unmarshaler.DecodeRapidash(values); err != nil {
return xerrors.Errorf("failed to decode values: %w", err)
}
}
return nil
}
func (c *FirstLevelCache) flatten(leafs []Leaf) *StructSliceValue {
values := NewStructSliceValue()
for _, leaf := range leafs {
sliceValue, ok := leaf.(*StructSliceValue)
if ok {
values.AppendSlice(sliceValue)
}
}
return values
}