Skip to content

Commit f33612a

Browse files
committed
[JAVA-251]: support for different output db. Fix for mrCommand limit param. Cleanup of mrCommand constructor.
1 parent 2cfef6f commit f33612a

File tree

3 files changed

+69
-91
lines changed

3 files changed

+69
-91
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -917,12 +917,12 @@ public List distinct( String key , DBObject query ){
917917
* @dochub mapreduce
918918
*/
919919
public MapReduceOutput mapReduce( String map , String reduce , DBObject query ) throws MongoException{
920-
return mapReduce( new MapReduceCommand( getName() , map , reduce , query ) );
920+
return mapReduce( new MapReduceCommand( this , map , reduce , null, MapReduceCommand.OutputType.INLINE, query ) );
921921
}
922922

923923
/**
924924
* performs a map reduce operation
925-
* Runs the command in STANDARD output mode (saves to named collection)
925+
* Runs the command in REPLACE output mode (saves to named collection)
926926
*
927927
* @param map
928928
* map function in javascript code
@@ -937,15 +937,14 @@ public MapReduceOutput mapReduce( String map , String reduce , DBObject query )
937937
* @dochub mapreduce
938938
*/
939939
public MapReduceOutput mapReduce( String map , String reduce , String outputTarget , DBObject query ) throws MongoException{
940-
return mapReduce( new MapReduceCommand( getName() , map , reduce , outputTarget , query ) );
940+
return mapReduce( new MapReduceCommand( this , map , reduce , outputTarget , MapReduceCommand.OutputType.REPLACE, query ) );
941941
}
942942

943943
/**
944944
* performs a map reduce operation
945-
* Specify an outputType (MapReduceCommand.OutputType) to control job
946-
* execution
945+
* Specify an outputType to control job execution
947946
* * INLINE - Return results inline
948-
* * STANDARD - Save the job output to outputTarget
947+
* * REPLACE - Replace the output collection with the job output
949948
* * MERGE - Merge the job output with the existing contents of outputTarget
950949
* * REDUCE - Reduce the job output with the existing contents of
951950
* outputTarget
@@ -966,7 +965,7 @@ public MapReduceOutput mapReduce( String map , String reduce , String outputTarg
966965
*/
967966
public MapReduceOutput mapReduce( String map , String reduce , String outputTarget , MapReduceCommand.OutputType outputType , DBObject query )
968967
throws MongoException{
969-
return mapReduce( new MapReduceCommand( getName() , map , reduce , outputTarget , outputType , query ) );
968+
return mapReduce( new MapReduceCommand( this , map , reduce , outputTarget , outputType , query ) );
970969
}
971970

972971
/**

src/main/com/mongodb/MapReduceCommand.java

Lines changed: 47 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -33,79 +33,28 @@ public static enum OutputType {
3333
* Represents the command for a map reduce operation
3434
* Runs the command in REPLACE output type to a named collection
3535
*
36-
* @param input
36+
* @param inputCollection
3737
* the collection to read from
3838
* @param map
3939
* map function in javascript code
4040
* @param reduce
4141
* 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
4446
* @param query
45-
* to match
47+
* the query to use on input
4648
* @return
4749
* @throws MongoException
4850
* @dochub mapreduce
4951
*/
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();
5254
_map = map;
5355
_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;
10958
_query = query;
11059
}
11160

@@ -205,7 +154,6 @@ public DBObject getQuery(){
205154
return _query;
206155
}
207156

208-
209157
/**
210158
* Gets the (optional) sort specification object
211159
*
@@ -226,21 +174,21 @@ public void setSort( DBObject sort ){
226174
}
227175

228176
/**
229-
* Gets the (optional) limit specification object
177+
* Gets the (optional) limit on input
230178
*
231179
* @return The limit specification object
232180
*/
233-
public DBObject getLimit(){
181+
public int getLimit(){
234182
return _limit;
235183
}
236184

237185
/**
238-
* Sets the (optional) limit specification object
186+
* Sets the (optional) limit on input
239187
*
240188
* @param limit
241189
* The limit specification object
242190
*/
243-
public void setLimit( DBObject limit ){
191+
public void setLimit( int limit ){
244192
_limit = limit;
245193
}
246194

@@ -263,6 +211,15 @@ public void setScope( String scope ){
263211
_scope = scope;
264212
}
265213

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+
266223
DBObject toDBObject() {
267224
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
268225

@@ -271,19 +228,27 @@ DBObject toDBObject() {
271228
.add("reduce", _reduce)
272229
.add("verbose", _verbose);
273230

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);
287252
}
288253

289254
if (_query != null)
@@ -295,7 +260,7 @@ DBObject toDBObject() {
295260
if (_sort != null)
296261
builder.add("sort", _sort);
297262

298-
if (_limit != null)
263+
if (_limit > 0)
299264
builder.add("limit", _limit);
300265

301266
if (_scope != null)
@@ -313,11 +278,12 @@ public String toString() {
313278
final String _map;
314279
final String _reduce;
315280
final String _outputTarget;
281+
String _outputDB = null;
316282
final OutputType _outputType;
317283
final DBObject _query;
318284
String _finalize;
319285
DBObject _sort;
320-
DBObject _limit;
286+
int _limit;
321287
String _scope;
322288
Boolean _verbose = true;
323289
}

src/main/com/mongodb/MapReduceOutput.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ public class MapReduceOutput {
1212
_raw = raw;
1313
_cmd = cmd;
1414

15-
if ( raw.containsKey( "results" ) ) {
15+
if ( raw.containsField( "results" ) ) {
1616
_coll = null;
1717
_collname = null;
1818
_resultSet = (Iterable<DBObject>) raw.get( "results" );
1919
} else {
20-
_collname = raw.getString( "result" );
21-
_coll = from._db.getCollection( _collname );
20+
Object res = raw.get("result");
21+
if (res instanceof String) {
22+
_collname = (String) res;
23+
} else {
24+
BasicDBObject output = (BasicDBObject) res;
25+
_collname = output.getString("collection");
26+
_dbname = output.getString("db");
27+
}
28+
29+
DB db = from._db;
30+
if (_dbname != null) {
31+
db = db.getSisterDB(_dbname);
32+
}
33+
_coll = db.getCollection( _collname );
2234
_resultSet = _coll.find();
2335
}
2436
_counts = (BasicDBObject)raw.get( "counts" );
@@ -64,6 +76,7 @@ public String toString(){
6476
final BasicDBObject _raw;
6577

6678
final String _collname;
79+
String _dbname = null;
6780
final Iterable<DBObject> _resultSet;
6881
final DBCollection _coll;
6982
final BasicDBObject _counts;

0 commit comments

Comments
 (0)