...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/operation/list_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  	"time"
    13  
    14  	"go.mongodb.org/mongo-driver/event"
    15  	"go.mongodb.org/mongo-driver/internal/driverutil"
    16  	"go.mongodb.org/mongo-driver/mongo/description"
    17  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    18  	"go.mongodb.org/mongo-driver/x/mongo/driver"
    19  	"go.mongodb.org/mongo-driver/x/mongo/driver/session"
    20  )
    21  
    22  // ListIndexes performs a listIndexes operation.
    23  type ListIndexes struct {
    24  	batchSize  *int32
    25  	maxTime    *time.Duration
    26  	session    *session.Client
    27  	clock      *session.ClusterClock
    28  	collection string
    29  	monitor    *event.CommandMonitor
    30  	database   string
    31  	deployment driver.Deployment
    32  	selector   description.ServerSelector
    33  	retry      *driver.RetryMode
    34  	crypt      driver.Crypt
    35  	serverAPI  *driver.ServerAPIOptions
    36  	timeout    *time.Duration
    37  
    38  	result driver.CursorResponse
    39  }
    40  
    41  // NewListIndexes constructs and returns a new ListIndexes.
    42  func NewListIndexes() *ListIndexes {
    43  	return &ListIndexes{}
    44  }
    45  
    46  // Result returns the result of executing this operation.
    47  func (li *ListIndexes) Result(opts driver.CursorOptions) (*driver.BatchCursor, error) {
    48  
    49  	clientSession := li.session
    50  
    51  	clock := li.clock
    52  	opts.ServerAPI = li.serverAPI
    53  	return driver.NewBatchCursor(li.result, clientSession, clock, opts)
    54  }
    55  
    56  func (li *ListIndexes) processResponse(info driver.ResponseInfo) error {
    57  	var err error
    58  
    59  	li.result, err = driver.NewCursorResponse(info)
    60  	return err
    61  
    62  }
    63  
    64  // Execute runs this operations and returns an error if the operation did not execute successfully.
    65  func (li *ListIndexes) Execute(ctx context.Context) error {
    66  	if li.deployment == nil {
    67  		return errors.New("the ListIndexes operation must have a Deployment set before Execute can be called")
    68  	}
    69  
    70  	return driver.Operation{
    71  		CommandFn:         li.command,
    72  		ProcessResponseFn: li.processResponse,
    73  
    74  		Client:         li.session,
    75  		Clock:          li.clock,
    76  		CommandMonitor: li.monitor,
    77  		Database:       li.database,
    78  		Deployment:     li.deployment,
    79  		MaxTime:        li.maxTime,
    80  		Selector:       li.selector,
    81  		Crypt:          li.crypt,
    82  		Legacy:         driver.LegacyListIndexes,
    83  		RetryMode:      li.retry,
    84  		Type:           driver.Read,
    85  		ServerAPI:      li.serverAPI,
    86  		Timeout:        li.timeout,
    87  		Name:           driverutil.ListIndexesOp,
    88  	}.Execute(ctx)
    89  
    90  }
    91  
    92  func (li *ListIndexes) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
    93  	dst = bsoncore.AppendStringElement(dst, "listIndexes", li.collection)
    94  	cursorIdx, cursorDoc := bsoncore.AppendDocumentStart(nil)
    95  
    96  	if li.batchSize != nil {
    97  
    98  		cursorDoc = bsoncore.AppendInt32Element(cursorDoc, "batchSize", *li.batchSize)
    99  	}
   100  	cursorDoc, _ = bsoncore.AppendDocumentEnd(cursorDoc, cursorIdx)
   101  	dst = bsoncore.AppendDocumentElement(dst, "cursor", cursorDoc)
   102  
   103  	return dst, nil
   104  }
   105  
   106  // BatchSize specifies the number of documents to return in every batch.
   107  func (li *ListIndexes) BatchSize(batchSize int32) *ListIndexes {
   108  	if li == nil {
   109  		li = new(ListIndexes)
   110  	}
   111  
   112  	li.batchSize = &batchSize
   113  	return li
   114  }
   115  
   116  // MaxTime specifies the maximum amount of time to allow the query to run on the server.
   117  func (li *ListIndexes) MaxTime(maxTime *time.Duration) *ListIndexes {
   118  	if li == nil {
   119  		li = new(ListIndexes)
   120  	}
   121  
   122  	li.maxTime = maxTime
   123  	return li
   124  }
   125  
   126  // Session sets the session for this operation.
   127  func (li *ListIndexes) Session(session *session.Client) *ListIndexes {
   128  	if li == nil {
   129  		li = new(ListIndexes)
   130  	}
   131  
   132  	li.session = session
   133  	return li
   134  }
   135  
   136  // ClusterClock sets the cluster clock for this operation.
   137  func (li *ListIndexes) ClusterClock(clock *session.ClusterClock) *ListIndexes {
   138  	if li == nil {
   139  		li = new(ListIndexes)
   140  	}
   141  
   142  	li.clock = clock
   143  	return li
   144  }
   145  
   146  // Collection sets the collection that this command will run against.
   147  func (li *ListIndexes) Collection(collection string) *ListIndexes {
   148  	if li == nil {
   149  		li = new(ListIndexes)
   150  	}
   151  
   152  	li.collection = collection
   153  	return li
   154  }
   155  
   156  // CommandMonitor sets the monitor to use for APM events.
   157  func (li *ListIndexes) CommandMonitor(monitor *event.CommandMonitor) *ListIndexes {
   158  	if li == nil {
   159  		li = new(ListIndexes)
   160  	}
   161  
   162  	li.monitor = monitor
   163  	return li
   164  }
   165  
   166  // Database sets the database to run this operation against.
   167  func (li *ListIndexes) Database(database string) *ListIndexes {
   168  	if li == nil {
   169  		li = new(ListIndexes)
   170  	}
   171  
   172  	li.database = database
   173  	return li
   174  }
   175  
   176  // Deployment sets the deployment to use for this operation.
   177  func (li *ListIndexes) Deployment(deployment driver.Deployment) *ListIndexes {
   178  	if li == nil {
   179  		li = new(ListIndexes)
   180  	}
   181  
   182  	li.deployment = deployment
   183  	return li
   184  }
   185  
   186  // ServerSelector sets the selector used to retrieve a server.
   187  func (li *ListIndexes) ServerSelector(selector description.ServerSelector) *ListIndexes {
   188  	if li == nil {
   189  		li = new(ListIndexes)
   190  	}
   191  
   192  	li.selector = selector
   193  	return li
   194  }
   195  
   196  // Retry enables retryable mode for this operation. Retries are handled automatically in driver.Operation.Execute based
   197  // on how the operation is set.
   198  func (li *ListIndexes) Retry(retry driver.RetryMode) *ListIndexes {
   199  	if li == nil {
   200  		li = new(ListIndexes)
   201  	}
   202  
   203  	li.retry = &retry
   204  	return li
   205  }
   206  
   207  // Crypt sets the Crypt object to use for automatic encryption and decryption.
   208  func (li *ListIndexes) Crypt(crypt driver.Crypt) *ListIndexes {
   209  	if li == nil {
   210  		li = new(ListIndexes)
   211  	}
   212  
   213  	li.crypt = crypt
   214  	return li
   215  }
   216  
   217  // ServerAPI sets the server API version for this operation.
   218  func (li *ListIndexes) ServerAPI(serverAPI *driver.ServerAPIOptions) *ListIndexes {
   219  	if li == nil {
   220  		li = new(ListIndexes)
   221  	}
   222  
   223  	li.serverAPI = serverAPI
   224  	return li
   225  }
   226  
   227  // Timeout sets the timeout for this operation.
   228  func (li *ListIndexes) Timeout(timeout *time.Duration) *ListIndexes {
   229  	if li == nil {
   230  		li = new(ListIndexes)
   231  	}
   232  
   233  	li.timeout = timeout
   234  	return li
   235  }
   236  

View as plain text