@@ -19,14 +19,15 @@ var (
19
19
)
20
20
21
21
func newSurgeryCobraCommand () * cobra.Command {
22
- cmd := & cobra.Command {
22
+ surgeryCmd := & cobra.Command {
23
23
Use : "surgery <subcommand>" ,
24
24
Short : "surgery related commands" ,
25
25
}
26
26
27
- cmd .AddCommand (newSurgeryClearPageElementsCommand ())
27
+ surgeryCmd .AddCommand (newSurgeryClearPageElementsCommand ())
28
+ surgeryCmd .AddCommand (newSurgeryFreelistCommand ())
28
29
29
- return cmd
30
+ return surgeryCmd
30
31
}
31
32
32
33
func newSurgeryClearPageElementsCommand () * cobra.Command {
@@ -42,7 +43,6 @@ func newSurgeryClearPageElementsCommand() *cobra.Command {
42
43
}
43
44
return nil
44
45
},
45
-
46
46
RunE : surgeryClearPageElementFunc ,
47
47
}
48
48
@@ -78,3 +78,53 @@ func surgeryClearPageElementFunc(cmd *cobra.Command, args []string) error {
78
78
fmt .Fprintf (os .Stdout , "All elements in [%d, %d) in page %d were cleared\n " , surgeryStartElementIdx , surgeryEndElementIdx , surgeryPageId )
79
79
return nil
80
80
}
81
+
82
+ // TODO(ahrtr): add `bbolt surgery freelist rebuild/check ...` commands,
83
+ // and move all `surgery freelist` commands into a separate file,
84
+ // e.g command_surgery_freelist.go.
85
+ func newSurgeryFreelistCommand () * cobra.Command {
86
+ cmd := & cobra.Command {
87
+ Use : "freelist <subcommand>" ,
88
+ Short : "freelist related surgery commands" ,
89
+ }
90
+
91
+ cmd .AddCommand (newSurgeryFreelistAbandonCommand ())
92
+
93
+ return cmd
94
+ }
95
+
96
+ func newSurgeryFreelistAbandonCommand () * cobra.Command {
97
+ abandonFreelistCmd := & cobra.Command {
98
+ Use : "abandon <bbolt-file> [options]" ,
99
+ Short : "Abandon the freelist from both meta pages" ,
100
+ Args : func (cmd * cobra.Command , args []string ) error {
101
+ if len (args ) == 0 {
102
+ return errors .New ("db file path not provided" )
103
+ }
104
+ if len (args ) > 1 {
105
+ return errors .New ("too many arguments" )
106
+ }
107
+ return nil
108
+ },
109
+ RunE : surgeryFreelistAbandonFunc ,
110
+ }
111
+
112
+ abandonFreelistCmd .Flags ().StringVar (& surgeryTargetDBFilePath , "output" , "" , "path to the target db file" )
113
+
114
+ return abandonFreelistCmd
115
+ }
116
+
117
+ func surgeryFreelistAbandonFunc (cmd * cobra.Command , args []string ) error {
118
+ srcDBPath := args [0 ]
119
+
120
+ if err := copyFile (srcDBPath , surgeryTargetDBFilePath ); err != nil {
121
+ return fmt .Errorf ("[abandon-freelist] copy file failed: %w" , err )
122
+ }
123
+
124
+ if err := surgeon .ClearFreelist (surgeryTargetDBFilePath ); err != nil {
125
+ return fmt .Errorf ("abandom-freelist command failed: %w" , err )
126
+ }
127
+
128
+ fmt .Fprintf (os .Stdout , "The freelist was abandoned in both meta pages.\n It may cause some delay on next startup because bbolt needs to scan the whole db to reconstruct the free list.\n " )
129
+ return nil
130
+ }
0 commit comments