github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/x/mongo/driver/operation/createIndexes.go (about) 1 // Copyright (C) MongoDB, Inc. 2019-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7 package operation 8 9 import ( 10 "context" 11 "errors" 12 "fmt" 13 "time" 14 15 "go.mongodb.org/mongo-driver/bson/bsontype" 16 "go.mongodb.org/mongo-driver/event" 17 "go.mongodb.org/mongo-driver/mongo/description" 18 "go.mongodb.org/mongo-driver/mongo/writeconcern" 19 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" 20 "go.mongodb.org/mongo-driver/x/mongo/driver" 21 "go.mongodb.org/mongo-driver/x/mongo/driver/session" 22 ) 23 24 // CreateIndexes performs a createIndexes operation. 25 type CreateIndexes struct { 26 commitQuorum bsoncore.Value 27 indexes bsoncore.Document 28 maxTime *time.Duration 29 session *session.Client 30 clock *session.ClusterClock 31 collection string 32 monitor *event.CommandMonitor 33 crypt driver.Crypt 34 database string 35 deployment driver.Deployment 36 selector description.ServerSelector 37 writeConcern *writeconcern.WriteConcern 38 result CreateIndexesResult 39 serverAPI *driver.ServerAPIOptions 40 timeout *time.Duration 41 } 42 43 // CreateIndexesResult represents a createIndexes result returned by the server. 44 type CreateIndexesResult struct { 45 // If the collection was created automatically. 46 CreatedCollectionAutomatically bool 47 // The number of indexes existing after this command. 48 IndexesAfter int32 49 // The number of indexes existing before this command. 50 IndexesBefore int32 51 } 52 53 func buildCreateIndexesResult(response bsoncore.Document) (CreateIndexesResult, error) { 54 elements, err := response.Elements() 55 if err != nil { 56 return CreateIndexesResult{}, err 57 } 58 cir := CreateIndexesResult{} 59 for _, element := range elements { 60 switch element.Key() { 61 case "createdCollectionAutomatically": 62 var ok bool 63 cir.CreatedCollectionAutomatically, ok = element.Value().BooleanOK() 64 if !ok { 65 return cir, fmt.Errorf("response field 'createdCollectionAutomatically' is type bool, but received BSON type %s", element.Value().Type) 66 } 67 case "indexesAfter": 68 var ok bool 69 cir.IndexesAfter, ok = element.Value().AsInt32OK() 70 if !ok { 71 return cir, fmt.Errorf("response field 'indexesAfter' is type int32, but received BSON type %s", element.Value().Type) 72 } 73 case "indexesBefore": 74 var ok bool 75 cir.IndexesBefore, ok = element.Value().AsInt32OK() 76 if !ok { 77 return cir, fmt.Errorf("response field 'indexesBefore' is type int32, but received BSON type %s", element.Value().Type) 78 } 79 } 80 } 81 return cir, nil 82 } 83 84 // NewCreateIndexes constructs and returns a new CreateIndexes. 85 func NewCreateIndexes(indexes bsoncore.Document) *CreateIndexes { 86 return &CreateIndexes{ 87 indexes: indexes, 88 } 89 } 90 91 // Result returns the result of executing this operation. 92 func (ci *CreateIndexes) Result() CreateIndexesResult { return ci.result } 93 94 func (ci *CreateIndexes) processResponse(info driver.ResponseInfo) error { 95 var err error 96 ci.result, err = buildCreateIndexesResult(info.ServerResponse) 97 return err 98 } 99 100 // Execute runs this operations and returns an error if the operation did not execute successfully. 101 func (ci *CreateIndexes) Execute(ctx context.Context) error { 102 if ci.deployment == nil { 103 return errors.New("the CreateIndexes operation must have a Deployment set before Execute can be called") 104 } 105 106 return driver.Operation{ 107 CommandFn: ci.command, 108 ProcessResponseFn: ci.processResponse, 109 Client: ci.session, 110 Clock: ci.clock, 111 CommandMonitor: ci.monitor, 112 Crypt: ci.crypt, 113 Database: ci.database, 114 Deployment: ci.deployment, 115 MaxTime: ci.maxTime, 116 Selector: ci.selector, 117 WriteConcern: ci.writeConcern, 118 ServerAPI: ci.serverAPI, 119 Timeout: ci.timeout, 120 }.Execute(ctx) 121 122 } 123 124 func (ci *CreateIndexes) command(dst []byte, desc description.SelectedServer) ([]byte, error) { 125 dst = bsoncore.AppendStringElement(dst, "createIndexes", ci.collection) 126 if ci.commitQuorum.Type != bsontype.Type(0) { 127 if desc.WireVersion == nil || !desc.WireVersion.Includes(9) { 128 return nil, errors.New("the 'commitQuorum' command parameter requires a minimum server wire version of 9") 129 } 130 dst = bsoncore.AppendValueElement(dst, "commitQuorum", ci.commitQuorum) 131 } 132 if ci.indexes != nil { 133 dst = bsoncore.AppendArrayElement(dst, "indexes", ci.indexes) 134 } 135 return dst, nil 136 } 137 138 // CommitQuorum specifies the number of data-bearing members of a replica set, including the primary, that must 139 // complete the index builds successfully before the primary marks the indexes as ready. This should either be a 140 // string or int32 value. 141 func (ci *CreateIndexes) CommitQuorum(commitQuorum bsoncore.Value) *CreateIndexes { 142 if ci == nil { 143 ci = new(CreateIndexes) 144 } 145 146 ci.commitQuorum = commitQuorum 147 return ci 148 } 149 150 // Indexes specifies an array containing index specification documents for the indexes being created. 151 func (ci *CreateIndexes) Indexes(indexes bsoncore.Document) *CreateIndexes { 152 if ci == nil { 153 ci = new(CreateIndexes) 154 } 155 156 ci.indexes = indexes 157 return ci 158 } 159 160 // MaxTime specifies the maximum amount of time to allow the query to run on the server. 161 func (ci *CreateIndexes) MaxTime(maxTime *time.Duration) *CreateIndexes { 162 if ci == nil { 163 ci = new(CreateIndexes) 164 } 165 166 ci.maxTime = maxTime 167 return ci 168 } 169 170 // Session sets the session for this operation. 171 func (ci *CreateIndexes) Session(session *session.Client) *CreateIndexes { 172 if ci == nil { 173 ci = new(CreateIndexes) 174 } 175 176 ci.session = session 177 return ci 178 } 179 180 // ClusterClock sets the cluster clock for this operation. 181 func (ci *CreateIndexes) ClusterClock(clock *session.ClusterClock) *CreateIndexes { 182 if ci == nil { 183 ci = new(CreateIndexes) 184 } 185 186 ci.clock = clock 187 return ci 188 } 189 190 // Collection sets the collection that this command will run against. 191 func (ci *CreateIndexes) Collection(collection string) *CreateIndexes { 192 if ci == nil { 193 ci = new(CreateIndexes) 194 } 195 196 ci.collection = collection 197 return ci 198 } 199 200 // CommandMonitor sets the monitor to use for APM events. 201 func (ci *CreateIndexes) CommandMonitor(monitor *event.CommandMonitor) *CreateIndexes { 202 if ci == nil { 203 ci = new(CreateIndexes) 204 } 205 206 ci.monitor = monitor 207 return ci 208 } 209 210 // Crypt sets the Crypt object to use for automatic encryption and decryption. 211 func (ci *CreateIndexes) Crypt(crypt driver.Crypt) *CreateIndexes { 212 if ci == nil { 213 ci = new(CreateIndexes) 214 } 215 216 ci.crypt = crypt 217 return ci 218 } 219 220 // Database sets the database to run this operation against. 221 func (ci *CreateIndexes) Database(database string) *CreateIndexes { 222 if ci == nil { 223 ci = new(CreateIndexes) 224 } 225 226 ci.database = database 227 return ci 228 } 229 230 // Deployment sets the deployment to use for this operation. 231 func (ci *CreateIndexes) Deployment(deployment driver.Deployment) *CreateIndexes { 232 if ci == nil { 233 ci = new(CreateIndexes) 234 } 235 236 ci.deployment = deployment 237 return ci 238 } 239 240 // ServerSelector sets the selector used to retrieve a server. 241 func (ci *CreateIndexes) ServerSelector(selector description.ServerSelector) *CreateIndexes { 242 if ci == nil { 243 ci = new(CreateIndexes) 244 } 245 246 ci.selector = selector 247 return ci 248 } 249 250 // WriteConcern sets the write concern for this operation. 251 func (ci *CreateIndexes) WriteConcern(writeConcern *writeconcern.WriteConcern) *CreateIndexes { 252 if ci == nil { 253 ci = new(CreateIndexes) 254 } 255 256 ci.writeConcern = writeConcern 257 return ci 258 } 259 260 // ServerAPI sets the server API version for this operation. 261 func (ci *CreateIndexes) ServerAPI(serverAPI *driver.ServerAPIOptions) *CreateIndexes { 262 if ci == nil { 263 ci = new(CreateIndexes) 264 } 265 266 ci.serverAPI = serverAPI 267 return ci 268 } 269 270 // Timeout sets the timeout for this operation. 271 func (ci *CreateIndexes) Timeout(timeout *time.Duration) *CreateIndexes { 272 if ci == nil { 273 ci = new(CreateIndexes) 274 } 275 276 ci.timeout = timeout 277 return ci 278 }