From 1e07775161ac8f122d9e76767d1cbb8421445eb9 Mon Sep 17 00:00:00 2001 From: Aleksey Savchuk Date: Thu, 25 Jul 2024 14:44:52 +0300 Subject: [PATCH] [#1223] lens/tui: Add schema handlers for all bucket types Signed-off-by: Aleksey Savchuk --- cmd/frostfs-lens/schema/handlers.go | 101 +++++++++++++++++- cmd/frostfs-lens/schema/parse/parser.go | 32 ++++++ .../schema/parse/types/counters.go | 9 ++ cmd/frostfs-lens/schema/parse/types/id.go | 2 +- cmd/frostfs-lens/schema/parse/types/prefix.go | 32 ++++++ .../schema/parse/types/prefix_string.go | 51 --------- 6 files changed, 174 insertions(+), 53 deletions(-) delete mode 100644 cmd/frostfs-lens/schema/parse/types/prefix_string.go diff --git a/cmd/frostfs-lens/schema/handlers.go b/cmd/frostfs-lens/schema/handlers.go index 9195bee29..02f177166 100644 --- a/cmd/frostfs-lens/schema/handlers.go +++ b/cmd/frostfs-lens/schema/handlers.go @@ -173,7 +173,10 @@ var SmallHandler = NewHandler( parse.EOF, NewHandler( parse.Chain(parse.OID, parse.EOF), - parse.StorageID, + parse.Any( + parse.StorageID, + parse.EOF, + ), nil, ), ) @@ -192,6 +195,34 @@ var EcInfoHandler = NewHandler( ), ) +var ParentHandler = NewHandler( + parse.Chain( + parse.Prefix(types.Parent), + parse.CID, + parse.EOF, + ), + parse.EOF, + NewHandler( + parse.Chain(parse.OID, parse.EOF), + parse.Chain(parse.OIDsList, parse.EOF), + nil, + ), +) + +var SplitHandler = NewHandler( + parse.Chain( + parse.Prefix(types.Split), + parse.CID, + parse.EOF, + ), + parse.EOF, + NewHandler( + parse.Chain(parse.UUID, parse.EOF), + parse.Chain(parse.OIDsList, parse.EOF), + nil, + ), +) + var ContainerCountersHandler = NewHandler( parse.Chain(parse.Prefix(types.ContainerCounters), parse.EOF), parse.EOF, @@ -202,6 +233,68 @@ var ContainerCountersHandler = NewHandler( ), ) +var InhumeHandler = NewHandler( + parse.Chain( + parse.Any( + parse.Prefix(types.Graveyard), + parse.Prefix(types.Garbage), + ), + parse.EOF, + ), + parse.EOF, + NewHandler( + parse.Chain(parse.CID, parse.OID, parse.EOF), + parse.Any( + parse.Chain(parse.CID, parse.OID, parse.EOF), + parse.Skip, + ), + nil, + ), +) + +var ShardInfoHandler = NewHandler( + parse.Chain(parse.Prefix(types.ShardInfo), parse.EOF), + parse.EOF, + NewHandler( + parse.Chain( + parse.Any( + parse.ExactString("logic_counter"), + parse.ExactString("phy_counter"), + parse.ExactString("user_counter"), + parse.ExactString("version"), + parse.ExactString("id"), + ), + parse.EOF, + ), + parse.Volume, + nil, + ), +) + +var LockedHandler = NewHandler( + parse.Chain(parse.Prefix(types.Locked), parse.EOF), + parse.EOF, + NewHandler( + parse.Chain(parse.CID, parse.EOF), + parse.EOF, + NewHandler( + parse.Chain(parse.OID, parse.EOF), + parse.Chain(parse.OIDsList, parse.EOF), + nil, + ), + ), +) + +var ContainerVolumeHandler = NewHandler( + parse.Chain(parse.Prefix(types.ContainerVolume), parse.EOF), + parse.EOF, + NewHandler( + parse.Chain(parse.CID, parse.EOF), + parse.Chain(parse.Volume, parse.EOF), + nil, + ), +) + var StartHandler = WithFallbackRecursively( Any( ObjectBucketHandler, @@ -212,6 +305,12 @@ var StartHandler = WithFallbackRecursively( RootHandler, EcInfoHandler, ContainerCountersHandler, + ParentHandler, + SplitHandler, + ContainerVolumeHandler, + InhumeHandler, + ShardInfoHandler, + LockedHandler, ), RawHandler, ) diff --git a/cmd/frostfs-lens/schema/parse/parser.go b/cmd/frostfs-lens/schema/parse/parser.go index 43bd8c20f..139daa464 100644 --- a/cmd/frostfs-lens/schema/parse/parser.go +++ b/cmd/frostfs-lens/schema/parse/parser.go @@ -2,6 +2,7 @@ package parse import ( "bytes" + "encoding/binary" "errors" "fmt" @@ -10,6 +11,7 @@ import ( objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user" + "github.com/google/uuid" "github.com/nspcc-dev/neo-go/pkg/io" ) @@ -120,6 +122,36 @@ func ObjectCounters(init []byte) (result Result, next []byte, err error) { return counters, next, nil } +func UUID(init []byte) (result Result, next []byte, err error) { + if len(init) < 16 { + return nil, init, errors.New("can't parse UUID, not enough data") + } + + id := uuid.UUID{} + if err := id.UnmarshalBinary(init[:16]); err != nil { + return nil, init, fmt.Errorf("can't parsse UUID: %w", err) + } + return id, init[16:], nil +} + +func Volume(init []byte) (result Result, next []byte, err error) { + if len(init) < 8 { + return nil, init, errors.New("can't parse volume, not enough data") + } + volume := binary.LittleEndian.Uint64(init[:8]) + return &types.Volume{Volume: volume}, init[8:], nil +} + +func ExactString(s string) Parser { + return func(init []byte) (result Result, next []byte, err error) { + next, found := bytes.CutPrefix(init, []byte(s)) + if !found { + return nil, init, errors.New("can't parse string") + } + return &resultFromString{str: s}, next, nil + } +} + func Skip(_ []byte) (result Result, next []byte, err error) { return &resultFromString{}, nil, nil } diff --git a/cmd/frostfs-lens/schema/parse/types/counters.go b/cmd/frostfs-lens/schema/parse/types/counters.go index f051ce365..2940ab9e8 100644 --- a/cmd/frostfs-lens/schema/parse/types/counters.go +++ b/cmd/frostfs-lens/schema/parse/types/counters.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "errors" "fmt" + "strconv" ) type ObjectCounters struct { @@ -28,3 +29,11 @@ func (c *ObjectCounters) Decode(b []byte) (rest []byte, err error) { c.User = binary.LittleEndian.Uint64(b[16:]) return b[24:], nil } + +type Volume struct { + Volume uint64 +} + +func (v *Volume) String() string { + return strconv.FormatUint(v.Volume, 10) +} diff --git a/cmd/frostfs-lens/schema/parse/types/id.go b/cmd/frostfs-lens/schema/parse/types/id.go index 6ab26498c..caaa03181 100644 --- a/cmd/frostfs-lens/schema/parse/types/id.go +++ b/cmd/frostfs-lens/schema/parse/types/id.go @@ -23,7 +23,7 @@ type ( ) func (id CID) String() string { - return fmt.Sprintf(`CID(%s)`, id.ID) + return fmt.Sprintf(`CID [red]%s[white]`, id.ID) } func (id OID) String() string { diff --git a/cmd/frostfs-lens/schema/parse/types/prefix.go b/cmd/frostfs-lens/schema/parse/types/prefix.go index cd5ec8c4d..c5f4d7c92 100644 --- a/cmd/frostfs-lens/schema/parse/types/prefix.go +++ b/cmd/frostfs-lens/schema/parse/types/prefix.go @@ -2,6 +2,8 @@ package types +import "fmt" + type Prefix byte const ( @@ -25,3 +27,33 @@ const ( ContainerCounters EcInfo ) + +var x = map[Prefix]string{ + Graveyard: "Graveyard", + Garbage: "Garbage", + ToMoveIt: "To Move It", + ContainerVolume: "Container Volume", + Locked: "Locked", + ShardInfo: "Shard Info", + Primary: "Primary", + Lockers: "Lockers", + Tombstone: "Tombstone", + Small: "Small", + Root: "Root", + Owner: "Owner", + UserAttribute: "User Attribute", + PayloadHash: "Payload Hash", + Parent: "Parent", + Split: "Split", + ContainerCounters: "Container Counters", + EcInfo: "EC Info", +} + +const format = "[green](%2d %-18s)[white]" + +func (p Prefix) String() string { + if s, ok := x[p]; ok { + return fmt.Sprintf(format, p, s) + } + return fmt.Sprintf(format, p, "--Unknown--") +} diff --git a/cmd/frostfs-lens/schema/parse/types/prefix_string.go b/cmd/frostfs-lens/schema/parse/types/prefix_string.go deleted file mode 100644 index 8cfa4d414..000000000 --- a/cmd/frostfs-lens/schema/parse/types/prefix_string.go +++ /dev/null @@ -1,51 +0,0 @@ -// Code generated by "stringer -type=Prefix"; DO NOT EDIT. - -package types - -import "strconv" - -func _() { - // An "invalid array index" compiler error signifies that the constant values have changed. - // Re-run the stringer command to generate them again. - var x [1]struct{} - _ = x[Graveyard-0] - _ = x[Garbage-1] - _ = x[ToMoveIt-2] - _ = x[ContainerVolume-3] - _ = x[Locked-4] - _ = x[ShardInfo-5] - _ = x[Primary-6] - _ = x[Lockers-7] - _ = x[Tombstone-9] - _ = x[Small-10] - _ = x[Root-11] - _ = x[Owner-12] - _ = x[UserAttribute-13] - _ = x[PayloadHash-14] - _ = x[Parent-15] - _ = x[Split-16] - _ = x[ContainerCounters-17] - _ = x[EcInfo-18] -} - -const ( - _Prefix_name_0 = "GraveyardGarbageToMoveItContainerVolumeLockedShardInfoPrimaryLockers" - _Prefix_name_1 = "TombstoneSmallRootOwnerUserAttributePayloadHashParentSplitContainerCountersEcInfo" -) - -var ( - _Prefix_index_0 = [...]uint8{0, 9, 16, 24, 39, 45, 54, 61, 68} - _Prefix_index_1 = [...]uint8{0, 9, 14, 18, 23, 36, 47, 53, 58, 75, 81} -) - -func (i Prefix) String() string { - switch { - case i <= 7: - return _Prefix_name_0[_Prefix_index_0[i]:_Prefix_index_0[i+1]] - case 9 <= i && i <= 18: - i -= 9 - return _Prefix_name_1[_Prefix_index_1[i]:_Prefix_index_1[i+1]] - default: - return "Prefix(" + strconv.FormatInt(int64(i), 10) + ")" - } -}