optimization version log

pull/2336/head
withchao 1 year ago
parent 0aaf8b93a0
commit 0f72de85b2

@ -54,7 +54,7 @@ func NewFriendMongo(db *mongo.Database) (database.Friend, error) {
// Create inserts multiple friend records. // Create inserts multiple friend records.
func (f *FriendMgo) Create(ctx context.Context, friends []*model.Friend) error { func (f *FriendMgo) Create(ctx context.Context, friends []*model.Friend) error {
return IncrVersion(func() error { return mongoutil.IncrVersion(func() error {
return mongoutil.InsertMany(ctx, f.coll, friends) return mongoutil.InsertMany(ctx, f.coll, friends)
}, func() error { }, func() error {
mp := make(map[string][]string) mp := make(map[string][]string)
@ -76,7 +76,7 @@ func (f *FriendMgo) Delete(ctx context.Context, ownerUserID string, friendUserID
"owner_user_id": ownerUserID, "owner_user_id": ownerUserID,
"friend_user_id": bson.M{"$in": friendUserIDs}, "friend_user_id": bson.M{"$in": friendUserIDs},
} }
return IncrVersion(func() error { return mongoutil.IncrVersion(func() error {
return mongoutil.DeleteOne(ctx, f.coll, filter) return mongoutil.DeleteOne(ctx, f.coll, filter)
}, func() error { }, func() error {
return f.owner.WriteLog(ctx, ownerUserID, friendUserIDs, true) return f.owner.WriteLog(ctx, ownerUserID, friendUserIDs, true)
@ -92,7 +92,7 @@ func (f *FriendMgo) UpdateByMap(ctx context.Context, ownerUserID string, friendU
"owner_user_id": ownerUserID, "owner_user_id": ownerUserID,
"friend_user_id": friendUserID, "friend_user_id": friendUserID,
} }
return IncrVersion(func() error { return mongoutil.IncrVersion(func() error {
return mongoutil.UpdateOne(ctx, f.coll, filter, bson.M{"$set": args}, true) return mongoutil.UpdateOne(ctx, f.coll, filter, bson.M{"$set": args}, true)
}, func() error { }, func() error {
return f.owner.WriteLog(ctx, ownerUserID, []string{friendUserID}, false) return f.owner.WriteLog(ctx, ownerUserID, []string{friendUserID}, false)
@ -152,13 +152,6 @@ func (f *FriendMgo) FindOwnerFriends(ctx context.Context, ownerUserID string, pa
func (f *FriendMgo) FindOwnerFriendUserIds(ctx context.Context, ownerUserID string, limit int) ([]string, error) { func (f *FriendMgo) FindOwnerFriendUserIds(ctx context.Context, ownerUserID string, limit int) ([]string, error) {
filter := bson.M{"owner_user_id": ownerUserID} filter := bson.M{"owner_user_id": ownerUserID}
opt := options.Find().SetProjection(bson.M{"_id": 0, "friend_user_id": 1}).SetSort(bson.D{{"friend_nickname", 1}, {"create_time", 1}}).SetLimit(int64(limit)) opt := options.Find().SetProjection(bson.M{"_id": 0, "friend_user_id": 1}).SetSort(bson.D{{"friend_nickname", 1}, {"create_time", 1}}).SetLimit(int64(limit))
//res, err := mongoutil.Find[string](ctx, f.coll, filter, opt)
//if err != nil {
// errMsg := err.Error()
// _ = errMsg
// return nil, err
//}
//return res, nil
return mongoutil.Find[string](ctx, f.coll, filter, opt) return mongoutil.Find[string](ctx, f.coll, filter, opt)
} }
@ -189,7 +182,7 @@ func (f *FriendMgo) UpdateFriends(ctx context.Context, ownerUserID string, frien
// Create an update document // Create an update document
update := bson.M{"$set": val} update := bson.M{"$set": val}
return IncrVersion(func() error { return mongoutil.IncrVersion(func() error {
return mongoutil.Ignore(mongoutil.UpdateMany(ctx, f.coll, filter, update)) return mongoutil.Ignore(mongoutil.UpdateMany(ctx, f.coll, filter, update))
}, func() error { }, func() error {
return f.owner.WriteLog(ctx, ownerUserID, friendUserIDs, false) return f.owner.WriteLog(ctx, ownerUserID, friendUserIDs, false)
@ -227,12 +220,3 @@ func (f *FriendMgo) SearchFriend(ctx context.Context, ownerUserID, keyword strin
//return f.aggregatePagination(ctx, where, pagination) //return f.aggregatePagination(ctx, where, pagination)
panic("todo") panic("todo")
} }
func IncrVersion(dbs ...func() error) error {
for _, fn := range dbs {
if err := fn(); err != nil {
return err
}
}
return nil
}

Loading…
Cancel
Save