00001 00018 #pragma once 00019 00020 #include "../util/mmap.h" 00021 #include "../util/paths.h" 00022 00023 namespace mongo { 00024 00029 class MongoMMF : private MemoryMappedFile { 00030 public: 00031 MongoMMF(); 00032 virtual ~MongoMMF(); 00033 virtual void close(); 00034 00036 bool open(string fname, bool sequentialHint); 00037 00039 unsigned long long length() const { return MemoryMappedFile::length(); } 00040 00041 string filename() const { return MemoryMappedFile::filename(); } 00042 00043 void flush(bool sync) { MemoryMappedFile::flush(sync); } 00044 00045 /* Creates with length if DNE, otherwise uses existing file length, 00046 passed length. 00047 @param sequentialHint if true will be sequentially accessed 00048 @return true for ok 00049 */ 00050 bool create(string fname, unsigned long long& len, bool sequentialHint); 00051 00052 /* Get the "standard" view (which is the private one). 00053 @return the private view. 00054 */ 00055 void* getView() const { return _view_private; } 00056 00057 /* Get the "write" view (which is required for writing). 00058 @return the write view. 00059 */ 00060 void* view_write() const { return _view_write; } 00061 00062 00063 /* switch to _view_write. normally, this is a bad idea since your changes will not 00064 show up in _view_private if there have been changes there; thus the leading underscore 00065 as a tad of a "warning". but useful when done with some care, such as during 00066 initialization. 00067 */ 00068 static void* _switchToWritableView(void *private_ptr); 00069 00075 RelativePath relativePath() const { 00076 DEV assert( !_p._p.empty() ); 00077 return _p; 00078 } 00079 00080 int fileSuffixNo() const { return _fileSuffixNo; } 00081 00086 bool& willNeedRemap() { return _willNeedRemap; } 00087 00088 void remapThePrivateView(); 00089 00090 virtual bool isMongoMMF() { return true; } 00091 00092 private: 00093 00094 void *_view_write; 00095 void *_view_private; 00096 bool _willNeedRemap; 00097 RelativePath _p; // e.g. "somepath/dbname" 00098 int _fileSuffixNo; // e.g. 3. -1="ns" 00099 00100 void setPath(string pathAndFileName); 00101 bool finishOpening(); 00102 }; 00103 00106 class PointerToMMF : boost::noncopyable { 00107 public: 00108 PointerToMMF(); 00109 00113 void add(void *view, MongoMMF *f); 00114 00118 void remove(void *view); 00119 00125 MongoMMF* find(void *p, /*out*/ size_t& ofs); 00126 00128 mutex& _mutex() { return _m; } 00129 MongoMMF* find_inlock(void *p, /*out*/ size_t& ofs); 00130 00131 map<void*,MongoMMF*>::iterator finditer_inlock(void *p) { return _views.upper_bound(p); } 00132 00133 private: 00134 mutex _m; 00135 map<void*, MongoMMF*> _views; 00136 }; 00137 00138 // allows a pointer into any private view of a MongoMMF to be resolved to the MongoMMF object 00139 extern PointerToMMF privateViews; 00140 }