-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoCollection.cs
260 lines (204 loc) · 8.42 KB
/
MongoCollection.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using CSMongo.Requests;
using CSMongo.Responses;
using CSMongo.Commands;
using CSMongo.Results;
using CSMongo.Query;
using CSMongo.Bson;
namespace CSMongo {
/// <summary>
/// A collection of documents within a Mongo database
/// </summary>
public class MongoCollection {
#region Constructors
/// <summary>
/// Creates a new MongoCollection - It is better to create a
/// collection using the MongoDatabase command GetCollection
/// so the database can keep track of multiple instances of
/// the same collection
/// </summary>
public MongoCollection(MongoDatabase database, string collection) {
this.Database = database;
this.Name = collection;
//set the containers for updating
this._Inserts = new List<MongoDocument>();
this._Deletes = new List<MongoDocument>();
this._Updates = new List<KeyValuePair<string, MongoDocument>>();
}
#endregion
#region Properties
/// <summary>
/// The name of the collection to query
/// </summary>
public string Name { get; private set; }
/// <summary>
/// The database connection information
/// </summary>
public MongoDatabase Database { get; private set; }
/// <summary>
/// Returns the connection used for this request
/// </summary>
public MongoConnection Connection {
get { return this.Database.Connection; }
}
//update information
internal List<MongoDocument> _Inserts;
internal List<MongoDocument> _Deletes;
//document with a hashcode to track changes
internal List<KeyValuePair<string, MongoDocument>> _Updates;
#endregion
#region Query Records
/// <summary>
/// Starts a new query for this collection
/// </summary>
public MongoQuery Find() {
return this.Find<MongoQuery>();
}
/// <summary>
/// Starts a new query for this collection using the provider requested
/// </summary>
public TQueryProvider Find<TQueryProvider>() where TQueryProvider : MongoQueryBase {
return Activator.CreateInstance(typeof(TQueryProvider), this) as TQueryProvider;
}
/// <summary>
/// Returns the current count of records for the database
/// </summary>
public long Count() {
return this.Find().Count();
}
#endregion
#region Updating Changes
/// <summary>
/// Adds a record to be inserted when changes are submitted
/// </summary>
public void InsertOnSubmit(object document) {
this._Inserts.Add(new MongoDocument(document));
}
/// <summary>
/// Adds a record to be inserted when changes are submitted
/// </summary>
public void InsertOnSubmit(MongoDocument document) {
this._Inserts.Add(document);
}
/// <summary>
/// Adds a set of records to be inserted when changes are submitted
/// </summary>
public void InsertOnSubmit(IEnumerable<MongoDocument> documents) {
this._Inserts.AddRange(documents);
}
/// <summary>
/// Adds a record to be deleted when changes are submitted
/// </summary>
public void DeleteOnSubmit(MongoDocument document) {
this._Deletes.Add(document);
}
/// <summary>
/// Adds a set of records to be deleted when changes are submitted
/// </summary>
public void DeleteOnSubmit(IEnumerable<MongoDocument> documents) {
this._Deletes.AddRange(documents);
}
/// <summary>
/// Appends a document to monitor for changes and updates
/// </summary>
public void UpdateOnSubmit(MongoDocument document) {
this.UpdateOnSubmit((new MongoDocument[] { document }).AsEnumerable());
}
/// <summary>
/// Appends a document to monitor for changes and updates
/// </summary>
public void UpdateOnSubmit(IEnumerable<MongoDocument> documents) {
//append each of the items to the updates
foreach (MongoDocument update in documents) {
if (this._Updates.Any(item => item.Equals(update))) { return; }
this._Updates.Add(new KeyValuePair<string, MongoDocument>(update.GetObjectHash(), update));
}
}
#endregion
#region Submitting Changes
/// <summary>
/// Handles updating changes for the database
/// </summary>
public void SubmitChanges() {
//check for changes
if (this._Inserts.Count > 0) { this._PerformInserts(); }
if (this._Updates.Count > 0) { this._PerformUpdates(); }
if (this._Deletes.Count > 0) { this._PerformDeletes(); }
//then clear the lists
this._Inserts = new List<MongoDocument>();
this._Updates = new List<KeyValuePair<string, MongoDocument>>();
this._Deletes = new List<MongoDocument>();
}
//handles inserting records waiting
private void _PerformInserts() {
InsertRequest insert = new InsertRequest(this);
insert.Documents.AddRange(this._Inserts);
this.Connection.SendRequest(insert);
}
//handles updating records that are changed
private void _PerformUpdates() {
//check for changed items and update them now
foreach (KeyValuePair<string, MongoDocument> item in this._Updates) {
//if this hasn't changed then skip it
if (item.Key.Equals(item.Value.GetObjectHash())) { continue; }
//create a bson document of the update to create
BsonDocument update = new BsonDocument();
update.Merge(item.Value);
update.Remove(Mongo.DocumentIdKey);
//start with the update
this.Find().FindById(item.Value.Id).Set(update);
//check for anything removed
IEnumerable<string> removed = item.Value.GetRemovedFields();
if (removed.Count() > 0) {
this.Find().FindById(item.Value.Id).Unset(removed.ToArray());
}
//Might want to try and merge this into the same
//request to avoid two trips to the database -- But
//this might cause an issue with older versions of
//the same database since an unset call would cause
//the inital set request to fail...
//UpdateRequest request = new UpdateRequest(this);
//request.Modifications["$set"] = item.Value;
//request.Modifications["$unset"] = item.Value.GetRemovedFields();
//request.Parameters["$in"] = new MongoOid[] { item.Value.Id };
//this.Database.SendRequest(request);
}
}
//handles deleting records that need to be removed
private void _PerformDeletes() {
IEnumerable<MongoOid> ids = this._Deletes.Select(item => item.Id);
this.Database.From(this.Name).In("_id", ids).Delete();
}
#endregion
#region Administrative
/// <summary>
/// Removes a collection from the database
/// </summary>
public DropCollectionResult DropCollection() {
return this.Database.DropCollection(this.Name);
}
/// <summary>
/// Returns details about the status of this collection
/// </summary>
public CollectionStatsResult GetStatus() {
return MongoDatabaseCommands.GetCollectionStats(this.Database, this.Name);
}
/// <summary>
/// Removes all indexes from this collection
/// </summary>
public DeleteCollectionIndexResult DeleteIndex() {
return this.Database.DeleteCollectionIndex(this.Name);
}
/// <summary>
/// Removes the specified index from this collection
/// </summary>
public DeleteCollectionIndexResult DeleteIndex(string collection, string index) {
return this.Database.DeleteCollectionIndex(collection, index);
}
#endregion
}
}