/
bulk.go
166 lines (159 loc) · 4.07 KB
/
bulk.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
package mgo
import (
"bytes"
"github.com/yaziming/mgo/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Bulk struct {
c *Collection
models []mongo.WriteModel
ordered bool
}
func (b *Bulk) Unordered() {
b.ordered = false
}
func (b *Bulk) Insert(docs ...interface{}) {
for _, doc := range docs {
model := mongo.NewInsertOneModel().SetDocument(doc)
b.models = append(b.models, model)
}
}
func (b *Bulk) Remove(selectors ...interface{}) {
for _, selector := range selectors {
if selector == nil {
selector = bson.D{}
}
model := mongo.NewDeleteOneModel().SetFilter(selector)
b.models = append(b.models, model)
}
}
func (b *Bulk) RemoveAll(selectors ...interface{}) {
for _, selector := range selectors {
if selector == nil {
selector = bson.D{}
}
model := mongo.NewDeleteManyModel().SetFilter(selector)
b.models = append(b.models, model)
}
}
func (b *Bulk) Update(pairs ...interface{}) {
if len(pairs)%2 != 0 {
panic("Bulk.Update requires an even number of parameters")
}
for i := 0; i < len(pairs); i += 2 {
selector := pairs[i]
if selector == nil {
selector = bson.D{}
}
model := mongo.NewUpdateOneModel().SetFilter(selector).
SetUpdate(pairs[i+1])
b.models = append(b.models, model)
}
}
func (b *Bulk) UpdateAll(pairs ...interface{}) {
if len(pairs)%2 != 0 {
panic("Bulk.UpdateAll requires an even number of parameters")
}
for i := 0; i < len(pairs); i += 2 {
selector := pairs[i]
if selector == nil {
selector = bson.D{}
}
model := mongo.NewUpdateManyModel().SetFilter(selector).
SetUpdate(pairs[i+1])
b.models = append(b.models, model)
}
}
func (b *Bulk) Upsert(pairs ...interface{}) {
if len(pairs)%2 != 0 {
panic("Bulk.Update requires an even number of parameters")
}
for i := 0; i < len(pairs); i += 2 {
selector := pairs[i]
if selector == nil {
selector = bson.D{}
}
model := mongo.NewUpdateOneModel().SetFilter(selector).
SetUpdate(pairs[i+1]).
SetUpsert(true)
b.models = append(b.models, model)
}
}
func (b *Bulk) Run(others ...*options.BulkWriteOptions) (br *BulkResult, bulkerr error) {
opts := options.BulkWrite().SetOrdered(b.ordered)
for _, other := range others {
opts = options.MergeBulkWriteOptions(opts, other)
}
result, err := b.c.collection.BulkWrite(nil, b.models, opts)
if err != nil {
var bulkError = &BulkError{ecases: []BulkErrorCase{}}
switch newType := err.(type) {
case mongo.BulkWriteException:
for _, e := range newType.WriteErrors {
bulkError.ecases = append(bulkError.ecases, BulkErrorCase{
Index: e.Index,
Err: e,
})
}
}
return br, bulkError
}
return &BulkResult{
Matched: int(result.MatchedCount),
Modified: int(result.ModifiedCount),
Inserted: int(result.InsertedCount),
Deleted: int(result.DeletedCount),
Upserted: int(result.UpsertedCount),
UpsertIds: result.UpsertedIDs,
}, nil
}
// BulkResult holds the results for a bulk operation.
type BulkResult struct {
Matched int
Modified int // Available only for MongoDB 2.6+
Inserted int
UpsertIds map[int64]interface{}
Deleted int
Upserted int
}
type BulkErrorCase struct {
Index int // Position of operation that failed, or -1 if unknown.
Err error
}
func (e *BulkError) Cases() []BulkErrorCase {
return e.ecases
}
func (e *BulkError) Error() string {
if len(e.ecases) == 0 {
return "invalid BulkError instance: no errors"
}
if len(e.ecases) == 1 {
return e.ecases[0].Err.Error()
}
msgs := make([]string, 0, len(e.ecases))
seen := make(map[string]bool)
for _, ecase := range e.ecases {
msg := ecase.Err.Error()
if !seen[msg] {
seen[msg] = true
msgs = append(msgs, msg)
}
}
if len(msgs) == 1 {
return msgs[0]
}
var buf bytes.Buffer
buf.WriteString("multiple errors in bulk operation:\n")
for _, msg := range msgs {
buf.WriteString(" - ")
buf.WriteString(msg)
buf.WriteByte('\n')
}
return buf.String()
}
// BulkError holds an error returned from running a Bulk operation.
// Individual errors may be obtained and inspected via the Cases method.
type BulkError struct {
ecases []BulkErrorCase
}