Source

mod_gridfs / patches / gridfs

--- 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-20 15:26:10.000000000 +0400
@@ -60,7 +60,7 @@
          * @param dbName - root database name
          * @param prefix - if you want your data somewhere besides <dbname>.fs
          */
-        GridFS( DBClientBase& client , const string& dbName , const string& prefix="fs" );
+        GridFS( DBClientBase& client , const string& dbName , const string& prefix="fs" , bool ensureIndexes = true );
         ~GridFS();
 
         /**
@@ -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-20 15:26:32.000000000 +0400
@@ -52,13 +52,15 @@
     }
 
 
-    GridFS::GridFS( DBClientBase& client , const string& dbName , const string& prefix ) : _client( client ) , _dbName( dbName ) , _prefix( prefix ) {
+    GridFS::GridFS( DBClientBase& client , const string& dbName , const string& prefix , const bool ensureIndexes ) : _client( client ) , _dbName( dbName ) , _prefix( prefix ) {
         _filesNS = dbName + "." + prefix + ".files";
         _chunksNS = dbName + "." + prefix + ".chunks";
         _chunkSize = DEFAULT_CHUNK_SIZE;
 
-        client.ensureIndex( _filesNS , BSON( "filename" << 1 ) );
-        client.ensureIndex( _chunksNS , BSON( "files_id" << 1 << "n" << 1 ) );
+        if (ensureIndexes) {
+            client.ensureIndex( _filesNS , BSON( "filename" << 1 ) );
+            client.ensureIndex( _chunksNS , BSON( "files_id" << 1 << "n" << 1 ) );
+        }
     }
 
     GridFS::~GridFS() {
@@ -184,25 +186,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 +232,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);
     }