Add per-user preferences support.
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium.h
blob6fb7180f55079bae2f8165e67263ea11efffcbc6
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
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
8 #include <deque>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/metrics/histogram.h"
16 #include "leveldb/env.h"
17 #include "port/port_chromium.h"
18 #include "util/mutexlock.h"
20 namespace leveldb_env {
22 // These entries map to values in tools/metrics/histograms/histograms.xml. New
23 // values should be appended at the end.
24 enum MethodID {
25 kSequentialFileRead,
26 kSequentialFileSkip,
27 kRandomAccessFileRead,
28 kWritableFileAppend,
29 kWritableFileClose,
30 kWritableFileFlush,
31 kWritableFileSync,
32 kNewSequentialFile,
33 kNewRandomAccessFile,
34 kNewWritableFile,
35 kDeleteFile,
36 kCreateDir,
37 kDeleteDir,
38 kGetFileSize,
39 kRenameFile,
40 kLockFile,
41 kUnlockFile,
42 kGetTestDirectory,
43 kNewLogger,
44 kSyncParent,
45 kGetChildren,
46 kNewAppendableFile,
47 kNumEntries
50 const char* MethodIDToString(MethodID method);
52 leveldb::Status MakeIOError(leveldb::Slice filename,
53 const std::string& message,
54 MethodID method,
55 base::File::Error error);
56 leveldb::Status MakeIOError(leveldb::Slice filename,
57 const std::string& message,
58 MethodID method);
60 enum ErrorParsingResult {
61 METHOD_ONLY,
62 METHOD_AND_PFE,
63 NONE,
66 ErrorParsingResult ParseMethodAndError(const leveldb::Status& status,
67 MethodID* method,
68 base::File::Error* error);
69 int GetCorruptionCode(const leveldb::Status& status);
70 int GetNumCorruptionCodes();
71 std::string GetCorruptionMessage(const leveldb::Status& status);
72 bool IndicatesDiskFull(const leveldb::Status& status);
74 class UMALogger {
75 public:
76 virtual void RecordErrorAt(MethodID method) const = 0;
77 virtual void RecordOSError(MethodID method,
78 base::File::Error error) const = 0;
79 virtual void RecordBackupResult(bool success) const = 0;
82 class RetrierProvider {
83 public:
84 virtual int MaxRetryTimeMillis() const = 0;
85 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0;
86 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
87 MethodID method) const = 0;
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;
97 class ChromiumEnv : public leveldb::Env,
98 public UMALogger,
99 public RetrierProvider,
100 public WriteTracker {
101 public:
102 ChromiumEnv();
104 typedef void(ScheduleFunc)(void*);
106 static bool MakeBackup(const std::string& fname);
107 virtual ~ChromiumEnv();
109 virtual bool FileExists(const std::string& fname);
110 virtual leveldb::Status GetChildren(const std::string& dir,
111 std::vector<std::string>* result);
112 virtual leveldb::Status DeleteFile(const std::string& fname);
113 virtual leveldb::Status CreateDir(const std::string& name);
114 virtual leveldb::Status DeleteDir(const std::string& name);
115 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size);
116 virtual leveldb::Status RenameFile(const std::string& src,
117 const std::string& dst);
118 virtual leveldb::Status LockFile(const std::string& fname,
119 leveldb::FileLock** lock);
120 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock);
121 virtual void Schedule(ScheduleFunc*, void* arg);
122 virtual void StartThread(void (*function)(void* arg), void* arg);
123 virtual leveldb::Status GetTestDirectory(std::string* path);
124 virtual uint64_t NowMicros();
125 virtual void SleepForMicroseconds(int micros);
126 virtual leveldb::Status NewSequentialFile(const std::string& fname,
127 leveldb::SequentialFile** result);
128 virtual leveldb::Status NewRandomAccessFile(
129 const std::string& fname,
130 leveldb::RandomAccessFile** result);
131 virtual leveldb::Status NewWritableFile(const std::string& fname,
132 leveldb::WritableFile** result);
133 virtual leveldb::Status NewAppendableFile(const std::string& fname,
134 leveldb::WritableFile** result);
135 virtual leveldb::Status NewLogger(const std::string& fname,
136 leveldb::Logger** result);
138 protected:
139 virtual void DidSyncDir(const std::string& fname);
141 std::string name_;
142 bool make_backup_;
144 private:
145 static const char* FileErrorString(base::File::Error error);
147 virtual void DidCreateNewFile(const std::string& fname);
148 virtual bool DoesDirNeedSync(const std::string& fname);
149 virtual void RecordErrorAt(MethodID method) const;
150 virtual void RecordOSError(MethodID method,
151 base::File::Error error) const;
152 void RecordOpenFilesLimit(const std::string& type);
153 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const;
154 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const;
156 // 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.
158 class LockTable {
159 public:
160 bool Insert(const std::string& fname) {
161 leveldb::MutexLock l(&mu_);
162 return locked_files_.insert(fname).second;
164 bool Remove(const std::string& fname) {
165 leveldb::MutexLock l(&mu_);
166 return locked_files_.erase(fname) == 1;
168 private:
169 leveldb::port::Mutex mu_;
170 std::set<std::string> locked_files_;
173 std::set<std::string> directories_needing_sync_;
174 base::Lock directory_sync_lock_;
176 const int kMaxRetryTimeMillis;
177 // BGThread() is the body of the background thread
178 void BGThread();
179 static void BGThreadWrapper(void* arg) {
180 reinterpret_cast<ChromiumEnv*>(arg)->BGThread();
183 virtual void RecordBackupResult(bool result) const;
184 void RestoreIfNecessary(const std::string& dir,
185 std::vector<std::string>* children);
186 base::FilePath RestoreFromBackup(const base::FilePath& base_name);
187 void RecordLockFileAncestors(int num_missing_ancestors) const;
188 base::HistogramBase* GetMethodIOErrorHistogram() const;
189 base::HistogramBase* GetLockFileAncestorHistogram() const;
191 // RetrierProvider implementation.
192 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; }
193 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const;
194 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
195 MethodID method) const;
197 base::FilePath test_directory_;
199 base::Lock mu_;
200 base::ConditionVariable bgsignal_;
201 bool started_bgthread_;
203 // Entry per Schedule() call
204 struct BGItem {
205 void* arg;
206 void (*function)(void*);
208 typedef std::deque<BGItem> BGQueue;
209 BGQueue queue_;
210 LockTable locks_;
213 } // namespace leveldb_env
215 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_