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_
12 #include "base/files/file.h"
13 #include "base/metrics/histogram.h"
14 #include "leveldb/env.h"
15 #include "port/port_chromium.h"
16 #include "util/mutexlock.h"
18 namespace leveldb_env
{
23 kRandomAccessFileRead
,
45 const char* MethodIDToString(MethodID method
);
47 leveldb::Status
MakeIOError(leveldb::Slice filename
,
51 leveldb::Status
MakeIOError(leveldb::Slice filename
,
54 base::File::Error error
);
55 leveldb::Status
MakeIOError(leveldb::Slice filename
,
59 enum ErrorParsingResult
{
66 ErrorParsingResult
ParseMethodAndError(const char* string
,
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
);
73 bool IsIOError(const leveldb::Status
& status
);
74 bool IsCorruption(const leveldb::Status
& status
);
75 std::string
FilePathToString(const base::FilePath
& file_path
);
79 virtual void RecordErrorAt(MethodID method
) const = 0;
80 virtual void RecordOSError(MethodID method
, int saved_errno
) const = 0;
81 virtual void RecordOSError(MethodID method
,
82 base::File::Error error
) const = 0;
83 virtual void RecordBackupResult(bool success
) const = 0;
86 class RetrierProvider
{
88 virtual int MaxRetryTimeMillis() const = 0;
89 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const = 0;
90 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
91 MethodID method
) const = 0;
96 virtual void DidCreateNewFile(const std::string
& fname
) = 0;
97 virtual bool DoesDirNeedSync(const std::string
& fname
) = 0;
98 virtual void DidSyncDir(const std::string
& fname
) = 0;
101 class ChromiumEnv
: public leveldb::Env
,
103 public RetrierProvider
,
104 public WriteTracker
{
106 static bool MakeBackup(const std::string
& fname
);
107 static base::FilePath
CreateFilePath(const std::string
& file_path
);
108 static const char* FileErrorString(::base::File::Error error
);
109 static bool HasTableExtension(const base::FilePath
& path
);
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(void (*function
)(void*), 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
);
133 virtual void DidCreateNewFile(const std::string
& fname
);
134 virtual bool DoesDirNeedSync(const std::string
& fname
);
135 virtual void DidSyncDir(const std::string
& fname
);
136 virtual base::File::Error
GetDirectoryEntries(
137 const base::FilePath
& dir_param
,
138 std::vector
<base::FilePath
>* result
) const = 0;
139 virtual void RecordErrorAt(MethodID method
) const;
140 virtual void RecordOSError(MethodID method
, int saved_errno
) const;
141 virtual void RecordOSError(MethodID method
,
142 base::File::Error error
) const;
143 base::HistogramBase
* GetMaxFDHistogram(const std::string
& type
) const;
144 base::HistogramBase
* GetOSErrorHistogram(MethodID method
, int limit
) const;
150 // File locks may not be exclusive within a process (e.g. on POSIX). Track
151 // locks held by the ChromiumEnv to prevent access within the process.
154 bool Insert(const std::string
& fname
) {
155 leveldb::MutexLock
l(&mu_
);
156 return locked_files_
.insert(fname
).second
;
158 bool Remove(const std::string
& fname
) {
159 leveldb::MutexLock
l(&mu_
);
160 return locked_files_
.erase(fname
) == 1;
163 leveldb::port::Mutex mu_
;
164 std::set
<std::string
> locked_files_
;
167 std::map
<std::string
, bool> needs_sync_map_
;
168 base::Lock map_lock_
;
170 const int kMaxRetryTimeMillis
;
171 // BGThread() is the body of the background thread
173 static void BGThreadWrapper(void* arg
) {
174 reinterpret_cast<ChromiumEnv
*>(arg
)->BGThread();
177 virtual void RecordBackupResult(bool result
) const;
178 void RestoreIfNecessary(const std::string
& dir
,
179 std::vector
<std::string
>* children
);
180 base::FilePath
RestoreFromBackup(const base::FilePath
& base_name
);
181 void RecordLockFileAncestors(int num_missing_ancestors
) const;
182 base::HistogramBase
* GetMethodIOErrorHistogram() const;
183 base::HistogramBase
* GetLockFileAncestorHistogram() const;
185 // RetrierProvider implementation.
186 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis
; }
187 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const;
188 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
189 MethodID method
) const;
191 base::FilePath test_directory_
;
194 ::base::ConditionVariable bgsignal_
;
195 bool started_bgthread_
;
197 // Entry per Schedule() call
200 void (*function
)(void*);
202 typedef std::deque
<BGItem
> BGQueue
;
207 } // namespace leveldb_env