include/leveldb/env.h

filter: Statementstmt 100% 6 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 6 / 6 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/leveldb/db.h ↑ Summary include/leveldb/table.h »
1 // Copyright (c) 2011 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.
4 //
5 // An Env is an interface used by the leveldb implementation to access
6 // operating system functionality like the filesystem etc. Callers
7 // may wish to provide a custom Env object when opening a database to
8 // get fine gain control; e.g., to rate limit file system operations.
9 //
10 // All Env implementations are safe for concurrent access from
11 // multiple threads without any external synchronization.
12  
13 #ifndef STORAGE_LEVELDB_INCLUDE_ENV_H_
14 #define STORAGE_LEVELDB_INCLUDE_ENV_H_
15  
16 #include <cstdarg>
17 #include <string>
18 #include <vector>
19 #include <stdint.h>
20 #include "leveldb/status.h"
21  
22 namespace leveldb {
23  
24 class FileLock;
25 class Logger;
26 class RandomAccessFile;
27 class SequentialFile;
28 class Slice;
29 class WritableFile;
30  
31 class Env {
32 public:
33 2 Env() { }
34 virtual ~Env();
35  
36 // Return a default environment suitable for the current operating
37 // system. Sophisticated users may wish to provide their own Env
38 // implementation instead of relying on this default environment.
39 //
40 // The result of Default() belongs to leveldb and must never be deleted.
41 static Env* Default();
42  
43 // Create a brand new sequentially-readable file with the specified name.
44 // On success, stores a pointer to the new file in *result and returns OK.
45 // On failure stores NULL in *result and returns non-OK. If the file does
46 // not exist, returns a non-OK status.
47 //
48 // The returned file will only be accessed by one thread at a time.
49 virtual Status NewSequentialFile(const std::string& fname,
50 SequentialFile** result) = 0;
51  
52 // Create a brand new random access read-only file with the
53 // specified name. On success, stores a pointer to the new file in
54 // *result and returns OK. On failure stores NULL in *result and
55 // returns non-OK. If the file does not exist, returns a non-OK
56 // status.
57 //
58 // The returned file may be concurrently accessed by multiple threads.
59 virtual Status NewRandomAccessFile(const std::string& fname,
60 RandomAccessFile** result) = 0;
61  
62 // Create an object that writes to a new file with the specified
63 // name. Deletes any existing file with the same name and creates a
64 // new file. On success, stores a pointer to the new file in
65 // *result and returns OK. On failure stores NULL in *result and
66 // returns non-OK.
67 //
68 // The returned file will only be accessed by one thread at a time.
69 virtual Status NewWritableFile(const std::string& fname,
70 WritableFile** result) = 0;
71  
72 // Returns true iff the named file exists.
73 virtual bool FileExists(const std::string& fname) = 0;
74  
75 // Store in *result the names of the children of the specified directory.
76 // The names are relative to "dir".
77 // Original contents of *results are dropped.
78 virtual Status GetChildren(const std::string& dir,
79 std::vector<std::string>* result) = 0;
80  
81 // Delete the named file.
82 virtual Status DeleteFile(const std::string& fname) = 0;
83  
84 // Create the specified directory.
85 virtual Status CreateDir(const std::string& dirname) = 0;
86  
87 // Delete the specified directory.
88 virtual Status DeleteDir(const std::string& dirname) = 0;
89  
90 // Store the size of fname in *file_size.
91 virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) = 0;
92  
93 // Rename file src to target.
94 virtual Status RenameFile(const std::string& src,
95 const std::string& target) = 0;
96  
97 // Lock the specified file. Used to prevent concurrent access to
98 // the same db by multiple processes. On failure, stores NULL in
99 // *lock and returns non-OK.
100 //
101 // On success, stores a pointer to the object that represents the
102 // acquired lock in *lock and returns OK. The caller should call
103 // UnlockFile(*lock) to release the lock. If the process exits,
104 // the lock will be automatically released.
105 //
106 // If somebody else already holds the lock, finishes immediately
107 // with a failure. I.e., this call does not wait for existing locks
108 // to go away.
109 //
110 // May create the named file if it does not already exist.
111 virtual Status LockFile(const std::string& fname, FileLock** lock) = 0;
112  
113 // Release the lock acquired by a previous successful call to LockFile.
114 // REQUIRES: lock was returned by a successful LockFile() call
115 // REQUIRES: lock has not already been unlocked.
116 virtual Status UnlockFile(FileLock* lock) = 0;
117  
118 // Arrange to run "(*function)(arg)" once in a background thread.
119 //
120 // "function" may run in an unspecified thread. Multiple functions
121 // added to the same Env may run concurrently in different threads.
122 // I.e., the caller may not assume that background work items are
123 // serialized.
124 virtual void Schedule(
125 void (*function)(void* arg),
126 void* arg) = 0;
127  
128 // Start a new thread, invoking "function(arg)" within the new thread.
129 // When "function(arg)" returns, the thread will be destroyed.
130 virtual void StartThread(void (*function)(void* arg), void* arg) = 0;
131  
132 // *path is set to a temporary directory that can be used for testing. It may
133 // or many not have just been created. The directory may or may not differ
134 // between runs of the same process, but subsequent calls will return the
135 // same directory.
136 virtual Status GetTestDirectory(std::string* path) = 0;
137  
138 // Create and return a log file for storing informational messages.
139 virtual Status NewLogger(const std::string& fname, Logger** result) = 0;
140  
141 // Returns the number of micro-seconds since some fixed point in time. Only
142 // useful for computing deltas of time.
143 virtual uint64_t NowMicros() = 0;
144  
145 // Sleep/delay the thread for the perscribed number of micro-seconds.
146 virtual void SleepForMicroseconds(int micros) = 0;
147  
148 private:
149 // No copying allowed
150 Env(const Env&);
151 void operator=(const Env&);
152 };
153  
154 // A file abstraction for reading sequentially through a file
155 class SequentialFile {
156 public:
157 7 SequentialFile() { }
158 virtual ~SequentialFile();
159  
160 // Read up to "n" bytes from the file. "scratch[0..n-1]" may be
161 // written by this routine. Sets "*result" to the data that was
162 // read (including if fewer than "n" bytes were successfully read).
163 // If an error was encountered, returns a non-OK status.
164 //
165 // REQUIRES: External synchronization
166 virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
167  
168 // Skip "n" bytes from the file. This is guaranteed to be no
169 // slower that reading the same data, but may be faster.
170 //
171 // If end of file is reached, skipping will stop at the end of the
172 // file, and Skip will return OK.
173 //
174 // REQUIRES: External synchronization
175 virtual Status Skip(uint64_t n) = 0;
176 };
177  
178 // A file abstraction for randomly reading the contents of a file.
179 class RandomAccessFile {
180 public:
181 1 RandomAccessFile() { }
182 virtual ~RandomAccessFile();
183  
184 // Read up to "n" bytes from the file starting at "offset".
185 // "scratch[0..n-1]" may be written by this routine. Sets "*result"
186 // to the data that was read (including if fewer than "n" bytes were
187 // successfully read). If an error was encountered, returns a
188 // non-OK status.
189 //
190 // Safe for concurrent use by multiple threads.
191 virtual Status Read(uint64_t offset, size_t n, Slice* result,
192 char* scratch) const = 0;
193 };
194  
195 // A file abstraction for sequential writing. The implementation
196 // must provide buffering since callers may append small fragments
197 // at a time to the file.
198 class WritableFile {
199 public:
200 14 WritableFile() { }
201 virtual ~WritableFile();
202  
203 virtual Status Append(const Slice& data) = 0;
204 virtual Status Close() = 0;
205 virtual Status Flush() = 0;
206 virtual Status Sync() = 0;
207  
208 private:
209 // No copying allowed
210 WritableFile(const WritableFile&);
211 void operator=(const WritableFile&);
212 };
213  
214 // An interface for writing log messages.
215 class Logger {
216 public:
217 4 Logger() { }
218 virtual ~Logger();
219  
220 // Write an entry to the log file with the specified format.
221 virtual void Logv(const char* format, va_list ap) = 0;
222  
223 private:
224 // No copying allowed
225 Logger(const Logger&);
226 void operator=(const Logger&);
227 };
228  
229  
230 // Identifies a locked file.
231 class FileLock {
232 public:
233 3 FileLock() { }
234 virtual ~FileLock();
235 private:
236 // No copying allowed
237 FileLock(const FileLock&);
238 void operator=(const FileLock&);
239 };
240  
241 // Log the specified data to *info_log if info_log is non-NULL.
242 extern void Log(Logger* info_log, const char* format, ...)
243 # if defined(__GNUC__) || defined(__clang__)
244 __attribute__((__format__ (__printf__, 2, 3)))
245 # endif
246 ;
247  
248 // A utility routine: write "data" to the named file.
249 extern Status WriteStringToFile(Env* env, const Slice& data,
250 const std::string& fname);
251  
252 // A utility routine: read contents of named file into *data
253 extern Status ReadFileToString(Env* env, const std::string& fname,
254 std::string* data);
255  
256 // An implementation of Env that forwards all calls to another Env.
257 // May be useful to clients who wish to override just part of the
258 // functionality of another Env.
259 class EnvWrapper : public Env {
260 public:
261 // Initialize an EnvWrapper that delegates all calls to *target
262 explicit EnvWrapper(Env* target) : target_(target) { }
263 virtual ~EnvWrapper();
264  
265 // Return the target to which this Env forwards all calls
266 Env* target() const { return target_; }
267  
268 // The following text is boilerplate that forwards all methods to target()
269 Status NewSequentialFile(const std::string& f, SequentialFile** r) {
270 return target_->NewSequentialFile(f, r);
271 }
272 Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
273 return target_->NewRandomAccessFile(f, r);
274 }
275 Status NewWritableFile(const std::string& f, WritableFile** r) {
276 return target_->NewWritableFile(f, r);
277 }
278 bool FileExists(const std::string& f) { return target_->FileExists(f); }
279 Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
280 return target_->GetChildren(dir, r);
281 }
282 Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
283 Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
284 Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
285 Status GetFileSize(const std::string& f, uint64_t* s) {
286 return target_->GetFileSize(f, s);
287 }
288 Status RenameFile(const std::string& s, const std::string& t) {
289 return target_->RenameFile(s, t);
290 }
291 Status LockFile(const std::string& f, FileLock** l) {
292 return target_->LockFile(f, l);
293 }
294 Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
295 void Schedule(void (*f)(void*), void* a) {
296 return target_->Schedule(f, a);
297 }
298 void StartThread(void (*f)(void*), void* a) {
299 return target_->StartThread(f, a);
300 }
301 virtual Status GetTestDirectory(std::string* path) {
302 return target_->GetTestDirectory(path);
303 }
304 virtual Status NewLogger(const std::string& fname, Logger** result) {
305 return target_->NewLogger(fname, result);
306 }
307 uint64_t NowMicros() {
308 return target_->NowMicros();
309 }
310 void SleepForMicroseconds(int micros) {
311 target_->SleepForMicroseconds(micros);
312 }
313 private:
314 Env* target_;
315 };
316  
317 }
318  
319 #endif // STORAGE_LEVELDB_INCLUDE_ENV_H_
« include/leveldb/db.h ↑ Summary include/leveldb/table.h »