blob: 669c797a32296942434902bad9a22e674394761b [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-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
7package driver
8
9import (
10 "context"
11
12 "time"
13
14 "github.com/mongodb/mongo-go-driver/bson"
15 "github.com/mongodb/mongo-go-driver/mongo/options"
16 "github.com/mongodb/mongo-go-driver/x/bsonx"
17 "github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
18 "github.com/mongodb/mongo-go-driver/x/mongo/driver/topology"
19 "github.com/mongodb/mongo-go-driver/x/mongo/driver/uuid"
20 "github.com/mongodb/mongo-go-driver/x/network/command"
21 "github.com/mongodb/mongo-go-driver/x/network/description"
22)
23
24// DropIndexes handles the full cycle dispatch and execution of a dropIndexes
25// command against the provided topology.
26func DropIndexes(
27 ctx context.Context,
28 cmd command.DropIndexes,
29 topo *topology.Topology,
30 selector description.ServerSelector,
31 clientID uuid.UUID,
32 pool *session.Pool,
33 opts ...*options.DropIndexesOptions,
34) (bson.Raw, error) {
35
36 ss, err := topo.SelectServer(ctx, selector)
37 if err != nil {
38 return nil, err
39 }
40
41 conn, err := ss.Connection(ctx)
42 if err != nil {
43 return nil, err
44 }
45 defer conn.Close()
46
47 dio := options.MergeDropIndexesOptions(opts...)
48 if dio.MaxTime != nil {
49 cmd.Opts = append(cmd.Opts, bsonx.Elem{"maxTimeMS", bsonx.Int64(int64(*dio.MaxTime / time.Millisecond))})
50 }
51
52 // If no explicit session and deployment supports sessions, start implicit session.
53 if cmd.Session == nil && topo.SupportsSessions() {
54 cmd.Session, err = session.NewClientSession(pool, clientID, session.Implicit)
55 if err != nil {
56 return nil, err
57 }
58 defer cmd.Session.EndSession()
59 }
60
61 return cmd.RoundTrip(ctx, ss.Description(), conn)
62}