From fa181004898da300d2d4e81b19b1251ea7b199f9 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 24 Aug 2022 15:09:52 +0300 Subject: [PATCH] [#1714] lens: Add `open*COMPONENT*` funcs Signed-off-by: Pavel Karpy --- .../internal/blobovnicza/inspect.go | 7 +------ cmd/neofs-lens/internal/blobovnicza/list.go | 8 +------- cmd/neofs-lens/internal/blobovnicza/root.go | 16 +++++++++++++++- cmd/neofs-lens/internal/meta/inspect.go | 14 ++------------ cmd/neofs-lens/internal/meta/list-garbage.go | 15 ++------------- .../internal/meta/list-graveyard.go | 15 ++------------- cmd/neofs-lens/internal/meta/root.go | 19 +++++++++++++++++++ cmd/neofs-lens/internal/writecache/inspect.go | 4 +--- cmd/neofs-lens/internal/writecache/list.go | 6 ++---- cmd/neofs-lens/internal/writecache/root.go | 14 +++++++++++++- 10 files changed, 58 insertions(+), 60 deletions(-) diff --git a/cmd/neofs-lens/internal/blobovnicza/inspect.go b/cmd/neofs-lens/internal/blobovnicza/inspect.go index db9601cc3..579958507 100644 --- a/cmd/neofs-lens/internal/blobovnicza/inspect.go +++ b/cmd/neofs-lens/internal/blobovnicza/inspect.go @@ -27,12 +27,7 @@ func inspectFunc(cmd *cobra.Command, _ []string) { err := addr.DecodeString(vAddress) common.ExitOnErr(cmd, common.Errf("invalid address argument: %w", err)) - blz := blobovnicza.New( - blobovnicza.WithPath(vPath), - blobovnicza.WithReadOnly(true), - ) - common.ExitOnErr(cmd, blz.Open()) - + blz := openBlobovnicza(cmd) defer blz.Close() var prm blobovnicza.GetPrm diff --git a/cmd/neofs-lens/internal/blobovnicza/list.go b/cmd/neofs-lens/internal/blobovnicza/list.go index 54b3c785e..2d116efe4 100644 --- a/cmd/neofs-lens/internal/blobovnicza/list.go +++ b/cmd/neofs-lens/internal/blobovnicza/list.go @@ -30,13 +30,7 @@ func listFunc(cmd *cobra.Command, _ []string) { return err } - blz := blobovnicza.New( - blobovnicza.WithPath(vPath), - blobovnicza.WithReadOnly(true), - ) - - common.ExitOnErr(cmd, blz.Open()) - + blz := openBlobovnicza(cmd) defer blz.Close() err := blobovnicza.IterateAddresses(blz, wAddr) diff --git a/cmd/neofs-lens/internal/blobovnicza/root.go b/cmd/neofs-lens/internal/blobovnicza/root.go index aa82c2516..59ce6e990 100644 --- a/cmd/neofs-lens/internal/blobovnicza/root.go +++ b/cmd/neofs-lens/internal/blobovnicza/root.go @@ -1,6 +1,10 @@ package blobovnicza -import "github.com/spf13/cobra" +import ( + common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" + "github.com/spf13/cobra" +) var ( vAddress string @@ -17,3 +21,13 @@ var Root = &cobra.Command{ func init() { Root.AddCommand(listCMD, inspectCMD) } + +func openBlobovnicza(cmd *cobra.Command) *blobovnicza.Blobovnicza { + blz := blobovnicza.New( + blobovnicza.WithPath(vPath), + blobovnicza.WithReadOnly(true), + ) + common.ExitOnErr(cmd, blz.Open()) + + return blz +} diff --git a/cmd/neofs-lens/internal/meta/inspect.go b/cmd/neofs-lens/internal/meta/inspect.go index 3a05e2fa5..7d84a1bd7 100644 --- a/cmd/neofs-lens/internal/meta/inspect.go +++ b/cmd/neofs-lens/internal/meta/inspect.go @@ -3,7 +3,6 @@ package meta import ( "errors" "fmt" - "time" common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" @@ -11,7 +10,6 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/spf13/cobra" - "go.etcd.io/bbolt" ) var inspectCMD = &cobra.Command{ @@ -32,16 +30,8 @@ func inspectFunc(cmd *cobra.Command, _ []string) { err := addr.DecodeString(vAddress) common.ExitOnErr(cmd, common.Errf("invalid address argument: %w", err)) - db := meta.New( - meta.WithPath(vPath), - meta.WithBoltDBOptions(&bbolt.Options{ - ReadOnly: true, - Timeout: 100 * time.Millisecond, - }), - meta.WithEpochState(epochState{}), - ) - - common.ExitOnErr(cmd, common.Errf("could not open metabase: %w", db.Open(true))) + db := openMeta(cmd) + defer db.Close() storageID := meta.StorageIDPrm{} storageID.SetAddress(addr) diff --git a/cmd/neofs-lens/internal/meta/list-garbage.go b/cmd/neofs-lens/internal/meta/list-garbage.go index 4ca749987..d5f70c950 100644 --- a/cmd/neofs-lens/internal/meta/list-garbage.go +++ b/cmd/neofs-lens/internal/meta/list-garbage.go @@ -1,12 +1,9 @@ package meta import ( - "time" - common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" "github.com/spf13/cobra" - "go.etcd.io/bbolt" ) var listGarbageCMD = &cobra.Command{ @@ -21,16 +18,8 @@ func init() { } func listGarbageFunc(cmd *cobra.Command, _ []string) { - db := meta.New( - meta.WithPath(vPath), - meta.WithBoltDBOptions(&bbolt.Options{ - ReadOnly: true, - Timeout: 100 * time.Millisecond, - }), - meta.WithEpochState(epochState{}), - ) - - common.ExitOnErr(cmd, common.Errf("could not open metabase: %w", db.Open(true))) + db := openMeta(cmd) + defer db.Close() var garbPrm meta.GarbageIterationPrm garbPrm.SetHandler( diff --git a/cmd/neofs-lens/internal/meta/list-graveyard.go b/cmd/neofs-lens/internal/meta/list-graveyard.go index 0853452e1..d5bcfe5d6 100644 --- a/cmd/neofs-lens/internal/meta/list-graveyard.go +++ b/cmd/neofs-lens/internal/meta/list-graveyard.go @@ -1,12 +1,9 @@ package meta import ( - "time" - common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" "github.com/spf13/cobra" - "go.etcd.io/bbolt" ) var listGraveyardCMD = &cobra.Command{ @@ -21,16 +18,8 @@ func init() { } func listGraveyardFunc(cmd *cobra.Command, _ []string) { - db := meta.New( - meta.WithPath(vPath), - meta.WithBoltDBOptions(&bbolt.Options{ - ReadOnly: true, - Timeout: 100 * time.Millisecond, - }), - meta.WithEpochState(epochState{}), - ) - - common.ExitOnErr(cmd, common.Errf("could not open metabase: %w", db.Open(true))) + db := openMeta(cmd) + defer db.Close() var gravePrm meta.GraveyardIterationPrm gravePrm.SetHandler( diff --git a/cmd/neofs-lens/internal/meta/root.go b/cmd/neofs-lens/internal/meta/root.go index 9534abdb7..fca99512b 100644 --- a/cmd/neofs-lens/internal/meta/root.go +++ b/cmd/neofs-lens/internal/meta/root.go @@ -1,7 +1,12 @@ package meta import ( + "time" + + common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" + meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" "github.com/spf13/cobra" + "go.etcd.io/bbolt" ) var ( @@ -28,3 +33,17 @@ func init() { listGarbageCMD, ) } + +func openMeta(cmd *cobra.Command) *meta.DB { + db := meta.New( + meta.WithPath(vPath), + meta.WithBoltDBOptions(&bbolt.Options{ + ReadOnly: true, + Timeout: 100 * time.Millisecond, + }), + meta.WithEpochState(epochState{}), + ) + common.ExitOnErr(cmd, common.Errf("could not open metabase: %w", db.Open(true))) + + return db +} diff --git a/cmd/neofs-lens/internal/writecache/inspect.go b/cmd/neofs-lens/internal/writecache/inspect.go index 1f5ef7ffa..f1dcfd9ae 100644 --- a/cmd/neofs-lens/internal/writecache/inspect.go +++ b/cmd/neofs-lens/internal/writecache/inspect.go @@ -21,9 +21,7 @@ func init() { } func inspectFunc(cmd *cobra.Command, _ []string) { - db, err := writecache.OpenDB(vPath, true) - common.ExitOnErr(cmd, common.Errf("could not open write-cache db: %w", err)) - + db := openWC(cmd) defer db.Close() data, err := writecache.Get(db, []byte(vAddress)) diff --git a/cmd/neofs-lens/internal/writecache/list.go b/cmd/neofs-lens/internal/writecache/list.go index 2da1c0836..e2d7e3d34 100644 --- a/cmd/neofs-lens/internal/writecache/list.go +++ b/cmd/neofs-lens/internal/writecache/list.go @@ -30,11 +30,9 @@ func listFunc(cmd *cobra.Command, _ []string) { return err } - db, err := writecache.OpenDB(vPath, true) - common.ExitOnErr(cmd, common.Errf("could not open write-cache db: %w", err)) - + db := openWC(cmd) defer db.Close() - err = writecache.IterateDB(db, wAddr) + err := writecache.IterateDB(db, wAddr) common.ExitOnErr(cmd, common.Errf("write-cache iterator failure: %w", err)) } diff --git a/cmd/neofs-lens/internal/writecache/root.go b/cmd/neofs-lens/internal/writecache/root.go index 493a787e4..0e0e77867 100644 --- a/cmd/neofs-lens/internal/writecache/root.go +++ b/cmd/neofs-lens/internal/writecache/root.go @@ -1,6 +1,11 @@ package writecache -import "github.com/spf13/cobra" +import ( + common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache" + "github.com/spf13/cobra" + "go.etcd.io/bbolt" +) var ( vAddress string @@ -17,3 +22,10 @@ var Root = &cobra.Command{ func init() { Root.AddCommand(listCMD, inspectCMD) } + +func openWC(cmd *cobra.Command) *bbolt.DB { + db, err := writecache.OpenDB(vPath, true) + common.ExitOnErr(cmd, common.Errf("could not open write-cache db: %w", err)) + + return db +}