mirror of https://github.com/etcd-io/bbolt.git
resolve some minor review comments
Signed-off-by: Benjamin Wang <wachao@vmware.com>pull/417/head
parent
a9a9356ea4
commit
8902ef92e9
|
@ -31,8 +31,8 @@ func newSurgeryCobraCommand() *cobra.Command {
|
|||
|
||||
func newSurgeryClearPageElementsCommand() *cobra.Command {
|
||||
clearElementCmd := &cobra.Command{
|
||||
Use: "clear-page-elements [options]",
|
||||
Short: "Clears elements from the given page",
|
||||
Use: "clear-page-elements <bbolt-file> [options]",
|
||||
Short: "Clears elements from the given page, which can be a branch or leaf page",
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("db file path not provided")
|
||||
|
|
|
@ -2,7 +2,6 @@ package main_test
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -155,7 +154,7 @@ func testSurgeryClearPageElement(t *testing.T, startIdx, endIdx int, setEndIdxAs
|
|||
|
||||
// clear elements [startIdx, endIdx) in the page
|
||||
rootCmd := main.NewRootCommand()
|
||||
output := filepath.Join(t.TempDir(), fmt.Sprintf("db_%d", rand.Intn(100)))
|
||||
output := filepath.Join(t.TempDir(), "db")
|
||||
rootCmd.SetArgs([]string{
|
||||
"surgery", "clear-page-elements", srcPath,
|
||||
"--output", output,
|
||||
|
|
|
@ -61,14 +61,14 @@ func main() {
|
|||
if err := m.Run(os.Args[1:]...); err == ErrUsage {
|
||||
os.Exit(2)
|
||||
} else if err == ErrUnknownCommand {
|
||||
execute()
|
||||
cobraExecute()
|
||||
} else if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func execute() {
|
||||
func cobraExecute() {
|
||||
rootCmd := NewRootCommand()
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
if rootCmd.SilenceErrors {
|
||||
|
|
|
@ -20,6 +20,12 @@ func ClearPage(path string, pgId common.Pgid) error {
|
|||
return ClearPageElements(path, pgId, 0, -1)
|
||||
}
|
||||
|
||||
// ClearPageElements supports clearing elements in both branch and leaf
|
||||
// pages. Note the freelist may be cleaned in the meta pages in the following
|
||||
// two cases, and bbolt needs to scan the db to reconstruct free list. It may
|
||||
// cause some delay on next startup, depending on the db size.
|
||||
// 1. Any branch elements are cleared;
|
||||
// 2. An object saved in overflow pages is cleared;
|
||||
func ClearPageElements(path string, pgId common.Pgid, start, end int) error {
|
||||
// Read the page
|
||||
p, buf, err := guts_cli.ReadPage(path, uint64(pgId))
|
||||
|
@ -93,16 +99,16 @@ func ClearPageElements(path string, pgId common.Pgid, start, end int) error {
|
|||
}
|
||||
|
||||
func clearFreelist(path string) error {
|
||||
if err := clearFreelistAt(path, 0); err != nil {
|
||||
if err := clearFreelistInMetaPage(path, 0); err != nil {
|
||||
return fmt.Errorf("clearFreelist on meta page 0 failed: %w", err)
|
||||
}
|
||||
if err := clearFreelistAt(path, 1); err != nil {
|
||||
if err := clearFreelistInMetaPage(path, 1); err != nil {
|
||||
return fmt.Errorf("clearFreelist on meta page 1 failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func clearFreelistAt(path string, pageId uint64) error {
|
||||
func clearFreelistInMetaPage(path string, pageId uint64) error {
|
||||
_, buf, err := guts_cli.ReadPage(path, pageId)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ReadPage %d failed: %w", pageId, err)
|
||||
|
|
Loading…
Reference in New Issue