From 1f08a4aea73087356bcb897441cbb5bf17ab58e6 Mon Sep 17 00:00:00 2001 From: Paul Lorenz Date: Fri, 17 Mar 2023 14:44:51 -0400 Subject: [PATCH] Add bbolt compact command to ziti CLI. Fixes #1041 --- ziti/cmd/database/cmd.go | 4 ++- ziti/cmd/database/compact.go | 63 ++++++++++++++++++++++++++++++++++++ ziti/cmd/log_filter.go | 2 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 ziti/cmd/database/compact.go diff --git a/ziti/cmd/database/cmd.go b/ziti/cmd/database/cmd.go index 31e07b588..62352fa5c 100644 --- a/ziti/cmd/database/cmd.go +++ b/ziti/cmd/database/cmd.go @@ -8,7 +8,7 @@ import ( "io" ) -func NewCmdDb(_ io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdDb(out io.Writer, errOut io.Writer) *cobra.Command { cmd := util.NewEmptyParentCmd("db", "Interact with Ziti database files") exploreCmd := &cobra.Command{ @@ -22,5 +22,7 @@ func NewCmdDb(_ io.Writer, errOut io.Writer) *cobra.Command { } cmd.AddCommand(exploreCmd) + cmd.AddCommand(NewCompactAction()) + return cmd } diff --git a/ziti/cmd/database/compact.go b/ziti/cmd/database/compact.go new file mode 100644 index 000000000..5837227db --- /dev/null +++ b/ziti/cmd/database/compact.go @@ -0,0 +1,63 @@ +/* + Copyright NetFoundry Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package database + +import ( + "github.com/spf13/cobra" + "go.etcd.io/bbolt" + "math" +) + +type CompactAction struct { + useArrayFreelists bool +} + +func NewCompactAction() *cobra.Command { + action := &CompactAction{} + + cmd := &cobra.Command{ + Use: "compact ", + Short: "Compact a bbolt database", + Args: cobra.ExactArgs(2), + RunE: action.Run, + } + + cmd.Flags().BoolVar(&action.useArrayFreelists, "array-freelist", false, "Use array freelist") + return cmd +} + +// Run implements this command +func (o *CompactAction) Run(cmd *cobra.Command, args []string) error { + srcOptions := *bbolt.DefaultOptions + srcOptions.ReadOnly = true + + srcDb, err := bbolt.Open(args[0], 0400, &srcOptions) + if err != nil { + return err + } + + dstOptions := *bbolt.DefaultOptions + if !o.useArrayFreelists { + dstOptions.FreelistType = bbolt.FreelistMapType + } + dstDb, err := bbolt.Open(args[1], 0600, &dstOptions) + if err != nil { + return err + } + + return bbolt.Compact(dstDb, srcDb, math.MaxUint16) +} diff --git a/ziti/cmd/log_filter.go b/ziti/cmd/log_filter.go index ffff98bc1..fbee7b723 100644 --- a/ziti/cmd/log_filter.go +++ b/ziti/cmd/log_filter.go @@ -24,7 +24,7 @@ import ( "os" ) -// AgentOptions contains the command line options +// LogFormatOptions contains the command line options type LogFormatOptions struct { CommonOptions