Class BackupEngine
- Namespace
- RocksDbNet
- Assembly
- RocksDb.Net.dll
Manages backups of a RocksDb database.
Maps to rocksdb_backup_engine_t.
public sealed class BackupEngine : RocksDbHandle, IDisposable
- Inheritance
-
BackupEngine
- Implements
- Inherited Members
Methods
AsEnumerable()
Returns metadata for all available backups (newest first).
public IReadOnlyList<BackupInfo> AsEnumerable()
Returns
Remarks
Read in full before returning, rather than streamed. This used to be a lazy iterator holding the native info object open across the caller's loop body, which leaked it outright if the enumerator was abandoned without being disposed: the object was a bare pointer in a local with no owner, so no finalizer would ever release it.
Reading eagerly costs nothing worth counting, since a backup directory holds tens of entries carrying a few numbers each, and it means the native snapshot lives only for the duration of this call instead of for as long as the caller takes to iterate.
CreateNewBackup(RocksDb, CreateBackupOptions)
Creates a new backup using options, and returns the
identifier RocksDb assigned to it.
public uint CreateNewBackup(RocksDb db, CreateBackupOptions options)
Parameters
dbRocksDboptionsCreateBackupOptions
Returns
CreateNewBackup(RocksDb, CreateBackupOptions, ReadOnlySpan<byte>)
Creates a new backup with application metadata attached, and returns the identifier RocksDb assigned to it.
public uint CreateNewBackup(RocksDb db, CreateBackupOptions options, ReadOnlySpan<byte> appMetadata)
Parameters
dbRocksDbThe database to back up.
optionsCreateBackupOptionsSettings for this backup.
appMetadataReadOnlySpan<byte>Opaque bytes stored alongside the backup and returned in AppMetadata. RocksDb copies them, and treats them as binary, so they need not be text.
Returns
CreateNewBackup(RocksDb, bool)
Creates a new backup of the database.
public void CreateNewBackup(RocksDb db, bool flushBeforeBackup = false)
Parameters
DisposeHandle()
Releases the native handle. Called during disposal.
protected override void DisposeHandle()
Remarks
Protected rather than public: it destroys the native object without marking this instance disposed or clearing the handle, so calling it from outside and then disposing normally would free the same pointer twice. It was the most Dispose-looking member on the type. Callers want Dispose().
DisposeUnmanagedResources()
Releases unmanaged resources used by the current instance.
protected override void DisposeUnmanagedResources()
Remarks
Protected for the same reason as DisposeHandle().
Open(BackupEngineOptions, Env?)
Opens a backup engine configured by options, which
carries the backup directory along with the sharing, rate limiting and
schema settings.
public static BackupEngine Open(BackupEngineOptions options, Env? env = null)
Parameters
optionsBackupEngineOptionsEngine configuration. Read here and not retained.
envEnvThe environment the engine reads the database through, or
nullfor the default one. Not the backup destination, which comes fromoptions.
Returns
Remarks
The environment is required by the C API, which dereferences it without a
null check, so a default one is created here when the caller passes
null.
It is kept for the life of the engine, not just for this call: RocksDb
hands it straight to BackupEngine::Open, which holds the pointer.
A caller-supplied environment therefore has to outlive the engine, and
the engine registers a hold on it so that disposing it early defers
rather than freeing something RocksDb still points at. This page used to
say the opposite, which for an in-memory environment was a use after
free waiting to happen.
Open(DbOptions, string)
Opens a backup engine at the given path.
public static BackupEngine Open(DbOptions options, string backupPath)
Parameters
Returns
PurgeOldBackups(uint)
Removes all but the numBackupsToKeep most recent backups.
public void PurgeOldBackups(uint numBackupsToKeep)
Parameters
numBackupsToKeepuint
RestoreDbFromBackup(string, string, uint, RestoreOptions?)
Restores a specific backup to dbDir.
public void RestoreDbFromBackup(string dbDir, string walDir, uint backupId, RestoreOptions? options = null)
Parameters
dbDirstringwalDirstringbackupIduintoptionsRestoreOptions
RestoreDbFromLatestBackup(string, string)
Restores the latest backup to dbDir.
public void RestoreDbFromLatestBackup(string dbDir, string walDir)
Parameters
RestoreDbFromLatestBackup(string, string, RestoreOptions)
Restores the latest backup to dbDir using explicit options.
public void RestoreDbFromLatestBackup(string dbDir, string walDir, RestoreOptions options)
Parameters
dbDirstringwalDirstringoptionsRestoreOptions
StopBackup()
Asks a backup running on another thread to stop early. The CreateNewBackup(RocksDb, CreateBackupOptions) call it interrupts fails with an error rather than returning a partial backup. This retires the engine permanently.
public void StopBackup()
Remarks
One-way, and this is the part worth knowing before calling it: every
later CreateNewBackup on this engine also fails, not just the one
being interrupted. To take backups again, dispose this engine and open a
new one against the same directory.
Returns immediately without waiting for the backup to wind down. The
interrupted backup leaves its partial state behind, which stays
consistent and is cleaned up by the next CreateNewBackup or
garbage collection on a new engine for that directory.
VerifyBackup(uint)
Checks that the files making up backupId are present
and the expected size.
public void VerifyBackup(uint backupId)
Parameters
backupIduint
Exceptions
- RocksDbException
The backup is missing files or damaged.