Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The LevelDB Authors. All rights reserved. | 1 // Copyright (c) 2013 The LevelDB Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. | 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 | 4 |
| 5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ | 5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ |
| 6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ | 6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 kSyncParent, | 43 kSyncParent, |
| 44 kGetChildren, | 44 kGetChildren, |
| 45 kNumEntries | 45 kNumEntries |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 const char* MethodIDToString(MethodID method); | 48 const char* MethodIDToString(MethodID method); |
| 49 | 49 |
| 50 leveldb::Status MakeIOError(leveldb::Slice filename, | 50 leveldb::Status MakeIOError(leveldb::Slice filename, |
| 51 const char* message, | 51 const char* message, |
| 52 MethodID method, | 52 MethodID method, |
| 53 int saved_errno); | |
| 54 leveldb::Status MakeIOError(leveldb::Slice filename, | |
| 55 const char* message, | |
| 56 MethodID method, | |
| 57 base::PlatformFileError error); | 53 base::PlatformFileError error); |
| 58 leveldb::Status MakeIOError(leveldb::Slice filename, | 54 leveldb::Status MakeIOError(leveldb::Slice filename, |
| 59 const char* message, | 55 const char* message, |
| 60 MethodID method); | 56 MethodID method); |
| 61 | 57 |
| 62 enum ErrorParsingResult { | 58 enum ErrorParsingResult { |
| 63 METHOD_ONLY, | 59 METHOD_ONLY, |
| 64 METHOD_AND_PFE, | 60 METHOD_AND_PFE, |
| 65 METHOD_AND_ERRNO, | 61 METHOD_AND_ERRNO, |
| 66 NONE, | 62 NONE, |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 94 MethodID method) const = 0; | 90 MethodID method) const = 0; |
| 95 }; | 91 }; |
| 96 | 92 |
| 97 class WriteTracker { | 93 class WriteTracker { |
| 98 public: | 94 public: |
| 99 virtual void DidCreateNewFile(const std::string& fname) = 0; | 95 virtual void DidCreateNewFile(const std::string& fname) = 0; |
| 100 virtual bool DoesDirNeedSync(const std::string& fname) = 0; | 96 virtual bool DoesDirNeedSync(const std::string& fname) = 0; |
| 101 virtual void DidSyncDir(const std::string& fname) = 0; | 97 virtual void DidSyncDir(const std::string& fname) = 0; |
| 102 }; | 98 }; |
| 103 | 99 |
| 104 class ChromiumWritableFile : public leveldb::WritableFile { | 100 class ChromiumEnvDelegate |
|
jsbell
2013/12/12 19:03:14
It's unfortunate to introduce this just for unit t
cmumford
2013/12/12 20:08:39
I agree. Yes, I did learn a bit more about gtest's
| |
| 101 { | |
| 105 public: | 102 public: |
| 106 ChromiumWritableFile(const std::string& fname, | 103 virtual ~ChromiumEnvDelegate() { } |
| 107 FILE* f, | 104 virtual void DidSyncDir(const std::string& fname) { } |
| 108 const UMALogger* uma_logger, | |
| 109 WriteTracker* tracker, | |
| 110 bool make_backup); | |
| 111 virtual ~ChromiumWritableFile(); | |
| 112 virtual leveldb::Status Append(const leveldb::Slice& data); | |
| 113 virtual leveldb::Status Close(); | |
| 114 virtual leveldb::Status Flush(); | |
| 115 virtual leveldb::Status Sync(); | |
| 116 | |
| 117 private: | |
| 118 enum Type { | |
| 119 kManifest, | |
| 120 kTable, | |
| 121 kOther | |
| 122 }; | |
| 123 leveldb::Status SyncParent(); | |
| 124 | |
| 125 std::string filename_; | |
| 126 FILE* file_; | |
| 127 const UMALogger* uma_logger_; | |
| 128 WriteTracker* tracker_; | |
| 129 Type file_type_; | |
| 130 std::string parent_dir_; | |
| 131 bool make_backup_; | |
| 132 }; | 105 }; |
| 133 | 106 |
| 134 class ChromiumEnv : public leveldb::Env, | 107 class ChromiumEnv : public leveldb::Env, |
| 135 public UMALogger, | 108 public UMALogger, |
| 136 public RetrierProvider, | 109 public RetrierProvider, |
| 137 public WriteTracker { | 110 public WriteTracker { |
| 138 public: | 111 public: |
| 139 ChromiumEnv(); | 112 |
| 113 static bool MakeBackup(const std::string& fname); | |
| 114 static base::FilePath CreateFilePath(const std::string& file_path); | |
| 115 static const char* PlatformFileErrorString(const ::base::PlatformFileError& er ror); | |
| 116 static bool HasTableExtension(const base::FilePath& path); | |
| 140 virtual ~ChromiumEnv(); | 117 virtual ~ChromiumEnv(); |
| 141 | 118 |
| 142 virtual leveldb::Status NewSequentialFile(const std::string& fname, | |
| 143 leveldb::SequentialFile** result); | |
| 144 virtual leveldb::Status NewRandomAccessFile( | |
| 145 const std::string& fname, | |
| 146 leveldb::RandomAccessFile** result); | |
| 147 virtual leveldb::Status NewWritableFile(const std::string& fname, | |
| 148 leveldb::WritableFile** result); | |
| 149 virtual bool FileExists(const std::string& fname); | 119 virtual bool FileExists(const std::string& fname); |
| 150 virtual leveldb::Status GetChildren(const std::string& dir, | 120 virtual leveldb::Status GetChildren(const std::string& dir, |
| 151 std::vector<std::string>* result); | 121 std::vector<std::string>* result); |
| 152 virtual leveldb::Status DeleteFile(const std::string& fname); | 122 virtual leveldb::Status DeleteFile(const std::string& fname); |
| 153 virtual leveldb::Status CreateDir(const std::string& name); | 123 virtual leveldb::Status CreateDir(const std::string& name); |
| 154 virtual leveldb::Status DeleteDir(const std::string& name); | 124 virtual leveldb::Status DeleteDir(const std::string& name); |
| 155 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size); | 125 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size); |
| 156 virtual leveldb::Status RenameFile(const std::string& src, | 126 virtual leveldb::Status RenameFile(const std::string& src, |
| 157 const std::string& dst); | 127 const std::string& dst); |
| 158 virtual leveldb::Status LockFile(const std::string& fname, | 128 virtual leveldb::Status LockFile(const std::string& fname, |
| 159 leveldb::FileLock** lock); | 129 leveldb::FileLock** lock); |
| 160 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock); | 130 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock); |
| 161 virtual void Schedule(void (*function)(void*), void* arg); | 131 virtual void Schedule(void (*function)(void*), void* arg); |
| 162 virtual void StartThread(void (*function)(void* arg), void* arg); | 132 virtual void StartThread(void (*function)(void* arg), void* arg); |
| 163 virtual leveldb::Status GetTestDirectory(std::string* path); | 133 virtual leveldb::Status GetTestDirectory(std::string* path); |
| 164 virtual leveldb::Status NewLogger(const std::string& fname, | |
| 165 leveldb::Logger** result); | |
| 166 virtual uint64_t NowMicros(); | 134 virtual uint64_t NowMicros(); |
| 167 virtual void SleepForMicroseconds(int micros); | 135 virtual void SleepForMicroseconds(int micros); |
| 136 void SetDelegate(ChromiumEnvDelegate* delegate); | |
| 168 | 137 |
| 169 protected: | 138 protected: |
| 139 ChromiumEnv(); | |
| 140 | |
| 170 virtual void DidCreateNewFile(const std::string& fname); | 141 virtual void DidCreateNewFile(const std::string& fname); |
| 171 virtual bool DoesDirNeedSync(const std::string& fname); | 142 virtual bool DoesDirNeedSync(const std::string& fname); |
| 172 virtual void DidSyncDir(const std::string& fname); | 143 virtual void DidSyncDir(const std::string& fname); |
| 144 virtual base::PlatformFileError GetDirectoryEntries( | |
| 145 const base::FilePath& dir_param, | |
| 146 std::vector<base::FilePath>* result) const = 0; | |
| 147 virtual void RecordErrorAt(MethodID method) const; | |
| 148 virtual void RecordOSError(MethodID method, int saved_errno) const; | |
| 149 virtual void RecordOSError(MethodID method, | |
| 150 base::PlatformFileError error) const; | |
| 151 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const; | |
| 152 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const; | |
| 173 | 153 |
| 174 std::string name_; | 154 std::string name_; |
| 175 bool make_backup_; | 155 bool make_backup_; |
| 156 scoped_ptr<ChromiumEnvDelegate> env_delegate_; | |
| 176 | 157 |
| 177 private: | 158 private: |
| 178 // File locks may not be exclusive within a process (e.g. on POSIX). Track | 159 // File locks may not be exclusive within a process (e.g. on POSIX). Track |
| 179 // locks held by the ChromiumEnv to prevent access within the process. | 160 // locks held by the ChromiumEnv to prevent access within the process. |
| 180 class LockTable { | 161 class LockTable { |
| 181 public: | 162 public: |
| 182 bool Insert(const std::string& fname) { | 163 bool Insert(const std::string& fname) { |
| 183 leveldb::MutexLock l(&mu_); | 164 leveldb::MutexLock l(&mu_); |
| 184 return locked_files_.insert(fname).second; | 165 return locked_files_.insert(fname).second; |
| 185 } | 166 } |
| 186 bool Remove(const std::string& fname) { | 167 bool Remove(const std::string& fname) { |
| 187 leveldb::MutexLock l(&mu_); | 168 leveldb::MutexLock l(&mu_); |
| 188 return locked_files_.erase(fname) == 1; | 169 return locked_files_.erase(fname) == 1; |
| 189 } | 170 } |
| 190 private: | 171 private: |
| 191 leveldb::port::Mutex mu_; | 172 leveldb::port::Mutex mu_; |
| 192 std::set<std::string> locked_files_; | 173 std::set<std::string> locked_files_; |
| 193 }; | 174 }; |
| 194 | 175 |
| 195 std::map<std::string, bool> needs_sync_map_; | 176 std::map<std::string, bool> needs_sync_map_; |
| 196 base::Lock map_lock_; | 177 base::Lock map_lock_; |
| 197 | 178 |
| 198 const int kMaxRetryTimeMillis; | 179 const int kMaxRetryTimeMillis; |
| 199 // BGThread() is the body of the background thread | 180 // BGThread() is the body of the background thread |
| 200 void BGThread(); | 181 void BGThread(); |
| 201 static void BGThreadWrapper(void* arg) { | 182 static void BGThreadWrapper(void* arg) { |
| 202 reinterpret_cast<ChromiumEnv*>(arg)->BGThread(); | 183 reinterpret_cast<ChromiumEnv*>(arg)->BGThread(); |
| 203 } | 184 } |
| 204 | 185 |
| 205 virtual void RecordErrorAt(MethodID method) const; | |
| 206 virtual void RecordOSError(MethodID method, int saved_errno) const; | |
| 207 virtual void RecordOSError(MethodID method, | |
| 208 base::PlatformFileError error) const; | |
| 209 virtual void RecordBackupResult(bool result) const; | 186 virtual void RecordBackupResult(bool result) const; |
| 210 void RestoreIfNecessary(const std::string& dir, | 187 void RestoreIfNecessary(const std::string& dir, |
| 211 std::vector<std::string>* children); | 188 std::vector<std::string>* children); |
| 212 base::FilePath RestoreFromBackup(const base::FilePath& base_name); | 189 base::FilePath RestoreFromBackup(const base::FilePath& base_name); |
| 213 void RecordOpenFilesLimit(const std::string& type); | |
| 214 void RecordLockFileAncestors(int num_missing_ancestors) const; | 190 void RecordLockFileAncestors(int num_missing_ancestors) const; |
| 215 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const; | |
| 216 base::HistogramBase* GetMethodIOErrorHistogram() const; | 191 base::HistogramBase* GetMethodIOErrorHistogram() const; |
| 217 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const; | |
| 218 base::HistogramBase* GetLockFileAncestorHistogram() const; | 192 base::HistogramBase* GetLockFileAncestorHistogram() const; |
| 219 | 193 |
| 220 // RetrierProvider implementation. | 194 // RetrierProvider implementation. |
| 221 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; } | 195 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; } |
| 222 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const; | 196 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const; |
| 223 virtual base::HistogramBase* GetRecoveredFromErrorHistogram( | 197 virtual base::HistogramBase* GetRecoveredFromErrorHistogram( |
| 224 MethodID method) const; | 198 MethodID method) const; |
| 225 | 199 |
| 226 base::FilePath test_directory_; | 200 base::FilePath test_directory_; |
| 227 | 201 |
| 228 ::base::Lock mu_; | 202 ::base::Lock mu_; |
| 229 ::base::ConditionVariable bgsignal_; | 203 ::base::ConditionVariable bgsignal_; |
| 230 bool started_bgthread_; | 204 bool started_bgthread_; |
| 231 | 205 |
| 232 // Entry per Schedule() call | 206 // Entry per Schedule() call |
| 233 struct BGItem { | 207 struct BGItem { |
| 234 void* arg; | 208 void* arg; |
| 235 void (*function)(void*); | 209 void (*function)(void*); |
| 236 }; | 210 }; |
| 237 typedef std::deque<BGItem> BGQueue; | 211 typedef std::deque<BGItem> BGQueue; |
| 238 BGQueue queue_; | 212 BGQueue queue_; |
| 239 LockTable locks_; | 213 LockTable locks_; |
| 240 }; | 214 }; |
| 241 | 215 |
| 242 } // namespace leveldb_env | 216 } // namespace leveldb_env |
| 243 | 217 |
| 244 #endif | 218 #endif |
| OLD | NEW |