-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct_cursor.go
214 lines (177 loc) · 4.71 KB
/
distinct_cursor.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
package mongorec
import (
"context"
"fmt"
"github.com/ischenkx/kantoku/pkg/common/data/record"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
var _ record.Cursor[int] = DistinctCursor[int]{}
type DistinctCursor[Item any] struct {
skip int
limit int
filter record.R
sorters []record.Sorter
masks []record.Mask
keys []string
storage *Storage[Item]
}
func (cursor DistinctCursor[Item]) Skip(num int) record.Cursor[Item] {
cursor.skip += num
return cursor
}
func (cursor DistinctCursor[Item]) Limit(num int) record.Cursor[Item] {
cursor.limit = num
return cursor
}
func (cursor DistinctCursor[Item]) Mask(masks ...record.Mask) record.Cursor[Item] {
cursor.masks = append(cursor.masks, masks...)
return cursor
}
func (cursor DistinctCursor[Item]) Sort(sorters ...record.Sorter) record.Cursor[Item] {
cursor.sorters = sorters
return cursor
}
func (cursor DistinctCursor[Item]) Iter() record.Iter[Item] {
return &DistinctIter[Item]{DistinctCursor: cursor}
}
func (cursor DistinctCursor[Item]) Count(ctx context.Context) (int, error) {
if len(cursor.keys) == 0 {
return 0, nil
}
filter, err := makeRecordFilter(cursor.filter)
if err != nil {
return 0, fmt.Errorf("failed to make a filter: %w", err)
}
pipeline := bson.A{
bson.M{"$match": filter},
bson.M{
"$group": bson.M{
"_id": lo.SliceToMap[string, string, any](
cursor.keys,
func(key string) (string, any) {
return key, fmt.Sprintf("$%s", key)
},
),
},
},
bson.M{"$group": bson.M{"_id": nil, "totalCount": bson.M{"$sum": 1}}},
}
result, err := cursor.storage.collection.Aggregate(ctx, pipeline)
if err != nil {
return 0, fmt.Errorf("failed to aggregate: %s", err)
}
defer result.Close(ctx)
var doc struct {
TotalCount int `bson:"totalCount"`
}
if !result.Next(ctx) {
return 0, nil
}
if err := result.Decode(&doc); err != nil {
return 0, fmt.Errorf("failed to decode the document: %s", err)
}
return doc.TotalCount, nil
}
type DistinctIter[Item any] struct {
DistinctCursor[Item]
mongoCursor *mongo.Cursor
}
func (iter *DistinctIter[Item]) Close(ctx context.Context) error {
if iter.mongoCursor == nil {
return nil
}
return iter.mongoCursor.Close(ctx)
}
func (iter *DistinctIter[Item]) Next(ctx context.Context) (Item, error) {
var zero Item
if len(iter.keys) == 0 {
return zero, record.ErrIterEmpty
}
cursor, err := iter.getCursor(ctx)
if err != nil {
return zero, fmt.Errorf("failed to make a mongo cursor: %s", err)
}
if !cursor.Next(ctx) {
return zero, record.ErrIterEmpty
}
var doc struct {
ID bson.M `bson:"_id"`
}
err = cursor.Decode(&doc)
if err != nil {
return zero, fmt.Errorf("failed to decode the received data: %s", err)
}
rec := bson2record(doc.ID)
for _, mask := range iter.masks {
if mask.Operation == record.IncludeMask {
if _, ok := rec[mask.PropertyPattern]; !ok {
rec[mask.PropertyPattern] = nil
}
}
}
item, err := iter.DistinctCursor.storage.codec.Decode(rec)
if err != nil {
return zero, fmt.Errorf("failed to decode: %w", err)
}
return item, nil
}
func (iter *DistinctIter[Item]) getCursor(ctx context.Context) (*mongo.Cursor, error) {
if iter.mongoCursor != nil {
return iter.mongoCursor, nil
}
filter, err := makeRecordFilter(iter.filter)
if err != nil {
return nil, fmt.Errorf("failed to make a filter: %w", err)
}
pipeline := bson.A{
bson.M{"$match": filter},
bson.M{
"$group": bson.M{
"_id": lo.SliceToMap[string, string, any](
iter.keys,
func(key string) (string, any) {
return key, bson.M{"$ifNull": bson.A{fmt.Sprintf("$%s", key), nil}}
},
),
},
},
}
if len(iter.sorters) > 0 {
sort := bson.D{}
for _, sorter := range iter.sorters {
switch sorter.Ordering {
case record.Asc:
sort = append(sort, bson.E{fmt.Sprintf("_id.%s", sorter.Key), 1})
case record.Desc:
sort = append(sort, bson.E{fmt.Sprintf("_id.%s", sorter.Key), -1})
}
}
pipeline = append(pipeline, bson.M{"$sort": sort})
}
if len(iter.masks) > 0 {
projection := bson.M{}
for _, mask := range iter.masks {
switch mask.Operation {
case record.IncludeMask:
projection[fmt.Sprintf("_id.%s", mask.PropertyPattern)] = 1
case record.ExcludeMask:
projection[fmt.Sprintf("_id.%s", mask.PropertyPattern)] = 0
}
}
pipeline = append(pipeline, bson.M{"$project": projection})
}
if iter.skip > 0 {
pipeline = append(pipeline, bson.M{"$skip": iter.skip})
}
if iter.limit > 0 {
pipeline = append(pipeline, bson.M{"$limit": iter.limit})
}
cursor, err := iter.storage.collection.Aggregate(ctx, pipeline)
if err != nil {
return nil, fmt.Errorf("failed to aggregate: %s", err)
}
iter.mongoCursor = cursor
return iter.mongoCursor, nil
}