Skip to content

Commit 7d0c537

Browse files
committed
JAVA-401: Catch and log exception on ensuring indices for GridFS collections. Exceptions can happen if the MongoClient is a direct connection to a secondary, so this can happen under normal circumstances.
1 parent 178e280 commit 7d0c537

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/main/com/mongodb/gridfs/CLI.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.mongodb.DBObject;
2222
import com.mongodb.Mongo;
2323
import com.mongodb.MongoClient;
24+
import com.mongodb.MongoClientURI;
25+
import com.mongodb.ReadPreference;
2426
import com.mongodb.util.Util;
2527

2628
import java.io.File;
@@ -37,24 +39,24 @@ public class CLI {
3739
* Dumps usage info to stdout
3840
*/
3941
private static void printUsage() {
40-
System.out.println("Usage : [--bucket bucketname] action");
42+
System.out.println("Usage : [--db database] action");
4143
System.out.println(" where action is one of:");
4244
System.out.println(" list : lists all files in the store");
4345
System.out.println(" put filename : puts the file filename into the store");
4446
System.out.println(" get filename1 filename2 : gets filename1 from store and sends to filename2");
4547
System.out.println(" md5 filename : does an md5 hash on a file in the db (for testing)");
4648
}
4749

48-
private static String host = "127.0.0.1";
4950
private static String db = "test";
50-
51+
private static String uri = "mongodb://127.0.0.1";
5152
private static Mongo _mongo = null;
5253

5354
@SuppressWarnings("deprecation")
5455
private static Mongo getMongo()
5556
throws Exception {
56-
if ( _mongo == null )
57-
_mongo = new MongoClient( host );
57+
if ( _mongo == null ) {
58+
_mongo = new MongoClient(new MongoClientURI(uri));
59+
}
5860
return _mongo;
5961
}
6062

@@ -73,8 +75,6 @@ public static void main(String[] args) throws Exception {
7375
return;
7476
}
7577

76-
Mongo m = null;
77-
7878
for ( int i=0; i<args.length; i++ ){
7979
String s = args[i];
8080

@@ -85,7 +85,13 @@ public static void main(String[] args) throws Exception {
8585
}
8686

8787
if ( s.equals( "--host" ) ){
88-
host = args[i+1];
88+
uri = "mongodb://" + args[i+1];
89+
i++;
90+
continue;
91+
}
92+
93+
if ( s.equals( "--uri" ) ){
94+
uri = args[i+1];
8995
i++;
9096
continue;
9197
}

src/main/com/mongodb/gridfs/GridFS.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import java.io.InputStream;
2626
import java.util.ArrayList;
2727
import java.util.List;
28+
import java.util.logging.Logger;
2829

30+
import com.mongodb.MongoException;
2931
import org.bson.types.ObjectId;
3032

3133
import com.mongodb.BasicDBObject;
@@ -43,6 +45,7 @@
4345
* @dochub gridfs
4446
*/
4547
public class GridFS {
48+
private static final Logger LOGGER = Logger.getLogger( "com.mongodb.gridfs" );
4649

4750
/**
4851
* file's chunk size
@@ -91,11 +94,16 @@ public GridFS(DB db, String bucket) {
9194
_chunkCollection = _db.getCollection( _bucketName + ".chunks" );
9295

9396
// ensure standard indexes as long as collections are small
94-
if (_filesCollection.count() < 1000)
95-
_filesCollection.ensureIndex( BasicDBObjectBuilder.start().add( "filename" , 1 ).add( "uploadDate" , 1 ).get() );
96-
if (_chunkCollection.count() < 1000)
97-
_chunkCollection.ensureIndex( BasicDBObjectBuilder.start().add( "files_id" , 1 ).add( "n" , 1 ).get() , BasicDBObjectBuilder.start().add( "unique" , 1 ).get() );
98-
97+
try {
98+
if (_filesCollection.count() < 1000) {
99+
_filesCollection.ensureIndex( BasicDBObjectBuilder.start().add( "filename" , 1 ).add( "uploadDate" , 1 ).get() );
100+
}
101+
if (_chunkCollection.count() < 1000) {
102+
_chunkCollection.ensureIndex( BasicDBObjectBuilder.start().add( "files_id" , 1 ).add( "n" , 1 ).get() , BasicDBObjectBuilder.start().add( "unique" , 1 ).get() );
103+
}
104+
} catch (MongoException e) {
105+
LOGGER.info(String.format("Unable to ensure indices on GridFS collections in database %s", db.getName()));
106+
}
99107
_filesCollection.setObjectClass( GridFSDBFile.class );
100108
}
101109

0 commit comments

Comments
 (0)