-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_result_ids.go
455 lines (376 loc) · 10.9 KB
/
manager_result_ids.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package dm
import (
"fmt"
"sort"
"time"
"git.100tal.com/wangxiao_xesbiz_operation/gently-utils/convert"
"git.100tal.com/wangxiao_xesbiz_operation/gently-utils/runtime"
"git.100tal.com/wangxiao_xesbiz_operation/gently-utils/utils"
"github.com/pkg/errors"
)
// BaseQueryIdsResult IQueryResult
type BaseQueryIdsResult struct {
dataManager *DataManager // 数据管理器
dataIds []DataIndex // 数据结果集
dataCompare Comparer // 数据比较函数
queryCost time.Duration // 查询耗时
missIndex FieldMap // 查询未命中字段
lastErr error // 执行过程中的错误
}
func NewBaseQueryIdsResult(manager *DataManager, id ...DataIndex) *BaseQueryIdsResult {
return &BaseQueryIdsResult{
dataManager: manager,
dataIds: id,
}
}
/************* Sort接口实现 *************/
func (bq *BaseQueryIdsResult) Len() int {
return len(bq.dataIds)
}
func (bq *BaseQueryIdsResult) Swap(i, j int) {
bq.dataIds[i], bq.dataIds[j] = bq.dataIds[j], bq.dataIds[i]
}
func (bq *BaseQueryIdsResult) Less(i, j int) bool {
if bq.dataCompare != nil {
iRow, has := bq.dataManager.getDataByIndexUnsafe(bq.dataIds[i])
if !has {
return false
}
jRow, has := bq.dataManager.getDataByIndexUnsafe(bq.dataIds[j])
if !has {
return false
}
return bq.dataCompare(iRow, jRow)
}
return false
}
/************* 数据操作类 *************/
// Sift 数据过滤
func (bq *BaseQueryIdsResult) Sift(f Filter) (next IQueryResult) {
next = bq
if bq.lastErr != nil || bq.GetSize() <= 0 || f == nil {
return
}
defer func() {
if err := recover(); err != nil {
bq.lastErr = errors.Wrap(convert.Error(err), runtime.CallerFunction())
}
}()
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
// 使用f进行数据过滤
if size := bq.GetSize(); size > 0 {
exchange := make([]DataIndex, 0, bq.GetSize())
for i := 0; i < size; i++ {
row, has := bq.dataManager.getDataByIndexUnsafe(bq.dataIds[i])
if !has {
continue
}
exclude, err := f(row)
if err != nil {
bq.lastErr = errors.Wrap(err, runtime.CallerFunction())
return
}
// 数据不被过滤 -> 加入到exchange
if !exclude {
exchange = append(exchange, bq.dataIds[i])
}
}
bq.dataIds = exchange
}
return
}
// Sort 数据排序
func (bq *BaseQueryIdsResult) Sort(c Comparer) (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
if c != nil {
bq.dataCompare = c
sort.Sort(bq)
}
return
}
// Slice 数据切片
func (bq *BaseQueryIdsResult) Slice(s, e int) (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
// 切片检测
s, e, _ = bq.checkRegion(s, e, len(bq.dataIds))
// 不涉及到指针内容读取 -> 不需要加锁
bq.dataIds = bq.dataIds[s:e]
return
}
// Page 数据分页
func (bq *BaseQueryIdsResult) Page(cur, per int) (next IQueryResult) {
start, end := utils.PageToSection(cur, per)
return bq.Slice(start, end)
}
// Reverse 数据反序
func (bq *BaseQueryIdsResult) Reverse() (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
// 不涉及到指针内容读取 -> 不需要加锁
if bq.dataIds == nil {
return
}
length := len(bq.dataIds)
for from, to := 0, length-1; from < to; from, to = from+1, to-1 {
bq.dataIds[from], bq.dataIds[to] = bq.dataIds[to], bq.dataIds[from]
}
return
}
/************* 数据获取类 *************/
func (bq *BaseQueryIdsResult) FetchCost(cost *time.Duration) (next IQueryResult) {
next = bq
if cost != nil {
*cost = bq.queryCost
}
return
}
func (bq *BaseQueryIdsResult) FetchCostDetail(detail *QueryCostDetail) (next IQueryResult) {
next = bq
if detail != nil {
detail.Cost = bq.queryCost
detail.Miss = bq.missIndex
// 存在未命中索引的字段 -> 慢查询
if len(detail.Miss) > 0 {
detail.IsSlow = true
}
}
return
}
func (bq *BaseQueryIdsResult) FetchCount(count *int) (next IQueryResult) {
next = bq
if count != nil {
*count = bq.GetSize()
}
return
}
func (bq *BaseQueryIdsResult) FetchOne(bean AnyData) (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
data, err := bq.getOne()
if err != nil {
bq.lastErr = errors.Wrap(err, runtime.CallerFunction())
return
}
if err := utils.Clone(data, bean); err != nil {
bq.lastErr = errors.Wrapf(err, "%s Clone", runtime.CallerFunction())
}
return
}
func (bq *BaseQueryIdsResult) FetchSome(beans AnyDatas, s, e int) (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
if bq.GetSize() == 0 {
bq.lastErr = ErrNoData
return
}
s, e, bq.lastErr = bq.checkRegion(s, e, len(bq.dataIds))
if bq.lastErr != nil {
return
}
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
// 获取数据切片
someData := bq.dataIds[s:e]
if size := len(someData); size > 0 {
data := make([]AnyData, 0, size)
for i := 0; i < size; i++ {
row, has := bq.dataManager.getDataByIndex(bq.dataIds[i])
if !has {
continue
}
data = append(data, row)
}
// 数据拷贝
if err := utils.Clone(data, beans); err != nil {
bq.lastErr = errors.Wrapf(err, "%s Clone", runtime.CallerFunction())
}
}
return
}
func (bq *BaseQueryIdsResult) FetchAll(beans AnyDatas) (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
if bq.GetSize() == 0 {
bq.lastErr = ErrNoData
return
}
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
data, err := bq.getAll()
if err != nil {
bq.lastErr = errors.Wrap(err, runtime.CallerFunction())
return
}
if err := utils.Clone(data, beans); err != nil {
bq.lastErr = errors.Wrapf(err, "%s Clone", runtime.CallerFunction())
}
return
}
func (bq *BaseQueryIdsResult) FetchMap(beansMap AnyData) (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
defer func() {
if err := recover(); err != nil {
bq.lastErr = errors.Wrap(convert.Error(err), runtime.CallerFunction())
}
}()
if bq.GetSize() == 0 {
bq.lastErr = ErrNoData
return
}
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
if size := bq.GetSize(); size > 0 {
dataMap := make(map[DataIndex]AnyData)
for i := 0; i < size; i++ {
row, has := bq.dataManager.getDataByIndex(bq.dataIds[i])
if !has {
continue
}
dataMap[bq.dataIds[i]] = row
}
if err := utils.Clone(dataMap, beansMap); err != nil {
bq.lastErr = errors.Wrapf(err, "%s Clone", runtime.CallerFunction())
}
}
return
}
func (bq *BaseQueryIdsResult) FetchIndexs(indexs AnyDatas) (next IQueryResult) {
next = bq
if bq.lastErr != nil {
return
}
defer func() {
if err := recover(); err != nil {
bq.lastErr = errors.Wrap(convert.Error(err), runtime.CallerFunction())
}
}()
if bq.GetSize() <= 0 {
bq.lastErr = ErrNoData
return
}
if err := utils.Clone(bq.dataIds, indexs); err != nil {
bq.lastErr = errors.Wrapf(err, "%s Clone", runtime.CallerFunction())
}
return bq
}
/************* 信息获取类 *************/
func (bq *BaseQueryIdsResult) GetSize() int {
return len(bq.dataIds)
}
func (bq *BaseQueryIdsResult) GetError() error {
// 没有错误 && 没有数据 -> 设置ErrNoData
if bq.lastErr == nil && bq.GetSize() == 0 {
bq.lastErr = ErrNoData
}
return bq.lastErr
}
func (bq *BaseQueryIdsResult) GetOne() (AnyData, error) {
if bq.lastErr != nil {
return nil, bq.lastErr
}
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
return bq.getOne() // todo 待修改
}
func (bq *BaseQueryIdsResult) GetAll() ([]AnyData, error) {
if bq.lastErr != nil {
return nil, bq.lastErr
}
// 涉及到指针内容读取 -> 需要加锁
bq.dataManager.RLock()
defer bq.dataManager.RUnlock()
return bq.getAll() // todo 待修改
}
func (bq *BaseQueryIdsResult) SetError(err error) {
if bq.lastErr != nil {
bq.lastErr = errors.Wrapf(err, "%+v", err)
} else {
bq.lastErr = err
}
}
func (bq *BaseQueryIdsResult) SetResult(result IQueryResult) {
}
func (bq *BaseQueryIdsResult) PushIds(ids []DataIndex) {
bq.dataIds = ids
}
func (bq *BaseQueryIdsResult) getOne() (AnyData, error) {
if bq.GetSize() == 0 {
return nil, ErrNoData
}
data, has := bq.dataManager.getDataByIndex(bq.dataIds[0])
if !has {
return nil, ErrNoData
}
return data, nil
}
func (bq *BaseQueryIdsResult) getAll() ([]AnyData, error) {
if size := len(bq.dataIds); size > 0 {
data := make([]AnyData, 0, size)
for i := 0; i < size; i++ {
row, has := bq.dataManager.getDataByIndex(bq.dataIds[i])
if !has {
// 如果数据不存在->读取下一个
continue
}
data = append(data, row)
}
return data, nil
}
return nil, nil
}
func (bq *BaseQueryIdsResult) makeDataResult(size int) {
bq.dataIds = make([]DataIndex, 0, size)
}
func (bq *BaseQueryIdsResult) pushId(index DataIndex) {
bq.dataIds = append(bq.dataIds, index)
}
func (bq *BaseQueryIdsResult) checkRegion(start, end, size int) (safeStart, safeEnd int, err error) {
if size == 0 {
return 0, 0, fmt.Errorf("size is 0")
}
// -1 -> 结尾
if end == -1 {
end = size
}
// 错误区间 -> 返回空slice or string
if start > end || start >= size || end <= 0 {
return 0, 0, fmt.Errorf("out of region")
}
// 尝试恢复错误区间
if start <= 0 {
start = 0
}
if end >= size {
end = size
}
return start, end, nil
}