leveldb: Removed unnecessary ChromiumEnv::CreateFilePath
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium.h
blobd882daa4deba16fcb5a573c9b98ef32a09e1677a
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 enum MethodID {
23 kSequentialFileRead,
24 kSequentialFileSkip,
25 kRandomAccessFileRead,
26 kWritableFileAppend,
27 kWritableFileClose,
28 kWritableFileFlush,
29 kWritableFileSync,
30 kNewSequentialFile,
31 kNewRandomAccessFile,
32 kNewWritableFile,
33 kDeleteFile,
34 kCreateDir,
35 kDeleteDir,
36 kGetFileSize,
37 kRenameFile,
38 kLockFile,
39 kUnlockFile,
40 kGetTestDirectory,
41 kNewLogger,
42 kSyncParent,
43 kGetChildren,
44 kNumEntries
47 const char* MethodIDToString(MethodID method);
49 leveldb::Status MakeIOError(leveldb::Slice filename,
50 const std::string& message,
51 MethodID method,
52 base::File::Error error);
53 leveldb::Status MakeIOError(leveldb::Slice filename,
54 const std::string& message,
55 MethodID method);
57 enum ErrorParsingResult {
58 METHOD_ONLY,
59 METHOD_AND_PFE,
60 METHOD_AND_ERRNO,
61 NONE,
64 ErrorParsingResult ParseMethodAndError(const char* string,
65 MethodID* method,
66 int* error);
67 int GetCorruptionCode(const leveldb::Status& status);
68 int GetNumCorruptionCodes();
69 std::string GetCorruptionMessage(const leveldb::Status& status);
70 bool IndicatesDiskFull(const leveldb::Status& status);
71 bool IsIOError(const leveldb::Status& status);
73 class UMALogger {
74 public:
75 virtual void RecordErrorAt(MethodID method) const = 0;
76 virtual void RecordOSError(MethodID method,
77 base::File::Error error) const = 0;
78 virtual void RecordBackupResult(bool success) const = 0;
81 class RetrierProvider {
82 public:
83 virtual int MaxRetryTimeMillis() const = 0;
84 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0;
85 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
86 MethodID method) const = 0;
89 class WriteTracker {
90 public:
91 virtual void DidCreateNewFile(const std::string& fname) = 0;
92 virtual bool DoesDirNeedSync(const std::string& fname) = 0;
93 virtual void DidSyncDir(const std::string& fname) = 0;
96 class ChromiumEnv : public leveldb::Env,
97 public UMALogger,
98 public RetrierProvider,
99 public WriteTracker {
100 public:
101 ChromiumEnv();
103 typedef void(ScheduleFunc)(void*);
105 static bool MakeBackup(const std::string& fname);
106 static const char* FileErrorString(::base::File::Error error);
107 static bool HasTableExtension(const base::FilePath& path);
108 virtual ~ChromiumEnv();
110 virtual bool FileExists(const std::string& fname);
111 virtual leveldb::Status GetChildren(const std::string& dir,
112 std::vector<std::string>* result);
113 virtual leveldb::Status DeleteFile(const std::string& fname);
114 virtual leveldb::Status CreateDir(const std::string& name);
115 virtual leveldb::Status DeleteDir(const std::string& name);
116 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size);
117 virtual leveldb::Status RenameFile(const std::string& src,
118 const std::string& dst);
119 virtual leveldb::Status LockFile(const std::string& fname,
120 leveldb::FileLock** lock);
121 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock);
122 virtual void Schedule(ScheduleFunc*, void* arg);
123 virtual void StartThread(void (*function)(void* arg), void* arg);
124 virtual leveldb::Status GetTestDirectory(std::string* path);
125 virtual uint64_t NowMicros();
126 virtual void SleepForMicroseconds(int micros);
127 virtual leveldb::Status NewSequentialFile(const std::string& fname,
128 leveldb::SequentialFile** result);
129 virtual leveldb::Status NewRandomAccessFile(
130 const std::string& fname,
131 leveldb::RandomAccessFile** result);
132 virtual leveldb::Status NewWritableFile(const std::string& fname,
133 leveldb::WritableFile** result);
134 virtual leveldb::Status NewLogger(const std::string& fname,
135 leveldb::Logger** result);
137 protected:
138 virtual void DidSyncDir(const std::string& fname);
140 std::string name_;
141 bool make_backup_;
143 private:
144 virtual void DidCreateNewFile(const std::string& fname);
145 virtual bool DoesDirNeedSync(const std::string& fname);
146 virtual void RecordErrorAt(MethodID method) const;
147 virtual void RecordOSError(MethodID method,
148 base::File::Error error) const;
149 void RecordOpenFilesLimit(const std::string& type);
150 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const;
151 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const;
153 // File locks may not be exclusive within a process (e.g. on POSIX). Track
154 // locks held by the ChromiumEnv to prevent access within the process.
155 class LockTable {
156 public:
157 bool Insert(const std::string& fname) {
158 leveldb::MutexLock l(&mu_);
159 return locked_files_.insert(fname).second;
161 bool Remove(const std::string& fname) {
162 leveldb::MutexLock l(&mu_);
163 return locked_files_.erase(fname) == 1;
165 private:
166 leveldb::port::Mutex mu_;
167 std::set<std::string> locked_files_;
170 std::set<std::string> directories_needing_sync_;
171 base::Lock directory_sync_lock_;
173 const int kMaxRetryTimeMillis;
174 // BGThread() is the body of the background thread
175 void BGThread();
176 static void BGThreadWrapper(void* arg) {
177 reinterpret_cast<ChromiumEnv*>(arg)->BGThread();
180 virtual void RecordBackupResult(bool result) const;
181 void RestoreIfNecessary(const std::string& dir,
182 std::vector<std::string>* children);
183 base::FilePath RestoreFromBackup(const base::FilePath& base_name);
184 void RecordLockFileAncestors(int num_missing_ancestors) const;
185 base::HistogramBase* GetMethodIOErrorHistogram() const;
186 base::HistogramBase* GetLockFileAncestorHistogram() const;
188 // RetrierProvider implementation.
189 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; }
190 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const;
191 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
192 MethodID method) const;
194 base::FilePath test_directory_;
196 ::base::Lock mu_;
197 ::base::ConditionVariable bgsignal_;
198 bool started_bgthread_;
200 // Entry per Schedule() call
201 struct BGItem {
202 void* arg;
203 void (*function)(void*);
205 typedef std::deque<BGItem> BGQueue;
206 BGQueue queue_;
207 LockTable locks_;
210 } // namespace leveldb_env
212 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_