Skip to content

Commit

Permalink
chore: sorted set testing followup (#288)
Browse files Browse the repository at this point in the history
* fix: add validation for start and end rank

* chore: adding sorted set testing

* filling out range validator test

* fix: convert nil result from fetchbyrank to empty result list
  • Loading branch information
pgautier404 authored Mar 16, 2023
1 parent 1b08548 commit bd87e16
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 33 deletions.
14 changes: 14 additions & 0 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,17 @@ func validateNotNil(value Value, label string) error {

return nil
}

func validateSortedSetRanks(start int32, end int32) error {
if start >= 0 && end >= 0 && start >= end {
return buildError(
momentoerrors.InvalidArgumentError, "start rank must be less than end rank", nil,
)
}
if start < 0 && end < 0 && start >= end {
return buildError(
momentoerrors.InvalidArgumentError, "negative start rank must be less than negative end rank", nil,
)
}
return nil
}
14 changes: 10 additions & 4 deletions momento/sorted_set_fetch_by_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,33 @@ func (r *SortedSetFetchByRankRequest) initGrpcRequest(scsDataClient) error {
}

// This is the default: fetch everything in ascending order.
by_index := pb.XSortedSetFetchRequest_ByIndex{
byIndex := pb.XSortedSetFetchRequest_ByIndex{
ByIndex: &pb.XSortedSetFetchRequest_XByIndex{
Start: &pb.XSortedSetFetchRequest_XByIndex_UnboundedStart{},
End: &pb.XSortedSetFetchRequest_XByIndex_UnboundedEnd{},
},
}

startForValidation := int32(0)
if r.StartRank != nil {

by_index.ByIndex.Start = &pb.XSortedSetFetchRequest_XByIndex_InclusiveStartIndex{
byIndex.ByIndex.Start = &pb.XSortedSetFetchRequest_XByIndex_InclusiveStartIndex{
InclusiveStartIndex: *r.StartRank,
}
startForValidation = *r.StartRank
}

if r.EndRank != nil {
by_index.ByIndex.End = &pb.XSortedSetFetchRequest_XByIndex_ExclusiveEndIndex{
byIndex.ByIndex.End = &pb.XSortedSetFetchRequest_XByIndex_ExclusiveEndIndex{
ExclusiveEndIndex: *r.EndRank,
}
endForValidation := *r.EndRank
if err := validateSortedSetRanks(startForValidation, endForValidation); err != nil {
return err
}
}

grpcReq.Range = &by_index
grpcReq.Range = &byIndex

r.grpcRequest = grpcReq

Expand Down
22 changes: 18 additions & 4 deletions momento/sorted_set_put_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"time"

"github.com/momentohq/client-sdk-go/internal/momentoerrors"
"github.com/momentohq/client-sdk-go/responses"
"github.com/momentohq/client-sdk-go/utils"

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

type SortedSetPutElementsRequest struct {
Expand Down Expand Up @@ -42,7 +43,10 @@ func (r *SortedSetPutElementsRequest) initGrpcRequest(client scsDataClient) erro
return err
}

elements := convertSortedSetElementsToGrpc(r.Elements)
elements, err := convertSortedSetElementsToGrpc(r.Elements)
if err != nil {
return err
}

r.grpcRequest = &pb.XSortedSetPutRequest{
SetName: []byte(r.SetName),
Expand All @@ -67,13 +71,23 @@ func (r *SortedSetPutElementsRequest) interpretGrpcResponse() error {
return nil
}

func convertSortedSetElementsToGrpc(modelSetElements []SortedSetElement) []*pb.XSortedSetElement {
func convertSortedSetElementsToGrpc(modelSetElements []SortedSetElement) ([]*pb.XSortedSetElement, error) {
if modelSetElements == nil {
return nil, buildError(
momentoerrors.InvalidArgumentError, "elements cannot be nil", nil,
)
}
var returnList []*pb.XSortedSetElement
for _, el := range modelSetElements {
if el.Value == nil {
return nil, buildError(
momentoerrors.InvalidArgumentError, "element value cannot be nil", nil,
)
}
returnList = append(returnList, &pb.XSortedSetElement{
Value: el.Value.asBytes(),
Score: el.Score,
})
}
return returnList
return returnList, nil
}
Loading

0 comments on commit bd87e16

Please sign in to comment.