Page Speed Optimization Libraries
1.5.27.2
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 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 "net/instaweb/util/public/basictypes.h" 00025 #include "net/instaweb/util/public/file_system.h" 00026 #include "net/instaweb/util/public/scoped_ptr.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 bool append, 00055 MessageHandler* message_handler); 00056 virtual OutputFile* OpenTempFileHelper(const StringPiece& prefix_name, 00057 MessageHandler* message_handle); 00058 00059 virtual bool ListContents(const StringPiece& dir, StringVector* files, 00060 MessageHandler* handler); 00061 virtual bool MakeDir(const char* directory_path, MessageHandler* handler); 00062 virtual bool RecursivelyMakeDir(const StringPiece& directory_path, 00063 MessageHandler* handler); 00064 virtual bool RemoveDir(const char* path, MessageHandler* handler); 00065 virtual bool RemoveFile(const char* filename, MessageHandler* handler); 00066 virtual bool RenameFileHelper(const char* old_file, const char* new_file, 00067 MessageHandler* handler); 00068 00071 virtual bool Atime(const StringPiece& path, int64* timestamp_sec, 00072 MessageHandler* handler); 00073 virtual bool Mtime(const StringPiece& path, int64* timestamp_sec, 00074 MessageHandler* handler); 00075 virtual bool Size(const StringPiece& path, int64* size, 00076 MessageHandler* handler); 00077 virtual BoolOrError Exists(const char* path, MessageHandler* handler); 00078 virtual BoolOrError IsDir(const char* path, MessageHandler* handler); 00079 00080 virtual BoolOrError TryLock(const StringPiece& lock_name, 00081 MessageHandler* handler); 00082 virtual BoolOrError TryLockWithTimeout(const StringPiece& lock_name, 00083 int64 timeout_ms, 00084 const Timer* timer, 00085 MessageHandler* handler); 00086 virtual bool Unlock(const StringPiece& lock_name, MessageHandler* handler); 00087 00089 void set_atime_enabled(bool enabled) { atime_enabled_ = enabled; } 00090 00096 bool advance_time_on_update() { return advance_time_on_update_; } 00097 void set_advance_time_on_update(bool x, MockTimer* mock_timer) { 00098 advance_time_on_update_ = x; 00099 mock_timer_ = mock_timer; 00100 } 00101 00104 void Clear(); 00105 00107 void Disable() { enabled_ = false; } 00108 void Enable() { enabled_ = true; } 00109 00111 void ClearStats() { 00112 num_input_file_opens_ = 0; 00113 num_input_file_stats_ = 0; 00114 num_output_file_opens_ = 0; 00115 num_temp_file_opens_ = 0; 00116 } 00117 int num_input_file_opens() const { return num_input_file_opens_; } 00118 00120 int num_input_file_stats() const { return num_input_file_stats_; } 00121 int num_output_file_opens() const { return num_output_file_opens_; } 00122 int num_temp_file_opens() const { return num_temp_file_opens_; } 00123 00124 private: 00126 inline void UpdateAtime(const StringPiece& path); 00127 inline void UpdateMtime(const StringPiece& path); 00128 00129 scoped_ptr<AbstractMutex> lock_map_mutex_; 00130 scoped_ptr<AbstractMutex> all_else_mutex_; 00131 00132 bool enabled_; 00133 00134 00135 00136 StringStringMap string_map_; 00137 Timer* timer_; 00138 MockTimer* mock_timer_; 00139 00144 std::map<GoogleString, int64> atime_map_; 00145 std::map<GoogleString, int64> mtime_map_; 00146 int temp_file_index_; 00149 std::map<GoogleString, int64> lock_map_; 00150 bool atime_enabled_; 00151 00154 bool advance_time_on_update_; 00155 00157 int num_input_file_opens_; 00158 int num_input_file_stats_; 00159 int num_output_file_opens_; 00160 int num_temp_file_opens_; 00161 00162 DISALLOW_COPY_AND_ASSIGN(MemFileSystem); 00163 }; 00164 00165 } 00166 00167 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_MEM_FILE_SYSTEM_H_