Try to work around that clang/win bug in another file.
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium.h
blob55a064a770c78ed265ce826a52034ef7231a7506
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 // The default value for leveldb::Options::reuse_logs. Currently log reuse is an
51 // experimental feature in leveldb. More info at:
52 // https://github.com/google/leveldb/commit/251ebf5dc70129ad3
53 #if defined(OS_CHROMEOS)
54 // Reusing logs on Chrome OS resulted in an unacceptably high leveldb corruption
55 // rate (at least for Indexed DB). More info at https://crbug.com/460568
56 const bool kDefaultLogReuseOptionValue = false;
57 #else
58 const bool kDefaultLogReuseOptionValue = true;
59 #endif
61 const char* MethodIDToString(MethodID method);
63 leveldb::Status MakeIOError(leveldb::Slice filename,
64 const std::string& message,
65 MethodID method,
66 base::File::Error error);
67 leveldb::Status MakeIOError(leveldb::Slice filename,
68 const std::string& message,
69 MethodID method);
71 enum ErrorParsingResult {
72 METHOD_ONLY,
73 METHOD_AND_BFE,
74 NONE,
77 ErrorParsingResult ParseMethodAndError(const leveldb::Status& status,
78 MethodID* method,
79 base::File::Error* error);
80 int GetCorruptionCode(const leveldb::Status& status);
81 int GetNumCorruptionCodes();
82 std::string GetCorruptionMessage(const leveldb::Status& status);
83 bool IndicatesDiskFull(const leveldb::Status& status);
85 class UMALogger {
86 public:
87 virtual void RecordErrorAt(MethodID method) const = 0;
88 virtual void RecordOSError(MethodID method,
89 base::File::Error error) const = 0;
90 virtual void RecordBackupResult(bool success) const = 0;
93 class RetrierProvider {
94 public:
95 virtual int MaxRetryTimeMillis() const = 0;
96 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0;
97 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
98 MethodID method) const = 0;
101 class ChromiumEnv : public leveldb::Env,
102 public UMALogger,
103 public RetrierProvider {
104 public:
105 ChromiumEnv();
107 typedef void(ScheduleFunc)(void*);
109 static bool MakeBackup(const std::string& fname);
110 virtual ~ChromiumEnv();
112 virtual bool FileExists(const std::string& fname);
113 virtual leveldb::Status GetChildren(const std::string& dir,
114 std::vector<std::string>* result);
115 virtual leveldb::Status DeleteFile(const std::string& fname);
116 virtual leveldb::Status CreateDir(const std::string& name);
117 virtual leveldb::Status DeleteDir(const std::string& name);
118 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size);
119 virtual leveldb::Status RenameFile(const std::string& src,
120 const std::string& dst);
121 virtual leveldb::Status LockFile(const std::string& fname,
122 leveldb::FileLock** lock);
123 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock);
124 virtual void Schedule(ScheduleFunc*, void* arg);
125 virtual void StartThread(void (*function)(void* arg), void* arg);
126 virtual leveldb::Status GetTestDirectory(std::string* path);
127 virtual uint64_t NowMicros();
128 virtual void SleepForMicroseconds(int micros);
129 virtual leveldb::Status NewSequentialFile(const std::string& fname,
130 leveldb::SequentialFile** result);
131 virtual leveldb::Status NewRandomAccessFile(
132 const std::string& fname,
133 leveldb::RandomAccessFile** result);
134 virtual leveldb::Status NewWritableFile(const std::string& fname,
135 leveldb::WritableFile** result);
136 virtual leveldb::Status NewAppendableFile(const std::string& fname,
137 leveldb::WritableFile** result);
138 virtual leveldb::Status NewLogger(const std::string& fname,
139 leveldb::Logger** result);
141 protected:
142 ChromiumEnv(const std::string& name, bool make_backup);
144 private:
145 static const char* FileErrorString(base::File::Error error);
147 virtual void RecordErrorAt(MethodID method) const;
148 virtual void RecordOSError(MethodID method,
149 base::File::Error error) const;
150 void RecordOpenFilesLimit(const std::string& type);
151 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const;
152 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const;
154 // File locks may not be exclusive within a process (e.g. on POSIX). Track
155 // locks held by the ChromiumEnv to prevent access within the process.
156 class LockTable {
157 public:
158 bool Insert(const std::string& fname) {
159 leveldb::MutexLock l(&mu_);
160 return locked_files_.insert(fname).second;
162 bool Remove(const std::string& fname) {
163 leveldb::MutexLock l(&mu_);
164 return locked_files_.erase(fname) == 1;
166 private:
167 leveldb::port::Mutex mu_;
168 std::set<std::string> locked_files_;
171 const int kMaxRetryTimeMillis;
172 // BGThread() is the body of the background thread
173 void BGThread();
174 static void BGThreadWrapper(void* arg) {
175 reinterpret_cast<ChromiumEnv*>(arg)->BGThread();
178 virtual void RecordBackupResult(bool result) const;
179 void RestoreIfNecessary(const std::string& dir,
180 std::vector<std::string>* children);
181 base::FilePath RestoreFromBackup(const base::FilePath& base_name);
182 void RecordLockFileAncestors(int num_missing_ancestors) const;
183 base::HistogramBase* GetMethodIOErrorHistogram() const;
184 base::HistogramBase* GetLockFileAncestorHistogram() const;
186 // RetrierProvider implementation.
187 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; }
188 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const;
189 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
190 MethodID method) const;
192 base::FilePath test_directory_;
194 std::string name_;
195 std::string uma_ioerror_base_name_;
196 bool make_backup_;
198 base::Lock mu_;
199 base::ConditionVariable bgsignal_;
200 bool started_bgthread_;
202 // Entry per Schedule() call
203 struct BGItem {
204 void* arg;
205 void (*function)(void*);
207 typedef std::deque<BGItem> BGQueue;
208 BGQueue queue_;
209 LockTable locks_;
212 } // namespace leveldb_env
214 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_