Skip to content

Commit

Permalink
chore: make CollectionTtl pointer and assign default value to Ttl
Browse files Browse the repository at this point in the history
… and `RefreshTtl` (#212)
  • Loading branch information
poppoerika authored and cprice404 committed Mar 14, 2023
1 parent d10fa12 commit 6e92b7f
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 63 deletions.
2 changes: 1 addition & 1 deletion examples/dictionary-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func incrementField(counterField momento.Value, amount int64) {
DictionaryName: dictionaryName,
Field: counterField,
Amount: amount,
Ttl: utils.CollectionTtl{
Ttl: &utils.CollectionTtl{
Ttl: time.Second * 30,
RefreshTtl: true,
},
Expand Down
4 changes: 2 additions & 2 deletions examples/list-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func pushFrontToList(value momento.Value) {
ListName: listName,
Value: value,
TruncateBackToSize: 0,
Ttl: utils.CollectionTtl{
Ttl: &utils.CollectionTtl{
Ttl: 5 * time.Second,
RefreshTtl: true,
},
Expand All @@ -52,7 +52,7 @@ func pushBackToList(value momento.Value) {
ListName: listName,
Value: value,
TruncateFrontToSize: 0,
Ttl: utils.CollectionTtl{
Ttl: &utils.CollectionTtl{
Ttl: 5 * time.Second,
RefreshTtl: true,
},
Expand Down
13 changes: 8 additions & 5 deletions momento/dictionary_increment.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type DictionaryIncrementRequest struct {
DictionaryName string
Field Value
Amount int64
Ttl utils.CollectionTtl
Ttl *utils.CollectionTtl

grpcRequest *pb.XDictionaryIncrementRequest
grpcResponse *pb.XDictionaryIncrementResponse
Expand All @@ -47,6 +47,8 @@ func (r *DictionaryIncrementRequest) field() Value { return r.Field }

func (r *DictionaryIncrementRequest) ttl() time.Duration { return r.Ttl.Ttl }

func (r *DictionaryIncrementRequest) collectionTtl() *utils.CollectionTtl { return r.Ttl }

func (r *DictionaryIncrementRequest) requestName() string { return "DictionaryFetch" }

func (r *DictionaryIncrementRequest) initGrpcRequest(client scsDataClient) error {
Expand All @@ -61,8 +63,9 @@ func (r *DictionaryIncrementRequest) initGrpcRequest(client scsDataClient) error
return err
}

var ttl uint64
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
var ttlMilliseconds uint64
var refreshTtl bool
if ttlMilliseconds, refreshTtl, err = prepareCollectionTtl(r, client.defaultTtl); err != nil {
return err
}

Expand All @@ -78,8 +81,8 @@ func (r *DictionaryIncrementRequest) initGrpcRequest(client scsDataClient) error
DictionaryName: []byte(r.DictionaryName),
Field: field,
Amount: r.Amount,
TtlMilliseconds: ttl,
RefreshTtl: r.Ttl.RefreshTtl,
TtlMilliseconds: ttlMilliseconds,
RefreshTtl: refreshTtl,
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion momento/dictionary_set_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ type DictionarySetFieldRequest struct {
DictionaryName string
Field Value
Value Value
Ttl utils.CollectionTtl
Ttl *utils.CollectionTtl
}
13 changes: 8 additions & 5 deletions momento/dictionary_set_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type DictionarySetFieldsRequest struct {
CacheName string
DictionaryName string
Elements map[string]Value
Ttl utils.CollectionTtl
Ttl *utils.CollectionTtl

grpcRequest *pb.XDictionarySetRequest
grpcResponse *pb.XDictionarySetResponse
Expand All @@ -38,6 +38,8 @@ func (r *DictionarySetFieldsRequest) elements() map[string]Value { return r.Elem

func (r *DictionarySetFieldsRequest) ttl() time.Duration { return r.Ttl.Ttl }

func (r *DictionarySetFieldsRequest) collectionTtl() *utils.CollectionTtl { return r.Ttl }

func (r *DictionarySetFieldsRequest) requestName() string { return "DictionarySetFields" }

func (r *DictionarySetFieldsRequest) initGrpcRequest(client scsDataClient) error {
Expand All @@ -60,16 +62,17 @@ func (r *DictionarySetFieldsRequest) initGrpcRequest(client scsDataClient) error
})
}

var ttl uint64
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
var ttlMilliseconds uint64
var refreshTtl bool
if ttlMilliseconds, refreshTtl, err = prepareCollectionTtl(r, client.defaultTtl); err != nil {
return err
}

r.grpcRequest = &pb.XDictionarySetRequest{
DictionaryName: []byte(r.DictionaryName),
Items: pbElements,
TtlMilliseconds: ttl,
RefreshTtl: r.Ttl.RefreshTtl,
TtlMilliseconds: ttlMilliseconds,
RefreshTtl: refreshTtl,
}

return nil
Expand Down
23 changes: 16 additions & 7 deletions momento/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ var _ = Describe("Dictionary methods", func() {
DictionaryName: dictionaryName,
Field: String("hi"),
Value: String("hi"),
Ttl: utils.CollectionTtl{},
}),
).Error().To(HaveMomentoErrorCode(expectedErrorCode))

Expand All @@ -89,7 +88,6 @@ var _ = Describe("Dictionary methods", func() {
CacheName: cacheName,
DictionaryName: dictionaryName,
Elements: nil,
Ttl: utils.CollectionTtl{},
}),
).Error().To(HaveMomentoErrorCode(expectedErrorCode))
},
Expand Down Expand Up @@ -148,6 +146,18 @@ var _ = Describe("Dictionary methods", func() {
Entry("both nil", nil, nil),
)

It("errors with a negative ttl for set", func() {
Expect(
sharedContext.Client.DictionarySetField(sharedContext.Ctx, &DictionarySetFieldRequest{
CacheName: sharedContext.CacheName,
DictionaryName: sharedContext.CollectionName,
Field: String("myField"),
Value: String("myValue"),
Ttl: &utils.CollectionTtl{Ttl: time.Duration(-1), RefreshTtl: true},
}),
).Error().To(HaveMomentoErrorCode(InvalidArgumentError))
})

DescribeTable("add string fields and string and bytes values for set fields happy path",
func(elements map[string]Value, expectedItemsStringValue map[string]string, expectedItemsByteValue map[string][]byte) {
Expect(
Expand Down Expand Up @@ -674,15 +684,14 @@ var _ = Describe("Dictionary methods", func() {

When("collection TTL is empty", func() {

It("will have a false refreshTTL and fetch will miss after client default ttl", func() {
It("will have a default ttl and refreshTtl and fetch will hit after client default ttl", func() {
time.Sleep(sharedContext.DefaultTtl / 2)
Expect(
sharedContext.Client.DictionarySetField(sharedContext.Ctx, &DictionarySetFieldRequest{
CacheName: sharedContext.CacheName,
DictionaryName: sharedContext.CollectionName,
Field: String("foo"),
Value: String("bar"),
Ttl: utils.CollectionTtl{},
}),
).To(BeAssignableToTypeOf(&DictionarySetFieldSuccess{}))

Expand All @@ -693,7 +702,7 @@ var _ = Describe("Dictionary methods", func() {
CacheName: sharedContext.CacheName,
DictionaryName: sharedContext.CollectionName,
}),
).To(BeAssignableToTypeOf(&DictionaryFetchMiss{}))
).To(BeAssignableToTypeOf(&DictionaryFetchHit{}))
})

})
Expand All @@ -707,7 +716,7 @@ var _ = Describe("Dictionary methods", func() {
DictionaryName: sharedContext.CollectionName,
Field: String("myField3"),
Value: String("myValue3"),
Ttl: utils.CollectionTtl{
Ttl: &utils.CollectionTtl{
Ttl: sharedContext.DefaultTtl + time.Second*60,
RefreshTtl: false,
},
Expand All @@ -731,7 +740,7 @@ var _ = Describe("Dictionary methods", func() {
DictionaryName: sharedContext.CollectionName,
Field: String("myField3"),
Value: String("myValue3"),
Ttl: utils.CollectionTtl{
Ttl: &utils.CollectionTtl{
Ttl: sharedContext.DefaultTtl + time.Second*60,
RefreshTtl: true,
},
Expand Down
13 changes: 8 additions & 5 deletions momento/list_concatenate_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ListConcatenateBackRequest struct {
ListName string
Values []Value
TruncateFrontToSize uint32
Ttl utils.CollectionTtl
Ttl *utils.CollectionTtl

grpcRequest *pb.XListConcatenateBackRequest
grpcResponse *pb.XListConcatenateBackResponse
Expand All @@ -44,6 +44,8 @@ func (r *ListConcatenateBackRequest) values() []Value { return r.Values }

func (r *ListConcatenateBackRequest) ttl() time.Duration { return r.Ttl.Ttl }

func (r *ListConcatenateBackRequest) collectionTtl() *utils.CollectionTtl { return r.Ttl }

func (r *ListConcatenateBackRequest) requestName() string { return "ListConcatenateBack" }

func (r *ListConcatenateBackRequest) initGrpcRequest(client scsDataClient) error {
Expand All @@ -58,16 +60,17 @@ func (r *ListConcatenateBackRequest) initGrpcRequest(client scsDataClient) error
return err
}

var ttl uint64
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
var ttlMilliseconds uint64
var refreshTtl bool
if ttlMilliseconds, refreshTtl, err = prepareCollectionTtl(r, client.defaultTtl); err != nil {
return err
}

r.grpcRequest = &pb.XListConcatenateBackRequest{
ListName: []byte(r.ListName),
Values: values,
TtlMilliseconds: ttl,
RefreshTtl: r.Ttl.RefreshTtl,
TtlMilliseconds: ttlMilliseconds,
RefreshTtl: refreshTtl,
TruncateFrontToSize: r.TruncateFrontToSize,
}

Expand Down
13 changes: 8 additions & 5 deletions momento/list_concatenate_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ListConcatenateFrontRequest struct {
ListName string
Values []Value
TruncateBackToSize uint32
Ttl utils.CollectionTtl
Ttl *utils.CollectionTtl

grpcRequest *pb.XListConcatenateFrontRequest
grpcResponse *pb.XListConcatenateFrontResponse
Expand All @@ -44,6 +44,8 @@ func (r *ListConcatenateFrontRequest) values() []Value { return r.Values }

func (r *ListConcatenateFrontRequest) ttl() time.Duration { return r.Ttl.Ttl }

func (r *ListConcatenateFrontRequest) collectionTtl() *utils.CollectionTtl { return r.Ttl }

func (r *ListConcatenateFrontRequest) requestName() string { return "ListConcatenateFront" }

func (r *ListConcatenateFrontRequest) initGrpcRequest(client scsDataClient) error {
Expand All @@ -58,16 +60,17 @@ func (r *ListConcatenateFrontRequest) initGrpcRequest(client scsDataClient) erro
return err
}

var ttl uint64
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
var ttlMilliseconds uint64
var refreshTtl bool
if ttlMilliseconds, refreshTtl, err = prepareCollectionTtl(r, client.defaultTtl); err != nil {
return err
}

r.grpcRequest = &pb.XListConcatenateFrontRequest{
ListName: []byte(r.ListName),
Values: values,
TtlMilliseconds: ttl,
RefreshTtl: r.Ttl.RefreshTtl,
TtlMilliseconds: ttlMilliseconds,
RefreshTtl: refreshTtl,
TruncateBackToSize: r.TruncateBackToSize,
}

Expand Down
13 changes: 8 additions & 5 deletions momento/list_push_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ListPushBackRequest struct {
ListName string
Value Value
TruncateFrontToSize uint32
Ttl utils.CollectionTtl
Ttl *utils.CollectionTtl

grpcRequest *pb.XListPushBackRequest
grpcResponse *pb.XListPushBackResponse
Expand All @@ -45,6 +45,8 @@ func (r *ListPushBackRequest) value() Value { return r.Value }

func (r *ListPushBackRequest) ttl() time.Duration { return r.Ttl.Ttl }

func (r *ListPushBackRequest) collectionTtl() *utils.CollectionTtl { return r.Ttl }

func (r *ListPushBackRequest) requestName() string { return "ListPushBack" }

func (r *ListPushBackRequest) initGrpcRequest(client scsDataClient) error {
Expand All @@ -59,16 +61,17 @@ func (r *ListPushBackRequest) initGrpcRequest(client scsDataClient) error {
return err
}

var ttl uint64
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
var ttlMilliseconds uint64
var refreshTtl bool
if ttlMilliseconds, refreshTtl, err = prepareCollectionTtl(r, client.defaultTtl); err != nil {
return err
}

r.grpcRequest = &pb.XListPushBackRequest{
ListName: []byte(r.ListName),
Value: value,
TtlMilliseconds: ttl,
RefreshTtl: r.Ttl.RefreshTtl,
TtlMilliseconds: ttlMilliseconds,
RefreshTtl: refreshTtl,
TruncateFrontToSize: r.TruncateFrontToSize,
}

Expand Down
13 changes: 8 additions & 5 deletions momento/list_push_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ListPushFrontRequest struct {
ListName string
Value Value
TruncateBackToSize uint32
Ttl utils.CollectionTtl
Ttl *utils.CollectionTtl

grpcRequest *pb.XListPushFrontRequest
grpcResponse *pb.XListPushFrontResponse
Expand All @@ -45,6 +45,8 @@ func (r *ListPushFrontRequest) value() Value { return r.Value }

func (r *ListPushFrontRequest) ttl() time.Duration { return r.Ttl.Ttl }

func (r *ListPushFrontRequest) collectionTtl() *utils.CollectionTtl { return r.Ttl }

func (r *ListPushFrontRequest) requestName() string { return "ListPushFront" }

func (r *ListPushFrontRequest) initGrpcRequest(client scsDataClient) error {
Expand All @@ -59,16 +61,17 @@ func (r *ListPushFrontRequest) initGrpcRequest(client scsDataClient) error {
return err
}

var ttl uint64
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
var ttlMilliseconds uint64
var refreshTtl bool
if ttlMilliseconds, refreshTtl, err = prepareCollectionTtl(r, client.defaultTtl); err != nil {
return err
}

r.grpcRequest = &pb.XListPushFrontRequest{
ListName: []byte(r.ListName),
Value: value,
TtlMilliseconds: ttl,
RefreshTtl: r.Ttl.RefreshTtl,
TtlMilliseconds: ttlMilliseconds,
RefreshTtl: refreshTtl,
TruncateBackToSize: r.TruncateBackToSize,
}

Expand Down
19 changes: 19 additions & 0 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

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

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

Expand Down Expand Up @@ -70,6 +72,10 @@ type hasTtl interface {
ttl() time.Duration
}

type hasCollectionTtl interface {
collectionTtl() *utils.CollectionTtl
}

func buildError(errorCode string, errorMessage string, originalError error) MomentoError {
return convertMomentoSvcErrorToCustomerError(
momentoerrors.NewMomentoSvcErr(errorCode, errorMessage, originalError),
Expand Down Expand Up @@ -181,6 +187,19 @@ func prepareElements(r hasElements) (map[string][]byte, error) {
return retMap, nil
}

func prepareCollectionTtl(r hasCollectionTtl, defaultTtl time.Duration) (uint64, bool, error) {
if r.collectionTtl() == nil {
return uint64(defaultTtl.Milliseconds()), true, nil
} else if r.collectionTtl().Ttl == time.Duration(0) {
return uint64(defaultTtl.Milliseconds()), r.collectionTtl().RefreshTtl, nil
} else if r.collectionTtl().Ttl <= time.Duration(0) {
return 0, false, buildError(
momentoerrors.InvalidArgumentError, "ttl must be a non-zero positive value", nil,
)
}
return uint64(r.collectionTtl().Ttl.Milliseconds()), r.collectionTtl().RefreshTtl, nil
}

func prepareTtl(r hasTtl, defaultTtl time.Duration) (uint64, error) {
ttl := r.ttl()
if r.ttl() == time.Duration(0) {
Expand Down
Loading

0 comments on commit 6e92b7f

Please sign in to comment.