...
1
2
3
4
5
6
7 package operation
8
9 import (
10 "context"
11 "errors"
12 "fmt"
13 "time"
14
15 "go.mongodb.org/mongo-driver/event"
16 "go.mongodb.org/mongo-driver/mongo/description"
17 "go.mongodb.org/mongo-driver/mongo/writeconcern"
18 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
19 "go.mongodb.org/mongo-driver/x/mongo/driver"
20 "go.mongodb.org/mongo-driver/x/mongo/driver/session"
21 )
22
23
24 type UpdateSearchIndex struct {
25 index string
26 definition bsoncore.Document
27 session *session.Client
28 clock *session.ClusterClock
29 collection string
30 monitor *event.CommandMonitor
31 crypt driver.Crypt
32 database string
33 deployment driver.Deployment
34 selector description.ServerSelector
35 writeConcern *writeconcern.WriteConcern
36 result UpdateSearchIndexResult
37 serverAPI *driver.ServerAPIOptions
38 timeout *time.Duration
39 }
40
41
42 type UpdateSearchIndexResult struct {
43 Ok int32
44 }
45
46 func buildUpdateSearchIndexResult(response bsoncore.Document) (UpdateSearchIndexResult, error) {
47 elements, err := response.Elements()
48 if err != nil {
49 return UpdateSearchIndexResult{}, err
50 }
51 usir := UpdateSearchIndexResult{}
52 for _, element := range elements {
53 switch element.Key() {
54 case "ok":
55 var ok bool
56 usir.Ok, ok = element.Value().AsInt32OK()
57 if !ok {
58 return usir, fmt.Errorf("response field 'ok' is type int32, but received BSON type %s", element.Value().Type)
59 }
60 }
61 }
62 return usir, nil
63 }
64
65
66 func NewUpdateSearchIndex(index string, definition bsoncore.Document) *UpdateSearchIndex {
67 return &UpdateSearchIndex{
68 index: index,
69 definition: definition,
70 }
71 }
72
73
74 func (usi *UpdateSearchIndex) Result() UpdateSearchIndexResult { return usi.result }
75
76 func (usi *UpdateSearchIndex) processResponse(info driver.ResponseInfo) error {
77 var err error
78 usi.result, err = buildUpdateSearchIndexResult(info.ServerResponse)
79 return err
80 }
81
82
83 func (usi *UpdateSearchIndex) Execute(ctx context.Context) error {
84 if usi.deployment == nil {
85 return errors.New("the UpdateSearchIndex operation must have a Deployment set before Execute can be called")
86 }
87
88 return driver.Operation{
89 CommandFn: usi.command,
90 ProcessResponseFn: usi.processResponse,
91 Client: usi.session,
92 Clock: usi.clock,
93 CommandMonitor: usi.monitor,
94 Crypt: usi.crypt,
95 Database: usi.database,
96 Deployment: usi.deployment,
97 Selector: usi.selector,
98 WriteConcern: usi.writeConcern,
99 ServerAPI: usi.serverAPI,
100 Timeout: usi.timeout,
101 }.Execute(ctx)
102
103 }
104
105 func (usi *UpdateSearchIndex) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
106 dst = bsoncore.AppendStringElement(dst, "updateSearchIndex", usi.collection)
107 dst = bsoncore.AppendStringElement(dst, "name", usi.index)
108 dst = bsoncore.AppendDocumentElement(dst, "definition", usi.definition)
109 return dst, nil
110 }
111
112
113 func (usi *UpdateSearchIndex) Index(name string) *UpdateSearchIndex {
114 if usi == nil {
115 usi = new(UpdateSearchIndex)
116 }
117
118 usi.index = name
119 return usi
120 }
121
122
123 func (usi *UpdateSearchIndex) Definition(definition bsoncore.Document) *UpdateSearchIndex {
124 if usi == nil {
125 usi = new(UpdateSearchIndex)
126 }
127
128 usi.definition = definition
129 return usi
130 }
131
132
133 func (usi *UpdateSearchIndex) Session(session *session.Client) *UpdateSearchIndex {
134 if usi == nil {
135 usi = new(UpdateSearchIndex)
136 }
137
138 usi.session = session
139 return usi
140 }
141
142
143 func (usi *UpdateSearchIndex) ClusterClock(clock *session.ClusterClock) *UpdateSearchIndex {
144 if usi == nil {
145 usi = new(UpdateSearchIndex)
146 }
147
148 usi.clock = clock
149 return usi
150 }
151
152
153 func (usi *UpdateSearchIndex) Collection(collection string) *UpdateSearchIndex {
154 if usi == nil {
155 usi = new(UpdateSearchIndex)
156 }
157
158 usi.collection = collection
159 return usi
160 }
161
162
163 func (usi *UpdateSearchIndex) CommandMonitor(monitor *event.CommandMonitor) *UpdateSearchIndex {
164 if usi == nil {
165 usi = new(UpdateSearchIndex)
166 }
167
168 usi.monitor = monitor
169 return usi
170 }
171
172
173 func (usi *UpdateSearchIndex) Crypt(crypt driver.Crypt) *UpdateSearchIndex {
174 if usi == nil {
175 usi = new(UpdateSearchIndex)
176 }
177
178 usi.crypt = crypt
179 return usi
180 }
181
182
183 func (usi *UpdateSearchIndex) Database(database string) *UpdateSearchIndex {
184 if usi == nil {
185 usi = new(UpdateSearchIndex)
186 }
187
188 usi.database = database
189 return usi
190 }
191
192
193 func (usi *UpdateSearchIndex) Deployment(deployment driver.Deployment) *UpdateSearchIndex {
194 if usi == nil {
195 usi = new(UpdateSearchIndex)
196 }
197
198 usi.deployment = deployment
199 return usi
200 }
201
202
203 func (usi *UpdateSearchIndex) ServerSelector(selector description.ServerSelector) *UpdateSearchIndex {
204 if usi == nil {
205 usi = new(UpdateSearchIndex)
206 }
207
208 usi.selector = selector
209 return usi
210 }
211
212
213 func (usi *UpdateSearchIndex) WriteConcern(writeConcern *writeconcern.WriteConcern) *UpdateSearchIndex {
214 if usi == nil {
215 usi = new(UpdateSearchIndex)
216 }
217
218 usi.writeConcern = writeConcern
219 return usi
220 }
221
222
223 func (usi *UpdateSearchIndex) ServerAPI(serverAPI *driver.ServerAPIOptions) *UpdateSearchIndex {
224 if usi == nil {
225 usi = new(UpdateSearchIndex)
226 }
227
228 usi.serverAPI = serverAPI
229 return usi
230 }
231
232
233 func (usi *UpdateSearchIndex) Timeout(timeout *time.Duration) *UpdateSearchIndex {
234 if usi == nil {
235 usi = new(UpdateSearchIndex)
236 }
237
238 usi.timeout = timeout
239 return usi
240 }
241
View as plain text