@@ -33,79 +33,28 @@ public static enum OutputType {
33
33
* Represents the command for a map reduce operation
34
34
* Runs the command in REPLACE output type to a named collection
35
35
*
36
- * @param input
36
+ * @param inputCollection
37
37
* the collection to read from
38
38
* @param map
39
39
* map function in javascript code
40
40
* @param reduce
41
41
* reduce function in javascript code
42
- * @param outputTarget
43
- * optional - leave null if want to use temp collection
42
+ * @param outputCollection
43
+ * optional - leave null if want to get the result inline
44
+ * @param type
45
+ * the type of output
44
46
* @param query
45
- * to match
47
+ * the query to use on input
46
48
* @return
47
49
* @throws MongoException
48
50
* @dochub mapreduce
49
51
*/
50
- MapReduceCommand (String input , String map , String reduce , String outputTarget , DBObject query ) throws MongoException {
51
- _input = input ;
52
+ public MapReduceCommand (DBCollection inputCollection , String map , String reduce , String outputCollection , OutputType type , DBObject query ) throws MongoException {
53
+ _input = inputCollection . getName () ;
52
54
_map = map ;
53
55
_reduce = reduce ;
54
- _outputTarget = outputTarget ;
55
- _outputType = OutputType .REPLACE ;
56
- _query = query ;
57
- }
58
-
59
- /**
60
- * Represents the command for a map reduce operation
61
- * Runs the command in INLINE output mode
62
- *
63
- * @param input
64
- * the collection to read from
65
- * @param map
66
- * map function in javascript code
67
- * @param reduce
68
- * reduce function in javascript code
69
- * @param query
70
- * to match
71
- * @return
72
- * @throws MongoException
73
- * @dochub mapreduce
74
- */
75
- MapReduceCommand (String input , String map , String reduce , DBObject query ) throws MongoException {
76
- _input = input ;
77
- _map = map ;
78
- _reduce = reduce ;
79
- _outputTarget = null ;
80
- _outputType = OutputType .INLINE ;
81
- _query = query ;
82
- }
83
-
84
- /**
85
- * Represents the command for a map reduce operation
86
- *
87
- * @param input
88
- * the collection to read from
89
- * @param map
90
- * map function in javascript code
91
- * @param reduce
92
- * reduce function in javascript code
93
- * @param outputTarget
94
- * optional - leave null if want to use temp collection
95
- * @param outputType
96
- * set the type of job output
97
- * @param query
98
- * to match
99
- * @return
100
- * @throws MongoException
101
- * @dochub mapreduce
102
- */
103
- MapReduceCommand (String input , String map , String reduce , String outputTarget , OutputType outputType , DBObject query ) throws MongoException {
104
- _input = input ;
105
- _map = map ;
106
- _reduce = reduce ;
107
- _outputTarget = outputTarget ;
108
- _outputType = outputType ;
56
+ _outputTarget = outputCollection ;
57
+ _outputType = type ;
109
58
_query = query ;
110
59
}
111
60
@@ -205,7 +154,6 @@ public DBObject getQuery(){
205
154
return _query ;
206
155
}
207
156
208
-
209
157
/**
210
158
* Gets the (optional) sort specification object
211
159
*
@@ -226,21 +174,21 @@ public void setSort( DBObject sort ){
226
174
}
227
175
228
176
/**
229
- * Gets the (optional) limit specification object
177
+ * Gets the (optional) limit on input
230
178
*
231
179
* @return The limit specification object
232
180
*/
233
- public DBObject getLimit (){
181
+ public int getLimit (){
234
182
return _limit ;
235
183
}
236
184
237
185
/**
238
- * Sets the (optional) limit specification object
186
+ * Sets the (optional) limit on input
239
187
*
240
188
* @param limit
241
189
* The limit specification object
242
190
*/
243
- public void setLimit ( DBObject limit ){
191
+ public void setLimit ( int limit ){
244
192
_limit = limit ;
245
193
}
246
194
@@ -263,6 +211,15 @@ public void setScope( String scope ){
263
211
_scope = scope ;
264
212
}
265
213
214
+ /**
215
+ * Sets the (optional) database name where the output collection should reside
216
+ * @param outputDB
217
+ */
218
+ public void setOutputDB (String outputDB ) {
219
+ this ._outputDB = outputDB ;
220
+ }
221
+
222
+
266
223
DBObject toDBObject () {
267
224
BasicDBObjectBuilder builder = BasicDBObjectBuilder .start ();
268
225
@@ -271,19 +228,27 @@ DBObject toDBObject() {
271
228
.add ("reduce" , _reduce )
272
229
.add ("verbose" , _verbose );
273
230
274
- switch (_outputType ) {
275
- case INLINE :
276
- builder .add ("out" , new BasicDBObject ("inline" , 1 ));
277
- break ;
278
- case REPLACE :
279
- builder .add ("out" , _outputTarget );
280
- break ;
281
- case MERGE :
282
- builder .add ("out" , new BasicDBObject ("merge" , _outputTarget ));
283
- break ;
284
- case REDUCE :
285
- builder .add ("out" , new BasicDBObject ("reduce" , _outputTarget ));
286
- break ;
231
+ if (_outputType == OutputType .REPLACE && _outputDB == null ) {
232
+ builder .add ("out" , _outputTarget );
233
+ } else {
234
+ BasicDBObject out = new BasicDBObject ();
235
+ switch (_outputType ) {
236
+ case INLINE :
237
+ out .put ("inline" , 1 );
238
+ break ;
239
+ case REPLACE :
240
+ out .put ("replace" , _outputTarget );
241
+ break ;
242
+ case MERGE :
243
+ out .put ("merge" , _outputTarget );
244
+ break ;
245
+ case REDUCE :
246
+ out .put ("reduce" , _outputTarget );
247
+ break ;
248
+ }
249
+ if (_outputDB != null )
250
+ out .put ("db" , _outputDB );
251
+ builder .add ("out" , out );
287
252
}
288
253
289
254
if (_query != null )
@@ -295,7 +260,7 @@ DBObject toDBObject() {
295
260
if (_sort != null )
296
261
builder .add ("sort" , _sort );
297
262
298
- if (_limit != null )
263
+ if (_limit > 0 )
299
264
builder .add ("limit" , _limit );
300
265
301
266
if (_scope != null )
@@ -313,11 +278,12 @@ public String toString() {
313
278
final String _map ;
314
279
final String _reduce ;
315
280
final String _outputTarget ;
281
+ String _outputDB = null ;
316
282
final OutputType _outputType ;
317
283
final DBObject _query ;
318
284
String _finalize ;
319
285
DBObject _sort ;
320
- DBObject _limit ;
286
+ int _limit ;
321
287
String _scope ;
322
288
Boolean _verbose = true ;
323
289
}
0 commit comments