Source

mod_gridfs / patches / gridfs-readpref

--- mongo-cxx-driver/src/mongo/client/gridfs.h	2012-08-13 21:38:00.000000000 +0400
+++ mongo-cxx-driver/src/mongo/client/gridfs.h	2012-12-18 14:13:46.000000000 +0400
@@ -103,22 +103,22 @@
         /**
          * returns a file object matching the query
          */
-        GridFile findFile( BSONObj query ) const;
+        GridFile findFile( BSONObj query , const string& readPrefMode = "" ) const;
 
         /**
          * equiv to findFile( { filename : filename } )
          */
-        GridFile findFile( const string& fileName ) const;
+        GridFile findFile( const string& fileName , const string& readPrefMode = "" ) const;
 
         /**
          * convenience method to get all the files
          */
-        auto_ptr<DBClientCursor> list() const;
+        auto_ptr<DBClientCursor> list( const string& readPrefMode = "" ) const;
 
         /**
          * convenience method to get all the files with a filter
          */
-        auto_ptr<DBClientCursor> list( BSONObj query ) const;
+        auto_ptr<DBClientCursor> list( BSONObj query , const string& readPrefMode = "" ) const;
 
     private:
         DBClientBase& _client;
@@ -194,12 +194,13 @@
         gridfs_offset write( const string& where ) const;
 
     private:
-        GridFile(const GridFS * grid , BSONObj obj );
+        GridFile(const GridFS * grid , BSONObj obj , const string& readPrefMode );
 
         void _exists() const;
 
         const GridFS * _grid;
         BSONObj        _obj;
+        string         _readPrefMode;
 
         friend class GridFS;
     };
--- mongo-cxx-driver/src/mongo/client/gridfs.cpp	2012-08-13 21:38:00.000000000 +0400
+++ mongo-cxx-driver/src/mongo/client/gridfs.cpp	2012-12-18 17:35:05.000000000 +0400
@@ -184,25 +184,35 @@
         }
     }
 
-    GridFile::GridFile(const GridFS * grid , BSONObj obj ) {
+    GridFile::GridFile(const GridFS * grid , BSONObj obj , const string& readPrefMode ) {
         _grid = grid;
         _obj = obj;
+        _readPrefMode = readPrefMode;
     }
 
-    GridFile GridFS::findFile( const string& fileName ) const {
-        return findFile( BSON( "filename" << fileName ) );
+    GridFile GridFS::findFile( const string& fileName, const string& readPrefMode ) const {
+        return findFile( BSON( "filename" << fileName ) , readPrefMode );
     };
 
-    GridFile GridFS::findFile( BSONObj query ) const {
-        query = BSON("query" << query << "orderby" << BSON("uploadDate" << -1));
-        return GridFile( this , _client.findOne( _filesNS.c_str() , query ) );
+    GridFile GridFS::findFile( BSONObj query, const string& readPrefMode ) const {
+        BSONObjBuilder b;
+        b.append("query", query);
+        b.append("orderby", BSON("uploadDate" << -1));
+        if (!readPrefMode.empty()) {
+            b.append("$readPreference", BSON("mode" << readPrefMode));
+        }
+        query = b.obj();
+        return GridFile( this , _client.findOne( _filesNS.c_str() , query ) , readPrefMode );
     }
 
-    auto_ptr<DBClientCursor> GridFS::list() const {
-        return _client.query( _filesNS.c_str() , BSONObj() );
+    auto_ptr<DBClientCursor> GridFS::list( const string& readPrefMode ) const {
+        return list( BSONObj() , readPrefMode );
     }
 
-    auto_ptr<DBClientCursor> GridFS::list( BSONObj o ) const {
+    auto_ptr<DBClientCursor> GridFS::list( BSONObj o, const string& readPrefMode ) const {
+        if (!readPrefMode.empty()) {
+        	o = BSON("query" << o << "$readPreference" << BSON("mode" << readPrefMode));
+        }
         return _client.query( _filesNS.c_str() , o );
     }
 
@@ -220,8 +230,12 @@
         BSONObjBuilder b;
         b.appendAs( _obj["_id"] , "files_id" );
         b.append( "n" , n );
+        BSONObj q = b.obj();
+        if (!_readPrefMode.empty()) {
+        	q = BSON("query" << q << "$readPreference" << BSON("mode" << _readPrefMode));
+        }
 
-        BSONObj o = _grid->_client.findOne( _grid->_chunksNS.c_str() , b.obj() );
+        BSONObj o = _grid->_client.findOne( _grid->_chunksNS.c_str() , q );
         uassert( 10014 ,  "chunk is empty!" , ! o.isEmpty() );
         return GridFSChunk(o);
     }