-
Notifications
You must be signed in to change notification settings - Fork 883
/
create_search_indexes.go
245 lines (205 loc) · 6.79 KB
/
create_search_indexes.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (C) MongoDB, Inc. 2023-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 operation
import (
"context"
"errors"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/mongo/description"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver"
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
)
// CreateSearchIndexes performs a createSearchIndexes operation.
type CreateSearchIndexes struct {
indexes bsoncore.Document
session *session.Client
clock *session.ClusterClock
collection string
monitor *event.CommandMonitor
crypt driver.Crypt
database string
deployment driver.Deployment
selector description.ServerSelector
writeConcern *writeconcern.WriteConcern
result CreateSearchIndexesResult
serverAPI *driver.ServerAPIOptions
timeout *time.Duration
}
// CreateSearchIndexResult represents a single search index result in CreateSearchIndexesResult.
type CreateSearchIndexResult struct {
Name string
}
// CreateSearchIndexesResult represents a createSearchIndexes result returned by the server.
type CreateSearchIndexesResult struct {
IndexesCreated []CreateSearchIndexResult
}
func buildCreateSearchIndexesResult(response bsoncore.Document) (CreateSearchIndexesResult, error) {
elements, err := response.Elements()
if err != nil {
return CreateSearchIndexesResult{}, err
}
csir := CreateSearchIndexesResult{}
for _, element := range elements {
switch element.Key() {
case "indexesCreated":
arr, ok := element.Value().ArrayOK()
if !ok {
return csir, fmt.Errorf("response field 'indexesCreated' is type array, but received BSON type %s", element.Value().Type)
}
var values []bsoncore.Value
values, err = arr.Values()
if err != nil {
break
}
for _, val := range values {
valDoc, ok := val.DocumentOK()
if !ok {
return csir, fmt.Errorf("indexesCreated value is type document, but received BSON type %s", val.Type)
}
var indexesCreated CreateSearchIndexResult
if err = bson.Unmarshal(valDoc, &indexesCreated); err != nil {
return csir, err
}
csir.IndexesCreated = append(csir.IndexesCreated, indexesCreated)
}
}
}
return csir, nil
}
// NewCreateSearchIndexes constructs and returns a new CreateSearchIndexes.
func NewCreateSearchIndexes(indexes bsoncore.Document) *CreateSearchIndexes {
return &CreateSearchIndexes{
indexes: indexes,
}
}
// Result returns the result of executing this operation.
func (csi *CreateSearchIndexes) Result() CreateSearchIndexesResult { return csi.result }
func (csi *CreateSearchIndexes) processResponse(info driver.ResponseInfo) error {
var err error
csi.result, err = buildCreateSearchIndexesResult(info.ServerResponse)
return err
}
// Execute runs this operations and returns an error if the operation did not execute successfully.
func (csi *CreateSearchIndexes) Execute(ctx context.Context) error {
if csi.deployment == nil {
return errors.New("the CreateSearchIndexes operation must have a Deployment set before Execute can be called")
}
return driver.Operation{
CommandFn: csi.command,
ProcessResponseFn: csi.processResponse,
CommandMonitor: csi.monitor,
Database: csi.database,
Deployment: csi.deployment,
}.Execute(ctx)
}
func (csi *CreateSearchIndexes) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
dst = bsoncore.AppendStringElement(dst, "createSearchIndexes", csi.collection)
if csi.indexes != nil {
dst = bsoncore.AppendArrayElement(dst, "indexes", csi.indexes)
}
return dst, nil
}
// Indexes specifies an array containing index specification documents for the indexes being created.
func (csi *CreateSearchIndexes) Indexes(indexes bsoncore.Document) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.indexes = indexes
return csi
}
// Session sets the session for this operation.
func (csi *CreateSearchIndexes) Session(session *session.Client) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.session = session
return csi
}
// ClusterClock sets the cluster clock for this operation.
func (csi *CreateSearchIndexes) ClusterClock(clock *session.ClusterClock) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.clock = clock
return csi
}
// Collection sets the collection that this command will run against.
func (csi *CreateSearchIndexes) Collection(collection string) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.collection = collection
return csi
}
// CommandMonitor sets the monitor to use for APM events.
func (csi *CreateSearchIndexes) CommandMonitor(monitor *event.CommandMonitor) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.monitor = monitor
return csi
}
// Crypt sets the Crypt object to use for automatic encryption and decryption.
func (csi *CreateSearchIndexes) Crypt(crypt driver.Crypt) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.crypt = crypt
return csi
}
// Database sets the database to run this operation against.
func (csi *CreateSearchIndexes) Database(database string) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.database = database
return csi
}
// Deployment sets the deployment to use for this operation.
func (csi *CreateSearchIndexes) Deployment(deployment driver.Deployment) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.deployment = deployment
return csi
}
// ServerSelector sets the selector used to retrieve a server.
func (csi *CreateSearchIndexes) ServerSelector(selector description.ServerSelector) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.selector = selector
return csi
}
// WriteConcern sets the write concern for this operation.
func (csi *CreateSearchIndexes) WriteConcern(writeConcern *writeconcern.WriteConcern) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.writeConcern = writeConcern
return csi
}
// ServerAPI sets the server API version for this operation.
func (csi *CreateSearchIndexes) ServerAPI(serverAPI *driver.ServerAPIOptions) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.serverAPI = serverAPI
return csi
}
// Timeout sets the timeout for this operation.
func (csi *CreateSearchIndexes) Timeout(timeout *time.Duration) *CreateSearchIndexes {
if csi == nil {
csi = new(CreateSearchIndexes)
}
csi.timeout = timeout
return csi
}