-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
505 lines (474 loc) · 12.1 KB
/
service.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package rest
import (
"context"
"fmt"
"github.com/bytedance/sonic"
"github.com/nats-io/nats.go"
"github.com/redis/go-redis/v9"
"github.com/weplanx/go/cipher"
"github.com/weplanx/go/passlib"
"github.com/weplanx/go/values"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"strings"
"time"
)
type Service struct {
Mgo *mongo.Client
Db *mongo.Database
RDb *redis.Client
JetStream nats.JetStreamContext
KeyValue nats.KeyValue
Values *values.DynamicValues
Cipher *cipher.Cipher
}
func (x *Service) IsForbid(name string) bool {
if x.Values.RestControls[name] == nil {
return true
}
return !x.Values.RestControls[name].Status
}
const (
ActionCreate = 1
ActionBulkCreate = 2
ActionUpdate = 3
ActionUpdateById = 4
ActionReplace = 5
ActionDelete = 6
ActionBulkDelete = 7
ActionSort = 8
)
func (x *Service) Create(ctx context.Context, name string, doc interface{}) (result interface{}, err error) {
if result, err = x.Db.Collection(name).InsertOne(ctx, doc); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionCreate,
Data: doc,
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) BulkCreate(ctx context.Context, name string, docs []interface{}) (result interface{}, err error) {
if result, err = x.Db.Collection(name).InsertMany(ctx, docs); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionBulkCreate,
Data: docs,
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) Size(ctx context.Context, name string, filter M) (_ int64, err error) {
if len(filter) == 0 {
return x.Db.Collection(name).EstimatedDocumentCount(ctx)
}
return x.Db.Collection(name).CountDocuments(ctx, filter)
}
func (x *Service) Find(ctx context.Context, name string, filter M, option *options.FindOptions) (data []M, err error) {
var cursor *mongo.Cursor
if cursor, err = x.Db.Collection(name).Find(ctx, filter, option); err != nil {
return
}
data = make([]M, 0)
for cursor.Next(ctx) {
var v M
if err = cursor.Decode(&v); err != nil {
return
}
x.Sensitive(name, v)
data = append(data, v)
}
return
}
func (x *Service) FindOne(ctx context.Context, name string, filter M, option *options.FindOneOptions) (data M, err error) {
if err = x.Db.Collection(name).FindOne(ctx, filter, option).Decode(&data); err != nil {
return
}
x.Sensitive(name, data)
return
}
func (x *Service) Update(ctx context.Context, name string, filter M, update interface{}, option *options.UpdateOptions) (result interface{}, err error) {
if result, err = x.Db.Collection(name).UpdateMany(ctx, filter, update, option); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionUpdate,
Filter: filter,
Data: update,
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) UpdateById(ctx context.Context, name string, id primitive.ObjectID, update interface{}, option *options.UpdateOptions) (result interface{}, err error) {
filter := M{"_id": id}
if result, err = x.Db.Collection(name).UpdateOne(ctx, filter, update, option); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionUpdateById,
Id: id.Hex(),
Data: update,
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) Replace(ctx context.Context, name string, id primitive.ObjectID, doc interface{}) (result interface{}, err error) {
filter := M{"_id": id}
if result, err = x.Db.Collection(name).ReplaceOne(ctx, filter, doc); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionReplace,
Id: id.Hex(),
Data: doc,
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) Delete(ctx context.Context, name string, id primitive.ObjectID, transaction bool) (result interface{}, err error) {
filter := M{
"_id": id,
"_durable": bson.M{"$exists": false},
}
var doc M
if !transaction {
if err = x.Db.Collection(name).
FindOne(ctx, filter).
Decode(&doc); err != nil {
return
}
}
if result, err = x.Db.Collection(name).
DeleteOne(ctx, filter); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionDelete,
Id: id.Hex(),
Data: doc,
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) BulkDelete(ctx context.Context, name string, filter M, transaction bool) (result interface{}, err error) {
filter["_durable"] = bson.M{"$exists": false}
var docs []M
if !transaction {
var cursor *mongo.Cursor
if cursor, err = x.Db.Collection(name).Find(ctx, filter); err != nil {
return
}
if err = cursor.All(ctx, &docs); err != nil {
return
}
}
if result, err = x.Db.Collection(name).
DeleteMany(ctx, filter); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionBulkDelete,
Filter: filter,
Data: docs,
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) Sort(ctx context.Context, name string, key string, ids []primitive.ObjectID) (result interface{}, err error) {
var wms []mongo.WriteModel
for i, id := range ids {
update := M{"$set": M{key: i, "update_time": time.Now()}}
wms = append(wms, mongo.NewUpdateOneModel().
SetFilter(M{"_id": id}).
SetUpdate(update),
)
}
if result, err = x.Db.Collection(name).BulkWrite(ctx, wms); err != nil {
return
}
if err = x.Publish(ctx, name, PublishDto{
Action: ActionSort,
Data: M{
"key": key,
"values": ids,
},
Result: result,
}); err != nil {
return
}
return
}
func (x *Service) Transaction(ctx context.Context, txn string) {
key := fmt.Sprintf(`transaction:%s`, txn)
x.RDb.LPush(ctx, key, time.Now().Format(time.RFC3339)).Val()
x.RDb.Expire(ctx, key, time.Hour*5).Val()
}
type PendingDto struct {
Action int `bson:"action"`
Name string `bson:"name"`
Id primitive.ObjectID `bson:"id,omitempty"`
Filter M `bson:"filter,omitempty"`
Data interface{} `bson:"data,omitempty"`
}
func (x *Service) TxnNotExists(ctx context.Context, key string) (err error) {
var exists int64
if exists, err = x.RDb.Exists(ctx, key).Result(); err != nil {
return
}
if exists != 1 {
return ErrTxnNotExist
}
return
}
func (x *Service) Pending(ctx context.Context, txn string, dto PendingDto) (err error) {
key := fmt.Sprintf(`transaction:%s`, txn)
if err = x.TxnNotExists(ctx, key); err != nil {
return
}
var b []byte
if b, err = bson.Marshal(dto); err != nil {
return
}
if err = x.RDb.LPush(ctx, key, b).Err(); err != nil {
return
}
return
}
func (x *Service) Commit(ctx context.Context, txn string) (_ interface{}, err error) {
key := fmt.Sprintf(`transaction:%s`, txn)
if err = x.TxnNotExists(ctx, key); err != nil {
return
}
var begin time.Time
if begin, err = x.RDb.RPop(ctx, key).Time(); err != nil {
return
}
if time.Since(begin) > x.Values.RestTxnTimeout {
err = ErrTxnTimeOut
return
}
var n int64
if n, err = x.RDb.LLen(ctx, key).Result(); err != nil {
return
}
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
var session mongo.Session
if session, err = x.Mgo.StartSession(opts); err != nil {
return
}
defer session.EndSession(ctx)
txnOpts := options.Transaction().SetReadPreference(readpref.PrimaryPreferred())
return session.WithTransaction(ctx, func(txnCtx mongo.SessionContext) (_ interface{}, err error) {
var results []interface{}
for n > 0 {
var b []byte
if b, err = x.RDb.RPop(ctx, key).Bytes(); err != nil {
return
}
var dto PendingDto
if err = bson.Unmarshal(b, &dto); err != nil {
return
}
var r interface{}
if r, err = x.Invoke(txnCtx, dto); err != nil {
return
}
results = append(results, r)
n--
}
return results, nil
}, txnOpts)
}
func (x *Service) Invoke(ctx context.Context, dto PendingDto) (_ interface{}, _ error) {
switch dto.Action {
case ActionCreate:
return x.Create(ctx, dto.Name, dto.Data)
case ActionBulkCreate:
return x.BulkCreate(ctx, dto.Name, dto.Data.(primitive.A))
case ActionUpdate:
return x.Update(ctx, dto.Name, dto.Filter, dto.Data, nil)
case ActionUpdateById:
return x.UpdateById(ctx, dto.Name, dto.Id, dto.Data, nil)
case ActionReplace:
return x.Replace(ctx, dto.Name, dto.Id, dto.Data)
case ActionDelete:
return x.Delete(ctx, dto.Name, dto.Id, true)
case ActionBulkDelete:
return x.BulkDelete(ctx, dto.Name, dto.Filter, true)
case ActionSort:
data := dto.Data.(primitive.D)
var key string
var ids []primitive.ObjectID
for _, v := range data {
switch v.Key {
case "key":
key = v.Value.(string)
break
case "values":
for _, id := range v.Value.(primitive.A) {
ids = append(ids, id.(primitive.ObjectID))
}
break
}
}
return x.Sort(ctx, dto.Name, key, ids)
}
return
}
func (x *Service) Transform(data M, rules M) (err error) {
for key, value := range rules {
paths := strings.Split(key, "->")
if err = x.Pipe(data, paths, value); err != nil {
return
}
}
return
}
func (x *Service) Pipe(input M, paths []string, kind interface{}) (err error) {
var cursor interface{} = input
n := len(paths) - 1
for i, path := range paths[:n] {
if path == "$" {
for _, item := range cursor.([]interface{}) {
if err = x.Pipe(item.(M), paths[i+1:], kind); err != nil {
return
}
}
return
}
if cursor.(M)[path] == nil {
return
}
cursor = cursor.(M)[path]
}
key := paths[n]
if cursor == nil || cursor.(M)[key] == nil {
return
}
unknow := cursor.(M)[key]
var data interface{}
switch kind {
case "oid":
if data, err = primitive.ObjectIDFromHex(unknow.(string)); err != nil {
return
}
break
case "oids":
oids := unknow.([]interface{})
for i, id := range oids {
if oids[i], err = primitive.ObjectIDFromHex(id.(string)); err != nil {
return
}
}
data = oids
break
case "date":
if data, err = time.Parse(time.RFC1123, unknow.(string)); err != nil {
return
}
break
case "dates":
dates := unknow.([]interface{})
for i, date := range dates {
if dates[i], err = time.Parse(time.RFC1123, date.(string)); err != nil {
return
}
}
data = dates
break
case "timestamp":
if data, err = time.Parse(time.RFC3339, unknow.(string)); err != nil {
return
}
break
case "timestamps":
timestamps := unknow.([]interface{})
for i, timestamp := range timestamps {
if timestamps[i], err = time.Parse(time.RFC3339, timestamp.(string)); err != nil {
return
}
}
data = timestamps
break
case "password":
data, _ = passlib.Hash(unknow.(string))
break
case "cipher":
if data, err = x.Cipher.Encode([]byte(unknow.(string))); err != nil {
return
}
break
}
cursor.(M)[key] = data
return
}
func (x *Service) Projection(name string, keys []string) (result bson.M) {
result = make(bson.M)
if x.Values.RestControls != nil && x.Values.RestControls[name] != nil {
for _, key := range x.Values.RestControls[name].Keys {
result[key] = 1
}
}
if len(keys) != 0 {
projection := make(bson.M)
for _, key := range keys {
if _, ok := result[key]; len(result) != 0 && !ok {
continue
}
projection[key] = 1
}
result = projection
}
return
}
func (x *Service) Sensitive(name string, v M) {
if x.Values.RestControls != nil && x.Values.RestControls[name] != nil {
for _, key := range x.Values.RestControls[name].Sensitives {
if v[key] == nil {
v[key] = "-"
} else {
v[key] = "*"
}
}
}
}
type PublishDto struct {
Action int `json:"action"`
Id string `json:"id,omitempty"`
Filter M `json:"filter,omitempty"`
Data interface{} `json:"data,omitempty"`
Result interface{} `json:"result"`
}
func (x *Service) Publish(ctx context.Context, name string, dto PublishDto) (err error) {
if v, ok := x.Values.RestControls[name]; ok {
if !v.Event {
return
}
b, _ := sonic.Marshal(dto)
subject := fmt.Sprintf(`events.%s`, name)
if _, err = x.JetStream.Publish(subject, b, nats.Context(ctx)); err != nil {
return
}
}
return
}