-
Notifications
You must be signed in to change notification settings - Fork 1
/
queryer_mapr.go
164 lines (130 loc) · 3.31 KB
/
queryer_mapr.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
package sqle
import (
"context"
"sort"
"strconv"
"strings"
"github.com/yaitoo/async"
)
// MapR is a Map/Reduce Query Provider based on databases.
type MapR[T any] struct {
dbs []*Client
}
// First executes the query and returns the first result.
func (q *MapR[T]) First(ctx context.Context, rotatedTables []string, b *Builder) (T, error) {
var it T
b.Input("rotate", "<rotate>") // lazy replace on async.Wait
query, args, err := b.Build()
if err != nil {
return it, err
}
w := async.New[T]()
for _, r := range rotatedTables {
qr := strings.ReplaceAll(query, "<rotate>", r)
for _, db := range q.dbs {
w.Add(func(db *Client, qr string) func(context.Context) (T, error) {
return func(ctx context.Context) (T, error) {
var t T
err := db.QueryRowContext(ctx, qr, args...).Bind(&t)
if err != nil {
return t, err
}
return t, nil
}
}(db, qr))
}
}
d, _, err := w.WaitAny(ctx)
return d, err
}
// Count executes the query and returns the count of results.
func (q *MapR[T]) Count(ctx context.Context, rotatedTables []string, b *Builder) (int64, error) {
b.Input("rotate", "<rotate>") // lazy replace on async.Wait
query, args, err := b.Build()
if err != nil {
return 0, err
}
w := async.New[int64]()
for _, r := range rotatedTables {
qr := strings.ReplaceAll(query, "<rotate>", r)
for _, db := range q.dbs {
w.Add(func(db *Client, qr string) func(context.Context) (int64, error) {
return func(ctx context.Context) (int64, error) {
var i int64
err := db.QueryRowContext(ctx, qr, args...).Scan(&i)
if err != nil {
return i, err
}
return i, nil
}
}(db, qr))
}
}
items, _, err := w.Wait(ctx)
if err != nil {
return 0, err
}
var total int64
for _, it := range items {
total += it
}
return total, nil
}
// Query executes the query and returns a list of results.
func (q *MapR[T]) Query(ctx context.Context, rotatedTables []string, b *Builder, less func(i, j T) bool) ([]T, error) {
b.Input("rotate", "<rotate>") // lazy replace on async.Wait
query, args, err := b.Build()
if err != nil {
return nil, err
}
w := async.New[[]T]()
for _, r := range rotatedTables {
qr := strings.ReplaceAll(query, "<rotate>", r)
for _, db := range q.dbs {
w.Add(func(db *Client, qr string) func(context.Context) ([]T, error) {
return func(context.Context) ([]T, error) {
var t []T
rows, err := db.QueryContext(ctx, qr, args...)
if err != nil {
return t, err
}
err = rows.Bind(&t)
if err != nil {
return t, err
}
return t, nil
}
}(db, qr))
}
}
items, _, err := w.Wait(ctx)
if err != nil {
return nil, err
}
var list []T
for _, it := range items {
if it != nil {
list = append(list, it...)
}
}
if less != nil {
sort.Slice(list, func(i, j int) bool {
return less(list[i], list[j])
})
}
return list, nil
}
// QueryLimit executes the query and returns a limited list of results.
func (q *MapR[T]) QueryLimit(ctx context.Context, rotatedTables []string, b *Builder, less func(i, j T) bool, limit int) ([]T, error) {
if limit > 0 {
b.SQL(" LIMIT " + strconv.Itoa(limit*len(q.dbs)))
}
list, err := q.Query(ctx, rotatedTables, b, less)
if err != nil {
return nil, err
}
if limit < len(list) {
return list[0:limit], nil
}
return list, nil
}