Files
zeekayandHanzo Dev fe8d85d663 rename package badger -> zapdb
The module has been github.com/luxfi/zapdb for a while, but the package inside
it still declared itself badger, so every consumer had to write

	zapdb "github.com/luxfi/zapdb" // package badger

and the alias existed only to paper over the mismatch. 56 root files and the
134 qualified references inside the repo now say zapdb.

Backward compatible in both directions: an existing consumer with the alias
keeps compiling (the alias is now merely redundant), and one without it starts
compiling instead of failing on an undefined zapdb.

Directory badger/ and its cmd package are untouched — that is the CLI, and its
binary is named badger.

Build and vet clean; suite green except TestZRestore, which wants a /tmp/zb/src
benchmark fixture and fails identically without this change.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 14:24:18 -07:00

77 lines
1.9 KiB
Go

/*
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package cmd
import (
"errors"
"math"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/luxfi/zapdb"
)
var restoreFile string
var maxPendingWrites int
// restoreCmd represents the restore command
var restoreCmd = &cobra.Command{
Use: "restore",
Short: "Restore Badger database.",
Long: `Restore Badger database from a file.
It reads a file generated using the backup command (or by calling the
DB.Backup() API method) and writes each key-value pair found in the file to
the Badger database.
Restore creates a new database, and currently does not work on an already
existing database.`,
RunE: doRestore,
}
func init() {
RootCmd.AddCommand(restoreCmd)
restoreCmd.Flags().StringVarP(&restoreFile, "backup-file", "f",
"badger.bak", "File to restore from")
// Default value for maxPendingWrites is 256, to minimise memory usage
// and overall finish time.
restoreCmd.Flags().IntVarP(&maxPendingWrites, "max-pending-writes", "w",
256, "Max number of pending writes at any time while restore")
}
func doRestore(cmd *cobra.Command, args []string) error {
// Check if the DB already exists
manifestFile := filepath.Join(sstDir, zapdb.ManifestFilename)
if _, err := os.Stat(manifestFile); err == nil { // No error. File already exists.
return errors.New("Cannot restore to an already existing database")
} else if os.IsNotExist(err) {
// pass
} else { // Return an error if anything other than the error above
return err
}
// Open DB
db, err := zapdb.Open(zapdb.DefaultOptions(sstDir).
WithValueDir(vlogDir).
WithNumVersionsToKeep(math.MaxInt32))
if err != nil {
return err
}
defer db.Close()
// Open File
f, err := os.Open(restoreFile)
if err != nil {
return err
}
defer f.Close()
// Run restore
return db.Load(f, maxPendingWrites)
}