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// CountOptions represents all possible options to the Count() function.
12type CountOptions struct {
13	Collation *Collation     // Specifies a collation
14	Hint      interface{}    // The index to use
15	Limit     *int64         // The maximum number of documents to count
16	MaxTime   *time.Duration // The maximum amount of time to allow the operation to run
17	Skip      *int64         // The number of documents to skip before counting
18}
19
20// Count returns a pointer to a new CountOptions
21func Count() *CountOptions {
22	return &CountOptions{}
23}
24
25// SetCollation specifies a collation
26// Valid for server versions >= 3.4
27func (co *CountOptions) SetCollation(c *Collation) *CountOptions {
28	co.Collation = c
29	return co
30}
31
32// SetHint specifies the index to use
33func (co *CountOptions) SetHint(h interface{}) *CountOptions {
34	co.Hint = h
35	return co
36}
37
38// SetLimit specifies the maximum number of documents to count
39func (co *CountOptions) SetLimit(i int64) *CountOptions {
40	co.Limit = &i
41	return co
42}
43
44// SetMaxTime specifies the maximum amount of time to allow the operation to run
45func (co *CountOptions) SetMaxTime(d time.Duration) *CountOptions {
46	co.MaxTime = &d
47	return co
48}
49
50// SetSkip specifies the number of documents to skip before counting
51func (co *CountOptions) SetSkip(i int64) *CountOptions {
52	co.Skip = &i
53	return co
54}
55
56// MergeCountOptions combines the argued CountOptions into a single CountOptions in a last-one-wins fashion
57func MergeCountOptions(opts ...*CountOptions) *CountOptions {
58	countOpts := Count()
59	for _, co := range opts {
60		if co == nil {
61			continue
62		}
63		if co.Collation != nil {
64			countOpts.Collation = co.Collation
65		}
66		if co.Hint != nil {
67			countOpts.Hint = co.Hint
68		}
69		if co.Limit != nil {
70			countOpts.Limit = co.Limit
71		}
72		if co.MaxTime != nil {
73			countOpts.MaxTime = co.MaxTime
74		}
75		if co.Skip != nil {
76			countOpts.Skip = co.Skip
77		}
78	}
79
80	return countOpts
81}
82