// todo: this is called a lot. streamline the common case MongoDataFile* Database::getFile( int n, int sizeNeeded , bool preallocateOnly) { assert(this); DEV assertDbAtLeastReadLocked(this); namespaceIndex.init(); if ( n < 0 || n >= DiskLoc::MaxFiles ) { out() << "getFile(): n=" << n << endl; massert( 10295 , "getFile(): bad file number value (corrupt db?): run repair", false); } DEV { if ( n > 100 ) { out() << "getFile(): n=" << n << endl; } } MongoDataFile* p = 0; if ( !preallocateOnly ) { while ( n >= (int) _files.size() ) { assert(this); if( !Lock::isWriteLocked(this->name) ) { log() << "error: getFile() called in a read lock, yet file to return is not yet open" << endl; log() << " getFile(" << n << ") _files.size:" <<_files.size() << ' ' << fileName(n).string() << endl; log() << " context ns: " << cc().ns() << " openallfiles:" << _openAllFiles << endl; assert(false); } _files.push_back(0); } p = _files[n]; } if ( p == 0 ) { assertDbWriteLocked(this); boost::filesystem::path fullName = fileName( n ); string fullNameString = fullName.string(); p = new MongoDataFile(n); int minSize = 0; if ( n != 0 && _files[ n - 1 ] ) minSize = _files[ n - 1 ]->getHeader()->fileLength; if ( sizeNeeded + DataFileHeader::HeaderSize > minSize ) minSize = sizeNeeded + DataFileHeader::HeaderSize; try { p->open( fullNameString.c_str(), minSize, preallocateOnly ); } catch ( AssertionException& ) { delete p; throw; } if ( preallocateOnly ) delete p; else _files[n] = p; } return preallocateOnly ? 0 : p; }
MongoDataFile* Database::getFile( int n, int sizeNeeded , bool preallocateOnly) { assert(this); namespaceIndex.init(); if ( n < 0 || n >= DiskLoc::MaxFiles ) { out() << "getFile(): n=" << n << endl; massert( 10295 , "getFile(): bad file number value (corrupt db?): run repair", false); } DEV { if ( n > 100 ) out() << "getFile(): n=" << n << "?" << endl; } MongoDataFile* p = 0; if ( !preallocateOnly ) { while ( n >= (int) files.size() ) files.push_back(0); p = files[n]; } if ( p == 0 ) { boost::filesystem::path fullName = fileName( n ); string fullNameString = fullName.string(); p = new MongoDataFile(n); int minSize = 0; if ( n != 0 && files[ n - 1 ] ) minSize = files[ n - 1 ]->getHeader()->fileLength; if ( sizeNeeded + DataFileHeader::HeaderSize > minSize ) minSize = sizeNeeded + DataFileHeader::HeaderSize; try { p->open( fullNameString.c_str(), minSize, preallocateOnly ); } catch ( AssertionException& ) { delete p; throw; } if ( preallocateOnly ) delete p; else files[n] = p; } return preallocateOnly ? 0 : p; }