Skip to content

Commit

Permalink
feat: add ttl validator for some interfaces (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
levy5307 authored Aug 18, 2021
1 parent 0a0602e commit 40afb54
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pegasus/op/check_and_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package op

import (
"context"
"time"

"github.com/XiaoMi/pegasus-go-client/idl/base"
"github.com/XiaoMi/pegasus-go-client/idl/rrdb"
Expand All @@ -45,6 +46,11 @@ func (r *CheckAndSet) Validate() error {
if err := validateHashKey(r.Req.HashKey.Data); err != nil {
return err
}
if err := validateTTL(time.Second * time.Duration(r.Req.SetExpireTsSeconds)); err != nil {
return err
}

r.Req.SetExpireTsSeconds = expireTsSeconds(time.Second * time.Duration(r.Req.SetExpireTsSeconds))
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions pegasus/op/multiset.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (r *MultiSet) Validate() error {
if err := validateValues(r.Values); err != nil {
return err
}
if err := validateTTL(r.TTL); err != nil {
return err
}
if len(r.SortKeys) != len(r.Values) {
return fmt.Errorf("InvalidParameter: unmatched key-value pairs: len(sortKeys)=%d len(values)=%d",
len(r.SortKeys), len(r.Values))
Expand Down
3 changes: 3 additions & 0 deletions pegasus/op/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (r *Set) Validate() error {
if err := validateValue(r.Value); err != nil {
return err
}
if err := validateTTL(r.TTL); err != nil {
return err
}

key := encodeHashKeySortKey(r.HashKey, r.SortKey)
val := &base.Blob{Data: r.Value}
Expand Down
7 changes: 7 additions & 0 deletions pegasus/op/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func validateSortKeys(sortKeys [][]byte) error {
return nil
}

func validateTTL(TTL time.Duration) error {
if TTL < 0 {
return fmt.Errorf("InvalidParameter: TTL[%d] must be greater than 0", TTL)
}
return nil
}

func encodeHashKeySortKey(hashKey []byte, sortKey []byte) *base.Blob {
hashKeyLen := len(hashKey)
sortKeyLen := len(sortKey)
Expand Down
5 changes: 1 addition & 4 deletions pegasus/table_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,7 @@ func (p *pegasusTableConnector) CheckAndSet(ctx context.Context, hashKey []byte,
request.CheckOperand = &base.Blob{Data: checkOperand}
request.CheckSortKey = &base.Blob{Data: checkSortKey}
request.HashKey = &base.Blob{Data: hashKey}
request.SetExpireTsSeconds = 0
if options.SetValueTTLSeconds != 0 {
request.SetExpireTsSeconds = expireTsSeconds(time.Second * time.Duration(options.SetValueTTLSeconds))
}
request.SetExpireTsSeconds = int32(options.SetValueTTLSeconds)
request.SetSortKey = &base.Blob{Data: setSortKey}
request.SetValue = &base.Blob{Data: setValue}
request.ReturnCheckValue = options.ReturnCheckValue
Expand Down
12 changes: 12 additions & 0 deletions pegasus/table_connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func testSingleKeyOperations(t *testing.T, tb TableConnector, hashKey []byte, so
// from the value we set.
return ttl <= 11 && ttl >= 9
})
// test with invalid ttl
assert.Error(t, tb.SetTTL(context.Background(), hashKey, sortKey, value, -10))

assert.Nil(t, tb.Del(context.Background(), hashKey, sortKey))
}
Expand Down Expand Up @@ -471,6 +473,9 @@ func testMultiKeyOperations(t *testing.T, tb TableConnector) {
return ttl <= 11 && ttl >= 9
})
}

// test with invalid ttl
assert.Error(t, tb.MultiSetOpt(context.Background(), hashKey, sortKeys, values, -1*time.Second))
}

func TestPegasusTableConnector_CheckAndSet(t *testing.T) {
Expand Down Expand Up @@ -576,6 +581,13 @@ func TestPegasusTableConnector_CheckAndSet(t *testing.T) {
assert.Equal(t, value, []byte("v2"))
}

// test with invalid ttl
{
_, err := tb.CheckAndSet(context.Background(), []byte("h1"), []byte("s1"), CheckTypeValueExist, []byte(""), []byte("s2"), []byte("v2"),
&CheckAndSetOptions{SetValueTTLSeconds: -1})
assert.Error(t, err)
}

// TODO(wutao1): add tests for other check type
}

Expand Down
9 changes: 0 additions & 9 deletions pegasus/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package pegasus
import (
"encoding/binary"
"hash/crc64"
"time"

"github.com/XiaoMi/pegasus-go-client/idl/base"
)
Expand Down Expand Up @@ -52,11 +51,3 @@ var crc64Table = crc64.MakeTable(0x9a6c9329ac4bc9b5)
func crc64Hash(data []byte) uint64 {
return crc64.Checksum(data, crc64Table)
}

func expireTsSeconds(ttl time.Duration) int32 {
if ttl == 0 {
return 0
}
// 1451606400 means seconds since 2016.01.01-00:00:00 GMT
return int32(ttl.Seconds()) + int32(time.Now().Unix()-1451606400)
}

0 comments on commit 40afb54

Please sign in to comment.