Skip to content
Newer
Older
100644 261 lines (233 sloc) 8.61 KB
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
1 using System;
2 using System.Collections.Generic;
1cdc535 @lanwin Move MapReduceCommand to new Commands folder.
lanwin authored
3 using MongoDB.Commands;
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
4 using MongoDB.Results;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
5
a054c05 @lanwin Fix namespaces.
lanwin authored
6 namespace MongoDB
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
7 {
8 /// <summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
9 /// Provides a Fluent interface to build and execute Map/Reduce calls.
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
10 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
11 public class MapReduce : IDisposable
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
12 {
51b1b70 @lanwin Add the possibility to create mapped classes from a protected constru…
lanwin authored
13 private readonly IMongoDatabase _database;
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
14 private readonly Type _rootType;
51b1b70 @lanwin Add the possibility to create mapped classes from a protected constru…
lanwin authored
15 private bool _disposing;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
16
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
17 /// <summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
18 /// Initializes a new instance of the <see cref = "MapReduce" /> class.
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
19 /// </summary>
20 /// <param name = "database">The database.</param>
21 /// <param name = "name">The name.</param>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
22 /// <param name = "rootType">Type of the root.</param>
23 public MapReduce(IMongoDatabase database, string name, Type rootType)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
24 {
9c723a1 @lanwin Sync naming of IsModifiable.
lanwin authored
25 IsModifiable = true;
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
26 if(database == null)
27 throw new ArgumentNullException("database");
28 if(name == null)
29 throw new ArgumentNullException("name");
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
30 if(rootType == null)
31 throw new ArgumentNullException("rootType");
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
32
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
33 _rootType = rootType;
51b1b70 @lanwin Add the possibility to create mapped classes from a protected constru…
lanwin authored
34 _database = database;
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
35 Command = new MapReduceCommand(name);
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
36 }
37
38 /// <summary>
9c723a1 @lanwin Sync naming of IsModifiable.
lanwin authored
39 /// Gets a value indicating whether this instance is modifiable.
40 /// </summary>
41 /// <value>
42 /// <c>true</c> if this instance is modifiable; otherwise, <c>false</c>.
43 /// </value>
44 public bool IsModifiable { get; private set; }
45
46 /// <summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
47 /// Gets the result.
48 /// </summary>
49 /// <value>The result.</value>
50 internal MapReduceResult Result { get; private set; }
51
52 /// <summary>
53 /// Gets the command.
54 /// </summary>
55 /// <value>The command.</value>
56 public MapReduceCommand Command { get; private set; }
57
58 /// <summary>
59 /// Gets the documents.
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
60 /// </summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
61 /// <value>The documents.</value>
56850c0 @craiggwilson fixed javascript translator to for map reduce.
craiggwilson authored
62 public IEnumerable<Document> Documents
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
63 {
64 get
65 {
66 if(Result == null)
67 RetrieveData();
68 if(Result == null || Result.Ok == false)
69 throw new InvalidOperationException("Documents cannot be iterated when an error was returned from execute.");
70
56850c0 @craiggwilson fixed javascript translator to for map reduce.
craiggwilson authored
71 var docs = _database.GetCollection<Document>(Result.CollectionName).FindAll().Documents;
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
72 using((IDisposable)docs)
73 {
74 foreach(var doc in docs)
75 yield return doc;
76 }
77 }
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
78 }
79
80 /// <summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
81 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
82 /// </summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
83 public void Dispose()
84 {
85 if(Command.KeepTemp || Command.Out != null || _disposing)
86 return;
87
88 _disposing = true;
89
90 if(Result == null || Result.Ok == false)
91 return; //Nothing to do.
92
93 //Drop the temporary collection that was created as part of results.
94 _database.Metadata.DropCollection(Result.CollectionName);
95 }
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
96
97 /// <summary>
98 /// The map function references the variable this to inspect the current object under consideration.
99 /// A map function must call emit(key,value) at least once, but may be invoked any number of times,
100 /// as may be appropriate.
101 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
102 public MapReduce Map(string function)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
103 {
104 return Map(new Code(function));
105 }
106
107 /// <summary>
108 /// The map function references the variable this to inspect the current object under consideration.
109 /// A map function must call emit(key,value) at least once, but may be invoked any number of times,
110 /// as may be appropriate.
111 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
112 public MapReduce Map(Code function)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
113 {
114 TryModify();
115 Command.Map = function;
116 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
117 }
118
119 /// <summary>
120 /// The reduce function receives a key and an array of values. To use, reduce the received values,
121 /// and return a result.
122 /// </summary>
123 /// <remarks>
124 /// The MapReduce engine may invoke reduce functions iteratively; thus, these functions
125 /// must be idempotent. If you need to perform an operation only once, use a finalize function.
126 /// </remarks>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
127 public MapReduce Reduce(string function)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
128 {
129 return Reduce(new Code(function));
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
130 }
131
132 /// <summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
133 /// The reduce function receives a key and an array of values. To use, reduce the received values,
134 /// and return a result.
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
135 /// </summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
136 /// <remarks>
137 /// The MapReduce engine may invoke reduce functions iteratively; thus, these functions
138 /// must be idempotent. If you need to perform an operation only once, use a finalize function.
139 /// </remarks>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
140 public MapReduce Reduce(Code function)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
141 {
142 TryModify();
143 Command.Reduce = function;
144 return this;
145 }
146
147 /// <summary>
148 /// Query filter object
149 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
150 public MapReduce Query(Document query)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
151 {
152 TryModify();
153 Command.Query = query;
154 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
155 }
156
157 /// <summary>
158 /// Sort the query. Useful for optimization
159 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
160 public MapReduce Sort(Document sort)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
161 {
162 TryModify();
163 Command.Sort = sort;
164 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
165 }
166
167 /// <summary>
168 /// Number of objects to return from collection
169 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
170 public MapReduce Limit(long limit)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
171 {
172 TryModify();
173 Command.Limit = limit;
174 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
175 }
176
177 /// <summary>
178 /// Name of the final collection the results should be stored in.
179 /// </summary>
180 /// <remarks>
181 /// A temporary collection is still used and then renamed to the target name atomically.
182 /// </remarks>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
183 public MapReduce Out(String name)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
184 {
185 TryModify();
186 Command.Out = name;
187 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
188 }
189
190 /// <summary>
191 /// When true the generated collection is not treated as temporary. Specifying out automatically makes
192 /// the collection permanent
193 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
194 public MapReduce KeepTemp(bool keep)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
195 {
196 TryModify();
197 Command.KeepTemp = keep;
198 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
199 }
200
201 /// <summary>
202 /// Provides statistics on job execution time.
203 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
204 public MapReduce Verbose(bool val)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
205 {
206 TryModify();
207 Command.Verbose = val;
208 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
209 }
210
211 /// <summary>
212 /// Function to apply to all the results when finished.
213 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
214 public MapReduce Finalize(Code function)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
215 {
216 TryModify();
217 Command.Finalize = function;
218 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
219 }
220
221 /// <summary>
222 /// Document where fields go into javascript global scope
223 /// </summary>
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
224 public MapReduce Scope(Document scope)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
225 {
226 TryModify();
227 Command.Scope = scope;
228 return this;
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
229 }
230
8e2aa1b @lanwin Ger right on the missing xml comment warnings on core driver.
lanwin authored
231 /// <summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
232 /// Retrieves the data.
8e2aa1b @lanwin Ger right on the missing xml comment warnings on core driver.
lanwin authored
233 /// </summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
234 internal void RetrieveData()
235 {
97623f7 @lanwin Remove Document.Contains(key) in favor of the IDictionary<>.ContainsK…
lanwin authored
236 if(Command.Command.ContainsKey("map") == false || Command.Command.ContainsKey("reduce") == false)
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
237 throw new InvalidOperationException("Cannot execute without a map and reduce function");
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
238
9c723a1 @lanwin Sync naming of IsModifiable.
lanwin authored
239 IsModifiable = false;
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
240
241 try
242 {
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
243 Result = new MapReduceResult(_database.SendCommand(_rootType, Command.Command));
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
244 }
41d4628 @lanwin Move MapReduce to use generics. Currently some linq tests are failing.
lanwin authored
245 catch(MongoCommandException exception)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
246 {
41d4628 @lanwin Move MapReduce to use generics. Currently some linq tests are failing.
lanwin authored
247 Result = new MapReduceResult(exception.Error);
ffa6baa @lanwin Made MapReduce non generic again, since we dose not need it to be gen…
lanwin authored
248 throw new MongoMapReduceException(exception);
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
249 }
250 }
251
8e2aa1b @lanwin Ger right on the missing xml comment warnings on core driver.
lanwin authored
252 /// <summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
253 /// Tries the modify.
8e2aa1b @lanwin Ger right on the missing xml comment warnings on core driver.
lanwin authored
254 /// </summary>
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
255 private void TryModify()
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
256 {
9c723a1 @lanwin Sync naming of IsModifiable.
lanwin authored
257 if(IsModifiable == false)
117fd65 @lanwin Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
lanwin authored
258 throw new InvalidOperationException("Cannot modify a map/reduce that has already executed");
cbd633c @lanwin Use ISerializationFactory while constructing the messages.
lanwin authored
259 }
260 }
261 }
Something went wrong with that request. Please try again.