Skip to content

Commit

Permalink
chore: adds sorted set increment in incubating (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
eaddingtonwhite authored Feb 13, 2023
1 parent 8d5b73b commit 19717ae
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
17 changes: 15 additions & 2 deletions incubating/simple_cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type ScsClient interface {
SortedSetGetScore(ctx context.Context, request *SortedSetGetScoreRequest) (SortedSetGetScoreResponse, error)
SortedSetRemove(ctx context.Context, request *SortedSetRemoveRequest) error
SortedSetGetRank(ctx context.Context, request *SortedSetGetRankRequest) (SortedSetGetRankResponse, error)
// TODO need to impl sortedset increment still
//SortedSetIncrement(ctx context.Context, request *SortedSetIncrementRequest)
SortedSetIncrement(ctx context.Context, request *SortedSetIncrementRequest) (SortedSetIncrementResponse, error)

Close()
}
Expand Down Expand Up @@ -448,6 +447,20 @@ func (c *DefaultScsClient) SortedSetGetRank(ctx context.Context, request *Sorted
}
}

func (c *DefaultScsClient) SortedSetIncrement(ctx context.Context, request *SortedSetIncrementRequest) (SortedSetIncrementResponse, error) {
rsp, err := c.dataClient.SortedSetIncrement(ctx, &models.SortedSetIncrementRequest{
CacheName: request.CacheName,
SetName: []byte(request.SetName),
ElementName: request.ElementName.AsBytes(),
Amount: request.Amount,
CollectionTTL: request.CollectionTTL,
})
if err != nil {
return nil, convertMomentoSvcErrorToCustomerError(err)
}
return &SortedSetIncrementResponseSuccess{Value: rsp.Value}, nil
}

// Close shutdown the client.
func (c *DefaultScsClient) Close() {
defer c.internalClient.Close()
Expand Down
2 changes: 1 addition & 1 deletion incubating/sortedset_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ type SortedSetIncrementRequest struct {
CacheName string
SetName string
ElementName momento.Bytes
Amount uint64
Amount float64
CollectionTTL utils.CollectionTTL
}
9 changes: 9 additions & 0 deletions incubating/sortedset_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ func (SortedSetRankHit) isSortedSetRankElement() {}
type SortedSetRankMiss struct{}

func (SortedSetRankMiss) isSortedSetRankElement() {}

type SortedSetIncrementResponse interface {
isSortedSetIncrementResponse()
}
type SortedSetIncrementResponseSuccess struct {
Value float64
}

func (SortedSetIncrementResponseSuccess) isSortedSetIncrementResponse() {}
2 changes: 1 addition & 1 deletion internal/models/sortedset_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ type SortedSetIncrementRequest struct {
CacheName string
SetName []byte
ElementName []byte
Amount uint64
Amount float64
CollectionTTL incubating.CollectionTTL
}
4 changes: 4 additions & 0 deletions internal/models/sortedset_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ type SortedSetGetRankHit struct {
}

func (SortedSetGetRankHit) isSortedSetGetRankResponse() {}

type SortedSetIncrementResponse struct {
Value float64
}
21 changes: 21 additions & 0 deletions internal/services/scs_data_sorted_set_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ func (client *ScsDataClient) SortedSetGetRank(ctx context.Context, request *mode
}
}

func (client *ScsDataClient) SortedSetIncrement(ctx context.Context, request *models.SortedSetIncrementRequest) (*models.SortedSetIncrementResponse, momentoerrors.MomentoSvcErr) {
ctx, cancel := context.WithTimeout(ctx, client.requestTimeout)
defer cancel()
resp, err := client.grpcClient.SortedSetIncrement(
metadata.NewOutgoingContext(ctx, createNewMetadata(request.CacheName)),
&pb.XSortedSetIncrementRequest{
SetName: request.SetName,
ElementName: request.ElementName,
Amount: request.Amount,
TtlMilliseconds: uint64(request.CollectionTTL.Ttl.Milliseconds()),
RefreshTtl: request.CollectionTTL.RefreshTtl,
},
)
if err != nil {
return nil, momentoerrors.ConvertSvcErr(err)
}
return &models.SortedSetIncrementResponse{
Value: resp.Value,
}, nil
}

func (client *ScsDataClient) SortedSetRemove(ctx context.Context, request *models.SortedSetRemoveRequest) momentoerrors.MomentoSvcErr {
ctx, cancel := context.WithTimeout(ctx, client.requestTimeout)
defer cancel()
Expand Down

0 comments on commit 19717ae

Please sign in to comment.