From 383d99079464699fb07cbe01d03ba675e5e87804 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Tue, 9 May 2023 08:38:50 +0800 Subject: [PATCH] cmd: wrap 'surgery clear-page' options into surgeryClearPageOptions Signed-off-by: Benjamin Wang --- cmd/bbolt/command_surgery.go | 62 +++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/cmd/bbolt/command_surgery.go b/cmd/bbolt/command_surgery.go index bb930d9e5..59e525c1c 100644 --- a/cmd/bbolt/command_surgery.go +++ b/cmd/bbolt/command_surgery.go @@ -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 } @@ -185,7 +181,7 @@ func (o *surgeryClearPageElementsOptions) Validate() error { } func newSurgeryClearPageCommand() *cobra.Command { - cfg := defaultSurgeryOptions() + var o surgeryClearPageOptions clearPageCmd := &cobra.Command{ Use: "clear-page [options]", Short: "Clears all elements from the given page, which can be a branch or leaf page", @@ -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) } @@ -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 }