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
7package options
8
9import "time"
10
11// AggregateOptions represents all possible options to the Aggregate() function.
12type AggregateOptions struct {
13	AllowDiskUse             *bool          // Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in the dbPath directory
14	BatchSize                *int32         // The number of documents to return per batch
15	BypassDocumentValidation *bool          // If true, allows the write to opt-out of document level validation. This only applies when the $out stage is specified
16	Collation                *Collation     // Specifies a collation
17	MaxTime                  *time.Duration // The maximum amount of time to allow the query to run
18	MaxAwaitTime             *time.Duration // The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query
19	Comment                  *string        // Enables users to specify an arbitrary string to help trace the operation through the database profiler, currentOp and logs.
20	Hint                     interface{}    // The index to use for the aggregation. The hint does not apply to $lookup and $graphLookup stages
21}
22
23// Aggregate returns a pointer to a new AggregateOptions
24func Aggregate() *AggregateOptions {
25	return &AggregateOptions{}
26}
27
28// SetAllowDiskUse enables writing to temporary files. When set to true,
29// aggregation stages can write data to the _tmp subdirectory in the
30// dbPath directory
31func (ao *AggregateOptions) SetAllowDiskUse(b bool) *AggregateOptions {
32	ao.AllowDiskUse = &b
33	return ao
34}
35
36// SetBatchSize specifies the number of documents to return per batch
37func (ao *AggregateOptions) SetBatchSize(i int32) *AggregateOptions {
38	ao.BatchSize = &i
39	return ao
40}
41
42// SetBypassDocumentValidation allows the write to opt-out of document level
43// validation. This only applies when the $out stage is specified
44// Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
45func (ao *AggregateOptions) SetBypassDocumentValidation(b bool) *AggregateOptions {
46	ao.BypassDocumentValidation = &b
47	return ao
48}
49
50// SetCollation specifies a collation.
51// Valid for server versions >= 3.4
52func (ao *AggregateOptions) SetCollation(c *Collation) *AggregateOptions {
53	ao.Collation = c
54	return ao
55}
56
57// SetMaxTime specifies the maximum amount of time to allow the query to run
58func (ao *AggregateOptions) SetMaxTime(d time.Duration) *AggregateOptions {
59	ao.MaxTime = &d
60	return ao
61}
62
63// SetMaxAwaitTime specifies the maximum amount of time for the server to
64// wait on new documents to satisfy a tailable cursor query
65// For servers < 3.2, this option is ignored
66func (ao *AggregateOptions) SetMaxAwaitTime(d time.Duration) *AggregateOptions {
67	ao.MaxAwaitTime = &d
68	return ao
69}
70
71// SetComment enables users to specify an arbitrary string to help trace the
72// operation through the database profiler, currentOp and logs.
73func (ao *AggregateOptions) SetComment(s string) *AggregateOptions {
74	ao.Comment = &s
75	return ao
76}
77
78// SetHint specifies the index to use for the aggregation. The hint does not
79// apply to $lookup and $graphLookup stages
80func (ao *AggregateOptions) SetHint(h interface{}) *AggregateOptions {
81	ao.Hint = h
82	return ao
83}
84
85// MergeAggregateOptions combines the argued AggregateOptions into a single AggregateOptions in a last-one-wins fashion
86func MergeAggregateOptions(opts ...*AggregateOptions) *AggregateOptions {
87	aggOpts := Aggregate()
88	for _, ao := range opts {
89		if ao == nil {
90			continue
91		}
92		if ao.AllowDiskUse != nil {
93			aggOpts.AllowDiskUse = ao.AllowDiskUse
94		}
95		if ao.BatchSize != nil {
96			aggOpts.BatchSize = ao.BatchSize
97		}
98		if ao.BypassDocumentValidation != nil {
99			aggOpts.BypassDocumentValidation = ao.BypassDocumentValidation
100		}
101		if ao.Collation != nil {
102			aggOpts.Collation = ao.Collation
103		}
104		if ao.MaxTime != nil {
105			aggOpts.MaxTime = ao.MaxTime
106		}
107		if ao.MaxAwaitTime != nil {
108			aggOpts.MaxAwaitTime = ao.MaxAwaitTime
109		}
110		if ao.Comment != nil {
111			aggOpts.Comment = ao.Comment
112		}
113		if ao.Hint != nil {
114			aggOpts.Hint = ao.Hint
115		}
116	}
117
118	return aggOpts
119}
120