Skip to content

Commit

Permalink
feat: implement sortedSetPutElement (#266)
Browse files Browse the repository at this point in the history
Implement `sortedSetPutElement`.
  • Loading branch information
malandis authored and cprice404 committed Mar 14, 2023
1 parent 47cebfe commit 9227dd7
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 15 deletions.
22 changes: 22 additions & 0 deletions momento/cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type CacheClient interface {
KeysExist(ctx context.Context, r *KeysExistRequest) (responses.KeysExistResponse, error)

SortedSetFetch(ctx context.Context, r *SortedSetFetchRequest) (responses.SortedSetFetchResponse, error)
SortedSetPutElement(ctx context.Context, r *SortedSetPutElementRequest) (responses.SortedSetPutElementResponse, error)
SortedSetPutElements(ctx context.Context, r *SortedSetPutElementsRequest) (responses.SortedSetPutElementsResponse, error)
SortedSetGetScores(ctx context.Context, r *SortedSetGetScoresRequest) (responses.SortedSetGetScoresResponse, error)
SortedSetRemoveElements(ctx context.Context, r *SortedSetRemoveElementsRequest) (responses.SortedSetRemoveElementsResponse, error)
Expand Down Expand Up @@ -224,6 +225,27 @@ func (c defaultScsClient) SortedSetFetch(ctx context.Context, r *SortedSetFetchR
return r.response, nil
}

func (c defaultScsClient) SortedSetPutElement(ctx context.Context, r *SortedSetPutElementRequest) (responses.SortedSetPutElementResponse, error) {
if r.Value == nil {
return nil, convertMomentoSvcErrorToCustomerError(
momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError, "value cannot be nil", nil,
),
)
}
newRequest := &SortedSetPutElementsRequest{
CacheName: r.CacheName,
SetName: r.SetName,
Elements: []*SortedSetPutElement{{Value: r.Value, Score: r.Score}},
Ttl: r.Ttl,
}
if err := c.dataClient.makeRequest(ctx, newRequest); err != nil {
return nil, err
}

return &responses.SortedSetPutElementSuccess{}, nil
}

func (c defaultScsClient) SortedSetPutElements(ctx context.Context, r *SortedSetPutElementsRequest) (responses.SortedSetPutElementsResponse, error) {
if err := c.dataClient.makeRequest(ctx, r); err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions momento/sorted_set_put_element.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package momento

import (
"github.com/momentohq/client-sdk-go/utils"
)

type SortedSetPutElementRequest struct {
CacheName string
SetName string
Value Value
Score float64
Ttl *utils.CollectionTtl
}
6 changes: 3 additions & 3 deletions momento/sorted_set_put_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *SortedSetPutElementsRequest) initGrpcRequest(client scsDataClient) erro
return err
}

elements := convertSortedSetElementToGrpc(r.Elements)
elements := convertSortedSetElementsToGrpc(r.Elements)

r.grpcRequest = &pb.XSortedSetPutRequest{
SetName: []byte(r.SetName),
Expand All @@ -68,11 +68,11 @@ func (r *SortedSetPutElementsRequest) makeGrpcRequest(metadata context.Context,
}

func (r *SortedSetPutElementsRequest) interpretGrpcResponse() error {
r.response = &responses.SortedSetPutSuccess{}
r.response = &responses.SortedSetPutElementsSuccess{}
return nil
}

func convertSortedSetElementToGrpc(modelSetElements []*SortedSetPutElement) []*pb.XSortedSetElement {
func convertSortedSetElementsToGrpc(modelSetElements []*SortedSetPutElement) []*pb.XSortedSetElement {
var returnList []*pb.XSortedSetElement
for _, el := range modelSetElements {
returnList = append(returnList, &pb.XSortedSetElement{
Expand Down
67 changes: 64 additions & 3 deletions momento/sorted_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var _ = Describe("SortedSet", func() {
Elements: elements,
},
),
).To(BeAssignableToTypeOf(&SortedSetPutSuccess{}))
).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{}))
}

// Convenience for fetching elements.
Expand Down Expand Up @@ -79,6 +79,12 @@ var _ = Describe("SortedSet", func() {
}),
).Error().To(HaveMomentoErrorCode(expectedError))

Expect(
client.SortedSetPutElement(ctx, &SortedSetPutElementRequest{
CacheName: cacheName, SetName: collectionName, Value: value, Score: float64(1),
}),
).Error().To(HaveMomentoErrorCode(expectedError))

putElements := []*SortedSetPutElement{{
Value: value,
Score: float64(1),
Expand Down Expand Up @@ -180,7 +186,24 @@ var _ = Describe("SortedSet", func() {
).To(BeAssignableToTypeOf(SortedSetIncrementScoreSuccess(0)))
},
),
Entry(`SortedSetPut`,
Entry(`SortedSetPutElement`,
func(element SortedSetPutElement, ttl *utils.CollectionTtl) {
request := &SortedSetPutElementRequest{
CacheName: sharedContext.CacheName,
SetName: sharedContext.CollectionName,
Value: element.Value,
Score: element.Score,
}
if ttl != nil {
request.Ttl = ttl
}

Expect(
sharedContext.Client.SortedSetPutElement(sharedContext.Ctx, request),
).To(BeAssignableToTypeOf(&SortedSetPutElementSuccess{}))
},
),
Entry(`SortedSetPutElements`,
func(element SortedSetPutElement, ttl *utils.CollectionTtl) {
request := &SortedSetPutElementsRequest{
CacheName: sharedContext.CacheName,
Expand All @@ -193,7 +216,7 @@ var _ = Describe("SortedSet", func() {

Expect(
sharedContext.Client.SortedSetPutElements(sharedContext.Ctx, request),
).To(BeAssignableToTypeOf(&SortedSetPutSuccess{}))
).To(BeAssignableToTypeOf(&SortedSetPutElementsSuccess{}))
},
),
)
Expand Down Expand Up @@ -699,6 +722,44 @@ var _ = Describe("SortedSet", func() {
})
})

Describe(`SortedSetPutElement`, func() {
// TODO
/*
It(`Puts an element with a string value`, func() {
resp, err := sharedContext.Client.SortedSetPutElement(
sharedContext.Ctx,
&SortedSetPutElementRequest{
CacheName: sharedContext.CacheName,
SetName: sharedContext.CollectionName,
Value: String("aValue"),
Score: 42,
})
Expect(err).To(BeNil())
Expect(resp).To(BeAssignableToTypeOf(&SortedSetPutElementSuccess{}))
fetchResp, fetchErr := sharedContext.Client.SortedSetFetch(
sharedContext.Ctx,
&SortedSetFetchRequest{
CacheName: sharedContext.CacheName,
SetName: sharedContext.CollectionName,
},
)
Expect(fetchErr).To(BeNil())
switch fetchResp := fetchResp.(type) {
case *SortedSetFetchHit:
Expect(fetchResp.elements).To(Equal(
[]SortedSetElement{
&SortedSetElement{Value: "aValue", Score: 42},
},
))
}
})*/
})

Describe(`SortedSetPutElements`, func() {
// TODO: add tests for SortedSetPutElements
})

Describe(`SortedSetRemove`, func() {
It(`Succeeds when the element does not exist`, func() {
Expect(
Expand Down
9 changes: 0 additions & 9 deletions responses/sorted_set_put.go

This file was deleted.

9 changes: 9 additions & 0 deletions responses/sorted_set_put_element.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package responses

type SortedSetPutElementResponse interface {
isSortedSetPutElementResponse()
}

type SortedSetPutElementSuccess struct{}

func (SortedSetPutElementSuccess) isSortedSetPutElementResponse() {}
9 changes: 9 additions & 0 deletions responses/sorted_set_put_elements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package responses

type SortedSetPutElementsResponse interface {
isSortedSetPutElementsResponse()
}

type SortedSetPutElementsSuccess struct{}

func (SortedSetPutElementsSuccess) isSortedSetPutElementsResponse() {}

0 comments on commit 9227dd7

Please sign in to comment.