Bitcoin
env.h
Go to the documentation of this file.
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 <string>
17 #include <vector>
18 #include <stdarg.h>
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  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  // Create an object that either appends to an existing file, or
73  // writes to a new file (if the file does not exist to begin with).
74  // On success, stores a pointer to the new file in *result and
75  // returns OK. On failure stores NULL in *result and returns
76  // non-OK.
77  //
78  // The returned file will only be accessed by one thread at a time.
79  //
80  // May return an IsNotSupportedError error if this Env does
81  // not allow appending to an existing file. Users of Env (including
82  // the leveldb implementation) must be prepared to deal with
83  // an Env that does not support appending.
84  virtual Status NewAppendableFile(const std::string& fname,
85  WritableFile** result);
86 
87  // Returns true iff the named file exists.
88  virtual bool FileExists(const std::string& fname) = 0;
89 
90  // Store in *result the names of the children of the specified directory.
91  // The names are relative to "dir".
92  // Original contents of *results are dropped.
93  virtual Status GetChildren(const std::string& dir,
94  std::vector<std::string>* result) = 0;
95 
96  // Delete the named file.
97  virtual Status DeleteFile(const std::string& fname) = 0;
98 
99  // Create the specified directory.
100  virtual Status CreateDir(const std::string& dirname) = 0;
101 
102  // Delete the specified directory.
103  virtual Status DeleteDir(const std::string& dirname) = 0;
104 
105  // Store the size of fname in *file_size.
106  virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) = 0;
107 
108  // Rename file src to target.
109  virtual Status RenameFile(const std::string& src,
110  const std::string& target) = 0;
111 
112  // Lock the specified file. Used to prevent concurrent access to
113  // the same db by multiple processes. On failure, stores NULL in
114  // *lock and returns non-OK.
115  //
116  // On success, stores a pointer to the object that represents the
117  // acquired lock in *lock and returns OK. The caller should call
118  // UnlockFile(*lock) to release the lock. If the process exits,
119  // the lock will be automatically released.
120  //
121  // If somebody else already holds the lock, finishes immediately
122  // with a failure. I.e., this call does not wait for existing locks
123  // to go away.
124  //
125  // May create the named file if it does not already exist.
126  virtual Status LockFile(const std::string& fname, FileLock** lock) = 0;
127 
128  // Release the lock acquired by a previous successful call to LockFile.
129  // REQUIRES: lock was returned by a successful LockFile() call
130  // REQUIRES: lock has not already been unlocked.
131  virtual Status UnlockFile(FileLock* lock) = 0;
132 
133  // Arrange to run "(*function)(arg)" once in a background thread.
134  //
135  // "function" may run in an unspecified thread. Multiple functions
136  // added to the same Env may run concurrently in different threads.
137  // I.e., the caller may not assume that background work items are
138  // serialized.
139  virtual void Schedule(
140  void (*function)(void* arg),
141  void* arg) = 0;
142 
143  // Start a new thread, invoking "function(arg)" within the new thread.
144  // When "function(arg)" returns, the thread will be destroyed.
145  virtual void StartThread(void (*function)(void* arg), void* arg) = 0;
146 
147  // *path is set to a temporary directory that can be used for testing. It may
148  // or many not have just been created. The directory may or may not differ
149  // between runs of the same process, but subsequent calls will return the
150  // same directory.
151  virtual Status GetTestDirectory(std::string* path) = 0;
152 
153  // Create and return a log file for storing informational messages.
154  virtual Status NewLogger(const std::string& fname, Logger** result) = 0;
155 
156  // Returns the number of micro-seconds since some fixed point in time. Only
157  // useful for computing deltas of time.
158  virtual uint64_t NowMicros() = 0;
159 
160  // Sleep/delay the thread for the prescribed number of micro-seconds.
161  virtual void SleepForMicroseconds(int micros) = 0;
162 
163  private:
164  // No copying allowed
165  Env(const Env&);
166  void operator=(const Env&);
167 };
168 
169 // A file abstraction for reading sequentially through a file
171  public:
173  virtual ~SequentialFile();
174 
175  // Read up to "n" bytes from the file. "scratch[0..n-1]" may be
176  // written by this routine. Sets "*result" to the data that was
177  // read (including if fewer than "n" bytes were successfully read).
178  // May set "*result" to point at data in "scratch[0..n-1]", so
179  // "scratch[0..n-1]" must be live when "*result" is used.
180  // If an error was encountered, returns a non-OK status.
181  //
182  // REQUIRES: External synchronization
183  virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
184 
185  // Skip "n" bytes from the file. This is guaranteed to be no
186  // slower that reading the same data, but may be faster.
187  //
188  // If end of file is reached, skipping will stop at the end of the
189  // file, and Skip will return OK.
190  //
191  // REQUIRES: External synchronization
192  virtual Status Skip(uint64_t n) = 0;
193 
194  // Get a name for the file, only for error reporting
195  virtual std::string GetName() const = 0;
196 
197  private:
198  // No copying allowed
200  void operator=(const SequentialFile&);
201 };
202 
203 // A file abstraction for randomly reading the contents of a file.
205  public:
207  virtual ~RandomAccessFile();
208 
209  // Read up to "n" bytes from the file starting at "offset".
210  // "scratch[0..n-1]" may be written by this routine. Sets "*result"
211  // to the data that was read (including if fewer than "n" bytes were
212  // successfully read). May set "*result" to point at data in
213  // "scratch[0..n-1]", so "scratch[0..n-1]" must be live when
214  // "*result" is used. If an error was encountered, returns a non-OK
215  // status.
216  //
217  // Safe for concurrent use by multiple threads.
218  virtual Status Read(uint64_t offset, size_t n, Slice* result,
219  char* scratch) const = 0;
220 
221  // Get a name for the file, only for error reporting
222  virtual std::string GetName() const = 0;
223 
224  private:
225  // No copying allowed
227  void operator=(const RandomAccessFile&);
228 };
229 
230 // A file abstraction for sequential writing. The implementation
231 // must provide buffering since callers may append small fragments
232 // at a time to the file.
234  public:
236  virtual ~WritableFile();
237 
238  virtual Status Append(const Slice& data) = 0;
239  virtual Status Close() = 0;
240  virtual Status Flush() = 0;
241  virtual Status Sync() = 0;
242 
243  // Get a name for the file, only for error reporting
244  virtual std::string GetName() const = 0;
245 
246  private:
247  // No copying allowed
248  WritableFile(const WritableFile&);
249  void operator=(const WritableFile&);
250 };
251 
252 // An interface for writing log messages.
253 class Logger {
254  public:
255  Logger() { }
256  virtual ~Logger();
257 
258  // Write an entry to the log file with the specified format.
259  virtual void Logv(const char* format, va_list ap) = 0;
260 
261  private:
262  // No copying allowed
263  Logger(const Logger&);
264  void operator=(const Logger&);
265 };
266 
267 
268 // Identifies a locked file.
269 class FileLock {
270  public:
271  FileLock() { }
272  virtual ~FileLock();
273  private:
274  // No copying allowed
275  FileLock(const FileLock&);
276  void operator=(const FileLock&);
277 };
278 
279 // Log the specified data to *info_log if info_log is non-NULL.
280 extern void Log(Logger* info_log, const char* format, ...)
281 # if defined(__GNUC__) || defined(__clang__)
282  __attribute__((__format__ (__printf__, 2, 3)))
283 # endif
284  ;
285 
286 // A utility routine: write "data" to the named file.
287 extern Status WriteStringToFile(Env* env, const Slice& data,
288  const std::string& fname);
289 
290 // A utility routine: read contents of named file into *data
291 extern Status ReadFileToString(Env* env, const std::string& fname,
292  std::string* data);
293 
294 // An implementation of Env that forwards all calls to another Env.
295 // May be useful to clients who wish to override just part of the
296 // functionality of another Env.
297 class EnvWrapper : public Env {
298  public:
299  // Initialize an EnvWrapper that delegates all calls to *t
300  explicit EnvWrapper(Env* t) : target_(t) { }
301  virtual ~EnvWrapper();
302 
303  // Return the target to which this Env forwards all calls
304  Env* target() const { return target_; }
305 
306  // The following text is boilerplate that forwards all methods to target()
307  Status NewSequentialFile(const std::string& f, SequentialFile** r) {
308  return target_->NewSequentialFile(f, r);
309  }
310  Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
311  return target_->NewRandomAccessFile(f, r);
312  }
313  Status NewWritableFile(const std::string& f, WritableFile** r) {
314  return target_->NewWritableFile(f, r);
315  }
316  Status NewAppendableFile(const std::string& f, WritableFile** r) {
317  return target_->NewAppendableFile(f, r);
318  }
319  bool FileExists(const std::string& f) { return target_->FileExists(f); }
320  Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
321  return target_->GetChildren(dir, r);
322  }
323  Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
324  Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
325  Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
326  Status GetFileSize(const std::string& f, uint64_t* s) {
327  return target_->GetFileSize(f, s);
328  }
329  Status RenameFile(const std::string& s, const std::string& t) {
330  return target_->RenameFile(s, t);
331  }
332  Status LockFile(const std::string& f, FileLock** l) {
333  return target_->LockFile(f, l);
334  }
336  void Schedule(void (*f)(void*), void* a) {
337  return target_->Schedule(f, a);
338  }
339  void StartThread(void (*f)(void*), void* a) {
340  return target_->StartThread(f, a);
341  }
342  virtual Status GetTestDirectory(std::string* path) {
343  return target_->GetTestDirectory(path);
344  }
345  virtual Status NewLogger(const std::string& fname, Logger** result) {
346  return target_->NewLogger(fname, result);
347  }
349  return target_->NowMicros();
350  }
351  void SleepForMicroseconds(int micros) {
352  target_->SleepForMicroseconds(micros);
353  }
354  private:
356 };
357 
358 } // namespace leveldb
359 
360 #endif // STORAGE_LEVELDB_INCLUDE_ENV_H_
virtual Status Flush()=0
Definition: env.h:170
SequentialFile()
Definition: env.h:172
virtual std::string GetName() const =0
FileLock()
Definition: env.h:271
virtual ~EnvWrapper()
Definition: env.cc:97
Definition: autocompact_test.cc:11
Status GetFileSize(const std::string &f, uint64_t *s)
Definition: env.h:326
Status NewWritableFile(const std::string &f, WritableFile **r)
Definition: env.h:313
bool FileExists(const std::string &f)
Definition: env.h:319
Status WriteStringToFile(Env *env, const Slice &data, const std::string &fname)
Definition: env.cc:62
virtual Status UnlockFile(FileLock *lock)=0
virtual Status Skip(uint64_t n)=0
void operator=(const FileLock &)
void operator=(const Logger &)
Definition: env.h:233
virtual std::string GetName() const =0
void SleepForMicroseconds(int micros)
Definition: env.h:351
virtual ~RandomAccessFile()
Definition: env.cc:19
virtual void SleepForMicroseconds(int micros)=0
virtual ~Logger()
Definition: env.cc:25
void operator=(const RandomAccessFile &)
void Log(Logger *info_log, const char *format,...)
Definition: env.cc:31
RandomAccessFile()
Definition: env.h:206
Definition: env.h:269
virtual Status GetFileSize(const std::string &fname, uint64_t *file_size)=0
virtual Status NewWritableFile(const std::string &fname, WritableFile **result)=0
virtual ~WritableFile()
Definition: env.cc:22
virtual Status GetTestDirectory(std::string *path)=0
f
Definition: linearize-data.py:263
virtual Status Close()=0
virtual std::string GetName() const =0
virtual Status NewRandomAccessFile(const std::string &fname, RandomAccessFile **result)=0
virtual Status CreateDir(const std::string &dirname)=0
Definition: env.h:253
Status DeleteDir(const std::string &d)
Definition: env.h:325
virtual void StartThread(void(*function)(void *arg), void *arg)=0
uint64_t NowMicros()
Definition: env.h:348
void Schedule(void(*f)(void *), void *a)
Definition: env.h:336
virtual Status GetTestDirectory(std::string *path)
Definition: env.h:342
unsigned long long uint64_t
Definition: stdint.h:22
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:967
virtual ~SequentialFile()
Definition: env.cc:16
Env * target_
Definition: env.h:355
Definition: env.h:204
Status RenameFile(const std::string &s, const std::string &t)
Definition: env.h:329
Status DeleteFile(const std::string &f)
Definition: env.h:323
virtual Status LockFile(const std::string &fname, FileLock **lock)=0
virtual Status RenameFile(const std::string &src, const std::string &target)=0
virtual bool FileExists(const std::string &fname)=0
Definition: env.h:31
virtual void Logv(const char *format, va_list ap)=0
virtual ~FileLock()
Definition: env.cc:28
Definition: env.h:297
Definition: slice.h:25
void operator=(const SequentialFile &)
virtual Status NewAppendableFile(const std::string &fname, WritableFile **result)
Definition: env.cc:12
void StartThread(void(*f)(void *), void *a)
Definition: env.h:339
virtual Status Read(size_t n, Slice *result, char *scratch)=0
virtual Status NewLogger(const std::string &fname, Logger **result)
Definition: env.h:345
virtual void Schedule(void(*function)(void *arg), void *arg)=0
std::vector< uint8_t > data
Definition: bech32.cpp:10
virtual Status NewSequentialFile(const std::string &fname, SequentialFile **result)=0
virtual ~Env()
Definition: env.cc:9
Status NewAppendableFile(const std::string &f, WritableFile **r)
Definition: env.h:316
Status NewSequentialFile(const std::string &f, SequentialFile **r)
Definition: env.h:307
virtual uint64_t NowMicros()=0
Definition: status.h:21
Env()
Definition: env.h:33
Status CreateDir(const std::string &d)
Definition: env.h:324
virtual Status Append(const Slice &data)=0
virtual Status Sync()=0
Status UnlockFile(FileLock *l)
Definition: env.h:335
Status GetChildren(const std::string &dir, std::vector< std::string > *r)
Definition: env.h:320
static Env * Default()
Definition: env_posix.cc:699
virtual Status DeleteFile(const std::string &fname)=0
virtual Status DeleteDir(const std::string &dirname)=0
virtual Status Read(uint64_t offset, size_t n, Slice *result, char *scratch) const =0
Logger()
Definition: env.h:255
virtual Status NewLogger(const std::string &fname, Logger **result)=0
void operator=(const WritableFile &)
EnvWrapper(Env *t)
Definition: env.h:300
Status NewRandomAccessFile(const std::string &f, RandomAccessFile **r)
Definition: env.h:310
virtual Status GetChildren(const std::string &dir, std::vector< std::string > *result)=0
void operator=(const Env &)
Status LockFile(const std::string &f, FileLock **l)
Definition: env.h:332
WritableFile()
Definition: env.h:235
Status ReadFileToString(Env *env, const std::string &fname, std::string *data)
Definition: env.cc:72
Env * target() const
Definition: env.h:304