Page Speed Optimization Libraries  1.2.24.1
net/instaweb/util/public/mem_file_system.h
Go to the documentation of this file.
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                                          MessageHandler* handler);
00085   virtual bool Unlock(const StringPiece& lock_name, MessageHandler* handler);
00086 
00088   void set_atime_enabled(bool enabled) { atime_enabled_ = enabled; }
00089 
00095   bool advance_time_on_update() { return advance_time_on_update_; }
00096   void set_advance_time_on_update(bool x, MockTimer* mock_timer) {
00097     advance_time_on_update_ = x;
00098     mock_timer_ = mock_timer;
00099   }
00100 
00103   void Clear();
00104 
00106   void Disable() { enabled_ = false; }
00107   void Enable() { enabled_ = true; }
00108 
00110   void ClearStats() {
00111     num_input_file_opens_ = 0;
00112     num_input_file_stats_ = 0;
00113     num_output_file_opens_ = 0;
00114     num_temp_file_opens_ = 0;
00115   }
00116   int num_input_file_opens() const { return num_input_file_opens_; }
00117 
00119   int num_input_file_stats() const { return num_input_file_stats_; }
00120   int num_output_file_opens() const { return num_output_file_opens_; }
00121   int num_temp_file_opens() const { return num_temp_file_opens_; }
00122 
00123  private:
00125   inline void UpdateAtime(const StringPiece& path);
00126   inline void UpdateMtime(const StringPiece& path);
00127 
00128   scoped_ptr<AbstractMutex> lock_map_mutex_; 
00129   scoped_ptr<AbstractMutex> all_else_mutex_; 
00130 
00131   bool enabled_; 
00132 
00133 
00134 
00135   StringStringMap string_map_;
00136   Timer* timer_;
00137   MockTimer* mock_timer_; 
00138 
00143   std::map<GoogleString, int64> atime_map_;
00144   std::map<GoogleString, int64> mtime_map_;
00145   int temp_file_index_;
00148   std::map<GoogleString, int64> lock_map_;
00149   bool atime_enabled_;
00150 
00153   bool advance_time_on_update_;
00154 
00156   int num_input_file_opens_;
00157   int num_input_file_stats_;
00158   int num_output_file_opens_;
00159   int num_temp_file_opens_;
00160 
00161   DISALLOW_COPY_AND_ASSIGN(MemFileSystem);
00162 };
00163 
00164 }  
00165 
00166 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_MEM_FILE_SYSTEM_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines