Skip to main content
The Env class abstracts operating system functionality including file system operations, thread management, and system time. It allows RocksDB to be portable across different platforms and enables custom implementations for specialized environments.

Getting the Default Env

Default

Returns the default environment suitable for the current operating system. The result belongs to RocksDB and must never be deleted.
Env*
Returns a pointer to the default environment.

File Operations

NewSequentialFile

Creates a sequentially-readable file.
const std::string&
Path to the file to open.
std::unique_ptr<SequentialFile>*
Output parameter for the created file object.
const EnvOptions&
Options for opening the file (direct I/O, mmap, etc.).
Status
Returns OK on success, or an error status if the file cannot be opened.

NewRandomAccessFile

Creates a random access read-only file.

NewWritableFile

Creates a writable file, deleting any existing file with the same name.

NewDirectory

Creates a directory object for fsync operations.

File System Operations

FileExists

Checks if a file exists.
Status
Returns OK if the file exists, NotFound if it doesn’t exist.

GetChildren

Lists children of a directory.

DeleteFile

Deletes the named file.

CreateDir

Creates a directory. Returns error if it already exists.

CreateDirIfMissing

Creates a directory if it doesn’t exist.

DeleteDir

Deletes a directory.

GetFileSize

Gets the size of a file.

RenameFile

Renames a file.

LinkFile

Creates a hard link.

File Locking

LockFile

Locks a file to prevent concurrent access by multiple processes.
const std::string&
Path to the file to lock. May be created if it doesn’t exist.
FileLock**
Output parameter for the lock object.
Status
Returns OK if the lock was acquired, or an error if another process holds the lock.

UnlockFile

Releases a file lock.

Thread Management

Schedule

Schedules a function to run in a background thread pool.
void (*)(void* arg)
Function to execute.
void*
Argument passed to the function.
Priority
Thread pool priority: BOTTOM, LOW, HIGH, USER.

StartThread

Starts a new thread.

SetBackgroundThreads

Sets the number of background worker threads for a priority level.

GetBackgroundThreads

Gets the number of background threads for a priority level.

Time Functions

NowMicros

Returns the current time in microseconds since some fixed point.
uint64_t
Current time in microseconds.

NowNanos

Returns the current time in nanoseconds. Should return monotonic time points.

GetCurrentTime

Gets seconds since Unix epoch (1970-01-01 00:00:00 UTC).

SleepForMicroseconds

Sleeps for the specified number of microseconds.

System Information

GetHostName

Gets the current host name.

GetAbsolutePath

Converts a relative path to an absolute path.

GetFreeSpace

Gets the amount of free disk space.

EnvOptions

bool
Use O_DIRECT for reading data.
bool
Use O_DIRECT for writing data.
uint64_t
Incrementally sync files to disk while writing. 0 means no incremental sync.

Write Life Time Hints

Hints about expected lifetime of data for storage optimization.

Thread Priorities

Priority
Lowest priority for background operations.
Priority
Default priority for most background operations.
Priority
High priority for operations like flush.

Example

FileSystem API

For more fine-grained control, use the FileSystem interface:
The FileSystem interface provides additional features like:
  • IOOptions for per-request hints
  • Temperature-aware file creation
  • Async I/O support
  • Better error reporting with IODebugContext

See Also