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_
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.
27 kRandomAccessFileRead
,
50 const char* MethodIDToString(MethodID method
);
52 leveldb::Status
MakeIOError(leveldb::Slice filename
,
53 const std::string
& message
,
55 base::File::Error error
);
56 leveldb::Status
MakeIOError(leveldb::Slice filename
,
57 const std::string
& message
,
60 enum ErrorParsingResult
{
66 ErrorParsingResult
ParseMethodAndError(const leveldb::Status
& status
,
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
);
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
{
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;
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
,
99 public RetrierProvider
,
100 public WriteTracker
{
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
);
139 virtual void DidSyncDir(const std::string
& fname
);
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.
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;
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
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_
;
200 base::ConditionVariable bgsignal_
;
201 bool started_bgthread_
;
203 // Entry per Schedule() call
206 void (*function
)(void*);
208 typedef std::deque
<BGItem
> BGQueue
;
213 } // namespace leveldb_env
215 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_