00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_MEM_FILE_SYSTEM_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_MEM_FILE_SYSTEM_H_
00021
00022 #include <map>
00023
00024 #include "base/scoped_ptr.h"
00025 #include "net/instaweb/util/public/basictypes.h"
00026 #include "net/instaweb/util/public/file_system.h"
00027 #include "net/instaweb/util/public/string.h"
00028 #include "net/instaweb/util/public/string_util.h"
00029
00030 namespace net_instaweb {
00031
00032 class AbstractMutex;
00033 class MessageHandler;
00034 class MockTimer;
00035 class ThreadSystem;
00036 class Timer;
00037
00046 class MemFileSystem : public FileSystem {
00047 public:
00048 explicit MemFileSystem(ThreadSystem* threads, Timer* timer);
00049 virtual ~MemFileSystem();
00050
00051 virtual InputFile* OpenInputFile(const char* filename,
00052 MessageHandler* message_handler);
00053 virtual OutputFile* OpenOutputFileHelper(const char* filename,
00054 MessageHandler* message_handler);
00055 virtual OutputFile* OpenTempFileHelper(const StringPiece& prefix_name,
00056 MessageHandler* message_handle);
00057
00058 virtual bool ListContents(const StringPiece& dir, StringVector* files,
00059 MessageHandler* handler);
00060 virtual bool MakeDir(const char* directory_path, MessageHandler* handler);
00061 virtual bool RecursivelyMakeDir(const StringPiece& directory_path,
00062 MessageHandler* handler);
00063 virtual bool RemoveFile(const char* filename, MessageHandler* handler);
00064 virtual bool RenameFileHelper(const char* old_file, const char* new_file,
00065 MessageHandler* handler);
00066
00069 virtual bool Atime(const StringPiece& path, int64* timestamp_sec,
00070 MessageHandler* handler);
00071 virtual bool Mtime(const StringPiece& path, int64* timestamp_sec,
00072 MessageHandler* handler);
00073 virtual bool Size(const StringPiece& path, int64* size,
00074 MessageHandler* handler);
00075 virtual BoolOrError Exists(const char* path, MessageHandler* handler);
00076 virtual BoolOrError IsDir(const char* path, MessageHandler* handler);
00077
00078 virtual BoolOrError TryLock(const StringPiece& lock_name,
00079 MessageHandler* handler);
00080 virtual BoolOrError TryLockWithTimeout(const StringPiece& lock_name,
00081 int64 timeout_ms,
00082 MessageHandler* handler);
00083 virtual bool Unlock(const StringPiece& lock_name, MessageHandler* handler);
00084
00086 void set_atime_enabled(bool enabled) { atime_enabled_ = enabled; }
00087
00093 bool advance_time_on_update() { return advance_time_on_update_; }
00094 void set_advance_time_on_update(bool x, MockTimer* mock_timer) {
00095 advance_time_on_update_ = x;
00096 mock_timer_ = mock_timer;
00097 }
00098
00101 void Clear();
00102
00104 void Disable() { enabled_ = false; }
00105 void Enable() { enabled_ = true; }
00106
00108 void ClearStats() {
00109 num_input_file_opens_ = 0;
00110 num_input_file_stats_ = 0;
00111 num_output_file_opens_ = 0;
00112 num_temp_file_opens_ = 0;
00113 }
00114 int num_input_file_opens() const { return num_input_file_opens_; }
00115
00117 int num_input_file_stats() const { return num_input_file_stats_; }
00118 int num_output_file_opens() const { return num_output_file_opens_; }
00119 int num_temp_file_opens() const { return num_temp_file_opens_; }
00120
00121 private:
00123 inline void UpdateAtime(const StringPiece& path);
00124 inline void UpdateMtime(const StringPiece& path);
00125
00126 scoped_ptr<AbstractMutex> lock_map_mutex_;
00127 scoped_ptr<AbstractMutex> all_else_mutex_;
00128
00129 bool enabled_;
00130 StringStringMap string_map_;
00131 Timer* timer_;
00132 MockTimer* mock_timer_;
00133
00138 std::map<GoogleString, int64> atime_map_;
00139 std::map<GoogleString, int64> mtime_map_;
00140 int temp_file_index_;
00143 std::map<GoogleString, int64> lock_map_;
00144 bool atime_enabled_;
00145
00148 bool advance_time_on_update_;
00149
00151 int num_input_file_opens_;
00152 int num_input_file_stats_;
00153 int num_output_file_opens_;
00154 int num_temp_file_opens_;
00155
00156 DISALLOW_COPY_AND_ASSIGN(MemFileSystem);
00157 };
00158
00159 }
00160
00161 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_MEM_FILE_SYSTEM_H_