-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SortByBuilder.cs
296 lines (272 loc) · 11 KB
/
SortByBuilder.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Copyright 2010-present MongoDB Inc.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Linq.Utils;
namespace MongoDB.Driver.Builders
{
/// <summary>
/// A builder for specifying a sort order.
/// </summary>
public static class SortBy
{
// public static properties
/// <summary>
/// Gets a null value with a type of IMongoSortBy.
/// </summary>
public static IMongoSortBy Null
{
get { return null; }
}
// public static methods
/// <summary>
/// Adds keys to be sorted by in ascending order.
/// </summary>
/// <param name="keys">One or more key names.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static SortByBuilder Ascending(params string[] keys)
{
return new SortByBuilder().Ascending(keys);
}
/// <summary>
/// Adds keys to be sorted by in descending order.
/// </summary>
/// <param name="keys">One or more key names.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static SortByBuilder Descending(params string[] keys)
{
return new SortByBuilder().Descending(keys);
}
/// <summary>
/// Adds a key to sort by the computed relevance score when using text search. The name of the key should be
/// the name of the projected relevance score field.
/// </summary>
/// <param name="key">The name of the computed relevance score field.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static SortByBuilder MetaTextScore(string key)
{
return new SortByBuilder().MetaTextScore(key);
}
}
/// <summary>
/// A builder for specifying a sort order.
/// </summary>
[Serializable]
[BsonSerializer(typeof(SortByBuilder.Serializer))]
public class SortByBuilder : BuilderBase, IMongoSortBy
{
// private fields
private BsonDocument _document;
// constructors
/// <summary>
/// Initializes a new instance of the SortByBuider class.
/// </summary>
public SortByBuilder()
{
_document = new BsonDocument();
}
// public methods
/// <summary>
/// Adds keys to be sorted by in ascending order.
/// </summary>
/// <param name="keys">One or more key names.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public SortByBuilder Ascending(params string[] keys)
{
foreach (var key in keys)
{
_document.Add(key, 1);
}
return this;
}
/// <summary>
/// Adds keys to be sorted by in descending order.
/// </summary>
/// <param name="keys">One or more key names.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public SortByBuilder Descending(params string[] keys)
{
foreach (var key in keys)
{
_document.Add(key, -1);
}
return this;
}
/// <summary>
/// Adds a key to sort by the computed relevance score when using text search. The name of the key should be
/// the name of the projected relevance score field.
/// </summary>
/// <param name="key">The name of the computed relevance score field.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public SortByBuilder MetaTextScore(string key)
{
_document.Add(key, new BsonDocument("$meta", "textScore"));
return this;
}
/// <summary>
/// Returns the result of the builder as a BsonDocument.
/// </summary>
/// <returns>A BsonDocument.</returns>
public override BsonDocument ToBsonDocument()
{
return _document;
}
// nested classes
new internal class Serializer : SerializerBase<SortByBuilder>
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, SortByBuilder value)
{
BsonDocumentSerializer.Instance.Serialize(context, value._document);
}
}
}
/// <summary>
/// A builder for specifying a sort order.
/// </summary>
/// <typeparam name="TDocument">The type of the document.</typeparam>
public static class SortBy<TDocument>
{
/// <summary>
/// Adds keys to be sorted by in ascending order.
/// </summary>
/// <param name="memberExpressions">The member expressions.</param>
/// <returns>
/// The builder (so method calls can be chained).
/// </returns>
public static SortByBuilder<TDocument> Ascending(params Expression<Func<TDocument, object>>[] memberExpressions)
{
return new SortByBuilder<TDocument>().Ascending(memberExpressions);
}
/// <summary>
/// Adds keys to be sorted by in descending order.
/// </summary>
/// <param name="memberExpressions">The member expressions.</param>
/// <returns>
/// The builder (so method calls can be chained).
/// </returns>
public static SortByBuilder<TDocument> Descending(params Expression<Func<TDocument, object>>[] memberExpressions)
{
return new SortByBuilder<TDocument>().Descending(memberExpressions);
}
/// <summary>
/// Adds a key to sort by the computed relevance score when using text search. The name of the key should be
/// the name of the projected relevance score field.
/// </summary>
/// <param name="memberExpression">The member expression specifying the projected field.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static SortByBuilder<TDocument> MetaTextScore(Expression<Func<TDocument, object>> memberExpression)
{
return new SortByBuilder<TDocument>().MetaTextScore(memberExpression);
}
}
/// <summary>
/// A builder for specifying a sort order.
/// </summary>
/// <typeparam name="TDocument">The type of the document.</typeparam>
[Serializable]
[BsonSerializer(typeof(SortByBuilder<>.Serializer))]
public class SortByBuilder<TDocument> : BuilderBase, IMongoSortBy
{
// private fields
private readonly BsonSerializationInfoHelper _serializationInfoHelper;
private SortByBuilder _sortByBuilder;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="SortByBuilder{TDocument}"/> class.
/// </summary>
public SortByBuilder()
: this(new BsonSerializationInfoHelper())
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SortByBuilder{TDocument}" /> class.
/// </summary>
/// <param name="serializationInfoHelper">The serialization info helper.</param>
internal SortByBuilder(BsonSerializationInfoHelper serializationInfoHelper)
{
_serializationInfoHelper = serializationInfoHelper;
_sortByBuilder = new SortByBuilder();
}
// public methods
/// <summary>
/// Adds keys to be sorted by in ascending order.
/// </summary>
/// <param name="memberExpressions">The member expressions indicating which elements to sort by.</param>
/// <returns>
/// The builder (so method calls can be chained).
/// </returns>
public SortByBuilder<TDocument> Ascending(params Expression<Func<TDocument, object>>[] memberExpressions)
{
var elementNames = GetElementNames(memberExpressions);
_sortByBuilder = _sortByBuilder.Ascending(elementNames.ToArray());
return this;
}
/// <summary>
/// Adds keys to be sorted by in descending order.
/// </summary>
/// <param name="memberExpressions">The member expressions indicating which elements to sort by.</param>
/// <returns>
/// The builder (so method calls can be chained).
/// </returns>
public SortByBuilder<TDocument> Descending(params Expression<Func<TDocument, object>>[] memberExpressions)
{
var elementNames = GetElementNames(memberExpressions);
_sortByBuilder = _sortByBuilder.Descending(elementNames.ToArray());
return this;
}
/// <summary>
/// Adds a key to sort by the computed relevance score when using text search. The name of the key should be
/// the name of the projected relevance score field.
/// </summary>
/// <param name="memberExpression">The member expression specifying the projected field.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public SortByBuilder<TDocument> MetaTextScore(Expression<Func<TDocument, object>> memberExpression)
{
var serializationInfo = _serializationInfoHelper.GetSerializationInfo(memberExpression);
_sortByBuilder = _sortByBuilder.MetaTextScore(serializationInfo.ElementName);
return this;
}
/// <summary>
/// Converts this object to a BsonDocument.
/// </summary>
/// <returns>
/// A BsonDocument.
/// </returns>
public override BsonDocument ToBsonDocument()
{
return _sortByBuilder.ToBsonDocument();
}
// private methods
private IEnumerable<string> GetElementNames(IEnumerable<Expression<Func<TDocument, object>>> memberExpressions)
{
return memberExpressions
.Select(x => _serializationInfoHelper.GetSerializationInfo(x))
.Select(x => x.ElementName);
}
// nested classes
new internal class Serializer : SerializerBase<SortByBuilder<TDocument>>
{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, SortByBuilder<TDocument> value)
{
BsonDocumentSerializer.Instance.Serialize(context, value._sortByBuilder.ToBsonDocument());
}
}
}
}