@@ -19,6 +19,8 @@ package db
19
19
import (
20
20
"time"
21
21
22
+ "gorm.io/gorm/clause"
23
+
22
24
"github.com/deepflowio/deepflow/server/controller/common"
23
25
"github.com/deepflowio/deepflow/server/controller/db/mysql"
24
26
rcommon "github.com/deepflowio/deepflow/server/controller/recorder/common"
@@ -34,10 +36,14 @@ type Operator[MPT constraint.MySQLModelPtr[MT], MT constraint.MySQLModel] interf
34
36
AddBatch (dbItems []* MT ) ([]* MT , bool )
35
37
// 更新数据
36
38
Update (lcuuid string , updateInfo map [string ]interface {}) (* MT , bool )
39
+ // 批量更新数据
40
+ UpdateBatch ([]* MT , map [string ]map [string ]interface {}) ([]* MT , bool )
37
41
// 批量删除数据
38
42
DeleteBatch (lcuuids []string ) ([]* MT , bool )
39
43
40
44
GetSoftDelete () bool
45
+ SetFieldsToUpdate ([]string )
46
+ SetMetadata (* rcommon.Metadata )
41
47
}
42
48
43
49
type OperatorBase [MPT constraint.MySQLModelPtr [MT ], MT constraint.MySQLModel ] struct {
@@ -47,6 +53,7 @@ type OperatorBase[MPT constraint.MySQLModelPtr[MT], MT constraint.MySQLModel] st
47
53
softDelete bool
48
54
allocateID bool
49
55
fieldsNeededAfterCreate []string // fields needed to be used after create
56
+ fieldsToUpdate []string // fields needed to be updated
50
57
}
51
58
52
59
func newOperatorBase [MPT constraint.MySQLModelPtr [MT ], MT constraint.MySQLModel ](resourceTypeName string , softDelete , allocateID bool ) OperatorBase [MPT , MT ] {
@@ -57,9 +64,14 @@ func newOperatorBase[MPT constraint.MySQLModelPtr[MT], MT constraint.MySQLModel]
57
64
}
58
65
}
59
66
60
- func (o * OperatorBase [MPT , MT ]) SetMetadata (md * rcommon.Metadata ) Operator [MPT , MT ] {
67
+ func (o * OperatorBase [MPT , MT ]) SetFieldsToUpdate (fs []string ) {
68
+ o .fieldsToUpdate = fs
69
+ return
70
+ }
71
+
72
+ func (o * OperatorBase [MPT , MT ]) SetMetadata (md * rcommon.Metadata ) {
61
73
o .metadata = md
62
- return o
74
+ return
63
75
}
64
76
65
77
func (o * OperatorBase [MPT , MT ]) GetSoftDelete () bool {
@@ -117,6 +129,23 @@ func (o *OperatorBase[MPT, MT]) Update(lcuuid string, updateInfo map[string]inte
117
129
return dbItem , true
118
130
}
119
131
132
+ func (o * OperatorBase [MPT , MT ]) UpdateBatch (dbItems []* MT , lcuuidToUpdateInfo map [string ]map [string ]interface {}) ([]* MT , bool ) {
133
+ startTime := time .Now ()
134
+ err := o .metadata .DB .Clauses (clause.OnConflict {
135
+ Columns : []clause.Column {{Name : "lcuuid" }},
136
+ DoUpdates : clause .AssignmentColumns (o .fieldsToUpdate ),
137
+ }).Create (& dbItems ).Error
138
+ log .Infof ("update batch cost: %v" , time .Since (startTime ))
139
+ if err != nil {
140
+ log .Errorf ("%s batch failed: %v" , rcommon .LogUpdate (o .resourceTypeName ), err .Error (), o .metadata .LogPrefixes )
141
+ return nil , false
142
+ }
143
+ for lcuuid , updateInfo := range lcuuidToUpdateInfo {
144
+ log .Infof ("%s (lcuuid: %s, detail: %+v) success" , rcommon .LogUpdate (o .resourceTypeName ), lcuuid , updateInfo , o .metadata .LogPrefixes )
145
+ }
146
+ return dbItems , true
147
+ }
148
+
120
149
func (o * OperatorBase [MPT , MT ]) DeleteBatch (lcuuids []string ) ([]* MT , bool ) {
121
150
var deletedItems []* MT
122
151
err := o .metadata .DB .Where ("lcuuid IN ?" , lcuuids ).Find (& deletedItems ).Error
0 commit comments