Skip to content

Commit

Permalink
Refactor integrate stage 2 (hazelcast#372)
Browse files Browse the repository at this point in the history
Integrate stage phase 2
  • Loading branch information
yuce authored Sep 8, 2023
1 parent c787c3a commit b0ee81d
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 225 deletions.
9 changes: 5 additions & 4 deletions base/commands/demo/demo_map_set_many.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/cmd"
. "github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
)
Expand Down Expand Up @@ -44,11 +45,11 @@ func (m MapSetManyCmd) Exec(ctx context.Context, ec plug.ExecContext) error {
count := ec.GetInt64Arg(argEntryCount)
mapName := ec.Props().GetString(flagName)
size := ec.Props().GetString(flagSize)
ci, err := ec.ClientInternal(ctx)
if err != nil {
return err
}
_, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
ci, err := cmd.ClientInternal(ctx, ec, sp)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Creating entries in map %s with %d entries", mapName, count))
mm, err := ci.Client().GetMap(ctx, mapName)
if err != nil {
Expand Down
113 changes: 111 additions & 2 deletions base/commands/list/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@
package list

import (
"context"
"fmt"
"strings"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/base"
"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/cmd"
"github.com/hazelcast/hazelcast-commandline-client/internal"
"github.com/hazelcast/hazelcast-commandline-client/internal/mk"
"github.com/hazelcast/hazelcast-commandline-client/internal/output"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
"github.com/hazelcast/hazelcast-commandline-client/internal/proto/codec"
"github.com/hazelcast/hazelcast-commandline-client/internal/serialization"
)

func addValueTypeFlag(cc plug.InitContext) {
help := fmt.Sprintf("value type (one of: %s)", strings.Join(internal.SupportedTypeNames, ", "))
cc.AddStringFlag(listFlagValueType, "v", "string", false, help)
cc.AddStringFlag(base.FlagValueType, "v", "string", false, help)
}

func makeValueData(ec plug.ExecContext, ci *hazelcast.ClientInternal, valueStr string) (hazelcast.Data, error) {
vt := ec.Props().GetString(listFlagValueType)
vt := ec.Props().GetString(base.FlagValueType)
if vt == "" {
vt = "string"
}
Expand All @@ -43,3 +50,105 @@ func stringToPartitionID(ci *hazelcast.ClientInternal, name string) (int32, erro
}
return partitionID, nil
}

func getList(ctx context.Context, ec plug.ExecContext, sp clc.Spinner) (*hazelcast.List, error) {
name := ec.Props().GetString(base.FlagName)
ci, err := cmd.ClientInternal(ctx, ec, sp)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Getting list %s", name))
return ci.Client().GetList(ctx, name)
}

func removeFromList(ctx context.Context, ec plug.ExecContext, name string, index int32, valueStr string) error {
rowV, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
indexCall := valueStr == ""
ci, err := cmd.ClientInternal(ctx, ec, sp)
if err != nil {
return nil, err
}
pid, err := stringToPartitionID(ci, name)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Removing value from list %s", name))
var req *hazelcast.ClientMessage
if indexCall {
req = codec.EncodeListRemoveWithIndexRequest(name, index)
} else {
vd, err := makeValueData(ec, ci, valueStr)
if err != nil {
return nil, err
}
req = codec.EncodeListRemoveRequest(name, vd)
}
resp, err := ci.InvokeOnPartition(ctx, req, pid, nil)
if err != nil {
return nil, err
}
var vt int32
var value any
var colName string
if indexCall {
raw := codec.DecodeListRemoveWithIndexResponse(resp)
vt = raw.Type()
value, err = ci.DecodeData(raw)
colName = "Removed Value"
if err != nil {
ec.Logger().Info("The value was not decoded, due to error: %s", err.Error())
value = serialization.NondecodedType(serialization.TypeToLabel(vt))
}
} else {
vt = serialization.TypeBool
value = codec.DecodeListRemoveResponse(resp)
colName = "Removed"
}
row := output.Row{
output.Column{
Name: colName,
Type: vt,
Value: value,
},
}
if ec.Props().GetBool(base.FlagShowType) {
row = append(row, output.Column{
Name: output.NameValueType,
Type: serialization.TypeString,
Value: serialization.TypeToLabel(vt),
})
}
return row, nil
})
if err != nil {
return err
}
stop()
msg := fmt.Sprintf("OK List %s was updated.\n", name)
ec.PrintlnUnnecessary(msg)
row := rowV.(output.Row)
return ec.AddOutputRows(ctx, row)
}

func convertDataToRow(ci *hazelcast.ClientInternal, name string, data hazelcast.Data, showType bool) (output.Row, error) {
vt := data.Type()
value, err := ci.DecodeData(data)
if err != nil {
return nil, err
}
row := output.Row{
output.Column{
Name: name,
Type: vt,
Value: value,
},
}
if showType {
row = append(row, output.Column{
Name: output.NameValueType,
Type: serialization.TypeString,
Value: serialization.TypeToLabel(vt),
})
}
return row, nil
}
11 changes: 3 additions & 8 deletions base/commands/list/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
package list

const (
listFlagKeyType = "key-type"
listFlagValueType = "value-type"
listFlagIndex = "index"
defaultListName = "default"
argValue = "value"
argTitleValue = "value"
argIndex = "index"
argTitleIndex = "index"
flagIndex = "index"
argIndex = "index"
argTitleIndex = "index"
)
52 changes: 6 additions & 46 deletions base/commands/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,23 @@ package list

import (
"context"
"fmt"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/base"
"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/paths"
. "github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
)

const (
listFlagName = "name"
listFlagShowType = "show-type"
listPropertyName = "list"
)

type ListCommand struct {
}

func (mc *ListCommand) Init(cc plug.InitContext) error {
cc.AddCommandGroup(clc.GroupDDSID, clc.GroupDDSTitle)
cc.SetCommandGroup(clc.GroupDDSID)
cc.AddStringFlag(listFlagName, "n", defaultListName, false, "list name")
cc.AddBoolFlag(listFlagShowType, "", false, false, "add the type names to the output")
if !cc.Interactive() {
cc.AddStringFlag(clc.PropertySchemaDir, "", paths.Schemas(), false, "set the schema directory")
}
cc.AddStringFlag(base.FlagName, "n", base.DefaultName, false, "list name")
cc.AddBoolFlag(base.FlagShowType, "", false, false, "add the type names to the output")
cc.SetTopLevel(true)
cc.SetCommandUsage("list [command] [flags]")
cc.SetCommandUsage("list")
help := "List operations"
cc.SetCommandHelp(help, help)
return nil
Expand All @@ -42,34 +30,6 @@ func (mc *ListCommand) Exec(context.Context, plug.ExecContext) error {
return nil
}

func (mc *ListCommand) Augment(ec plug.ExecContext, props *plug.Properties) error {
ctx := context.TODO()
props.SetBlocking(listPropertyName, func() (any, error) {
listName := ec.Props().GetString(listFlagName)
// empty list name is allowed
ci, err := ec.ClientInternal(ctx)
if err != nil {
return nil, err
}
mv, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
sp.SetText(fmt.Sprintf("Getting list %s", listName))
m, err := ci.Client().GetList(ctx, listName)
if err != nil {
return nil, err
}
return m, nil
})
if err != nil {
return nil, err
}
stop()
return mv.(*hazelcast.List), nil
})
return nil
}

func init() {
cmd := &ListCommand{}
Must(plug.Registry.RegisterCommand("list", cmd))
plug.Registry.RegisterAugmentor("20-list", cmd)
check.Must(plug.Registry.RegisterCommand("list", &ListCommand{}))
}
80 changes: 50 additions & 30 deletions base/commands/list/list_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,79 @@ import (

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/base"
"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/cmd"
. "github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/output"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
"github.com/hazelcast/hazelcast-commandline-client/internal/proto/codec"
"github.com/hazelcast/hazelcast-commandline-client/internal/serialization"
)

type ListAddCommand struct{}

func (mc *ListAddCommand) Unwrappable() {}

func (mc *ListAddCommand) Init(cc plug.InitContext) error {
cc.SetCommandUsage("add")
help := "Add a value in the given list"
cc.SetCommandHelp(help, help)
addValueTypeFlag(cc)
cc.AddIntFlag(listFlagIndex, "", -1, false, "index for the value")
cc.AddStringArg(argValue, argTitleValue)
cc.AddIntFlag(flagIndex, "", -1, false, "index for the value")
cc.AddStringArg(base.ArgValue, base.ArgTitleValue)
return nil
}

func (mc *ListAddCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
name := ec.Props().GetString(listFlagName)
ci, err := ec.ClientInternal(ctx)
if err != nil {
return err
}
// get the list just to ensure the corresponding proxy is created
if _, err := ec.Props().GetBlocking(listPropertyName); err != nil {
return err
}
valueStr := ec.GetStringArg(argValue)
vd, err := makeValueData(ec, ci, valueStr)
if err != nil {
return err
}
index := ec.Props().GetInt(listFlagIndex)
var req *hazelcast.ClientMessage
if index >= 0 {
req = codec.EncodeListAddWithIndexRequest(name, int32(index), vd)
} else {
req = codec.EncodeListAddRequest(name, vd)
}
pid, err := stringToPartitionID(ci, name)
if err != nil {
return err
}
_, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
name := ec.Props().GetString(base.FlagName)
val, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
ci, err := cmd.ClientInternal(ctx, ec, sp)
if err != nil {
return nil, err
}
// get the list just to ensure the corresponding proxy is created
_, err = getList(ctx, ec, sp)
if err != nil {
return nil, err
}
valueStr := ec.GetStringArg(base.ArgValue)
vd, err := makeValueData(ec, ci, valueStr)
if err != nil {
return nil, err
}
index := ec.Props().GetInt(flagIndex)
var req *hazelcast.ClientMessage
if index >= 0 {
req = codec.EncodeListAddWithIndexRequest(name, int32(index), vd)
} else {
req = codec.EncodeListAddRequest(name, vd)
}
pid, err := stringToPartitionID(ci, name)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Adding value at index %d into list %s", index, name))
return ci.InvokeOnPartition(ctx, req, pid, nil)
resp, err := ci.InvokeOnPartition(ctx, req, pid, nil)
if err != nil {
return nil, err
}
return codec.DecodeListAddResponse(resp), err
})
if err != nil {
return err
}
stop()
return nil
msg := fmt.Sprintf("OK Updated list %s.\n", name)
ec.PrintlnUnnecessary(msg)
row := output.Row{
output.Column{
Name: "Value",
Type: serialization.TypeBool,
Value: val,
},
}
return ec.AddOutputRows(ctx, row)
}

func init() {
Expand Down
Loading

0 comments on commit b0ee81d

Please sign in to comment.