Skip to content

Commit

Permalink
cmd: wrap 'surgery clear-page' options into surgeryClearPageOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed May 9, 2023
1 parent 6e12e08 commit 383d990
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions cmd/bbolt/command_surgery.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,17 @@ func surgeryCopyPageFunc(srcDBPath string, cfg surgeryCopyPageOptions) error {
return nil
}

type surgeryClearPageElementsOptions struct {
type surgeryClearPageOptions struct {
surgeryBaseOptions
pageId uint64
startElementIdx int
endElementIdx int
pageId uint64
}

func (o *surgeryClearPageElementsOptions) AddFlags(fs *pflag.FlagSet) {
func (o *surgeryClearPageOptions) AddFlags(fs *pflag.FlagSet) {
o.surgeryBaseOptions.AddFlags(fs)
fs.Uint64VarP(&o.pageId, "pageId", "", o.pageId, "page id")
fs.IntVarP(&o.startElementIdx, "from-index", "", o.startElementIdx, "start element index (included) to clear, starting from 0")
fs.IntVarP(&o.endElementIdx, "to-index", "", o.endElementIdx, "end element index (excluded) to clear, starting from 0, -1 means to the end of page")
fs.Uint64VarP(&o.pageId, "pageId", "", o.pageId, "page Id")
}

func (o *surgeryClearPageElementsOptions) Validate() error {
func (o *surgeryClearPageOptions) Validate() error {
if err := o.surgeryBaseOptions.Validate(); err != nil {
return err
}
Expand All @@ -185,7 +181,7 @@ func (o *surgeryClearPageElementsOptions) Validate() error {
}

func newSurgeryClearPageCommand() *cobra.Command {
cfg := defaultSurgeryOptions()
var o surgeryClearPageOptions
clearPageCmd := &cobra.Command{
Use: "clear-page <bbolt-file> [options]",
Short: "Clears all elements from the given page, which can be a branch or leaf page",
Expand All @@ -199,26 +195,22 @@ func newSurgeryClearPageCommand() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return surgeryClearPageFunc(args[0], cfg)
if err := o.Validate(); err != nil {
return err
}
return surgeryClearPageFunc(args[0], o)
},
}

clearPageCmd.Flags().StringVar(&cfg.surgeryTargetDBFilePath, "output", "", "path to the target db file")
clearPageCmd.Flags().Uint64VarP(&cfg.surgeryPageId, "pageId", "", 0, "page id")

o.AddFlags(clearPageCmd.Flags())
return clearPageCmd
}

func surgeryClearPageFunc(srcDBPath string, cfg surgeryOptions) error {
if err := common.CopyFile(srcDBPath, cfg.surgeryTargetDBFilePath); err != nil {
func surgeryClearPageFunc(srcDBPath string, cfg surgeryClearPageOptions) error {
if err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {
return fmt.Errorf("[clear-page] copy file failed: %w", err)
}

if cfg.surgeryPageId < 2 {
return fmt.Errorf("the pageId must be at least 2, but got %d", cfg.surgeryPageId)
}

needAbandonFreelist, err := surgeon.ClearPage(cfg.surgeryTargetDBFilePath, common.Pgid(cfg.surgeryPageId))
needAbandonFreelist, err := surgeon.ClearPage(cfg.outputDBFilePath, common.Pgid(cfg.pageId))
if err != nil {
return fmt.Errorf("clear-page command failed: %w", err)
}
Expand All @@ -228,7 +220,31 @@ func surgeryClearPageFunc(srcDBPath string, cfg surgeryOptions) error {
fmt.Fprintf(os.Stdout, "Please consider executing `./bbolt surgery abandon-freelist ...`\n")
}

fmt.Fprintf(os.Stdout, "The page (%d) was cleared\n", cfg.surgeryPageId)
fmt.Fprintf(os.Stdout, "The page (%d) was cleared\n", cfg.pageId)
return nil
}

type surgeryClearPageElementsOptions struct {
surgeryBaseOptions
pageId uint64
startElementIdx int
endElementIdx int
}

func (o *surgeryClearPageElementsOptions) AddFlags(fs *pflag.FlagSet) {
o.surgeryBaseOptions.AddFlags(fs)
fs.Uint64VarP(&o.pageId, "pageId", "", o.pageId, "page id")
fs.IntVarP(&o.startElementIdx, "from-index", "", o.startElementIdx, "start element index (included) to clear, starting from 0")
fs.IntVarP(&o.endElementIdx, "to-index", "", o.endElementIdx, "end element index (excluded) to clear, starting from 0, -1 means to the end of page")
}

func (o *surgeryClearPageElementsOptions) Validate() error {
if err := o.surgeryBaseOptions.Validate(); err != nil {
return err
}
if o.pageId < 2 {
return fmt.Errorf("the pageId must be at least 2, but got %d", o.pageId)
}
return nil
}

Expand Down

0 comments on commit 383d990

Please sign in to comment.