Skip to content

Commit 0505a50

Browse files
author
Steve Briskin
committed
added readpreference to calls
1 parent 58b9467 commit 0505a50

File tree

2 files changed

+108
-17
lines changed

2 files changed

+108
-17
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -862,45 +862,82 @@ public long count(DBObject query){
862862
/**
863863
* calls {@link DBCollection#getCount(com.mongodb.DBObject, com.mongodb.DBObject)} with an empty query and null fields.
864864
* @return number of documents that match query
865-
* @throws MongoException
865+
* @throws MongoException
866866
*/
867867
public long getCount(){
868868
return getCount(new BasicDBObject(), null);
869869
}
870+
871+
/**
872+
* calls {@link DBCollection#getCount(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.ReadPreference)} with empty query and null fields.
873+
* @param readPrefs ReadPreferences for this command
874+
* @return number of documents that match query
875+
* @throws MongoException
876+
*/
877+
public long getCount(ReadPreference readPrefs){
878+
return getCount(new BasicDBObject(), null, readPrefs);
879+
}
870880

871881
/**
872882
* calls {@link DBCollection#getCount(com.mongodb.DBObject, com.mongodb.DBObject)} with null fields.
873883
* @param query query to match
874884
* @return
875-
* @throws MongoException
885+
* @throws MongoException
876886
*/
877887
public long getCount(DBObject query){
878888
return getCount(query, null);
879889
}
880890

891+
881892
/**
882893
* calls {@link DBCollection#getCount(com.mongodb.DBObject, com.mongodb.DBObject, long, long)} with limit=0 and skip=0
883894
* @param query query to match
884895
* @param fields fields to return
885896
* @return
886-
* @throws MongoException
897+
* @throws MongoException
887898
*/
888899
public long getCount(DBObject query, DBObject fields){
889900
return getCount( query , fields , 0 , 0 );
890901
}
902+
903+
/**
904+
* calls {@link DBCollection#getCount(com.mongodb.DBObject, com.mongodb.DBObject, long, long, com.mongodb.ReadPreference)} with limit=0 and skip=0
905+
* @param query query to match
906+
* @param fields fields to return
907+
* @param readPrefs ReadPreferences for this command
908+
* @return
909+
* @throws MongoException
910+
*/
911+
public long getCount(DBObject query, DBObject fields, ReadPreference readPrefs){
912+
return getCount( query , fields , 0 , 0, readPrefs );
913+
}
891914

915+
/**
916+
* calls {@link DBCollection#getCount(com.mongodb.DBObject, com.mongodb.DBObject, long, long, com.mongodb.ReadPreference)} with the DBCollection's ReadPreference
917+
* @param query query to match
918+
* @param fields fields to return
919+
* @param limit limit the count to this value
920+
* @param skip skip number of entries to skip
921+
* @return
922+
* @throws MongoException
923+
*/
924+
public long getCount(DBObject query, DBObject fields, long limit, long skip){
925+
return getCount(query, fields, limit, skip, getReadPreference());
926+
}
927+
892928
/**
893929
* Returns the number of documents in the collection
894930
* that match the specified query
895931
*
896932
* @param query query to select documents to count
897933
* @param fields fields to return
898934
* @param limit limit the count to this value
899-
* @param skip number of entries to skip
900-
* @return number of documents that match query and fields
901-
* @throws MongoException
935+
* @param skip number of entries to skip
936+
* @param readPrefs ReadPreferences for this command
937+
* @return number of documents that match query and fields
938+
* @throws MongoException
902939
*/
903-
public long getCount(DBObject query, DBObject fields, long limit, long skip ){
940+
public long getCount(DBObject query, DBObject fields, long limit, long skip, ReadPreference readPrefs ){
904941

905942
BasicDBObject cmd = new BasicDBObject();
906943
cmd.put("count", getName());
@@ -914,8 +951,7 @@ public long getCount(DBObject query, DBObject fields, long limit, long skip ){
914951
if ( skip > 0 )
915952
cmd.put( "skip" , skip );
916953

917-
CommandResult res = _db.command(cmd,getOptions(),getReadPreference());
918-
954+
CommandResult res = _db.command(cmd,getOptions(),readPrefs);
919955
if ( ! res.ok() ){
920956
String errmsg = res.getErrorMessage();
921957

@@ -930,6 +966,10 @@ public long getCount(DBObject query, DBObject fields, long limit, long skip ){
930966

931967
return res.getLong("n");
932968
}
969+
970+
CommandResult command(DBObject cmd, int options, ReadPreference readPrefs){
971+
return _db.command(cmd,getOptions(),readPrefs);
972+
}
933973

934974
/**
935975
* Calls {@link DBCollection#rename(java.lang.String, boolean)} with dropTarget=false
@@ -973,8 +1013,8 @@ public DBCollection rename( String newName, boolean dropTarget ){
9731013
*/
9741014
public DBObject group( DBObject key , DBObject cond , DBObject initial , String reduce ){
9751015
return group( key , cond , initial , reduce , null );
976-
}
977-
1016+
}
1017+
9781018
/**
9791019
* Applies a group operation
9801020
* @param key - { a : true }
@@ -990,6 +1030,23 @@ public DBObject group( DBObject key , DBObject cond , DBObject initial , String
9901030
GroupCommand cmd = new GroupCommand(this, key, cond, initial, reduce, finalize);
9911031
return group( cmd );
9921032
}
1033+
1034+
/**
1035+
* Applies a group operation
1036+
* @param key - { a : true }
1037+
* @param cond - optional condition on query
1038+
* @param reduce javascript reduce function
1039+
* @param initial initial value for first match on a key
1040+
* @param finalize An optional function that can operate on the result(s) of the reduce function.
1041+
* @param readPrefs ReadPreferences for this command
1042+
* @return
1043+
* @throws MongoException
1044+
* @see <a href="http://www.mongodb.org/display/DOCS/Aggregation">http://www.mongodb.org/display/DOCS/Aggregation</a>
1045+
*/
1046+
public DBObject group( DBObject key , DBObject cond , DBObject initial , String reduce , String finalize, ReadPreference readPrefs ){
1047+
GroupCommand cmd = new GroupCommand(this, key, cond, initial, reduce, finalize);
1048+
return group( cmd, readPrefs );
1049+
}
9931050

9941051
/**
9951052
* Applies a group operation
@@ -999,12 +1056,23 @@ public DBObject group( DBObject key , DBObject cond , DBObject initial , String
9991056
* @see <a href="http://www.mongodb.org/display/DOCS/Aggregation">http://www.mongodb.org/display/DOCS/Aggregation</a>
10001057
*/
10011058
public DBObject group( GroupCommand cmd ) {
1002-
CommandResult res = _db.command( cmd.toDBObject(), getOptions(), getReadPreference() );
1059+
return group(cmd, getReadPreference());
1060+
}
1061+
1062+
/**
1063+
* Applies a group operation
1064+
* @param cmd the group command
1065+
* @param readPrefs ReadPreferences for this command
1066+
* @return
1067+
* @throws MongoException
1068+
* @see <a href="http://www.mongodb.org/display/DOCS/Aggregation">http://www.mongodb.org/display/DOCS/Aggregation</a>
1069+
*/
1070+
public DBObject group( GroupCommand cmd, ReadPreference readPrefs ) {
1071+
CommandResult res = _db.command( cmd.toDBObject(), getOptions(), readPrefs );
10031072
res.throwOnError();
10041073
return (DBObject)res.get( "retval" );
10051074
}
10061075

1007-
10081076
/**
10091077
* @deprecated prefer the {@link DBCollection#group(com.mongodb.GroupCommand)} which is more standard
10101078
* Applies a group operation
@@ -1030,6 +1098,17 @@ public DBObject group( DBObject args ){
10301098
public List distinct( String key ){
10311099
return distinct( key , new BasicDBObject() );
10321100
}
1101+
1102+
/**
1103+
* find distinct values for a key
1104+
* @param key
1105+
* @param readPrefs
1106+
* @return
1107+
* @throws MongoException
1108+
*/
1109+
public List distinct( String key, ReadPreference readPrefs ){
1110+
return distinct( key , new BasicDBObject(), readPrefs );
1111+
}
10331112

10341113
/**
10351114
* find distinct values for a key
@@ -1039,17 +1118,29 @@ public List distinct( String key ){
10391118
* @throws MongoException
10401119
*/
10411120
public List distinct( String key , DBObject query ){
1121+
return distinct(key, query, getReadPreference());
1122+
}
1123+
1124+
/**
1125+
* find distinct values for a key
1126+
* @param key
1127+
* @param query query to match
1128+
* @param readPrefs
1129+
* @return
1130+
* @throws MongoException
1131+
*/
1132+
public List distinct( String key , DBObject query, ReadPreference readPrefs ){
10421133
DBObject c = BasicDBObjectBuilder.start()
10431134
.add( "distinct" , getName() )
10441135
.add( "key" , key )
10451136
.add( "query" , query )
10461137
.get();
10471138

1048-
CommandResult res = _db.command( c, getOptions(), getReadPreference() );
1139+
CommandResult res = _db.command( c, getOptions(), readPrefs );
10491140
res.throwOnError();
10501141
return (List)(res.get( "values" ));
10511142
}
1052-
1143+
10531144
/**
10541145
* performs a map reduce operation
10551146
* Runs the command in REPLACE output mode (saves to named collection)

src/main/com/mongodb/DBCursor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ public int count() {
578578
if ( _collection._db == null )
579579
throw new IllegalArgumentException( "why is _collection._db null" );
580580

581-
return (int)_collection.getCount(this._query, this._keysWanted);
581+
return (int)_collection.getCount(this._query, this._keysWanted, getReadPreference());
582582
}
583583

584584
/**
@@ -594,7 +594,7 @@ public int size() {
594594
if ( _collection._db == null )
595595
throw new IllegalArgumentException( "why is _collection._db null" );
596596

597-
return (int)_collection.getCount(this._query, this._keysWanted, this._limit, this._skip );
597+
return (int)_collection.getCount(this._query, this._keysWanted, this._limit, this._skip, getReadPreference() );
598598
}
599599

600600

0 commit comments

Comments
 (0)