/
update.go
161 lines (137 loc) · 4.02 KB
/
update.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
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package command
import (
"context"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/description"
"github.com/mongodb/mongo-go-driver/core/result"
"github.com/mongodb/mongo-go-driver/core/session"
"github.com/mongodb/mongo-go-driver/core/wiremessage"
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
"github.com/mongodb/mongo-go-driver/x/bsonx"
)
// Update represents the update command.
//
// The update command updates a set of documents with the database.
type Update struct {
ContinueOnError bool
Clock *session.ClusterClock
NS Namespace
Docs []bsonx.Doc
Opts []bsonx.Elem
WriteConcern *writeconcern.WriteConcern
Session *session.Client
batches []*WriteBatch
result result.Update
err error
}
// Encode will encode this command into a wire message for the given server description.
func (u *Update) Encode(desc description.SelectedServer) ([]wiremessage.WireMessage, error) {
err := u.encode(desc)
if err != nil {
return nil, err
}
return batchesToWireMessage(u.batches, desc)
}
func (u *Update) encode(desc description.SelectedServer) error {
batches, err := splitBatches(u.Docs, int(desc.MaxBatchCount), int(desc.MaxDocumentSize))
if err != nil {
return err
}
for _, docs := range batches {
cmd, err := u.encodeBatch(docs, desc)
if err != nil {
return err
}
u.batches = append(u.batches, cmd)
}
return nil
}
func (u *Update) encodeBatch(docs []bsonx.Doc, desc description.SelectedServer) (*WriteBatch, error) {
copyDocs := make([]bsonx.Doc, 0, len(docs)) // copy of all the documents
for _, doc := range docs {
newDoc := doc.Copy()
copyDocs = append(copyDocs, newDoc)
}
var options []bsonx.Elem
for _, opt := range u.Opts {
switch opt.Key {
case "upsert", "collation", "arrayFilters":
// options that are encoded on each individual document
for idx := range copyDocs {
copyDocs[idx] = append(copyDocs[idx], opt)
}
default:
options = append(options, opt)
}
}
command, err := encodeBatch(copyDocs, options, UpdateCommand, u.NS.Collection)
if err != nil {
return nil, err
}
return &WriteBatch{
&Write{
Clock: u.Clock,
DB: u.NS.DB,
Command: command,
WriteConcern: u.WriteConcern,
Session: u.Session,
},
len(docs),
}, nil
}
// Decode will decode the wire message using the provided server description. Errors during decoding
// are deferred until either the Result or Err methods are called.
func (u *Update) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *Update {
rdr, err := (&Write{}).Decode(desc, wm).Result()
if err != nil {
u.err = err
return u
}
return u.decode(desc, rdr)
}
func (u *Update) decode(desc description.SelectedServer, rdr bson.Raw) *Update {
u.err = bson.Unmarshal(rdr, &u.result)
return u
}
// Result returns the result of a decoded wire message and server description.
func (u *Update) Result() (result.Update, error) {
if u.err != nil {
return result.Update{}, u.err
}
return u.result, nil
}
// Err returns the error set on this command.
func (u *Update) Err() error { return u.err }
// RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.
func (u *Update) RoundTrip(
ctx context.Context,
desc description.SelectedServer,
rw wiremessage.ReadWriter,
) (result.Update, error) {
if u.batches == nil {
err := u.encode(desc)
if err != nil {
return result.Update{}, err
}
}
r, batches, err := roundTripBatches(
ctx, desc, rw,
u.batches,
u.ContinueOnError,
u.Session,
UpdateCommand,
)
// if there are leftover batches, save them for retry
if batches != nil {
u.batches = batches
}
if err != nil {
return result.Update{}, err
}
return r.(result.Update), nil
}