forked from latolukasz/beeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
310 lines (274 loc) · 10.3 KB
/
engine.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
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
package beeorm
import (
"fmt"
"reflect"
"sync"
)
type Engine interface {
Clone() Engine
EnableRequestCache()
SetQueryTimeLimit(seconds int)
GetMysql(code ...string) *DB
GetLocalCache(code ...string) *LocalCache
GetRedis(code ...string) *RedisCache
SetLogMetaData(key string, value interface{})
NewFlusher() Flusher
Flush(entity ...Entity)
FlushLazy(entity ...Entity)
FlushWithCheck(entity ...Entity) error
FlushWithFullCheck(entity ...Entity) error
Delete(entity ...Entity)
DeleteLazy(entity ...Entity)
ForceDelete(entity ...Entity)
GetRegistry() ValidatedRegistry
SearchWithCount(where *Where, pager *Pager, entities interface{}, references ...string) (totalRows int)
Search(where *Where, pager *Pager, entities interface{}, references ...string)
SearchIDsWithCount(where *Where, pager *Pager, entity Entity) (results []uint64, totalRows int)
SearchIDs(where *Where, pager *Pager, entity Entity) []uint64
SearchOne(where *Where, entity Entity, references ...string) (found bool)
CachedSearchOne(entity Entity, indexName string, arguments ...interface{}) (found bool)
CachedSearchOneWithReferences(entity Entity, indexName string, arguments []interface{}, references []string) (found bool)
CachedSearch(entities interface{}, indexName string, pager *Pager, arguments ...interface{}) (totalRows int)
CachedSearchIDs(entity Entity, indexName string, pager *Pager, arguments ...interface{}) (totalRows int, ids []uint64)
CachedSearchCount(entity Entity, indexName string, arguments ...interface{}) int
CachedSearchWithReferences(entities interface{}, indexName string, pager *Pager, arguments []interface{}, references []string) (totalRows int)
ClearCacheByIDs(entity Entity, ids ...uint64)
LoadByID(id uint64, entity Entity, references ...string) (found bool)
Load(entity Entity, references ...string) (found bool)
LoadByIDs(ids []uint64, entities interface{}, references ...string) (found bool)
GetAlters() (alters []Alter)
GetEventBroker() EventBroker
RegisterQueryLogger(handler LogHandler, mysql, redis, local bool)
EnableQueryDebug()
EnableQueryDebugCustom(mysql, redis, local bool)
}
type engineImplementation struct {
registry *validatedRegistry
dbs map[string]*DB
localCache map[string]*LocalCache
redis map[string]*RedisCache
logMetaData Bind
hasRequestCache bool
queryLoggersDB []LogHandler
queryLoggersRedis []LogHandler
queryLoggersLocalCache []LogHandler
hasRedisLogger bool
hasDBLogger bool
hasLocalCacheLogger bool
afterCommitLocalCacheSets map[string][]interface{}
afterCommitRedisFlusher *redisFlusher
eventBroker *eventBroker
queryTimeLimit uint16
sync.Mutex
}
func (e *engineImplementation) Clone() Engine {
return &engineImplementation{
registry: e.registry,
queryTimeLimit: e.queryTimeLimit,
logMetaData: e.logMetaData,
hasRequestCache: e.hasRequestCache,
queryLoggersDB: e.queryLoggersDB,
queryLoggersRedis: e.queryLoggersRedis,
queryLoggersLocalCache: e.queryLoggersLocalCache,
hasRedisLogger: e.hasRedisLogger,
hasDBLogger: e.hasDBLogger,
hasLocalCacheLogger: e.hasLocalCacheLogger,
}
}
func (e *engineImplementation) EnableRequestCache() {
e.hasRequestCache = true
}
func (e *engineImplementation) SetQueryTimeLimit(seconds int) {
e.queryTimeLimit = uint16(seconds)
}
func (e *engineImplementation) GetMysql(code ...string) *DB {
dbCode := "default"
if len(code) > 0 {
dbCode = code[0]
}
e.Mutex.Lock()
defer e.Mutex.Unlock()
db, has := e.dbs[dbCode]
if !has {
config, has := e.registry.mySQLServers[dbCode]
if !has {
panic(fmt.Errorf("unregistered mysql pool '%s'", dbCode))
}
db = &DB{engine: e, config: config, client: &standardSQLClient{db: config.getClient()}}
if e.dbs == nil {
e.dbs = map[string]*DB{dbCode: db}
} else {
e.dbs[dbCode] = db
}
}
return db
}
func (e *engineImplementation) GetLocalCache(code ...string) *LocalCache {
dbCode := "default"
if len(code) > 0 {
dbCode = code[0]
}
e.Mutex.Lock()
defer e.Mutex.Unlock()
cache, has := e.localCache[dbCode]
if !has {
config, has := e.registry.localCacheServers[dbCode]
if !has {
if dbCode == requestCacheKey {
cache = &LocalCache{config: newLocalCacheConfig(dbCode, 5000), engine: e}
if e.localCache == nil {
e.localCache = map[string]*LocalCache{dbCode: cache}
} else {
e.localCache[dbCode] = cache
}
return cache
}
panic(fmt.Errorf("unregistered local cache pool '%s'", dbCode))
}
cache = &LocalCache{engine: e, config: config.(*localCachePoolConfig)}
if e.localCache == nil {
e.localCache = make(map[string]*LocalCache)
}
e.localCache[dbCode] = cache
}
return cache
}
func (e *engineImplementation) GetRedis(code ...string) *RedisCache {
dbCode := "default"
if len(code) > 0 {
dbCode = code[0]
}
e.Mutex.Lock()
defer e.Mutex.Unlock()
cache, has := e.redis[dbCode]
if !has {
config, has := e.registry.redisServers[dbCode]
if !has {
panic(fmt.Errorf("unregistered redis cache pool '%s'", dbCode))
}
client := config.getClient()
cache = &RedisCache{engine: e, config: config, client: client}
if e.redis == nil {
e.redis = map[string]*RedisCache{dbCode: cache}
} else {
e.redis[dbCode] = cache
}
}
return cache
}
func (e *engineImplementation) SetLogMetaData(key string, value interface{}) {
e.Mutex.Lock()
defer e.Mutex.Unlock()
if e.logMetaData == nil {
e.logMetaData = make(Bind)
}
e.logMetaData[key] = value
}
func (e *engineImplementation) NewFlusher() Flusher {
return &flusher{engine: e}
}
func (e *engineImplementation) Flush(entity ...Entity) {
e.NewFlusher().Track(entity...).Flush()
}
func (e *engineImplementation) FlushLazy(entity ...Entity) {
e.NewFlusher().Track(entity...).FlushLazy()
}
func (e *engineImplementation) FlushWithCheck(entity ...Entity) error {
return e.NewFlusher().Track(entity...).FlushWithCheck()
}
func (e *engineImplementation) FlushWithFullCheck(entity ...Entity) error {
return e.NewFlusher().Track(entity...).FlushWithFullCheck()
}
func (e *engineImplementation) Delete(entity ...Entity) {
for _, e := range entity {
e.markToDelete()
}
e.Flush(entity...)
}
func (e *engineImplementation) DeleteLazy(entity ...Entity) {
for _, e := range entity {
e.markToDelete()
}
e.FlushLazy(entity...)
}
func (e *engineImplementation) ForceDelete(entity ...Entity) {
for _, entity := range entity {
entity.forceMarkToDelete()
}
e.Flush(entity...)
}
func (e *engineImplementation) GetRegistry() ValidatedRegistry {
return e.registry
}
func (e *engineImplementation) SearchWithCount(where *Where, pager *Pager, entities interface{}, references ...string) (totalRows int) {
return search(newSerializer(nil), e, where, pager, true, true, reflect.ValueOf(entities).Elem(), references...)
}
func (e *engineImplementation) Search(where *Where, pager *Pager, entities interface{}, references ...string) {
search(newSerializer(nil), e, where, pager, false, true, reflect.ValueOf(entities).Elem(), references...)
}
func (e *engineImplementation) SearchIDsWithCount(where *Where, pager *Pager, entity Entity) (results []uint64, totalRows int) {
return searchIDsWithCount(e, where, pager, reflect.TypeOf(entity).Elem())
}
func (e *engineImplementation) SearchIDs(where *Where, pager *Pager, entity Entity) []uint64 {
results, _ := searchIDs(e, where, pager, false, reflect.TypeOf(entity).Elem())
return results
}
func (e *engineImplementation) SearchOne(where *Where, entity Entity, references ...string) (found bool) {
found, _, _ = searchOne(newSerializer(nil), e, where, entity, references)
return found
}
func (e *engineImplementation) CachedSearchOne(entity Entity, indexName string, arguments ...interface{}) (found bool) {
return cachedSearchOne(newSerializer(nil), e, entity, indexName, true, arguments, nil)
}
func (e *engineImplementation) CachedSearchOneWithReferences(entity Entity, indexName string, arguments []interface{}, references []string) (found bool) {
return cachedSearchOne(newSerializer(nil), e, entity, indexName, true, arguments, references)
}
func (e *engineImplementation) CachedSearch(entities interface{}, indexName string, pager *Pager, arguments ...interface{}) (totalRows int) {
total, _ := cachedSearch(newSerializer(nil), e, entities, indexName, pager, arguments, true, nil)
return total
}
func (e *engineImplementation) CachedSearchIDs(entity Entity, indexName string, pager *Pager, arguments ...interface{}) (totalRows int, ids []uint64) {
return cachedSearch(newSerializer(nil), e, entity, indexName, pager, arguments, false, nil)
}
func (e *engineImplementation) CachedSearchCount(entity Entity, indexName string, arguments ...interface{}) int {
total, _ := cachedSearch(newSerializer(nil), e, entity, indexName, NewPager(1, 1), arguments, false, nil)
return total
}
func (e *engineImplementation) CachedSearchWithReferences(entities interface{}, indexName string, pager *Pager,
arguments []interface{}, references []string) (totalRows int) {
total, _ := cachedSearch(newSerializer(nil), e, entities, indexName, pager, arguments, true, references)
return total
}
func (e *engineImplementation) ClearCacheByIDs(entity Entity, ids ...uint64) {
clearByIDs(e, entity, ids...)
}
func (e *engineImplementation) LoadByID(id uint64, entity Entity, references ...string) (found bool) {
found, _ = loadByID(newSerializer(nil), e, id, entity, true, references...)
return found
}
func (e *engineImplementation) Load(entity Entity, references ...string) (found bool) {
return e.load(newSerializer(nil), entity, references...)
}
func (e *engineImplementation) LoadByIDs(ids []uint64, entities interface{}, references ...string) (found bool) {
_, hasMissing := tryByIDs(newSerializer(nil), e, ids, reflect.ValueOf(entities).Elem(), references)
return !hasMissing
}
func (e *engineImplementation) GetAlters() (alters []Alter) {
return getAlters(e)
}
func (e *engineImplementation) load(serializer *serializer, entity Entity, references ...string) bool {
if entity.IsLoaded() {
if len(references) > 0 {
orm := entity.getORM()
warmUpReferences(serializer, e, orm.tableSchema, orm.elem, references, false)
}
return true
}
orm := initIfNeeded(e.registry, entity)
id := orm.GetID()
found := false
if id > 0 {
found, _ = loadByID(serializer, e, id, entity, true, references...)
}
return found
}