-
Notifications
You must be signed in to change notification settings - Fork 883
/
aggregate.go
118 lines (102 loc) · 3.39 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
// 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 driver
import (
"context"
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
"github.com/mongodb/mongo-go-driver/mongo/options"
"github.com/mongodb/mongo-go-driver/x/bsonx"
"time"
"github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
"github.com/mongodb/mongo-go-driver/x/mongo/driver/topology"
"github.com/mongodb/mongo-go-driver/x/mongo/driver/uuid"
"github.com/mongodb/mongo-go-driver/x/network/command"
"github.com/mongodb/mongo-go-driver/x/network/description"
)
// Aggregate handles the full cycle dispatch and execution of an aggregate command against the provided
// topology.
func Aggregate(
ctx context.Context,
cmd command.Aggregate,
topo *topology.Topology,
readSelector, writeSelector description.ServerSelector,
clientID uuid.UUID,
pool *session.Pool,
registry *bsoncodec.Registry,
opts ...*options.AggregateOptions,
) (command.Cursor, error) {
dollarOut := cmd.HasDollarOut()
var ss *topology.SelectedServer
var err error
switch dollarOut {
case true:
ss, err = topo.SelectServer(ctx, writeSelector)
if err != nil {
return nil, err
}
case false:
ss, err = topo.SelectServer(ctx, readSelector)
if err != nil {
return nil, err
}
}
desc := ss.Description()
conn, err := ss.Connection(ctx)
if err != nil {
return nil, err
}
defer conn.Close()
rp, err := getReadPrefBasedOnTransaction(cmd.ReadPref, cmd.Session)
if err != nil {
return nil, err
}
cmd.ReadPref = rp
// If no explicit session and deployment supports sessions, start implicit session.
if cmd.Session == nil && topo.SupportsSessions() {
cmd.Session, err = session.NewClientSession(pool, clientID, session.Implicit)
if err != nil {
return nil, err
}
}
aggOpts := options.MergeAggregateOptions(opts...)
if aggOpts.AllowDiskUse != nil {
cmd.Opts = append(cmd.Opts, bsonx.Elem{"allowDiskUse", bsonx.Boolean(*aggOpts.AllowDiskUse)})
}
if aggOpts.BatchSize != nil {
elem := bsonx.Elem{"batchSize", bsonx.Int32(*aggOpts.BatchSize)}
cmd.Opts = append(cmd.Opts, elem)
cmd.CursorOpts = append(cmd.CursorOpts, elem)
}
if aggOpts.BypassDocumentValidation != nil && desc.WireVersion.Includes(4) {
cmd.Opts = append(cmd.Opts, bsonx.Elem{"bypassDocumentValidation", bsonx.Boolean(*aggOpts.BypassDocumentValidation)})
}
if aggOpts.Collation != nil {
if desc.WireVersion.Max < 5 {
return nil, ErrCollation
}
cmd.Opts = append(cmd.Opts, bsonx.Elem{"collation", bsonx.Document(aggOpts.Collation.ToDocument())})
}
if aggOpts.MaxTime != nil {
cmd.Opts = append(cmd.Opts, bsonx.Elem{"maxTimeMS", bsonx.Int64(int64(*aggOpts.MaxTime / time.Millisecond))})
}
if aggOpts.MaxAwaitTime != nil {
// specified as maxTimeMS on getMore commands
cmd.CursorOpts = append(cmd.CursorOpts, bsonx.Elem{
"maxTimeMS", bsonx.Int64(int64(*aggOpts.MaxAwaitTime / time.Millisecond)),
})
}
if aggOpts.Comment != nil {
cmd.Opts = append(cmd.Opts, bsonx.Elem{"comment", bsonx.String(*aggOpts.Comment)})
}
if aggOpts.Hint != nil {
hintElem, err := interfaceToElement("hint", aggOpts.Hint, registry)
if err != nil {
return nil, err
}
cmd.Opts = append(cmd.Opts, hintElem)
}
return cmd.RoundTrip(ctx, desc, ss, conn)
}