-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Split sortedSetFetch up into ByRank and ByScore functions (#280)
* feat: Split sortedSetFetch up into ByRank and ByScore functions As per the API design discussion and the pattern we established in the node.js SDK, this commit splits the SortedSetFetch into two functions, one for fetching by score and one for by rank. This prevents the user from needing to reason about which combinations of arguments are and are not legal together.
- Loading branch information
Showing
6 changed files
with
355 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package momento | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/momentohq/client-sdk-go/responses" | ||
|
||
pb "github.com/momentohq/client-sdk-go/internal/protos" | ||
) | ||
|
||
type SortedSetFetchByRankRequest struct { | ||
CacheName string | ||
SetName string | ||
Order SortedSetOrder | ||
StartRank *int32 | ||
EndRank *int32 | ||
|
||
grpcRequest *pb.XSortedSetFetchRequest | ||
grpcResponse *pb.XSortedSetFetchResponse | ||
response responses.SortedSetFetchResponse | ||
} | ||
|
||
func (r *SortedSetFetchByRankRequest) cacheName() string { return r.CacheName } | ||
|
||
func (r *SortedSetFetchByRankRequest) requestName() string { return "Sorted set fetch" } | ||
|
||
func (r *SortedSetFetchByRankRequest) initGrpcRequest(scsDataClient) error { | ||
var err error | ||
|
||
if _, err = prepareName(r.SetName, "Set name"); err != nil { | ||
return err | ||
} | ||
|
||
grpcReq := &pb.XSortedSetFetchRequest{ | ||
SetName: []byte(r.SetName), | ||
Order: pb.XSortedSetFetchRequest_Order(r.Order), | ||
WithScores: true, | ||
} | ||
|
||
// This is the default: fetch everything in ascending order. | ||
by_index := pb.XSortedSetFetchRequest_ByIndex{ | ||
ByIndex: &pb.XSortedSetFetchRequest_XByIndex{ | ||
Start: &pb.XSortedSetFetchRequest_XByIndex_UnboundedStart{}, | ||
End: &pb.XSortedSetFetchRequest_XByIndex_UnboundedEnd{}, | ||
}, | ||
} | ||
|
||
if r.StartRank != nil { | ||
|
||
by_index.ByIndex.Start = &pb.XSortedSetFetchRequest_XByIndex_InclusiveStartIndex{ | ||
InclusiveStartIndex: *r.StartRank, | ||
} | ||
} | ||
|
||
if r.EndRank != nil { | ||
by_index.ByIndex.End = &pb.XSortedSetFetchRequest_XByIndex_ExclusiveEndIndex{ | ||
ExclusiveEndIndex: *r.EndRank, | ||
} | ||
} | ||
|
||
grpcReq.Range = &by_index | ||
|
||
r.grpcRequest = grpcReq | ||
|
||
return nil | ||
} | ||
|
||
func (r *SortedSetFetchByRankRequest) makeGrpcRequest(metadata context.Context, client scsDataClient) (grpcResponse, error) { | ||
resp, err := client.grpcClient.SortedSetFetch(metadata, r.grpcRequest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r.grpcResponse = resp | ||
|
||
return resp, nil | ||
} | ||
|
||
func (r *SortedSetFetchByRankRequest) interpretGrpcResponse() error { | ||
switch grpcResp := r.grpcResponse.SortedSet.(type) { | ||
case *pb.XSortedSetFetchResponse_Found: | ||
r.response = responses.NewSortedSetFetchHit(sortedSetByRankGrpcElementToModel(grpcResp.Found.GetValuesWithScores().Elements)) | ||
case *pb.XSortedSetFetchResponse_Missing: | ||
r.response = &responses.SortedSetFetchMiss{} | ||
default: | ||
return errUnexpectedGrpcResponse(r, r.grpcResponse) | ||
} | ||
return nil | ||
} | ||
|
||
func sortedSetByRankGrpcElementToModel(grpcSetElements []*pb.XSortedSetElement) []responses.SortedSetBytesElement { | ||
var returnList []responses.SortedSetBytesElement | ||
for _, element := range grpcSetElements { | ||
returnList = append(returnList, responses.SortedSetBytesElement{ | ||
Value: element.Value, | ||
Score: element.Score, | ||
}) | ||
} | ||
return returnList | ||
} |
Oops, something went wrong.