diff --git a/base/commands/demo/demo_it_test.go b/base/commands/demo/demo_it_test.go index 3212d07d..b0e7fd3a 100644 --- a/base/commands/demo/demo_it_test.go +++ b/base/commands/demo/demo_it_test.go @@ -54,3 +54,16 @@ func generateData_Wikipedia_MaxValues_Test(t *testing.T) { }) }) } + +func TestMapSetMany(t *testing.T) { + it.MapTester(t, func(tcx it.TestContext, m *hz.Map) { + t := tcx.T + ctx := context.Background() + count := 10 + tcx.WithReset(func() { + tcx.CLCExecute(ctx, "demo", "map-setmany", "10", "--name", m.Name(), "--size", "1") + require.Equal(t, count, check.MustValue(m.Size(context.Background()))) + require.Equal(t, "a", check.MustValue(m.Get(ctx, "k1"))) + }) + }) +} diff --git a/base/commands/demo/demo_map_set_many.go b/base/commands/demo/demo_map_set_many.go new file mode 100644 index 00000000..3bb32bcc --- /dev/null +++ b/base/commands/demo/demo_map_set_many.go @@ -0,0 +1,115 @@ +//go:build std || demo + +package demo + +import ( + "context" + "fmt" + "strconv" + "strings" + + "github.com/hazelcast/hazelcast-commandline-client/clc" + . "github.com/hazelcast/hazelcast-commandline-client/internal/check" + "github.com/hazelcast/hazelcast-commandline-client/internal/plug" + "github.com/hazelcast/hazelcast-go-client" +) + +type MapSetManyCmd struct{} + +const ( + flagName = "name" + flagSize = "size" + kb = "KB" + mb = "MB" + kbs = 1024 + mbs = kbs * 1024 +) + +func (m MapSetManyCmd) Init(cc plug.InitContext) error { + cc.SetCommandUsage("map-setmany [entry-count] [flags]") + cc.SetPositionalArgCount(1, 1) + cc.AddStringFlag(flagName, "n", "default", false, "Name of the map.") + cc.AddStringFlag(flagSize, "", "", true, `Size of the map value in bytes, the following suffixes can also be used: kb, mb, e.g., 42kb)`) + help := "Generates multiple map entries." + cc.SetCommandHelp(help, help) + return nil +} + +func (m MapSetManyCmd) Exec(ctx context.Context, ec plug.ExecContext) error { + entryCount := ec.Args()[0] + c, err := strconv.Atoi(entryCount) + if err != nil { + return err + } + 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) { + sp.SetText(fmt.Sprintf("Creating entries in map %s with %d entries", mapName, c)) + mm, err := ci.Client().GetMap(ctx, mapName) + if err != nil { + return nil, err + } + return nil, createEntries(ctx, c, size, mm) + }) + if err != nil { + return err + } + stop() + return nil +} + +func createEntries(ctx context.Context, entryCount int, size string, m *hazelcast.Map) error { + v, err := makeValue(size) + if err != nil { + return err + } + for i := 1; i <= entryCount; i++ { + k := fmt.Sprintf("k%d", i) + err := m.Set(ctx, k, v) + if err != nil { + return err + } + } + return nil +} + +func makeValue(size string) (string, error) { + b, err := getValueSize(size) + if err != nil { + return "", err + } + return strings.Repeat("a", int(b)), nil +} + +func getValueSize(sizeStr string) (int64, error) { + sizeStr = strings.ToUpper(sizeStr) + if strings.HasSuffix(sizeStr, kb) { + sizeStr = strings.TrimSuffix(sizeStr, kb) + size, err := strconv.ParseInt(sizeStr, 10, 64) + if err != nil { + return 0, err + } + return size * kbs, nil + } + if strings.HasSuffix(sizeStr, mb) { + sizeStr = strings.TrimSuffix(sizeStr, mb) + size, err := strconv.ParseInt(sizeStr, 10, 64) + if err != nil { + return 0, err + } + return size * mbs, nil + } + size, err := strconv.ParseInt(sizeStr, 10, 64) + if err != nil { + return 0, err + } + return size, nil +} + +func init() { + Must(plug.Registry.RegisterCommand("demo:map-setmany", &MapSetManyCmd{})) +} diff --git a/docs/modules/ROOT/pages/clc-demo.adoc b/docs/modules/ROOT/pages/clc-demo.adoc index d5e090bf..cdacc2ac 100644 --- a/docs/modules/ROOT/pages/clc-demo.adoc +++ b/docs/modules/ROOT/pages/clc-demo.adoc @@ -12,8 +12,9 @@ clc demo [command] [options] == Commands * <> +* <> -== clc demo submit +== clc demo generate data Generates stream events @@ -63,3 +64,39 @@ Example: ---- clc demo generate-data wikipedia-event-stream map=wiki-events --preview ---- + +== clc demo map setmany + +Generates multiple map entries. + +Usage: + +[source,bash] +---- +clc demo map-setmany [entry-count] [flags] +---- + +Parameters: + +[cols="1m,1a,2a,1a"] +|=== +|Parameter|Required|Description|Default + +|`--name`, `-n` +|Optional +|Name of the map. +|`default` + +|`--size` +|Optional +|Size of the map value in bytes, the following suffixes can also be used: kb, mb, e.g., 42kb +|`1` + +|=== + +Example: + +[source,bash] +---- +clc demo map-setmany 10 --name myMap --size 1kb +---- \ No newline at end of file