...

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

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

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

View as plain text