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_
14 #include "base/files/file.h"
15 #include "base/files/file_path.h"
16 #include "base/metrics/histogram.h"
17 #include "leveldb/env.h"
18 #include "port/port_chromium.h"
19 #include "util/mutexlock.h"
21 namespace leveldb_env
{
26 kRandomAccessFileRead
,
48 const char* MethodIDToString(MethodID method
);
50 leveldb::Status
MakeIOError(leveldb::Slice filename
,
54 leveldb::Status
MakeIOError(leveldb::Slice filename
,
57 base::File::Error error
);
58 leveldb::Status
MakeIOError(leveldb::Slice filename
,
62 enum ErrorParsingResult
{
69 ErrorParsingResult
ParseMethodAndError(const char* string
,
72 int GetCorruptionCode(const leveldb::Status
& status
);
73 int GetNumCorruptionCodes();
74 std::string
GetCorruptionMessage(const leveldb::Status
& status
);
75 bool IndicatesDiskFull(const leveldb::Status
& status
);
76 bool IsIOError(const leveldb::Status
& status
);
77 bool IsCorruption(const leveldb::Status
& status
);
78 std::string
FilePathToString(const base::FilePath
& file_path
);
82 virtual void RecordErrorAt(MethodID method
) const = 0;
83 virtual void RecordOSError(MethodID method
, int saved_errno
) const = 0;
84 virtual void RecordOSError(MethodID method
,
85 base::File::Error error
) const = 0;
86 virtual void RecordBackupResult(bool success
) const = 0;
89 class RetrierProvider
{
91 virtual int MaxRetryTimeMillis() const = 0;
92 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const = 0;
93 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
94 MethodID method
) const = 0;
99 virtual void DidCreateNewFile(const std::string
& fname
) = 0;
100 virtual bool DoesDirNeedSync(const std::string
& fname
) = 0;
101 virtual void DidSyncDir(const std::string
& fname
) = 0;
104 class ChromiumEnv
: public leveldb::Env
,
106 public RetrierProvider
,
107 public WriteTracker
{
109 typedef void(ScheduleFunc
)(void*);
111 static bool MakeBackup(const std::string
& fname
);
112 static base::FilePath
CreateFilePath(const std::string
& file_path
);
113 static const char* FileErrorString(::base::File::Error error
);
114 static bool HasTableExtension(const base::FilePath
& path
);
115 virtual ~ChromiumEnv();
117 virtual bool FileExists(const std::string
& fname
);
118 virtual leveldb::Status
GetChildren(const std::string
& dir
,
119 std::vector
<std::string
>* result
);
120 virtual leveldb::Status
DeleteFile(const std::string
& fname
);
121 virtual leveldb::Status
CreateDir(const std::string
& name
);
122 virtual leveldb::Status
DeleteDir(const std::string
& name
);
123 virtual leveldb::Status
GetFileSize(const std::string
& fname
, uint64_t* size
);
124 virtual leveldb::Status
RenameFile(const std::string
& src
,
125 const std::string
& dst
);
126 virtual leveldb::Status
LockFile(const std::string
& fname
,
127 leveldb::FileLock
** lock
);
128 virtual leveldb::Status
UnlockFile(leveldb::FileLock
* lock
);
129 virtual void Schedule(ScheduleFunc
*, void* arg
);
130 virtual void StartThread(void (*function
)(void* arg
), void* arg
);
131 virtual leveldb::Status
GetTestDirectory(std::string
* path
);
132 virtual uint64_t NowMicros();
133 virtual void SleepForMicroseconds(int micros
);
138 virtual void DidCreateNewFile(const std::string
& fname
);
139 virtual bool DoesDirNeedSync(const std::string
& fname
);
140 virtual void DidSyncDir(const std::string
& fname
);
141 virtual base::File::Error
GetDirectoryEntries(
142 const base::FilePath
& dir_param
,
143 std::vector
<base::FilePath
>* result
) const = 0;
144 virtual void RecordErrorAt(MethodID method
) const;
145 virtual void RecordOSError(MethodID method
, int saved_errno
) const;
146 virtual void RecordOSError(MethodID method
,
147 base::File::Error error
) const;
148 base::HistogramBase
* GetMaxFDHistogram(const std::string
& type
) const;
149 base::HistogramBase
* GetOSErrorHistogram(MethodID method
, int limit
) const;
155 // File locks may not be exclusive within a process (e.g. on POSIX). Track
156 // locks held by the ChromiumEnv to prevent access within the process.
159 bool Insert(const std::string
& fname
) {
160 leveldb::MutexLock
l(&mu_
);
161 return locked_files_
.insert(fname
).second
;
163 bool Remove(const std::string
& fname
) {
164 leveldb::MutexLock
l(&mu_
);
165 return locked_files_
.erase(fname
) == 1;
168 leveldb::port::Mutex mu_
;
169 std::set
<std::string
> locked_files_
;
172 std::map
<std::string
, bool> needs_sync_map_
;
173 base::Lock map_lock_
;
175 const int kMaxRetryTimeMillis
;
176 // BGThread() is the body of the background thread
178 static void BGThreadWrapper(void* arg
) {
179 reinterpret_cast<ChromiumEnv
*>(arg
)->BGThread();
182 virtual void RecordBackupResult(bool result
) const;
183 void RestoreIfNecessary(const std::string
& dir
,
184 std::vector
<std::string
>* children
);
185 base::FilePath
RestoreFromBackup(const base::FilePath
& base_name
);
186 void RecordLockFileAncestors(int num_missing_ancestors
) const;
187 base::HistogramBase
* GetMethodIOErrorHistogram() const;
188 base::HistogramBase
* GetLockFileAncestorHistogram() const;
190 // RetrierProvider implementation.
191 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis
; }
192 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const;
193 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
194 MethodID method
) const;
196 base::FilePath test_directory_
;
199 ::base::ConditionVariable bgsignal_
;
200 bool started_bgthread_
;
202 // Entry per Schedule() call
205 void (*function
)(void*);
207 typedef std::deque
<BGItem
> BGQueue
;
212 } // namespace leveldb_env
214 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_