...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/operation/update_search_index.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/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  // UpdateSearchIndex performs a updateSearchIndex operation.
    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  // UpdateSearchIndexResult represents a single index in the updateSearchIndexResult result.
    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  // NewUpdateSearchIndex constructs and returns a new UpdateSearchIndex.
    66  func NewUpdateSearchIndex(index string, definition bsoncore.Document) *UpdateSearchIndex {
    67  	return &UpdateSearchIndex{
    68  		index:      index,
    69  		definition: definition,
    70  	}
    71  }
    72  
    73  // Result returns the result of executing this operation.
    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  // Execute runs this operations and returns an error if the operation did not execute successfully.
    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  // Index specifies the index of the document being updated.
   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  // Definition specifies the definition for the document being created.
   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  // Session sets the session for this operation.
   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  // ClusterClock sets the cluster clock for this operation.
   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  // Collection sets the collection that this command will run against.
   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  // CommandMonitor sets the monitor to use for APM events.
   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  // Crypt sets the Crypt object to use for automatic encryption and decryption.
   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  // Database sets the database to run this operation against.
   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  // Deployment sets the deployment to use for this operation.
   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  // ServerSelector sets the selector used to retrieve a server.
   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  // WriteConcern sets the write concern for this operation.
   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  // ServerAPI sets the server API version for this operation.
   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  // Timeout sets the timeout for this operation.
   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