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
{
25 kRandomAccessFileRead
,
47 const char* MethodIDToString(MethodID method
);
49 leveldb::Status
MakeIOError(leveldb::Slice filename
,
50 const std::string
& message
,
52 base::File::Error error
);
53 leveldb::Status
MakeIOError(leveldb::Slice filename
,
54 const std::string
& message
,
57 enum ErrorParsingResult
{
64 ErrorParsingResult
ParseMethodAndError(const char* string
,
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
);
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
{
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;
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
,
98 public RetrierProvider
,
103 typedef void(ScheduleFunc
)(void*);
105 static bool MakeBackup(const std::string
& fname
);
106 static base::FilePath
CreateFilePath(const std::string
& file_path
);
107 static const char* FileErrorString(::base::File::Error error
);
108 static bool HasTableExtension(const base::FilePath
& path
);
109 virtual ~ChromiumEnv();
111 virtual bool FileExists(const std::string
& fname
);
112 virtual leveldb::Status
GetChildren(const std::string
& dir
,
113 std::vector
<std::string
>* result
);
114 virtual leveldb::Status
DeleteFile(const std::string
& fname
);
115 virtual leveldb::Status
CreateDir(const std::string
& name
);
116 virtual leveldb::Status
DeleteDir(const std::string
& name
);
117 virtual leveldb::Status
GetFileSize(const std::string
& fname
, uint64_t* size
);
118 virtual leveldb::Status
RenameFile(const std::string
& src
,
119 const std::string
& dst
);
120 virtual leveldb::Status
LockFile(const std::string
& fname
,
121 leveldb::FileLock
** lock
);
122 virtual leveldb::Status
UnlockFile(leveldb::FileLock
* lock
);
123 virtual void Schedule(ScheduleFunc
*, void* arg
);
124 virtual void StartThread(void (*function
)(void* arg
), void* arg
);
125 virtual leveldb::Status
GetTestDirectory(std::string
* path
);
126 virtual uint64_t NowMicros();
127 virtual void SleepForMicroseconds(int micros
);
128 virtual leveldb::Status
NewSequentialFile(const std::string
& fname
,
129 leveldb::SequentialFile
** result
);
130 virtual leveldb::Status
NewRandomAccessFile(
131 const std::string
& fname
,
132 leveldb::RandomAccessFile
** result
);
133 virtual leveldb::Status
NewWritableFile(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 virtual void DidCreateNewFile(const std::string
& fname
);
146 virtual bool DoesDirNeedSync(const std::string
& fname
);
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.
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;
167 leveldb::port::Mutex mu_
;
168 std::set
<std::string
> locked_files_
;
171 std::set
<std::string
> directories_needing_sync_
;
172 base::Lock directory_sync_lock_
;
174 const int kMaxRetryTimeMillis
;
175 // BGThread() is the body of the background thread
177 static void BGThreadWrapper(void* arg
) {
178 reinterpret_cast<ChromiumEnv
*>(arg
)->BGThread();
181 virtual void RecordBackupResult(bool result
) const;
182 void RestoreIfNecessary(const std::string
& dir
,
183 std::vector
<std::string
>* children
);
184 base::FilePath
RestoreFromBackup(const base::FilePath
& base_name
);
185 void RecordLockFileAncestors(int num_missing_ancestors
) const;
186 base::HistogramBase
* GetMethodIOErrorHistogram() const;
187 base::HistogramBase
* GetLockFileAncestorHistogram() const;
189 // RetrierProvider implementation.
190 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis
; }
191 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const;
192 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
193 MethodID method
) const;
195 base::FilePath test_directory_
;
198 ::base::ConditionVariable bgsignal_
;
199 bool started_bgthread_
;
201 // Entry per Schedule() call
204 void (*function
)(void*);
206 typedef std::deque
<BGItem
> BGQueue
;
211 } // namespace leveldb_env
213 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_