forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
142 lines (121 loc) · 3.51 KB
/
aggregate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package command
import (
"context"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/description"
"github.com/mongodb/mongo-go-driver/core/option"
"github.com/mongodb/mongo-go-driver/core/readpref"
"github.com/mongodb/mongo-go-driver/core/wiremessage"
)
// Aggregate represents the aggregate command.
//
// The aggregate command performs an aggregation.
type Aggregate struct {
NS Namespace
Pipeline *bson.Array
Opts []option.AggregateOptioner
ReadPref *readpref.ReadPref
result Cursor
err error
}
// Encode will encode this command into a wire message for the given server description.
func (a *Aggregate) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) {
if err := a.NS.Validate(); err != nil {
return nil, err
}
command := bson.NewDocument()
command.Append(bson.EC.String("aggregate", a.NS.Collection), bson.EC.Array("pipeline", a.Pipeline))
cursor := bson.NewDocument()
command.Append(bson.EC.SubDocument("cursor", cursor))
for _, opt := range a.Opts {
switch t := opt.(type) {
case nil:
continue
case option.OptBatchSize:
if t == 0 && a.HasDollarOut() {
continue
}
err := opt.Option(cursor)
if err != nil {
return nil, err
}
default:
err := opt.Option(command)
if err != nil {
return nil, err
}
}
}
return (&Command{DB: a.NS.DB, Command: command, ReadPref: a.ReadPref}).Encode(desc)
}
// HasDollarOut returns true if the Pipeline field contains a $out stage.
func (a *Aggregate) HasDollarOut() bool {
if a.Pipeline == nil {
return false
}
if a.Pipeline.Len() == 0 {
return false
}
val, err := a.Pipeline.Lookup(uint(a.Pipeline.Len() - 1))
if err != nil {
return false
}
doc, ok := val.MutableDocumentOK()
if !ok || doc.Len() != 1 {
return false
}
elem, ok := doc.ElementAtOK(0)
if !ok {
return false
}
return elem.Key() == "$out"
}
// Decode will decode the wire message using the provided server description. Errors during decoding
// are deferred until either the Result or Err methods are called.
func (a *Aggregate) Decode(desc description.SelectedServer, cb CursorBuilder, wm wiremessage.WireMessage) *Aggregate {
rdr, err := (&Command{}).Decode(desc, wm).Result()
if err != nil {
a.err = err
return a
}
opts := make([]option.CursorOptioner, 0)
for _, opt := range a.Opts {
curOpt, ok := opt.(option.CursorOptioner)
if !ok {
continue
}
opts = append(opts, curOpt)
}
a.result, a.err = cb.BuildCursor(rdr, opts...)
return a
}
// Result returns the result of a decoded wire message and server description.
func (a *Aggregate) Result() (Cursor, error) {
if a.err != nil {
return nil, a.err
}
return a.result, nil
}
// Err returns the error set on this command.
func (a *Aggregate) Err() error { return a.err }
// RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter.
func (a *Aggregate) RoundTrip(ctx context.Context, desc description.SelectedServer, cb CursorBuilder, rw wiremessage.ReadWriter) (Cursor, error) {
wm, err := a.Encode(desc)
if err != nil {
return nil, err
}
err = rw.WriteWireMessage(ctx, wm)
if err != nil {
return nil, err
}
wm, err = rw.ReadWireMessage(ctx)
if err != nil {
return nil, err
}
return a.Decode(desc, cb, wm).Result()
}