Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat!: Sortedset inconsistencies #239

Merged
merged 7 commits into from
Mar 7, 2023
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
7 changes: 0 additions & 7 deletions momento/cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,6 @@ func (c defaultScsClient) SortedSetGetScores(ctx context.Context, r *SortedSetGe
}

func (c defaultScsClient) SortedSetRemove(ctx context.Context, r *SortedSetRemoveRequest) (responses.SortedSetRemoveResponse, error) {
if r.ElementsToRemove == nil {
return nil, convertMomentoSvcErrorToCustomerError(
momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError, "elements to remove cannot be nil", nil,
),
)
}
if err := c.dataClient.makeRequest(ctx, r); err != nil {
return nil, err
}
Expand Down
11 changes: 5 additions & 6 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,20 @@ func prepareName(name string, label string) (string, error) {
return name, nil
}

func prepareElementValue(name Value) ([]byte, error) {
if name == nil {
func prepareElementValue(value Value) ([]byte, error) {
if value == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error below should say "element value cannot be nil" instead?

return nil, buildError(
momentoerrors.InvalidArgumentError, "element value cannot be nil or empty", nil,
momentoerrors.InvalidArgumentError, "element value cannot be nil", nil,
)
}

// just validate not empty using prepareName
nameBytes := name.asBytes()
_, err := prepareName(string(nameBytes), "element value")
_, err := prepareName(value.asString(), "element value")
if err != nil {
return nil, err
}

return nameBytes, nil
return value.asBytes(), nil
}

func prepareCacheName(r hasCacheName) (string, error) {
Expand Down
12 changes: 5 additions & 7 deletions momento/sorted_set_get_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type SortedSetGetRankRequest struct {
CacheName string
SetName string
ElementValue Value
CacheName string
SetName string
Value Value

grpcRequest *pb.XSortedSetGetRankRequest
grpcResponse *pb.XSortedSetGetRankResponse
Expand All @@ -30,7 +30,7 @@ func (r *SortedSetGetRankRequest) initGrpcRequest(scsDataClient) error {
}

var value []byte
if value, err = prepareElementValue(r.ElementValue); err != nil {
if value, err = prepareElementValue(r.Value); err != nil {
return err
}

Expand Down Expand Up @@ -61,9 +61,7 @@ func (r *SortedSetGetRankRequest) interpretGrpcResponse() error {
var resp responses.SortedSetGetRankResponse
switch rank := grpcResp.Rank.(type) {
case *pb.XSortedSetGetRankResponse_ElementRank:
resp = &responses.SortedSetGetRankHit{
Rank: rank.ElementRank.Rank,
}
resp = responses.SortedSetGetRankHit(rank.ElementRank.Rank)
case *pb.XSortedSetGetRankResponse_Missing:
resp = &responses.SortedSetGetRankMiss{}
default:
Expand Down
8 changes: 4 additions & 4 deletions momento/sorted_set_get_scores.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type SortedSetGetScoresRequest struct {
CacheName string
SetName string
ElementValues []Value
CacheName string
SetName string
Values []Value

grpcRequest *pb.XSortedSetGetScoreRequest
grpcResponse *pb.XSortedSetGetScoreResponse
Expand All @@ -29,7 +29,7 @@ func (r *SortedSetGetScoresRequest) initGrpcRequest(scsDataClient) error {
return err
}

values, err := momentoValuesToPrimitiveByteList(r.ElementValues)
values, err := momentoValuesToPrimitiveByteList(r.Values)
if err != nil {
return err
}
Expand Down
17 changes: 8 additions & 9 deletions momento/sorted_set_increment_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
)

type SortedSetIncrementScoreRequest struct {
CacheName string
SetName string
ElementValue Value
Amount float64
Ttl *utils.CollectionTtl
CacheName string
SetName string
Value Value
Amount float64
Ttl *utils.CollectionTtl

grpcRequest *pb.XSortedSetIncrementRequest
grpcResponse *pb.XSortedSetIncrementResponse
Expand Down Expand Up @@ -47,7 +47,7 @@ func (r *SortedSetIncrementScoreRequest) initGrpcRequest(client scsDataClient) e
}

var value []byte
if value, err = prepareElementValue(r.ElementValue); err != nil {
if value, err = prepareElementValue(r.Value); err != nil {
return err
}

Expand Down Expand Up @@ -79,8 +79,7 @@ func (r *SortedSetIncrementScoreRequest) makeGrpcRequest(metadata context.Contex
}

func (r *SortedSetIncrementScoreRequest) interpretGrpcResponse() error {
r.response = &responses.SortedSetIncrementScoreSuccess{
Value: r.grpcResponse.Score,
}
r.response = responses.SortedSetIncrementScoreSuccess(r.grpcResponse.Score)

return nil
}
61 changes: 12 additions & 49 deletions momento/sorted_set_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,46 @@ package momento

import (
"context"
"fmt"

"github.com/momentohq/client-sdk-go/responses"

pb "github.com/momentohq/client-sdk-go/internal/protos"
)

type SortedSetRemoveRequest struct {
CacheName string
SetName string
ElementsToRemove SortedSetRemoveNumElements
CacheName string
SetName string
Values []Value

grpcRequest *pb.XSortedSetRemoveRequest
grpcResponse *pb.XSortedSetRemoveResponse
response responses.SortedSetRemoveResponse
}

type SortedSetRemoveRequestElement struct {
Name Value
}

type SortedSetRemoveNumElements interface {
isSortedSetRemoveNumElement()
}

type RemoveAllElements struct{}

func (RemoveAllElements) isSortedSetRemoveNumElement() {}

type RemoveSomeElements struct {
Elements []Value
}

func (RemoveSomeElements) isSortedSetRemoveNumElement() {}

func (r *SortedSetRemoveRequest) cacheName() string { return r.CacheName }

func (r *SortedSetRemoveRequest) requestName() string { return "Sorted set remove" }

func (r *SortedSetRemoveRequest) values() []Value { return r.Values }

func (r *SortedSetRemoveRequest) initGrpcRequest(scsDataClient) error {
var err error

if _, err = prepareName(r.SetName, "Set name"); err != nil {
return err
}

var valuesToRemove [][]byte
if valuesToRemove, err = prepareValues(r); err != nil {
return err
}

grpcReq := &pb.XSortedSetRemoveRequest{
SetName: []byte(r.SetName),
}

switch toRemove := r.ElementsToRemove.(type) {
case RemoveAllElements:
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_All{}
case *RemoveAllElements:
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_All{}
case RemoveSomeElements:
elemToRemove, err := momentoValuesToPrimitiveByteList(toRemove.Elements)
if err != nil {
return err
}
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_Some{
Some: &pb.XSortedSetRemoveRequest_XSome{
Values: elemToRemove,
},
}
case *RemoveSomeElements:
elemToRemove, err := momentoValuesToPrimitiveByteList(toRemove.Elements)
if err != nil {
return err
}
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_Some{
Some: &pb.XSortedSetRemoveRequest_XSome{
Values: elemToRemove,
},
}
default:
return fmt.Errorf("%T is an unrecognized type for ElementsToRemove", r.ElementsToRemove)
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_Some{
Some: &pb.XSortedSetRemoveRequest_XSome{Values: valuesToRemove},
}

r.grpcRequest = grpcReq
Expand Down
Loading