|
| 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 | + "github.com/mongodb/mongo-go-driver/bson" |
| 13 | + "github.com/mongodb/mongo-go-driver/core/description" |
| 14 | + "github.com/mongodb/mongo-go-driver/core/option" |
| 15 | + "github.com/mongodb/mongo-go-driver/core/readconcern" |
| 16 | + "github.com/mongodb/mongo-go-driver/core/readpref" |
| 17 | + "github.com/mongodb/mongo-go-driver/core/session" |
| 18 | + "github.com/mongodb/mongo-go-driver/core/wiremessage" |
| 19 | +) |
| 20 | + |
| 21 | +// CountDocuments represents the CountDocuments command. |
| 22 | +// |
| 23 | +// The countDocuments command counts how many documents in a collection match the given query. |
| 24 | +type CountDocuments struct { |
| 25 | + NS Namespace |
| 26 | + Pipeline *bson.Array |
| 27 | + Opts []option.CountOptioner |
| 28 | + ReadPref *readpref.ReadPref |
| 29 | + ReadConcern *readconcern.ReadConcern |
| 30 | + Clock *session.ClusterClock |
| 31 | + Session *session.Client |
| 32 | + |
| 33 | + result int64 |
| 34 | + err error |
| 35 | +} |
| 36 | + |
| 37 | +// Encode will encode this command into a wire message for the given server description. |
| 38 | +func (c *CountDocuments) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 39 | + if err := c.NS.Validate(); err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + command := bson.NewDocument() |
| 43 | + command.Append(bson.EC.String("aggregate", c.NS.Collection), bson.EC.Array("pipeline", c.Pipeline)) |
| 44 | + |
| 45 | + cursor := bson.NewDocument() |
| 46 | + command.Append(bson.EC.SubDocument("cursor", cursor)) |
| 47 | + for _, opt := range c.Opts { |
| 48 | + if opt == nil { |
| 49 | + continue |
| 50 | + } |
| 51 | + //because we already have these options in the pipeline |
| 52 | + switch opt.(type) { |
| 53 | + case option.OptSkip: |
| 54 | + continue |
| 55 | + case option.OptLimit: |
| 56 | + continue |
| 57 | + } |
| 58 | + err := opt.Option(command) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return (&Read{DB: c.NS.DB, ReadPref: c.ReadPref, Command: command}).Encode(desc) |
| 65 | +} |
| 66 | + |
| 67 | +// Decode will decode the wire message using the provided server description. Errors during decoding |
| 68 | +// are deferred until either the Result or Err methods are called. |
| 69 | +func (c *CountDocuments) Decode(ctx context.Context, desc description.SelectedServer, cb CursorBuilder, wm wiremessage.WireMessage) *CountDocuments { |
| 70 | + rdr, err := (&Read{}).Decode(desc, wm).Result() |
| 71 | + if err != nil { |
| 72 | + c.err = err |
| 73 | + return c |
| 74 | + } |
| 75 | + cur, err := cb.BuildCursor(rdr, c.Session, c.Clock) |
| 76 | + if err != nil { |
| 77 | + c.err = err |
| 78 | + return c |
| 79 | + } |
| 80 | + |
| 81 | + var doc = bson.NewDocument() |
| 82 | + if cur.Next(ctx) { |
| 83 | + err = cur.Decode(doc) |
| 84 | + if err != nil { |
| 85 | + c.err = err |
| 86 | + return c |
| 87 | + } |
| 88 | + val, err := doc.LookupErr("n") |
| 89 | + switch { |
| 90 | + case err == bson.ErrElementNotFound: |
| 91 | + c.err = errors.New("Invalid response from server, no 'n' field") |
| 92 | + return c |
| 93 | + case err != nil: |
| 94 | + c.err = err |
| 95 | + return c |
| 96 | + } |
| 97 | + switch val.Type() { |
| 98 | + case bson.TypeInt32: |
| 99 | + c.result = int64(val.Int32()) |
| 100 | + case bson.TypeInt64: |
| 101 | + c.result = val.Int64() |
| 102 | + default: |
| 103 | + c.err = errors.New("Invalid response from server, value field is not a number") |
| 104 | + } |
| 105 | + |
| 106 | + return c |
| 107 | + } |
| 108 | + |
| 109 | + c.result = 0 |
| 110 | + return c |
| 111 | +} |
| 112 | + |
| 113 | +// Result returns the result of a decoded wire message and server description. |
| 114 | +func (c *CountDocuments) Result() (int64, error) { |
| 115 | + if c.err != nil { |
| 116 | + return 0, c.err |
| 117 | + } |
| 118 | + return c.result, nil |
| 119 | +} |
| 120 | + |
| 121 | +// Err returns the error set on this command. |
| 122 | +func (c *CountDocuments) Err() error { return c.err } |
| 123 | + |
| 124 | +// RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 125 | +func (c *CountDocuments) RoundTrip(ctx context.Context, desc description.SelectedServer, cb CursorBuilder, rw wiremessage.ReadWriter) (int64, error) { |
| 126 | + wm, err := c.Encode(desc) |
| 127 | + if err != nil { |
| 128 | + return 0, err |
| 129 | + } |
| 130 | + |
| 131 | + err = rw.WriteWireMessage(ctx, wm) |
| 132 | + if err != nil { |
| 133 | + return 0, err |
| 134 | + } |
| 135 | + wm, err = rw.ReadWireMessage(ctx) |
| 136 | + if err != nil { |
| 137 | + return 0, err |
| 138 | + } |
| 139 | + return c.Decode(ctx, desc, cb, wm).Result() |
| 140 | +} |
0 commit comments