Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mem_file_system.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_MEM_FILE_SYSTEM_H_
20 #define PAGESPEED_KERNEL_BASE_MEM_FILE_SYSTEM_H_
21 
22 #include <map>
23 
31 #include "pagespeed/kernel/base/thread_annotations.h"
32 
33 namespace net_instaweb {
34 
35 class MessageHandler;
36 class MockTimer;
37 class ThreadSystem;
38 class Timer;
39 
48 class MemFileSystem : public FileSystem {
49  public:
51 
52  explicit MemFileSystem(ThreadSystem* threads, Timer* timer);
53  virtual ~MemFileSystem();
54 
55  virtual InputFile* OpenInputFile(const char* filename,
56  MessageHandler* message_handler);
57  virtual OutputFile* OpenOutputFileHelper(const char* filename,
58  bool append,
59  MessageHandler* message_handler);
60  virtual OutputFile* OpenTempFileHelper(const StringPiece& prefix_name,
61  MessageHandler* message_handle);
62 
63  virtual bool ListContents(const StringPiece& dir, StringVector* files,
64  MessageHandler* handler);
65  virtual bool MakeDir(const char* directory_path, MessageHandler* handler);
66  virtual bool RecursivelyMakeDir(const StringPiece& directory_path,
67  MessageHandler* handler);
68  virtual bool RemoveDir(const char* path, MessageHandler* handler);
69  virtual bool RemoveFile(const char* filename, MessageHandler* handler);
70  virtual bool RenameFileHelper(const char* old_file, const char* new_file,
71  MessageHandler* handler);
72 
75  virtual bool Atime(const StringPiece& path, int64* timestamp_sec,
76  MessageHandler* handler);
77  virtual bool Mtime(const StringPiece& path, int64* timestamp_sec,
78  MessageHandler* handler);
79  virtual bool Size(const StringPiece& path, int64* size,
80  MessageHandler* handler) const;
81  virtual BoolOrError Exists(const char* path, MessageHandler* handler);
82  virtual BoolOrError IsDir(const char* path, MessageHandler* handler);
83 
84  virtual BoolOrError TryLock(const StringPiece& lock_name,
85  MessageHandler* handler);
86  virtual BoolOrError TryLockWithTimeout(const StringPiece& lock_name,
87  int64 timeout_ms,
88  const Timer* timer,
89  MessageHandler* handler);
90  virtual bool BumpLockTimeout(const StringPiece& lock_name,
91  MessageHandler* handler);
92  virtual bool Unlock(const StringPiece& lock_name, MessageHandler* handler);
93 
95  void set_atime_enabled(bool enabled) {
96  ScopedMutex lock(all_else_mutex_.get());
97  atime_enabled_ = enabled;
98  }
99 
106  ScopedMutex lock(all_else_mutex_.get());
107  return advance_time_on_update_;
108  }
109  void set_advance_time_on_update(bool x, MockTimer* mock_timer) {
110  ScopedMutex lock(all_else_mutex_.get());
111  advance_time_on_update_ = x;
112  mock_timer_ = mock_timer;
113  }
114 
117  void Clear();
118 
120  void Disable() {
121  ScopedMutex lock(all_else_mutex_.get());
122  enabled_ = false;
123  }
124  void Enable() {
125  ScopedMutex lock(all_else_mutex_.get());
126  enabled_ = true;
127  }
128 
130  void ClearStats() {
131  ScopedMutex lock(all_else_mutex_.get());
132  num_input_file_opens_ = 0;
133  num_input_file_stats_ = 0;
134  num_output_file_opens_ = 0;
135  num_temp_file_opens_ = 0;
136  }
137  int num_input_file_opens() const {
138  ScopedMutex lock(all_else_mutex_.get());
139  return num_input_file_opens_;
140  }
141 
143  int num_input_file_stats() const {
144  ScopedMutex lock(all_else_mutex_.get());
145  return num_input_file_stats_;
146  }
147  int num_output_file_opens() const {
148  ScopedMutex lock(all_else_mutex_.get());
149  return num_output_file_opens_;
150  }
151  int num_temp_file_opens() const {
152  ScopedMutex lock(all_else_mutex_.get());
153  return num_temp_file_opens_;
154  }
155 
161  void set_write_callback(FileCallback* x) { write_callback_.reset(x); }
162 
163  virtual bool WriteFile(const char* filename,
164  const StringPiece& buffer,
165  MessageHandler* handler);
166  virtual bool WriteTempFile(const StringPiece& prefix_name,
167  const StringPiece& buffer,
168  GoogleString* filename,
169  MessageHandler* handler);
170 
171 
172  private:
173  inline void UpdateAtime(const StringPiece& path)
174  SHARED_LOCKS_REQUIRED(all_else_mutex_);
175  inline void UpdateMtime(const StringPiece& path)
176  SHARED_LOCKS_REQUIRED(all_else_mutex_);
177 
178  scoped_ptr<AbstractMutex> lock_map_mutex_;
179  scoped_ptr<AbstractMutex> all_else_mutex_;
180 
182  bool enabled_ GUARDED_BY(all_else_mutex_);
186  StringStringMap string_map_ GUARDED_BY(all_else_mutex_);
187  Timer* timer_;
189  MockTimer* mock_timer_ GUARDED_BY(all_else_mutex_);
190 
195  std::map<GoogleString, int64> atime_map_ GUARDED_BY(all_else_mutex_);
196  std::map<GoogleString, int64> mtime_map_ GUARDED_BY(all_else_mutex_);
197  int temp_file_index_ GUARDED_BY(all_else_mutex_);
200  std::map<GoogleString, int64> lock_map_ GUARDED_BY(lock_map_mutex_);
201  bool atime_enabled_ GUARDED_BY(all_else_mutex_);
202 
205  bool advance_time_on_update_ GUARDED_BY(all_else_mutex_);
206 
208  int num_input_file_opens_ GUARDED_BY(all_else_mutex_);
209  int num_input_file_stats_ GUARDED_BY(all_else_mutex_);
210  int num_output_file_opens_ GUARDED_BY(all_else_mutex_);
211  int num_temp_file_opens_ GUARDED_BY(all_else_mutex_);
212 
214  scoped_ptr<FileCallback> write_callback_;
215 
216 
217 };
218 
219 }
220 
221 #endif
Definition: mock_timer.h:33
virtual bool RecursivelyMakeDir(const StringPiece &directory_path, MessageHandler *handler)
void Disable()
Test-specific functionality to disable and re-enable the filesystem.
Definition: mem_file_system.h:120
Definition: file_system.h:35
virtual bool Size(const StringPiece &path, int64 *size, MessageHandler *handler) const
virtual bool WriteFile(const char *filename, const StringPiece &buffer, MessageHandler *handler)
Non-atomic. Use WriteFileAtomic() for atomic version.
Definition: scoped_ptr.h:30
virtual BoolOrError TryLock(const StringPiece &lock_name, MessageHandler *handler)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
#define ScopedMutex(x)
Definition: abstract_mutex.h:69
Helper class for lexically scoped mutexing.
Definition: abstract_mutex.h:46
virtual bool RemoveFile(const char *filename, MessageHandler *handler)
Like POSIX 'rm'.
int num_input_file_stats() const
returns number of times MTime was called.
Definition: mem_file_system.h:143
Definition: file_system.h:76
virtual bool WriteTempFile(const StringPiece &prefix_name, const StringPiece &buffer, GoogleString *filename, MessageHandler *handler)
virtual bool Unlock(const StringPiece &lock_name, MessageHandler *handler)
virtual BoolOrError TryLockWithTimeout(const StringPiece &lock_name, int64 timeout_ms, const Timer *timer, MessageHandler *handler)
void set_write_callback(FileCallback *x)
Definition: mem_file_system.h:161
virtual bool RemoveDir(const char *path, MessageHandler *handler)
Like POSIX 'rmdir', remove a directory only if it is empty.
virtual bool MakeDir(const char *directory_path, MessageHandler *handler)
Definition: file_system.h:96
virtual bool ListContents(const StringPiece &dir, StringVector *files, MessageHandler *handler)
void ClearStats()
Access statistics.
Definition: mem_file_system.h:130
virtual bool Mtime(const StringPiece &path, int64 *timestamp_sec, MessageHandler *handler)
Modified time. Time the file contents were modified.
virtual bool Atime(const StringPiece &path, int64 *timestamp_sec, MessageHandler *handler)
Definition: thread_system.h:40
Definition: message_handler.h:39
Definition: mem_file_system.h:48
virtual bool BumpLockTimeout(const StringPiece &lock_name, MessageHandler *handler)
virtual OutputFile * OpenOutputFileHelper(const char *filename, bool append, MessageHandler *message_handler)
virtual BoolOrError IsDir(const char *path, MessageHandler *handler)
Like POSIX 'test -d', checks if path exists and refers to a directory.
bool advance_time_on_update()
Definition: mem_file_system.h:105
virtual BoolOrError Exists(const char *path, MessageHandler *handler)
Like POSIX 'test -e', checks if path exists (is a file, directory, etc.).
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
Definition: callback.h:44
void set_atime_enabled(bool enabled)
When atime is disabled, reading a file will not update its atime.
Definition: mem_file_system.h:95
Definition: file_system.h:113