...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/operation/drop_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  // DropSearchIndex performs an dropSearchIndex operation.
    24  type DropSearchIndex struct {
    25  	index        string
    26  	session      *session.Client
    27  	clock        *session.ClusterClock
    28  	collection   string
    29  	monitor      *event.CommandMonitor
    30  	crypt        driver.Crypt
    31  	database     string
    32  	deployment   driver.Deployment
    33  	selector     description.ServerSelector
    34  	writeConcern *writeconcern.WriteConcern
    35  	result       DropSearchIndexResult
    36  	serverAPI    *driver.ServerAPIOptions
    37  	timeout      *time.Duration
    38  }
    39  
    40  // DropSearchIndexResult represents a dropSearchIndex result returned by the server.
    41  type DropSearchIndexResult struct {
    42  	Ok int32
    43  }
    44  
    45  func buildDropSearchIndexResult(response bsoncore.Document) (DropSearchIndexResult, error) {
    46  	elements, err := response.Elements()
    47  	if err != nil {
    48  		return DropSearchIndexResult{}, err
    49  	}
    50  	dsir := DropSearchIndexResult{}
    51  	for _, element := range elements {
    52  		switch element.Key() {
    53  		case "ok":
    54  			var ok bool
    55  			dsir.Ok, ok = element.Value().AsInt32OK()
    56  			if !ok {
    57  				return dsir, fmt.Errorf("response field 'ok' is type int32, but received BSON type %s", element.Value().Type)
    58  			}
    59  		}
    60  	}
    61  	return dsir, nil
    62  }
    63  
    64  // NewDropSearchIndex constructs and returns a new DropSearchIndex.
    65  func NewDropSearchIndex(index string) *DropSearchIndex {
    66  	return &DropSearchIndex{
    67  		index: index,
    68  	}
    69  }
    70  
    71  // Result returns the result of executing this operation.
    72  func (dsi *DropSearchIndex) Result() DropSearchIndexResult { return dsi.result }
    73  
    74  func (dsi *DropSearchIndex) processResponse(info driver.ResponseInfo) error {
    75  	var err error
    76  	dsi.result, err = buildDropSearchIndexResult(info.ServerResponse)
    77  	return err
    78  }
    79  
    80  // Execute runs this operations and returns an error if the operation did not execute successfully.
    81  func (dsi *DropSearchIndex) Execute(ctx context.Context) error {
    82  	if dsi.deployment == nil {
    83  		return errors.New("the DropSearchIndex operation must have a Deployment set before Execute can be called")
    84  	}
    85  
    86  	return driver.Operation{
    87  		CommandFn:         dsi.command,
    88  		ProcessResponseFn: dsi.processResponse,
    89  		Client:            dsi.session,
    90  		Clock:             dsi.clock,
    91  		CommandMonitor:    dsi.monitor,
    92  		Crypt:             dsi.crypt,
    93  		Database:          dsi.database,
    94  		Deployment:        dsi.deployment,
    95  		Selector:          dsi.selector,
    96  		WriteConcern:      dsi.writeConcern,
    97  		ServerAPI:         dsi.serverAPI,
    98  		Timeout:           dsi.timeout,
    99  	}.Execute(ctx)
   100  
   101  }
   102  
   103  func (dsi *DropSearchIndex) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
   104  	dst = bsoncore.AppendStringElement(dst, "dropSearchIndex", dsi.collection)
   105  	dst = bsoncore.AppendStringElement(dst, "name", dsi.index)
   106  	return dst, nil
   107  }
   108  
   109  // Index specifies the name of the index to drop. If '*' is specified, all indexes will be dropped.
   110  func (dsi *DropSearchIndex) Index(index string) *DropSearchIndex {
   111  	if dsi == nil {
   112  		dsi = new(DropSearchIndex)
   113  	}
   114  
   115  	dsi.index = index
   116  	return dsi
   117  }
   118  
   119  // Session sets the session for this operation.
   120  func (dsi *DropSearchIndex) Session(session *session.Client) *DropSearchIndex {
   121  	if dsi == nil {
   122  		dsi = new(DropSearchIndex)
   123  	}
   124  
   125  	dsi.session = session
   126  	return dsi
   127  }
   128  
   129  // ClusterClock sets the cluster clock for this operation.
   130  func (dsi *DropSearchIndex) ClusterClock(clock *session.ClusterClock) *DropSearchIndex {
   131  	if dsi == nil {
   132  		dsi = new(DropSearchIndex)
   133  	}
   134  
   135  	dsi.clock = clock
   136  	return dsi
   137  }
   138  
   139  // Collection sets the collection that this command will run against.
   140  func (dsi *DropSearchIndex) Collection(collection string) *DropSearchIndex {
   141  	if dsi == nil {
   142  		dsi = new(DropSearchIndex)
   143  	}
   144  
   145  	dsi.collection = collection
   146  	return dsi
   147  }
   148  
   149  // CommandMonitor sets the monitor to use for APM events.
   150  func (dsi *DropSearchIndex) CommandMonitor(monitor *event.CommandMonitor) *DropSearchIndex {
   151  	if dsi == nil {
   152  		dsi = new(DropSearchIndex)
   153  	}
   154  
   155  	dsi.monitor = monitor
   156  	return dsi
   157  }
   158  
   159  // Crypt sets the Crypt object to use for automatic encryption and decryption.
   160  func (dsi *DropSearchIndex) Crypt(crypt driver.Crypt) *DropSearchIndex {
   161  	if dsi == nil {
   162  		dsi = new(DropSearchIndex)
   163  	}
   164  
   165  	dsi.crypt = crypt
   166  	return dsi
   167  }
   168  
   169  // Database sets the database to run this operation against.
   170  func (dsi *DropSearchIndex) Database(database string) *DropSearchIndex {
   171  	if dsi == nil {
   172  		dsi = new(DropSearchIndex)
   173  	}
   174  
   175  	dsi.database = database
   176  	return dsi
   177  }
   178  
   179  // Deployment sets the deployment to use for this operation.
   180  func (dsi *DropSearchIndex) Deployment(deployment driver.Deployment) *DropSearchIndex {
   181  	if dsi == nil {
   182  		dsi = new(DropSearchIndex)
   183  	}
   184  
   185  	dsi.deployment = deployment
   186  	return dsi
   187  }
   188  
   189  // ServerSelector sets the selector used to retrieve a server.
   190  func (dsi *DropSearchIndex) ServerSelector(selector description.ServerSelector) *DropSearchIndex {
   191  	if dsi == nil {
   192  		dsi = new(DropSearchIndex)
   193  	}
   194  
   195  	dsi.selector = selector
   196  	return dsi
   197  }
   198  
   199  // WriteConcern sets the write concern for this operation.
   200  func (dsi *DropSearchIndex) WriteConcern(writeConcern *writeconcern.WriteConcern) *DropSearchIndex {
   201  	if dsi == nil {
   202  		dsi = new(DropSearchIndex)
   203  	}
   204  
   205  	dsi.writeConcern = writeConcern
   206  	return dsi
   207  }
   208  
   209  // ServerAPI sets the server API version for this operation.
   210  func (dsi *DropSearchIndex) ServerAPI(serverAPI *driver.ServerAPIOptions) *DropSearchIndex {
   211  	if dsi == nil {
   212  		dsi = new(DropSearchIndex)
   213  	}
   214  
   215  	dsi.serverAPI = serverAPI
   216  	return dsi
   217  }
   218  
   219  // Timeout sets the timeout for this operation.
   220  func (dsi *DropSearchIndex) Timeout(timeout *time.Duration) *DropSearchIndex {
   221  	if dsi == nil {
   222  		dsi = new(DropSearchIndex)
   223  	}
   224  
   225  	dsi.timeout = timeout
   226  	return dsi
   227  }
   228  

View as plain text