...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/operation/create_search_indexes.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/operation

     1  // Copyright (C) MongoDB, Inc. 2023-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"
    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  // CreateSearchIndexes performs a createSearchIndexes operation.
    25  type CreateSearchIndexes struct {
    26  	indexes      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       CreateSearchIndexesResult
    37  	serverAPI    *driver.ServerAPIOptions
    38  	timeout      *time.Duration
    39  }
    40  
    41  // CreateSearchIndexResult represents a single search index result in CreateSearchIndexesResult.
    42  type CreateSearchIndexResult struct {
    43  	Name string
    44  }
    45  
    46  // CreateSearchIndexesResult represents a createSearchIndexes result returned by the server.
    47  type CreateSearchIndexesResult struct {
    48  	IndexesCreated []CreateSearchIndexResult
    49  }
    50  
    51  func buildCreateSearchIndexesResult(response bsoncore.Document) (CreateSearchIndexesResult, error) {
    52  	elements, err := response.Elements()
    53  	if err != nil {
    54  		return CreateSearchIndexesResult{}, err
    55  	}
    56  	csir := CreateSearchIndexesResult{}
    57  	for _, element := range elements {
    58  		switch element.Key() {
    59  		case "indexesCreated":
    60  			arr, ok := element.Value().ArrayOK()
    61  			if !ok {
    62  				return csir, fmt.Errorf("response field 'indexesCreated' is type array, but received BSON type %s", element.Value().Type)
    63  			}
    64  
    65  			var values []bsoncore.Value
    66  			values, err = arr.Values()
    67  			if err != nil {
    68  				break
    69  			}
    70  
    71  			for _, val := range values {
    72  				valDoc, ok := val.DocumentOK()
    73  				if !ok {
    74  					return csir, fmt.Errorf("indexesCreated value is type document, but received BSON type %s", val.Type)
    75  				}
    76  				var indexesCreated CreateSearchIndexResult
    77  				if err = bson.Unmarshal(valDoc, &indexesCreated); err != nil {
    78  					return csir, err
    79  				}
    80  				csir.IndexesCreated = append(csir.IndexesCreated, indexesCreated)
    81  			}
    82  		}
    83  	}
    84  	return csir, nil
    85  }
    86  
    87  // NewCreateSearchIndexes constructs and returns a new CreateSearchIndexes.
    88  func NewCreateSearchIndexes(indexes bsoncore.Document) *CreateSearchIndexes {
    89  	return &CreateSearchIndexes{
    90  		indexes: indexes,
    91  	}
    92  }
    93  
    94  // Result returns the result of executing this operation.
    95  func (csi *CreateSearchIndexes) Result() CreateSearchIndexesResult { return csi.result }
    96  
    97  func (csi *CreateSearchIndexes) processResponse(info driver.ResponseInfo) error {
    98  	var err error
    99  	csi.result, err = buildCreateSearchIndexesResult(info.ServerResponse)
   100  	return err
   101  }
   102  
   103  // Execute runs this operations and returns an error if the operation did not execute successfully.
   104  func (csi *CreateSearchIndexes) Execute(ctx context.Context) error {
   105  	if csi.deployment == nil {
   106  		return errors.New("the CreateSearchIndexes operation must have a Deployment set before Execute can be called")
   107  	}
   108  
   109  	return driver.Operation{
   110  		CommandFn:         csi.command,
   111  		ProcessResponseFn: csi.processResponse,
   112  		CommandMonitor:    csi.monitor,
   113  		Database:          csi.database,
   114  		Deployment:        csi.deployment,
   115  	}.Execute(ctx)
   116  
   117  }
   118  
   119  func (csi *CreateSearchIndexes) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
   120  	dst = bsoncore.AppendStringElement(dst, "createSearchIndexes", csi.collection)
   121  	if csi.indexes != nil {
   122  		dst = bsoncore.AppendArrayElement(dst, "indexes", csi.indexes)
   123  	}
   124  	return dst, nil
   125  }
   126  
   127  // Indexes specifies an array containing index specification documents for the indexes being created.
   128  func (csi *CreateSearchIndexes) Indexes(indexes bsoncore.Document) *CreateSearchIndexes {
   129  	if csi == nil {
   130  		csi = new(CreateSearchIndexes)
   131  	}
   132  
   133  	csi.indexes = indexes
   134  	return csi
   135  }
   136  
   137  // Session sets the session for this operation.
   138  func (csi *CreateSearchIndexes) Session(session *session.Client) *CreateSearchIndexes {
   139  	if csi == nil {
   140  		csi = new(CreateSearchIndexes)
   141  	}
   142  
   143  	csi.session = session
   144  	return csi
   145  }
   146  
   147  // ClusterClock sets the cluster clock for this operation.
   148  func (csi *CreateSearchIndexes) ClusterClock(clock *session.ClusterClock) *CreateSearchIndexes {
   149  	if csi == nil {
   150  		csi = new(CreateSearchIndexes)
   151  	}
   152  
   153  	csi.clock = clock
   154  	return csi
   155  }
   156  
   157  // Collection sets the collection that this command will run against.
   158  func (csi *CreateSearchIndexes) Collection(collection string) *CreateSearchIndexes {
   159  	if csi == nil {
   160  		csi = new(CreateSearchIndexes)
   161  	}
   162  
   163  	csi.collection = collection
   164  	return csi
   165  }
   166  
   167  // CommandMonitor sets the monitor to use for APM events.
   168  func (csi *CreateSearchIndexes) CommandMonitor(monitor *event.CommandMonitor) *CreateSearchIndexes {
   169  	if csi == nil {
   170  		csi = new(CreateSearchIndexes)
   171  	}
   172  
   173  	csi.monitor = monitor
   174  	return csi
   175  }
   176  
   177  // Crypt sets the Crypt object to use for automatic encryption and decryption.
   178  func (csi *CreateSearchIndexes) Crypt(crypt driver.Crypt) *CreateSearchIndexes {
   179  	if csi == nil {
   180  		csi = new(CreateSearchIndexes)
   181  	}
   182  
   183  	csi.crypt = crypt
   184  	return csi
   185  }
   186  
   187  // Database sets the database to run this operation against.
   188  func (csi *CreateSearchIndexes) Database(database string) *CreateSearchIndexes {
   189  	if csi == nil {
   190  		csi = new(CreateSearchIndexes)
   191  	}
   192  
   193  	csi.database = database
   194  	return csi
   195  }
   196  
   197  // Deployment sets the deployment to use for this operation.
   198  func (csi *CreateSearchIndexes) Deployment(deployment driver.Deployment) *CreateSearchIndexes {
   199  	if csi == nil {
   200  		csi = new(CreateSearchIndexes)
   201  	}
   202  
   203  	csi.deployment = deployment
   204  	return csi
   205  }
   206  
   207  // ServerSelector sets the selector used to retrieve a server.
   208  func (csi *CreateSearchIndexes) ServerSelector(selector description.ServerSelector) *CreateSearchIndexes {
   209  	if csi == nil {
   210  		csi = new(CreateSearchIndexes)
   211  	}
   212  
   213  	csi.selector = selector
   214  	return csi
   215  }
   216  
   217  // WriteConcern sets the write concern for this operation.
   218  func (csi *CreateSearchIndexes) WriteConcern(writeConcern *writeconcern.WriteConcern) *CreateSearchIndexes {
   219  	if csi == nil {
   220  		csi = new(CreateSearchIndexes)
   221  	}
   222  
   223  	csi.writeConcern = writeConcern
   224  	return csi
   225  }
   226  
   227  // ServerAPI sets the server API version for this operation.
   228  func (csi *CreateSearchIndexes) ServerAPI(serverAPI *driver.ServerAPIOptions) *CreateSearchIndexes {
   229  	if csi == nil {
   230  		csi = new(CreateSearchIndexes)
   231  	}
   232  
   233  	csi.serverAPI = serverAPI
   234  	return csi
   235  }
   236  
   237  // Timeout sets the timeout for this operation.
   238  func (csi *CreateSearchIndexes) Timeout(timeout *time.Duration) *CreateSearchIndexes {
   239  	if csi == nil {
   240  		csi = new(CreateSearchIndexes)
   241  	}
   242  
   243  	csi.timeout = timeout
   244  	return csi
   245  }
   246  

View as plain text