restore.go
restore.go - Overview
This file implements the restore
command for the Badger database, allowing users to restore a database from a backup file.
Detailed Documentation
var restoreFile string
- Purpose: Stores the path to the backup file used for restoring the database. Defaults to "badger.bak".
var maxPendingWrites int
- Purpose: Defines the maximum number of pending writes during the restore process. Defaults to 256.
var restoreCmd *cobra.Command
- Purpose: Defines the Cobra command for the
restore
functionality.- Use: "restore"
- Short: "Restore Badger database."
- Long: Describes the usage of the restore command, including reading a backup file and writing key-value pairs to a new database.
- RunE:
doRestore
function, which executes the restore operation.
func init()
-
Purpose: Initializes the
restoreCmd
by adding it to the root command and defining its flags. -
Parameters: None
-
Returns: None
- Defines flags:
backup-file
: Specifies the backup file to restore from (shorthand: "f", default: "badger.bak").max-pending-writes
: Sets the maximum number of pending writes during restore (shorthand: "w", default: 256).
- Defines flags:
func doRestore(cmd *cobra.Command, args []string) error
-
Purpose: Executes the database restoration process.
-
Parameters:
cmd
: *cobra.Command - The Cobra command being executed.args
:[]string
- Command-line arguments.
-
Returns:
error
- An error if the restoration fails, nil otherwise.Steps:
- Checks if a database already exists at the specified directory. If so, returns an error.
- Opens the Badger database with default options, sets the value directory and number of versions to keep.
- Opens the specified backup file.
- Calls
db.Load
to restore the database from the file. - Closes the database and the backup file using
defer
statements to ensure resources are released.