Skip to content

Commit

Permalink
neofs-lens: add new command meta object put
Browse files Browse the repository at this point in the history
This command places the object into the metabase. It can be useful if
you want to add an object directly to the metabase.

Closes #1816.

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
  • Loading branch information
AliceInHunterland committed Sep 12, 2023
1 parent 4aab937 commit a9605f6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ minor release, the component will be purged, so be prepared (see `Updating` sect
- `neofs-adm morph netmap-candidates` CLI command (#1889)
- SN network validation (is available by its announced addresses) on bootstrap by the IR (#2475)
- Display of container alias fee info in `neofs-cli netmap netinfo` (#2553)
- `neofs-lens meta put` CLI command (#1816)

### Fixed
- `neo-go` RPC connection loss handling (#1337)
Expand Down
9 changes: 9 additions & 0 deletions cmd/neofs-lens/internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
flagAddress = "address"
flagEnginePath = "path"
flagOutFile = "out"
flagInFile = "obj"
)

// AddAddressFlag adds the address flag to the passed cobra command.
Expand All @@ -33,3 +34,11 @@ func AddOutputFileFlag(cmd *cobra.Command, v *string) {
"File to save object payload")
_ = cmd.MarkFlagFilename(flagOutFile)
}

// AddInputPathFile adds the input file with object flag to the passed cobra command.
func AddInputPathFile(cmd *cobra.Command, v *string) {
cmd.Flags().StringVar(v, flagInFile, "",
"Path to file with object")
_ = cmd.MarkFlagFilename(flagInFile)
_ = cmd.MarkFlagRequired(flagInFile)
}
2 changes: 1 addition & 1 deletion cmd/neofs-lens/internal/meta/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func inspectFunc(cmd *cobra.Command, _ []string) {
err := addr.DecodeString(vAddress)
common.ExitOnErr(cmd, common.Errf("invalid address argument: %w", err))

db := openMeta(cmd)
db := openMeta(cmd, true)
defer db.Close()

storageID := meta.StorageIDPrm{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-lens/internal/meta/list-garbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
}

func listGarbageFunc(cmd *cobra.Command, _ []string) {
db := openMeta(cmd)
db := openMeta(cmd, true)
defer db.Close()

var garbPrm meta.GarbageIterationPrm
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-lens/internal/meta/list-graveyard.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
}

func listGraveyardFunc(cmd *cobra.Command, _ []string) {
db := openMeta(cmd)
db := openMeta(cmd, true)
defer db.Close()

var gravePrm meta.GraveyardIterationPrm
Expand Down
57 changes: 57 additions & 0 deletions cmd/neofs-lens/internal/meta/put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package meta

import (
"errors"
"os"

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/nspcc-dev/neofs-sdk-go/object"
"github.com/spf13/cobra"
)

var writeObjectCMD = &cobra.Command{
Use: "put",
Short: "Put object to metabase",
Long: "Put object from file to metabase",
Args: cobra.NoArgs,
Run: writeObject,
}

func init() {
common.AddComponentPathFlag(writeObjectCMD, &vPath)
common.AddInputPathFile(writeObjectCMD, &vInputObj)
}

func writeObject(cmd *cobra.Command, _ []string) {
db := openMeta(cmd, false)
defer db.Close()

err := db.Init()
common.ExitOnErr(cmd, common.Errf("can't init metabase: %w", err))

buf, err := os.ReadFile(vInputObj)
common.ExitOnErr(cmd, common.Errf("unable to read given file: %w", err))

obj := object.New()
common.ExitOnErr(cmd, common.Errf("can't unmarshal object from given file: %w", obj.Unmarshal(buf)))

id, ok := obj.ID()
if !ok {
common.ExitOnErr(cmd, errors.New("missing ID in object"))
}

cnr, ok := obj.ContainerID()
if !ok {
common.ExitOnErr(cmd, errors.New("missing container ID in object"))
}

var pPrm meta.PutPrm
pPrm.SetObject(obj)

_, err = db.Put(pPrm)
common.ExitOnErr(cmd, common.Errf("can't put object: %w", err))

cmd.Printf("[%s] Object successfully stored\n", vInputObj)
cmd.Printf(" OID: %s\n CID: %s\n", id, cnr)
}
16 changes: 11 additions & 5 deletions cmd/neofs-lens/internal/meta/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meta

import (
"os"
"time"

common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal"
Expand All @@ -10,8 +11,9 @@ import (
)

var (
vAddress string
vPath string
vAddress string
vPath string
vInputObj string
)

type epochState struct{}
Expand All @@ -31,19 +33,23 @@ func init() {
inspectCMD,
listGraveyardCMD,
listGarbageCMD,
writeObjectCMD,
)
}

func openMeta(cmd *cobra.Command) *meta.DB {
func openMeta(cmd *cobra.Command, readOnly bool) *meta.DB {
_, err := os.Stat(vPath)
common.ExitOnErr(cmd, err)

db := meta.New(
meta.WithPath(vPath),
meta.WithBoltDBOptions(&bbolt.Options{
ReadOnly: true,
ReadOnly: readOnly,
Timeout: 100 * time.Millisecond,
}),
meta.WithEpochState(epochState{}),
)
common.ExitOnErr(cmd, common.Errf("could not open metabase: %w", db.Open(true)))
common.ExitOnErr(cmd, common.Errf("could not open metabase: %w", db.Open(readOnly)))

return db
}

0 comments on commit a9605f6

Please sign in to comment.