@ -54,17 +54,16 @@ const size_t kDefaultPageSize = 4 * 1024;
// Options while opening a file to read/write
// Options while opening a file to read/write
struct EnvOptions {
struct EnvOptions {
// Construct with default Options
// Construct with default Options
EnvOptions ( ) ;
EnvOptions ( ) ;
// Construct from Options
// Construct from Options
explicit EnvOptions ( const DBOptions & options ) ;
explicit EnvOptions ( const DBOptions & options ) ;
// If true, then use mmap to read data
// If true, then use mmap to read data
bool use_mmap_reads = false ;
bool use_mmap_reads = false ;
// If true, then use mmap to write data
// If true, then use mmap to write data
bool use_mmap_writes = true ;
bool use_mmap_writes = true ;
// If true, then use O_DIRECT for reading data
// If true, then use O_DIRECT for reading data
@ -150,12 +149,12 @@ class Env {
// These values match Linux definition
// These values match Linux definition
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/fcntl.h#n56
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/fcntl.h#n56
enum WriteLifeTimeHint {
enum WriteLifeTimeHint {
WLTH_NOT_SET = 0 , // No hint information set
WLTH_NOT_SET = 0 , // No hint information set
WLTH_NONE , // No hints about write life time
WLTH_NONE , // No hints about write life time
WLTH_SHORT , // Data written has a short life time
WLTH_SHORT , // Data written has a short life time
WLTH_MEDIUM , // Data written has a medium life time
WLTH_MEDIUM , // Data written has a medium life time
WLTH_LONG , // Data written has a long life time
WLTH_LONG , // Data written has a long life time
WLTH_EXTREME , // Data written has an extremely long life time
WLTH_EXTREME , // Data written has an extremely long life time
} ;
} ;
// Create an object that writes to a new file with the specified
// Create an object that writes to a new file with the specified
@ -321,11 +320,7 @@ class Env {
static std : : string PriorityToString ( Priority priority ) ;
static std : : string PriorityToString ( Priority priority ) ;
// Priority for requesting bytes in rate limiter scheduler
// Priority for requesting bytes in rate limiter scheduler
enum IOPriority {
enum IOPriority { IO_LOW = 0 , IO_HIGH = 1 , IO_TOTAL = 2 } ;
IO_LOW = 0 ,
IO_HIGH = 1 ,
IO_TOTAL = 2
} ;
// Arrange to run "(*function)(arg)" once in a background thread, in
// Arrange to run "(*function)(arg)" once in a background thread, in
// the thread pool specified by pri. By default, jobs go to the 'LOW'
// the thread pool specified by pri. By default, jobs go to the 'LOW'
@ -377,9 +372,7 @@ class Env {
// Default implementation simply relies on NowMicros.
// Default implementation simply relies on NowMicros.
// In platform-specific implementations, NowNanos() should return time points
// In platform-specific implementations, NowNanos() should return time points
// that are MONOTONIC.
// that are MONOTONIC.
virtual uint64_t NowNanos ( ) {
virtual uint64_t NowNanos ( ) { return NowMicros ( ) * 1000 ; }
return NowMicros ( ) * 1000 ;
}
// 0 indicates not supported.
// 0 indicates not supported.
virtual uint64_t NowCPUNanos ( ) { return 0 ; }
virtual uint64_t NowCPUNanos ( ) { return 0 ; }
@ -396,7 +389,7 @@ class Env {
// Get full directory name for this db.
// Get full directory name for this db.
virtual Status GetAbsolutePath ( const std : : string & db_path ,
virtual Status GetAbsolutePath ( const std : : string & db_path ,
std : : string * output_path ) = 0 ;
std : : string * output_path ) = 0 ;
// The number of background worker threads of a specific thread pool
// The number of background worker threads of a specific thread pool
// for this environment. 'LOW' is the default pool.
// for this environment. 'LOW' is the default pool.
@ -503,7 +496,7 @@ ThreadStatusUpdater* CreateThreadStatusUpdater();
// A file abstraction for reading sequentially through a file
// A file abstraction for reading sequentially through a file
class SequentialFile {
class SequentialFile {
public :
public :
SequentialFile ( ) { }
SequentialFile ( ) { }
virtual ~ SequentialFile ( ) ;
virtual ~ SequentialFile ( ) ;
// Read up to "n" bytes from the file. "scratch[0..n-1]" may be
// Read up to "n" bytes from the file. "scratch[0..n-1]" may be
@ -551,8 +544,7 @@ class SequentialFile {
// A file abstraction for randomly reading the contents of a file.
// A file abstraction for randomly reading the contents of a file.
class RandomAccessFile {
class RandomAccessFile {
public :
public :
RandomAccessFile ( ) { }
RandomAccessFile ( ) { }
virtual ~ RandomAccessFile ( ) ;
virtual ~ RandomAccessFile ( ) ;
// Read up to "n" bytes from the file starting at "offset".
// Read up to "n" bytes from the file starting at "offset".
@ -589,8 +581,8 @@ class RandomAccessFile {
//
//
// Note: these IDs are only valid for the duration of the process.
// Note: these IDs are only valid for the duration of the process.
virtual size_t GetUniqueId ( char * /*id*/ , size_t /*max_size*/ ) const {
virtual size_t GetUniqueId ( char * /*id*/ , size_t /*max_size*/ ) const {
return 0 ; // Default implementation to prevent issues with backwards
return 0 ; // Default implementation to prevent issues with backwards
// compatibility.
// compatibility.
} ;
} ;
enum AccessPattern { NORMAL , RANDOM , SEQUENTIAL , WILLNEED , DONTNEED } ;
enum AccessPattern { NORMAL , RANDOM , SEQUENTIAL , WILLNEED , DONTNEED } ;
@ -619,11 +611,10 @@ class RandomAccessFile {
class WritableFile {
class WritableFile {
public :
public :
WritableFile ( )
WritableFile ( )
: last_preallocated_block_ ( 0 ) ,
: last_preallocated_block_ ( 0 ) ,
preallocation_block_size_ ( 0 ) ,
preallocation_block_size_ ( 0 ) ,
io_priority_ ( Env : : IO_TOTAL ) ,
io_priority_ ( Env : : IO_TOTAL ) ,
write_hint_ ( Env : : WLTH_NOT_SET ) {
write_hint_ ( Env : : WLTH_NOT_SET ) { }
}
virtual ~ WritableFile ( ) ;
virtual ~ WritableFile ( ) ;
// Append data to the end of the file
// Append data to the end of the file
@ -651,7 +642,8 @@ class WritableFile {
//
//
// PositionedAppend() requires aligned buffer to be passed in. The alignment
// PositionedAppend() requires aligned buffer to be passed in. The alignment
// required is queried via GetRequiredBufferAlignment()
// required is queried via GetRequiredBufferAlignment()
virtual Status PositionedAppend ( const Slice & /* data */ , uint64_t /* offset */ ) {
virtual Status PositionedAppend ( const Slice & /* data */ ,
uint64_t /* offset */ ) {
return Status : : NotSupported ( ) ;
return Status : : NotSupported ( ) ;
}
}
@ -662,7 +654,7 @@ class WritableFile {
virtual Status Truncate ( uint64_t /*size*/ ) { return Status : : OK ( ) ; }
virtual Status Truncate ( uint64_t /*size*/ ) { return Status : : OK ( ) ; }
virtual Status Close ( ) = 0 ;
virtual Status Close ( ) = 0 ;
virtual Status Flush ( ) = 0 ;
virtual Status Flush ( ) = 0 ;
virtual Status Sync ( ) = 0 ; // sync data
virtual Status Sync ( ) = 0 ; // sync data
/*
/*
* Sync data and / or metadata as well .
* Sync data and / or metadata as well .
@ -670,15 +662,11 @@ class WritableFile {
* Override this method for environments where we need to sync
* Override this method for environments where we need to sync
* metadata as well .
* metadata as well .
*/
*/
virtual Status Fsync ( ) {
virtual Status Fsync ( ) { return Sync ( ) ; }
return Sync ( ) ;
}
// true if Sync() and Fsync() are safe to call concurrently with Append()
// true if Sync() and Fsync() are safe to call concurrently with Append()
// and Flush().
// and Flush().
virtual bool IsSyncThreadSafe ( ) const {
virtual bool IsSyncThreadSafe ( ) const { return false ; }
return false ;
}
// Indicates the upper layers if the current WritableFile implementation
// Indicates the upper layers if the current WritableFile implementation
// uses direct IO.
// uses direct IO.
@ -691,9 +679,7 @@ class WritableFile {
* Change the priority in rate limiter if rate limiting is enabled .
* Change the priority in rate limiter if rate limiting is enabled .
* If rate limiting is not enabled , this call has no effect .
* If rate limiting is not enabled , this call has no effect .
*/
*/
virtual void SetIOPriority ( Env : : IOPriority pri ) {
virtual void SetIOPriority ( Env : : IOPriority pri ) { io_priority_ = pri ; }
io_priority_ = pri ;
}
virtual Env : : IOPriority GetIOPriority ( ) { return io_priority_ ; }
virtual Env : : IOPriority GetIOPriority ( ) { return io_priority_ ; }
@ -705,9 +691,7 @@ class WritableFile {
/*
/*
* Get the size of valid data in the file .
* Get the size of valid data in the file .
*/
*/
virtual uint64_t GetFileSize ( ) {
virtual uint64_t GetFileSize ( ) { return 0 ; }
return 0 ;
}
/*
/*
* Get and set the default pre - allocation block size for writes to
* Get and set the default pre - allocation block size for writes to
@ -727,7 +711,7 @@ class WritableFile {
// For documentation, refer to RandomAccessFile::GetUniqueId()
// For documentation, refer to RandomAccessFile::GetUniqueId()
virtual size_t GetUniqueId ( char * /*id*/ , size_t /*max_size*/ ) const {
virtual size_t GetUniqueId ( char * /*id*/ , size_t /*max_size*/ ) const {
return 0 ; // Default implementation to prevent issues with backwards
return 0 ; // Default implementation to prevent issues with backwards
}
}
// Remove any kind of caching of data from the offset to offset+length
// Remove any kind of caching of data from the offset to offset+length
@ -762,10 +746,10 @@ class WritableFile {
// cover this write would be and Allocate to that point.
// cover this write would be and Allocate to that point.
const auto block_size = preallocation_block_size_ ;
const auto block_size = preallocation_block_size_ ;
size_t new_last_preallocated_block =
size_t new_last_preallocated_block =
( offset + len + block_size - 1 ) / block_size ;
( offset + len + block_size - 1 ) / block_size ;
if ( new_last_preallocated_block > last_preallocated_block_ ) {
if ( new_last_preallocated_block > last_preallocated_block_ ) {
size_t num_spanned_blocks =
size_t num_spanned_blocks =
new_last_preallocated_block - last_preallocated_block_ ;
new_last_preallocated_block - last_preallocated_block_ ;
Allocate ( block_size * last_preallocated_block_ ,
Allocate ( block_size * last_preallocated_block_ ,
block_size * num_spanned_blocks ) ;
block_size * num_spanned_blocks ) ;
last_preallocated_block_ = new_last_preallocated_block ;
last_preallocated_block_ = new_last_preallocated_block ;
@ -835,7 +819,7 @@ class RandomRWFile {
// MemoryMappedFileBuffer object represents a memory-mapped file's raw buffer.
// MemoryMappedFileBuffer object represents a memory-mapped file's raw buffer.
// Subclasses should release the mapping upon destruction.
// Subclasses should release the mapping upon destruction.
class MemoryMappedFileBuffer {
class MemoryMappedFileBuffer {
public :
public :
MemoryMappedFileBuffer ( void * _base , size_t _length )
MemoryMappedFileBuffer ( void * _base , size_t _length )
: base_ ( _base ) , length_ ( _length ) { }
: base_ ( _base ) , length_ ( _length ) { }
@ -846,11 +830,11 @@ public:
MemoryMappedFileBuffer ( const MemoryMappedFileBuffer & ) = delete ;
MemoryMappedFileBuffer ( const MemoryMappedFileBuffer & ) = delete ;
MemoryMappedFileBuffer & operator = ( const MemoryMappedFileBuffer & ) = delete ;
MemoryMappedFileBuffer & operator = ( const MemoryMappedFileBuffer & ) = delete ;
void * GetBase ( ) const { return base_ ; }
void * GetBase ( ) const { return base_ ; }
size_t GetLen ( ) const { return length_ ; }
size_t GetLen ( ) const { return length_ ; }
protected :
protected :
void * base_ ;
void * base_ ;
const size_t length_ ;
const size_t length_ ;
} ;
} ;
@ -907,7 +891,8 @@ class Logger {
// and format. Any log with level under the internal log level
// and format. Any log with level under the internal log level
// of *this (see @SetInfoLogLevel and @GetInfoLogLevel) will not be
// of *this (see @SetInfoLogLevel and @GetInfoLogLevel) will not be
// printed.
// printed.
virtual void Logv ( const InfoLogLevel log_level , const char * format , va_list ap ) ;
virtual void Logv ( const InfoLogLevel log_level , const char * format ,
va_list ap ) ;
virtual size_t GetLogFileSize ( ) const { return kDoNotSupportGetLogFileSize ; }
virtual size_t GetLogFileSize ( ) const { return kDoNotSupportGetLogFileSize ; }
// Flush to the OS buffers
// Flush to the OS buffers
@ -928,12 +913,12 @@ class Logger {
InfoLogLevel log_level_ ;
InfoLogLevel log_level_ ;
} ;
} ;
// Identifies a locked file.
// Identifies a locked file.
class FileLock {
class FileLock {
public :
public :
FileLock ( ) { }
FileLock ( ) { }
virtual ~ FileLock ( ) ;
virtual ~ FileLock ( ) ;
private :
private :
// No copying allowed
// No copying allowed
FileLock ( const FileLock & ) ;
FileLock ( const FileLock & ) ;
@ -964,21 +949,21 @@ extern void Fatal(const std::shared_ptr<Logger>& info_log, const char* format,
// The default info log level is InfoLogLevel::INFO_LEVEL.
// The default info log level is InfoLogLevel::INFO_LEVEL.
extern void Log ( const std : : shared_ptr < Logger > & info_log , const char * format ,
extern void Log ( const std : : shared_ptr < Logger > & info_log , const char * format ,
. . . )
. . . )
# if defined(__GNUC__) || defined(__clang__)
# if defined(__GNUC__) || defined(__clang__)
__attribute__ ( ( __format__ ( __printf__ , 2 , 3 ) ) )
__attribute__ ( ( __format__ ( __printf__ , 2 , 3 ) ) )
# endif
# endif
;
;
extern void LogFlush ( Logger * info_log ) ;
extern void LogFlush ( Logger * info_log ) ;
extern void Log ( const InfoLogLevel log_level , Logger * info_log ,
extern void Log ( const InfoLogLevel log_level , Logger * info_log ,
const char * format , . . . ) ;
const char * format , . . . ) ;
// The default info log level is InfoLogLevel::INFO_LEVEL.
// The default info log level is InfoLogLevel::INFO_LEVEL.
extern void Log ( Logger * info_log , const char * format , . . . )
extern void Log ( Logger * info_log , const char * format , . . . )
# if defined(__GNUC__) || defined(__clang__)
# if defined(__GNUC__) || defined(__clang__)
__attribute__ ( ( __format__ ( __printf__ , 2 , 3 ) ) )
__attribute__ ( ( __format__ ( __printf__ , 2 , 3 ) ) )
# endif
# endif
;
;
// a set of log functions with different log levels.
// a set of log functions with different log levels.
@ -1004,7 +989,7 @@ extern Status ReadFileToString(Env* env, const std::string& fname,
class EnvWrapper : public Env {
class EnvWrapper : public Env {
public :
public :
// Initialize an EnvWrapper that delegates all calls to *t
// Initialize an EnvWrapper that delegates all calls to *t
explicit EnvWrapper ( Env * t ) : target_ ( t ) { }
explicit EnvWrapper ( Env * t ) : target_ ( t ) { }
~ EnvWrapper ( ) override ;
~ EnvWrapper ( ) override ;
// Return the target to which this Env forwards all calls
// Return the target to which this Env forwards all calls
@ -1174,9 +1159,7 @@ class EnvWrapper : public Env {
return target_ - > GetThreadStatusUpdater ( ) ;
return target_ - > GetThreadStatusUpdater ( ) ;
}
}
uint64_t GetThreadID ( ) const override {
uint64_t GetThreadID ( ) const override { return target_ - > GetThreadID ( ) ; }
return target_ - > GetThreadID ( ) ;
}
std : : string GenerateUniqueId ( ) override {
std : : string GenerateUniqueId ( ) override {
return target_ - > GenerateUniqueId ( ) ;
return target_ - > GenerateUniqueId ( ) ;
@ -1219,7 +1202,7 @@ class EnvWrapper : public Env {
// protected virtual methods.
// protected virtual methods.
class WritableFileWrapper : public WritableFile {
class WritableFileWrapper : public WritableFile {
public :
public :
explicit WritableFileWrapper ( WritableFile * t ) : target_ ( t ) { }
explicit WritableFileWrapper ( WritableFile * t ) : target_ ( t ) { }
Status Append ( const Slice & data ) override { return target_ - > Append ( data ) ; }
Status Append ( const Slice & data ) override { return target_ - > Append ( data ) ; }
Status PositionedAppend ( const Slice & data , uint64_t offset ) override {
Status PositionedAppend ( const Slice & data , uint64_t offset ) override {