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 <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 }; | 80 }; |
81 | 81 |
82 class RetrierProvider { | 82 class RetrierProvider { |
83 public: | 83 public: |
84 virtual int MaxRetryTimeMillis() const = 0; | 84 virtual int MaxRetryTimeMillis() const = 0; |
85 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0; | 85 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0; |
86 virtual base::HistogramBase* GetRecoveredFromErrorHistogram( | 86 virtual base::HistogramBase* GetRecoveredFromErrorHistogram( |
87 MethodID method) const = 0; | 87 MethodID method) const = 0; |
88 }; | 88 }; |
89 | 89 |
90 class WriteTracker { | |
91 public: | |
92 virtual void DidCreateNewFile(const std::string& fname) = 0; | |
93 virtual bool DoesDirNeedSync(const std::string& fname) = 0; | |
94 virtual void DidSyncDir(const std::string& fname) = 0; | |
95 }; | |
96 | |
97 class ChromiumEnv : public leveldb::Env, | 90 class ChromiumEnv : public leveldb::Env, |
98 public UMALogger, | 91 public UMALogger, |
99 public RetrierProvider, | 92 public RetrierProvider { |
100 public WriteTracker { | |
101 public: | 93 public: |
102 ChromiumEnv(); | 94 ChromiumEnv(); |
103 | 95 |
104 typedef void(ScheduleFunc)(void*); | 96 typedef void(ScheduleFunc)(void*); |
105 | 97 |
106 static bool MakeBackup(const std::string& fname); | 98 static bool MakeBackup(const std::string& fname); |
107 virtual ~ChromiumEnv(); | 99 virtual ~ChromiumEnv(); |
108 | 100 |
109 virtual bool FileExists(const std::string& fname); | 101 virtual bool FileExists(const std::string& fname); |
110 virtual leveldb::Status GetChildren(const std::string& dir, | 102 virtual leveldb::Status GetChildren(const std::string& dir, |
(...skipping 18 matching lines...) Expand all Loading... |
129 const std::string& fname, | 121 const std::string& fname, |
130 leveldb::RandomAccessFile** result); | 122 leveldb::RandomAccessFile** result); |
131 virtual leveldb::Status NewWritableFile(const std::string& fname, | 123 virtual leveldb::Status NewWritableFile(const std::string& fname, |
132 leveldb::WritableFile** result); | 124 leveldb::WritableFile** result); |
133 virtual leveldb::Status NewAppendableFile(const std::string& fname, | 125 virtual leveldb::Status NewAppendableFile(const std::string& fname, |
134 leveldb::WritableFile** result); | 126 leveldb::WritableFile** result); |
135 virtual leveldb::Status NewLogger(const std::string& fname, | 127 virtual leveldb::Status NewLogger(const std::string& fname, |
136 leveldb::Logger** result); | 128 leveldb::Logger** result); |
137 | 129 |
138 protected: | 130 protected: |
139 virtual void DidSyncDir(const std::string& fname); | |
140 | |
141 std::string name_; | 131 std::string name_; |
142 bool make_backup_; | 132 bool make_backup_; |
143 | 133 |
144 private: | 134 private: |
145 static const char* FileErrorString(base::File::Error error); | 135 static const char* FileErrorString(base::File::Error error); |
146 | 136 |
147 virtual void DidCreateNewFile(const std::string& fname); | |
148 virtual bool DoesDirNeedSync(const std::string& fname); | |
149 virtual void RecordErrorAt(MethodID method) const; | 137 virtual void RecordErrorAt(MethodID method) const; |
150 virtual void RecordOSError(MethodID method, | 138 virtual void RecordOSError(MethodID method, |
151 base::File::Error error) const; | 139 base::File::Error error) const; |
152 void RecordOpenFilesLimit(const std::string& type); | 140 void RecordOpenFilesLimit(const std::string& type); |
153 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const; | 141 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const; |
154 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const; | 142 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const; |
155 | 143 |
156 // File locks may not be exclusive within a process (e.g. on POSIX). Track | 144 // File locks may not be exclusive within a process (e.g. on POSIX). Track |
157 // locks held by the ChromiumEnv to prevent access within the process. | 145 // locks held by the ChromiumEnv to prevent access within the process. |
158 class LockTable { | 146 class LockTable { |
159 public: | 147 public: |
160 bool Insert(const std::string& fname) { | 148 bool Insert(const std::string& fname) { |
161 leveldb::MutexLock l(&mu_); | 149 leveldb::MutexLock l(&mu_); |
162 return locked_files_.insert(fname).second; | 150 return locked_files_.insert(fname).second; |
163 } | 151 } |
164 bool Remove(const std::string& fname) { | 152 bool Remove(const std::string& fname) { |
165 leveldb::MutexLock l(&mu_); | 153 leveldb::MutexLock l(&mu_); |
166 return locked_files_.erase(fname) == 1; | 154 return locked_files_.erase(fname) == 1; |
167 } | 155 } |
168 private: | 156 private: |
169 leveldb::port::Mutex mu_; | 157 leveldb::port::Mutex mu_; |
170 std::set<std::string> locked_files_; | 158 std::set<std::string> locked_files_; |
171 }; | 159 }; |
172 | 160 |
173 std::set<std::string> directories_needing_sync_; | |
174 base::Lock directory_sync_lock_; | |
175 | |
176 const int kMaxRetryTimeMillis; | 161 const int kMaxRetryTimeMillis; |
177 // BGThread() is the body of the background thread | 162 // BGThread() is the body of the background thread |
178 void BGThread(); | 163 void BGThread(); |
179 static void BGThreadWrapper(void* arg) { | 164 static void BGThreadWrapper(void* arg) { |
180 reinterpret_cast<ChromiumEnv*>(arg)->BGThread(); | 165 reinterpret_cast<ChromiumEnv*>(arg)->BGThread(); |
181 } | 166 } |
182 | 167 |
183 virtual void RecordBackupResult(bool result) const; | 168 virtual void RecordBackupResult(bool result) const; |
184 void RestoreIfNecessary(const std::string& dir, | 169 void RestoreIfNecessary(const std::string& dir, |
185 std::vector<std::string>* children); | 170 std::vector<std::string>* children); |
(...skipping 20 matching lines...) Expand all Loading... |
206 void (*function)(void*); | 191 void (*function)(void*); |
207 }; | 192 }; |
208 typedef std::deque<BGItem> BGQueue; | 193 typedef std::deque<BGItem> BGQueue; |
209 BGQueue queue_; | 194 BGQueue queue_; |
210 LockTable locks_; | 195 LockTable locks_; |
211 }; | 196 }; |
212 | 197 |
213 } // namespace leveldb_env | 198 } // namespace leveldb_env |
214 | 199 |
215 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ | 200 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ |
OLD | NEW |