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 | "errors" |
| 12 | |
| 13 | "github.com/mongodb/mongo-go-driver/bson" |
| 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 | // ErrEmptyCursor is a signaling error when a cursor for list indexes is empty. |
| 21 | var ErrEmptyCursor = errors.New("empty cursor") |
| 22 | |
| 23 | // ListIndexes represents the listIndexes command. |
| 24 | // |
| 25 | // The listIndexes command lists the indexes for a namespace. |
| 26 | type ListIndexes struct { |
| 27 | Clock *session.ClusterClock |
| 28 | NS Namespace |
| 29 | CursorOpts []bsonx.Elem |
| 30 | Opts []bsonx.Elem |
| 31 | Session *session.Client |
| 32 | |
| 33 | result bson.Raw |
| 34 | err error |
| 35 | } |
| 36 | |
| 37 | // Encode will encode this command into a wire message for the given server description. |
| 38 | func (li *ListIndexes) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 39 | encoded, err := li.encode(desc) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | return encoded.Encode(desc) |
| 44 | } |
| 45 | |
| 46 | func (li *ListIndexes) encode(desc description.SelectedServer) (*Read, error) { |
| 47 | cmd := bsonx.Doc{{"listIndexes", bsonx.String(li.NS.Collection)}} |
| 48 | cmd = append(cmd, li.Opts...) |
| 49 | |
| 50 | return &Read{ |
| 51 | Clock: li.Clock, |
| 52 | DB: li.NS.DB, |
| 53 | Command: cmd, |
| 54 | Session: li.Session, |
| 55 | }, nil |
| 56 | } |
| 57 | |
| 58 | // Decode will decode the wire message using the provided server description. Errors during decoling |
| 59 | // are deferred until either the Result or Err methods are called. |
| 60 | func (li *ListIndexes) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *ListIndexes { |
| 61 | rdr, err := (&Read{}).Decode(desc, wm).Result() |
| 62 | if err != nil { |
| 63 | if IsNotFound(err) { |
| 64 | li.err = ErrEmptyCursor |
| 65 | return li |
| 66 | } |
| 67 | li.err = err |
| 68 | return li |
| 69 | } |
| 70 | |
| 71 | return li.decode(desc, rdr) |
| 72 | } |
| 73 | |
| 74 | func (li *ListIndexes) decode(desc description.SelectedServer, rdr bson.Raw) *ListIndexes { |
| 75 | li.result = rdr |
| 76 | return li |
| 77 | } |
| 78 | |
| 79 | // Result returns the result of a decoded wire message and server description. |
| 80 | func (li *ListIndexes) Result() (bson.Raw, error) { |
| 81 | if li.err != nil { |
| 82 | return nil, li.err |
| 83 | } |
| 84 | return li.result, nil |
| 85 | } |
| 86 | |
| 87 | // Err returns the error set on this command. |
| 88 | func (li *ListIndexes) Err() error { return li.err } |
| 89 | |
| 90 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 91 | func (li *ListIndexes) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (bson.Raw, error) { |
| 92 | cmd, err := li.encode(desc) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | rdr, err := cmd.RoundTrip(ctx, desc, rw) |
| 98 | if err != nil { |
| 99 | if IsNotFound(err) { |
| 100 | return nil, ErrEmptyCursor |
| 101 | } |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | return li.decode(desc, rdr).Result() |
| 106 | } |