-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.go
More file actions
211 lines (171 loc) · 5.75 KB
/
plugin.go
File metadata and controls
211 lines (171 loc) · 5.75 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
package redisearch
import (
"reflect"
"strconv"
"sync"
"github.com/latolukasz/beeorm/v2"
)
const (
pluginCode = "github.com/coretrix/hitrix/pkg/plugin/redisearch"
optionsKey = "redisearch_options"
entityIndexerPage = 5000
)
type BeeormRedisearchPlugin struct {
mu sync.Mutex
pool string
entitySchemaProcessed map[string]*struct{}
}
func Init(pool string) *BeeormRedisearchPlugin {
return &BeeormRedisearchPlugin{pool: pool, entitySchemaProcessed: map[string]*struct{}{}}
}
func (p *BeeormRedisearchPlugin) RegisterCustomIndex(customIndex *RedisSearchIndex) {
customIndices, ok := customIndicesInit[p.pool]
if !ok {
customIndices = append(customIndices, customIndex)
}
customIndicesInit[p.pool] = customIndices
}
func (p *BeeormRedisearchPlugin) GetCode() string {
return pluginCode
}
//nolint //Function has too many statements
func (p *BeeormRedisearchPlugin) InterfaceInitEntitySchema(schema beeorm.SettableEntitySchema, registry *beeorm.Registry) error {
p.mu.Lock()
defer p.mu.Unlock()
if _, has := p.entitySchemaProcessed[schema.GetEntityName()]; has {
return nil
}
p.entitySchemaProcessed[schema.GetEntityName()] = &struct{}{}
entityType := schema.GetType()
redisSearchIndex := &tableSchemaRedisSearch{
index: nil,
columnMapping: map[string]int{},
mapBindToRedisSearch: map[string]func(val interface{}) interface{}{},
mapBindToScanPointer: map[string]func() interface{}{},
mapPointerToValue: map[string]func(val interface{}) interface{}{},
}
hsaFakeDelete := false
hasSearchableFakeDelete := false
for i, column := range schema.GetColumns() {
redisSearchIndex.columnMapping[column] = i
isSearchable := schema.GetTag(column, "searchable", "true", "") == "true"
isSortable := schema.GetTag(column, "sortable", "true", "") == "true"
hasEnum := schema.GetTag(column, "enum", "true", "") != ""
stem := schema.GetTag(column, "stem", "true", "")
hasStem := stem != ""
if column == "FakeDelete" {
hsaFakeDelete = true
hasSearchableFakeDelete = isSearchable
}
if !isSearchable && !isSortable {
continue
}
if redisSearchIndex.index == nil {
redisSearchIndex.index = &RedisSearchIndex{}
}
structField, ok := entityType.FieldByName(column)
if !ok {
continue
}
kind := structField.Type.Kind()
typeName := structField.Type.Name()
isPointer := kind == reflect.Pointer
isSlice := kind == reflect.Array || kind == reflect.Slice
if isPointer {
typeName = "*" + structField.Type.Elem().Name()
}
if isSlice {
typeName = "[]" + structField.Type.Elem().Name()
}
switch typeName {
case "uint",
"uint8",
"uint16",
"uint32",
"uint64":
buildUintField(redisSearchIndex, column, typeName, isSortable, isSearchable)
case "*uint",
"*uint8",
"*uint16",
"*uint32",
"*uint64":
buildUintPointerField(redisSearchIndex, column, typeName, isSortable, isSearchable)
case "int",
"int8",
"int16",
"int32",
"int64":
buildIntField(redisSearchIndex, column, typeName, isSortable, isSearchable)
case "*int",
"*int8",
"*int16",
"*int32",
"*int64":
buildIntPointerField(redisSearchIndex, column, typeName, isSortable, isSearchable)
case "string":
buildStringField(redisSearchIndex, column, isSortable, isSearchable, hasEnum, stem, hasStem)
case "*string":
buildStringPointerField(redisSearchIndex, column, isSortable, isSearchable, hasEnum, stem, hasStem)
case "[]string":
buildStringSliceField(redisSearchIndex, column, isSortable, isSearchable)
case "bool":
buildBoolField(redisSearchIndex, column, isSortable, isSearchable)
case "*bool":
buildBoolPointerField(redisSearchIndex, column, isSortable, isSearchable)
case "float32",
"float64":
buildFloatField(redisSearchIndex, column, isSortable, isSearchable)
case "*float32",
"*float64":
buildFloatPointerField(redisSearchIndex, column, isSortable, isSearchable)
case "*beeorm.CachedQuery":
continue
case "*Time":
buildTimePointerField(redisSearchIndex, column, isSortable, isSearchable)
case "Time":
buildTimeField(redisSearchIndex, column, isSortable, isSearchable)
default:
if isPointer {
buildPointerField(redisSearchIndex, column, isSortable, isSearchable)
} else {
buildPointersSliceField(redisSearchIndex, column, isSortable, isSearchable)
}
}
}
if redisSearchIndex.index == nil {
return nil
}
redisSearchIndex.hasFakeDelete = hsaFakeDelete
redisSearchIndex.hasSearchableFakeDelete = hasSearchableFakeDelete
if err := redisSearchIndex.buildRedisSearchIndex(schema, registry); err != nil {
return err
}
if indexes := redisSearchIndicesInit[redisSearchIndex.searchCacheName]; indexes == nil {
redisSearchIndicesInit[redisSearchIndex.searchCacheName] = map[string]*RedisSearchIndex{}
}
redisSearchIndicesInit[redisSearchIndex.searchCacheName][redisSearchIndex.index.Name] = redisSearchIndex.index
schema.SetPluginOption(pluginCode, optionsKey, redisSearchIndex)
return nil
}
func (p *BeeormRedisearchPlugin) PluginInterfaceEntityFlushed(
engine beeorm.Engine,
event beeorm.EventEntityFlushed,
setter beeorm.FlusherCacheSetter,
) {
entitySchema := engine.GetRegistry().GetEntitySchema(event.EntityName())
options := entitySchema.GetPluginOption(pluginCode, optionsKey)
if options == nil {
return
}
redisSearchSchema, ok := options.(*tableSchemaRedisSearch)
if !ok {
return
}
redisSetter := setter.GetRedisCacheSetter(redisSearchSchema.index.RedisPool)
key := redisSearchSchema.redisSearchPrefix + strconv.FormatUint(event.EntityID(), 10)
if event.Type() == beeorm.Delete {
redisSetter.Del(redisSearchSchema.searchCacheName, key)
return
}
redisSearchSchema.fillRedisSearchFromBind(redisSetter, event.After(), event.EntityID(), event.Type() == beeorm.Insert)
}