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/readconcern" |
| 14 | "github.com/mongodb/mongo-go-driver/mongo/readpref" |
| 15 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 16 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 18 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 19 | ) |
| 20 | |
| 21 | // Find represents the find command. |
| 22 | // |
| 23 | // The find command finds documents within a collection that match a filter. |
| 24 | type Find struct { |
| 25 | NS Namespace |
| 26 | Filter bsonx.Doc |
| 27 | CursorOpts []bsonx.Elem |
| 28 | Opts []bsonx.Elem |
| 29 | ReadPref *readpref.ReadPref |
| 30 | ReadConcern *readconcern.ReadConcern |
| 31 | Clock *session.ClusterClock |
| 32 | Session *session.Client |
| 33 | |
| 34 | result bson.Raw |
| 35 | err error |
| 36 | } |
| 37 | |
| 38 | // Encode will encode this command into a wire message for the given server description. |
| 39 | func (f *Find) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 40 | cmd, err := f.encode(desc) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | |
| 45 | return cmd.Encode(desc) |
| 46 | } |
| 47 | |
| 48 | func (f *Find) encode(desc description.SelectedServer) (*Read, error) { |
| 49 | if err := f.NS.Validate(); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | command := bsonx.Doc{{"find", bsonx.String(f.NS.Collection)}} |
| 54 | |
| 55 | if f.Filter != nil { |
| 56 | command = append(command, bsonx.Elem{"filter", bsonx.Document(f.Filter)}) |
| 57 | } |
| 58 | |
| 59 | command = append(command, f.Opts...) |
| 60 | |
| 61 | return &Read{ |
| 62 | Clock: f.Clock, |
| 63 | DB: f.NS.DB, |
| 64 | ReadPref: f.ReadPref, |
| 65 | Command: command, |
| 66 | ReadConcern: f.ReadConcern, |
| 67 | Session: f.Session, |
| 68 | }, nil |
| 69 | } |
| 70 | |
| 71 | // Decode will decode the wire message using the provided server description. Errors during decoding |
| 72 | // are deferred until either the Result or Err methods are called. |
| 73 | func (f *Find) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *Find { |
| 74 | rdr, err := (&Read{}).Decode(desc, wm).Result() |
| 75 | if err != nil { |
| 76 | f.err = err |
| 77 | return f |
| 78 | } |
| 79 | |
| 80 | return f.decode(desc, rdr) |
| 81 | } |
| 82 | |
| 83 | func (f *Find) decode(desc description.SelectedServer, rdr bson.Raw) *Find { |
| 84 | f.result = rdr |
| 85 | return f |
| 86 | } |
| 87 | |
| 88 | // Result returns the result of a decoded wire message and server description. |
| 89 | func (f *Find) Result() (bson.Raw, error) { |
| 90 | if f.err != nil { |
| 91 | return nil, f.err |
| 92 | } |
| 93 | |
| 94 | return f.result, nil |
| 95 | } |
| 96 | |
| 97 | // Err returns the error set on this command. |
| 98 | func (f *Find) Err() error { return f.err } |
| 99 | |
| 100 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 101 | func (f *Find) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (bson.Raw, error) { |
| 102 | cmd, err := f.encode(desc) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | rdr, err := cmd.RoundTrip(ctx, desc, rw) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | return f.decode(desc, rdr).Result() |
| 113 | } |