Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // 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 | |
| 7 | package command |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | "github.com/mongodb/mongo-go-driver/mongo/writeconcern" |
| 14 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 15 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 16 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 18 | ) |
| 19 | |
| 20 | // DropIndexes represents the dropIndexes command. |
| 21 | // |
| 22 | // The dropIndexes command drops indexes for a namespace. |
| 23 | type DropIndexes struct { |
| 24 | NS Namespace |
| 25 | Index string |
| 26 | Opts []bsonx.Elem |
| 27 | WriteConcern *writeconcern.WriteConcern |
| 28 | Clock *session.ClusterClock |
| 29 | Session *session.Client |
| 30 | |
| 31 | result bson.Raw |
| 32 | err error |
| 33 | } |
| 34 | |
| 35 | // Encode will encode this command into a wire message for the given server description. |
| 36 | func (di *DropIndexes) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 37 | cmd, err := di.encode(desc) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | return cmd.Encode(desc) |
| 43 | } |
| 44 | |
| 45 | func (di *DropIndexes) encode(desc description.SelectedServer) (*Write, error) { |
| 46 | cmd := bsonx.Doc{ |
| 47 | {"dropIndexes", bsonx.String(di.NS.Collection)}, |
| 48 | {"index", bsonx.String(di.Index)}, |
| 49 | } |
| 50 | cmd = append(cmd, di.Opts...) |
| 51 | |
| 52 | write := &Write{ |
| 53 | Clock: di.Clock, |
| 54 | DB: di.NS.DB, |
| 55 | Command: cmd, |
| 56 | Session: di.Session, |
| 57 | } |
| 58 | if desc.WireVersion != nil && desc.WireVersion.Max >= 5 { |
| 59 | write.WriteConcern = di.WriteConcern |
| 60 | } |
| 61 | return write, nil |
| 62 | } |
| 63 | |
| 64 | // Decode will decode the wire message using the provided server description. Errors during decoding |
| 65 | // are deferred until either the Result or Err methods are called. |
| 66 | func (di *DropIndexes) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *DropIndexes { |
| 67 | rdr, err := (&Write{}).Decode(desc, wm).Result() |
| 68 | if err != nil { |
| 69 | di.err = err |
| 70 | return di |
| 71 | } |
| 72 | |
| 73 | return di.decode(desc, rdr) |
| 74 | } |
| 75 | |
| 76 | func (di *DropIndexes) decode(desc description.SelectedServer, rdr bson.Raw) *DropIndexes { |
| 77 | di.result = rdr |
| 78 | return di |
| 79 | } |
| 80 | |
| 81 | // Result returns the result of a decoded wire message and server description. |
| 82 | func (di *DropIndexes) Result() (bson.Raw, error) { |
| 83 | if di.err != nil { |
| 84 | return nil, di.err |
| 85 | } |
| 86 | |
| 87 | return di.result, nil |
| 88 | } |
| 89 | |
| 90 | // Err returns the error set on this command. |
| 91 | func (di *DropIndexes) Err() error { return di.err } |
| 92 | |
| 93 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 94 | func (di *DropIndexes) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (bson.Raw, error) { |
| 95 | cmd, err := di.encode(desc) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | |
| 100 | di.result, err = cmd.RoundTrip(ctx, desc, rw) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | return di.Result() |
| 106 | } |