Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added CLI options for partition-key, comparison type and sequence keys #477

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/client/del/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (

type flags struct {
expectedVersion int64
partitionKey string
}

func (flags *flags) Reset() {
Expand All @@ -37,6 +38,7 @@ func (flags *flags) Reset() {

func init() {
Cmd.Flags().Int64VarP(&Config.expectedVersion, "expected-version", "e", -1, "Version of entry expected to be on the server")
Cmd.Flags().StringVarP(&Config.partitionKey, "partition-key", "p", "", "Partition Key to be used in override the shard routing")
}

var Cmd = &cobra.Command{
Expand All @@ -60,6 +62,9 @@ func exec(_ *cobra.Command, args []string) error {
if Config.expectedVersion >= 0 {
options = append(options, oxia.ExpectedVersionId(Config.expectedVersion))
}
if Config.partitionKey != "" {
options = append(options, oxia.PartitionKey(Config.partitionKey))
}

return client.Delete(context.Background(), key, options...)
}
11 changes: 11 additions & 0 deletions cmd/client/del/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,16 @@ func TestDelete_exec(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "Error: failed to delete", out)

common.MockedClient.On("Delete", "my-key", []oxia.DeleteOption{oxia.PartitionKey("xyz")}).Return(nil)
out, err = runCmd(Cmd, "my-key -p xyz")
assert.NoError(t, err)
assert.Empty(t, out)

common.MockedClient.On("Delete", "my-key",
[]oxia.DeleteOption{oxia.ExpectedVersionId(4), oxia.PartitionKey("xyz")}).Return(nil)
out, err = runCmd(Cmd, "my-key -e 4 -p xyz")
assert.NoError(t, err)
assert.Empty(t, out)

common.MockedClient.AssertExpectations(t)
}
34 changes: 33 additions & 1 deletion cmd/client/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"context"
"time"

"github.com/pkg/errors"

"github.com/streamnative/oxia/oxia"

"github.com/spf13/cobra"

"github.com/streamnative/oxia/cmd/client/common"
Expand All @@ -31,17 +35,25 @@ type flags struct {
key string
hexDump bool
includeVersion bool
partitionKey string
comparisonType string
}

func (flags *flags) Reset() {
flags.key = ""
flags.hexDump = false
flags.includeVersion = false
flags.partitionKey = ""
flags.comparisonType = "equal"
}

func init() {
Cmd.Flags().BoolVarP(&Config.includeVersion, "include-version", "v", false, "Include the record version object")
Cmd.Flags().BoolVar(&Config.hexDump, "hex", false, "Print the value in HexDump format")
Cmd.Flags().StringVarP(&Config.partitionKey, "partition-key", "p", "", "Partition Key to be used in override the shard routing")

Cmd.Flags().StringVarP(&Config.comparisonType, "comparison-type", "t", "equal",
"The type of get comparison. Allowed value: equal, floor, ceiling, lower, higher")
}

var Cmd = &cobra.Command{
Expand All @@ -58,8 +70,28 @@ func exec(cmd *cobra.Command, args []string) error {
return err
}

var options []oxia.GetOption
if Config.partitionKey != "" {
options = append(options, oxia.PartitionKey(Config.partitionKey))
}

switch Config.comparisonType {
case "equal":
// Nothing to do, this is default
case "floor":
options = append(options, oxia.ComparisonFloor())
case "ceiling":
options = append(options, oxia.ComparisonCeiling())
case "lower":
options = append(options, oxia.ComparisonLower())
case "higher":
options = append(options, oxia.ComparisonHigher())
default:
return errors.Errorf("invalid comparison type: %s", Config.comparisonType)
}

queryKey := args[0]
key, value, version, err := client.Get(context.Background(), queryKey)
key, value, version, err := client.Get(context.Background(), queryKey, options...)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/client/get/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func Test_exec(t *testing.T) {
}{
{"key", "x", []any{"x", emptyOptions}, "value-x"},
{"other-key", "y", []any{"y", emptyOptions}, "value-y"},
{"partition-key", "y -p xyz", []any{"y", []oxia.GetOption{oxia.PartitionKey("xyz")}}, "value-y"},
{"equal", "y -t equal", []any{"y", emptyOptions}, "value-y"},
{"floor", "y -t floor", []any{"y", []oxia.GetOption{oxia.ComparisonFloor()}}, "value-y"},
{"ceiling", "y -t ceiling", []any{"y", []oxia.GetOption{oxia.ComparisonCeiling()}}, "value-y"},
{"lower", "y -t lower", []any{"y", []oxia.GetOption{oxia.ComparisonLower()}}, "value-y"},
{"higher", "y -t higher", []any{"y", []oxia.GetOption{oxia.ComparisonHigher()}}, "value-y"},
} {
t.Run(test.name, func(t *testing.T) {
common.MockedClient = common.NewMockClient()
Expand Down
11 changes: 9 additions & 2 deletions cmd/client/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ var (
)

type flags struct {
keyMin string
keyMax string
keyMin string
keyMax string
partitionKey string
}

func (flags *flags) Reset() {
Expand All @@ -41,6 +42,7 @@ func (flags *flags) Reset() {
func init() {
Cmd.Flags().StringVarP(&Config.keyMin, "key-min", "s", "", "Key range minimum (inclusive)")
Cmd.Flags().StringVarP(&Config.keyMax, "key-max", "e", "", "Key range maximum (exclusive)")
Cmd.Flags().StringVarP(&Config.partitionKey, "partition-key", "p", "", "Partition Key to be used in override the shard routing")
}

var Cmd = &cobra.Command{
Expand All @@ -62,6 +64,11 @@ func exec(cmd *cobra.Command, _ []string) error {
// By default, do not list internal keys
Config.keyMax = "__oxia/"
}

if Config.partitionKey != "" {
options = append(options, oxia.PartitionKey(Config.partitionKey))
}

list, err := client.List(context.Background(), Config.keyMin, Config.keyMax, options...)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/client/list/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestList_exec(t *testing.T) {
{"short", "-s a -e c", []any{"a", "c", emptyOptions}},
{"range-no-min", "--key-max c", []any{"", "c", emptyOptions}},
{"range-no-max", "--key-min a", []any{"a", "__oxia/", emptyOptions}},
{"partition-key", "-s a -e c -p xyz", []any{"a", "c", []oxia.ListOption{oxia.PartitionKey("xyz")}}},
} {
t.Run(test.name, func(t *testing.T) {
common.MockedClient = common.NewMockClient()
Expand Down
32 changes: 26 additions & 6 deletions cmd/client/put/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ var (
type flags struct {
expectedVersion int64
readValueFromStdIn bool
partitionKey string
sequenceKeysDeltas []int64
}

func (flags *flags) Reset() {
flags.expectedVersion = -1
flags.readValueFromStdIn = false
flags.partitionKey = ""
flags.sequenceKeysDeltas = nil
}

func init() {
Cmd.Flags().Int64VarP(&Config.expectedVersion, "expected-version", "e", -1, "Version of entry expected to be on the server")
Cmd.Flags().BoolVarP(&Config.readValueFromStdIn, "std-in", "c", false, "Read value from stdin")
Cmd.Flags().StringVarP(&Config.partitionKey, "partition-key", "p", "", "Partition Key to be used in override the shard routing")
Cmd.Flags().Int64SliceVarP(&Config.sequenceKeysDeltas, "sequence-keys-deltas", "d", nil, "Specify one or more sequence keys deltas to be added to the inserted key")
}

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -77,12 +83,7 @@ func exec(cmd *cobra.Command, args []string) error {
}
}

var options []oxia.PutOption
if Config.expectedVersion != -1 {
options = append(options, oxia.ExpectedVersionId(Config.expectedVersion))
}

key, version, err := client.Put(context.Background(), key, value, options...)
key, version, err := client.Put(context.Background(), key, value, getOptions()...)
if err != nil {
return err
}
Expand All @@ -98,3 +99,22 @@ func exec(cmd *cobra.Command, args []string) error {
})
return nil
}

func getOptions() []oxia.PutOption {
var options []oxia.PutOption
if Config.expectedVersion != -1 {
options = append(options, oxia.ExpectedVersionId(Config.expectedVersion))
}
if Config.partitionKey != "" {
options = append(options, oxia.PartitionKey(Config.partitionKey))
}
if len(Config.sequenceKeysDeltas) > 0 {
deltas := make([]uint64, len(Config.sequenceKeysDeltas))
for i := range Config.sequenceKeysDeltas {
deltas[i] = uint64(Config.sequenceKeysDeltas[i])
}
options = append(options, oxia.SequenceKeysDeltas(deltas...))
}

return options
}
4 changes: 4 additions & 0 deletions cmd/client/put/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/spf13/cobra"

"github.com/streamnative/oxia/cmd/client/common"
"github.com/streamnative/oxia/oxia"
)
Expand All @@ -46,6 +47,9 @@ func TestPut_exec(t *testing.T) {
{"entry", "x y", "", []any{"x", []byte("y"), emptyOptions}},
{"entry-expected-version", "x y -e 5", "", []any{"x", []byte("y"), []oxia.PutOption{oxia.ExpectedVersionId(5)}}},
{"stdin", "x -c -e 5", "my-value", []any{"x", []byte("my-value"), []oxia.PutOption{oxia.ExpectedVersionId(5)}}},
{"partition-key", "x y -p abc", "", []any{"x", []byte("y"), []oxia.PutOption{oxia.PartitionKey("abc")}}},
{"sequence-keys", "x y -p abc -d 1,2,3", "", []any{"x", []byte("y"),
[]oxia.PutOption{oxia.PartitionKey("abc"), oxia.SequenceKeysDeltas(1, 2, 3)}}},
} {
t.Run(test.name, func(t *testing.T) {
common.MockedClient = common.NewMockClient()
Expand Down
Loading