Skip to content

Commit

Permalink
feat(server/v2): Add snapshots commands (#21065)
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk authored Aug 6, 2024
1 parent ca8122c commit 8fb47b3
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 35 deletions.
16 changes: 6 additions & 10 deletions server/v2/store/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
}

logger := log.NewLogger(cmd.OutOrStdout())
home, err := cmd.Flags().GetString(serverv2.FlagHome)
if err != nil {
return err
}

rootStore, keepRecent, err := createRootStore(cmd, home, vp, logger)
rootStore, keepRecent, err := createRootStore(cmd, vp, logger)
if err != nil {
return fmt.Errorf("can not create root store %w", err)
}
Expand Down Expand Up @@ -78,14 +74,14 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
}

cmd.Flags().String(FlagAppDBBackend, "", "The type of database for application and snapshots databases")
cmd.Flags().Uint64(FlagPruningKeepRecent, 0, "Number of recent heights to keep on disk (ignored if pruning is not 'custom')")
cmd.Flags().Uint64(FlagKeepRecent, 0, "Number of recent heights to keep on disk (ignored if pruning is not 'custom')")

return cmd
}

func createRootStore(cmd *cobra.Command, rootDir string, v *viper.Viper, logger log.Logger) (storev2.RootStore, uint64, error) {
func createRootStore(cmd *cobra.Command, v *viper.Viper, logger log.Logger) (storev2.RootStore, uint64, error) {
tempViper := v

rootDir := v.GetString(serverv2.FlagHome)
// handle FlagAppDBBackend
var dbType db.DBType
if cmd.Flags().Changed(FlagAppDBBackend) {
Expand All @@ -103,8 +99,8 @@ func createRootStore(cmd *cobra.Command, rootDir string, v *viper.Viper, logger
}

// handle KeepRecent & Interval flags
if cmd.Flags().Changed(FlagPruningKeepRecent) {
keepRecent, err := cmd.Flags().GetUint64(FlagPruningKeepRecent)
if cmd.Flags().Changed(FlagKeepRecent) {
keepRecent, err := cmd.Flags().GetUint64(FlagKeepRecent)
if err != nil {
return nil, 0, err
}
Expand Down
5 changes: 3 additions & 2 deletions server/v2/store/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store

const (
FlagAppDBBackend = "app-db-backend"
FlagPruningKeepRecent = "keep-recent"
FlagAppDBBackend = "app-db-backend"
FlagKeepRecent = "keep-recent"
FlagInterval = "interval"
)
26 changes: 10 additions & 16 deletions server/v2/store/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
// and contains prune & snapshot commands
type StoreComponent[T transaction.Tx] struct {
config *Config
// saving appCreator for only RestoreSnapshotCmd
appCreator serverv2.AppCreator[T]
}

func New[T transaction.Tx]() *StoreComponent[T] {
return &StoreComponent[T]{}
func New[T transaction.Tx](appCreator serverv2.AppCreator[T]) *StoreComponent[T] {
return &StoreComponent[T]{appCreator: appCreator}
}

func (s *StoreComponent[T]) Init(appI serverv2.AppI[T], v *viper.Viper, logger log.Logger) error {
Expand All @@ -45,24 +47,16 @@ func (s *StoreComponent[T]) Stop(ctx context.Context) error {
return nil
}

func (s *StoreComponent[T]) GetCommands() []*cobra.Command {
return []*cobra.Command{
s.PrunesCmd(),
}
}

func (s *StoreComponent[T]) GetTxs() []*cobra.Command {
return nil
}

func (s *StoreComponent[T]) GetQueries() []*cobra.Command {
return nil
}

func (s *StoreComponent[T]) CLICommands() serverv2.CLIConfig {
return serverv2.CLIConfig{
Commands: []*cobra.Command{
s.PrunesCmd(),
s.ExportSnapshotCmd(),
s.DeleteSnapshotCmd(),
s.ListSnapshotsCmd(),
s.DumpArchiveCmd(),
s.LoadArchiveCmd(),
s.RestoreSnapshotCmd(s.appCreator),
},
}
}
Expand Down
Loading

0 comments on commit 8fb47b3

Please sign in to comment.