-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CollectionStatsResult.cs
300 lines (273 loc) · 8.49 KB
/
CollectionStatsResult.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
297
298
299
300
/* 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 MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Driver
{
/// <summary>
/// Represents collection system flags.
/// </summary>
[Flags]
public enum CollectionSystemFlags
{
/// <summary>
/// No flags.
/// </summary>
None = 0,
/// <summary>
/// The collection has an _id index.
/// </summary>
HasIdIndex = 1 // called HaveIdIndex in the server but renamed here to follow .NET naming conventions
}
/// <summary>
/// Represents collection user flags.
/// </summary>
[Flags]
public enum CollectionUserFlags
{
/// <summary>
/// No flags.
/// </summary>
None = 0,
/// <summary>
/// User power of 2 size.
/// </summary>
UsePowerOf2Sizes = 1,
/// <summary>
/// Whether padding should not be used.
/// </summary>
NoPadding = 2
}
/// <summary>
/// Represents the results of the collection stats command.
/// </summary>
[Serializable]
[BsonSerializer(typeof(CommandResultSerializer<CollectionStatsResult>))]
public class CollectionStatsResult : CommandResult
{
// private fields
private IndexSizesResult _indexSizes;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="CollectionStatsResult"/> class.
/// </summary>
/// <param name="response">The response.</param>
public CollectionStatsResult(BsonDocument response)
: base(response)
{
}
// public properties
/// <summary>
/// Gets the average object size.
/// </summary>
public double AverageObjectSize
{
get { return Response["avgObjSize"].ToDouble(); }
}
/// <summary>
/// Gets the data size.
/// </summary>
public long DataSize
{
get { return Response["size"].ToInt64(); }
}
/// <summary>
/// Gets the extent count.
/// </summary>
public int ExtentCount
{
get { return Response["numExtents"].ToInt32(); }
}
/// <summary>
/// Gets the index count.
/// </summary>
public int IndexCount
{
get { return Response["nindexes"].ToInt32(); }
}
/// <summary>
/// Gets the index sizes.
/// </summary>
public IndexSizesResult IndexSizes
{
get
{
if (_indexSizes == null)
{
// can't initialize indexSizes in the constructor because at that time the document is still empty
_indexSizes = new IndexSizesResult(Response["indexSizes"].AsBsonDocument);
}
return _indexSizes;
}
}
/// <summary>
/// Gets a value indicating whether the collection is capped.
/// </summary>
public bool IsCapped
{
get { return Response.GetValue("capped", false).ToBoolean(); }
}
/// <summary>
/// Gets the last extent size.
/// </summary>
public long LastExtentSize
{
get { return Response["lastExtentSize"].ToInt64(); }
}
/// <summary>
/// Gets the index count.
/// </summary>
public long MaxDocuments
{
get { return Response.GetValue("max", 0).ToInt32(); }
}
/// <summary>
/// Gets the namespace.
/// </summary>
public string Namespace
{
get { return Response["ns"].AsString; }
}
/// <summary>
/// Gets the object count.
/// </summary>
public long ObjectCount
{
get { return Response["count"].ToInt64(); }
}
/// <summary>
/// Gets the padding factor.
/// </summary>
public double PaddingFactor
{
get { return Response["paddingFactor"].ToDouble(); }
}
/// <summary>
/// Gets the storage size.
/// </summary>
public long StorageSize
{
get { return Response["storageSize"].ToInt64(); }
}
/// <summary>
/// Gets the system flags.
/// </summary>
public CollectionSystemFlags SystemFlags
{
get
{
// systemFlags was first introduced in server version 2.2 (check "flags" also for compatibility with older servers)
BsonValue systemFlags;
if (Response.TryGetValue("systemFlags", out systemFlags) || Response.TryGetValue("flags", out systemFlags))
{
return (CollectionSystemFlags)systemFlags.ToInt32();
}
else
{
return CollectionSystemFlags.HasIdIndex;
}
}
}
/// <summary>
/// Gets the total index size.
/// </summary>
public long TotalIndexSize
{
get { return Response["totalIndexSize"].ToInt64(); }
}
/// <summary>
/// Gets the user flags.
/// </summary>
public CollectionUserFlags UserFlags
{
get
{
// userFlags was first introduced in server version 2.2
BsonValue userFlags;
if (Response.TryGetValue("userFlags", out userFlags))
{
return (CollectionUserFlags)userFlags.ToInt32();
}
else
{
return CollectionUserFlags.None;
}
}
}
// nested classes
/// <summary>
/// Represents a collection of index sizes.
/// </summary>
public class IndexSizesResult
{
// private fields
private BsonDocument _indexSizes;
// constructors
/// <summary>
/// Initializes a new instance of the IndexSizesResult class.
/// </summary>
/// <param name="indexSizes">The index sizes document.</param>
public IndexSizesResult(BsonDocument indexSizes)
{
_indexSizes = indexSizes;
}
// indexers
/// <summary>
/// Gets the size of an index.
/// </summary>
/// <param name="indexName">The name of the index.</param>
/// <returns>The size of the index.</returns>
public long this[string indexName]
{
get { return _indexSizes[indexName].ToInt64(); }
}
// public properties
/// <summary>
/// Gets the count of indexes.
/// </summary>
public int Count
{
get { return _indexSizes.ElementCount; }
}
/// <summary>
/// Gets the names of the indexes.
/// </summary>
public IEnumerable<string> Keys
{
get { return _indexSizes.Names; }
}
/// <summary>
/// Gets the sizes of the indexes.
/// </summary>
public IEnumerable<long> Values
{
get { return _indexSizes.Values.Select(v => v.ToInt64()); }
}
// public methods
/// <summary>
/// Tests whether the results contain the size of an index.
/// </summary>
/// <param name="indexName">The name of the index.</param>
/// <returns>True if the results contain the size of the index.</returns>
public bool ContainsKey(string indexName)
{
return _indexSizes.Contains(indexName);
}
}
}
}