Skip to content

Commit

Permalink
feat: Change the entry form(#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
qishenonly committed Jul 6, 2023
1 parent a5bd0d5 commit a751a63
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 83 deletions.
93 changes: 70 additions & 23 deletions structure/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,49 @@ func NewStringStructure(options config.Options) (*StringStructure, error) {
return &StringStructure{db: db}, nil
}

// encodeStringValue encodes a string value
func StringToBytesWithKeyAndValue(key, value string) ([]byte, []byte) {
return []byte(key), []byte(value)
}

// stringToBytesWithKey converts a string to a byte slice
func stringToBytesWithKey(key string) []byte {
return []byte(key)
}

// interfaceToBytes converts an interface to a byte slice
func interfaceToBytes(value interface{}) ([]byte, error) {
switch value := value.(type) {
case string:
return []byte(value), nil
case int:
return []byte(strconv.Itoa(value)), nil
case int64:
return []byte(strconv.FormatInt(value, 10)), nil
case float64:
return []byte(strconv.FormatFloat(value, 'f', -1, 64)), nil
case bool:
return []byte(strconv.FormatBool(value)), nil
case []byte:
return value, nil
default:
return nil, errors.New("unsupported type")
}
}

// Set sets the value of a key
// If the key does not exist, it will be created
// If the key exists, it will be overwritten
// If the key is expired, it will be deleted
// If the key is not expired, it will be updated
func (s *StringStructure) Set(key, value []byte, ttl time.Duration) error {
// func (s *StringStructure) Set(key, value []byte, ttl time.Duration) error {
func (s *StringStructure) Set(k string, v interface{}, ttl time.Duration) error {
key := stringToBytesWithKey(k)
value, err := interfaceToBytes(v)
if err != nil {
return err
}

if value == nil {
return nil
}
Expand All @@ -57,7 +94,9 @@ func (s *StringStructure) Set(key, value []byte, ttl time.Duration) error {
// If the key exists, it will return the value
// If the key is expired, it will be deleted and return nil
// If the key is not expired, it will be updated and return the value
func (s *StringStructure) Get(key []byte) ([]byte, error) {
func (s *StringStructure) Get(k string) ([]byte, error) {
key := stringToBytesWithKey(k)

// Get the value
value, err := s.db.Get(key)
if err != nil {
Expand All @@ -73,7 +112,8 @@ func (s *StringStructure) Get(key []byte) ([]byte, error) {
// If the key exists, it will be deleted
// If the key is expired, it will be deleted and return nil
// If the key is not expired, it will be updated and return nil
func (s *StringStructure) Del(key []byte) error {
func (s *StringStructure) Del(k string) error {
key := stringToBytesWithKey(k)
// Delete the value
return s.db.Delete(key)
}
Expand All @@ -83,7 +123,8 @@ func (s *StringStructure) Del(key []byte) error {
// If the key exists, it will return "string"
// If the key is expired, it will be deleted and return ""
// If the key is not expired, it will be updated and return "string"
func (s *StringStructure) Type(key []byte) (string, error) {
func (s *StringStructure) Type(k string) (string, error) {
key := stringToBytesWithKey(k)
// Get the value
value, err := s.db.Get(key)
if err != nil {
Expand All @@ -105,7 +146,8 @@ func (s *StringStructure) Type(key []byte) (string, error) {
// If the key exists, it will return the length of the value
// If the key is expired, it will be deleted and return 0
// If the key is not expired, it will be updated and return the length of the value
func (s *StringStructure) StrLen(key []byte) (int, error) {
func (s *StringStructure) StrLen(k string) (int, error) {
key := stringToBytesWithKey(k)
// Get the value
value, err := s.db.Get(key)
if err != nil {
Expand All @@ -123,7 +165,7 @@ func (s *StringStructure) StrLen(key []byte) (int, error) {
}

// GetSet sets the value of a key and returns its old value
func (s *StringStructure) GetSet(key, value []byte, ttl time.Duration) ([]byte, error) {
func (s *StringStructure) GetSet(key string, value interface{}, ttl time.Duration) ([]byte, error) {
// Get the old value
oldValue, err := s.Get(key)
if err != nil {
Expand All @@ -141,22 +183,27 @@ func (s *StringStructure) GetSet(key, value []byte, ttl time.Duration) ([]byte,
}

// Append appends a value to the value of a key
func (s *StringStructure) Append(key, value []byte, ttl time.Duration) error {
func (s *StringStructure) Append(key string, v interface{}, ttl time.Duration) error {
// Get the old value
oldValue, err := s.Get(key)
if err != nil {
return err
}

value, err := interfaceToBytes(v)
if err != nil {
return err
}

// Append the value
newValue := append(oldValue, value...)

// Set the value
return s.Set(key, newValue, ttl)
return s.Set(key, string(newValue), ttl)
}

// Incr increments the integer value of a key by 1
func (s *StringStructure) Incr(key []byte, ttl time.Duration) error {
func (s *StringStructure) Incr(key string, ttl time.Duration) error {
// Get the old value
oldValue, err := s.Get(key)
if err != nil {
Expand All @@ -176,11 +223,11 @@ func (s *StringStructure) Incr(key []byte, ttl time.Duration) error {
newValue := []byte(strconv.Itoa(newIntValue))

// Set the value
return s.Set(key, newValue, ttl)
return s.Set(key, string(newValue), ttl)
}

// IncrBy increments the integer value of a key by the given amount
func (s *StringStructure) IncrBy(key []byte, amount int, ttl time.Duration) error {
func (s *StringStructure) IncrBy(key string, amount int, ttl time.Duration) error {
// Get the old value
oldValue, err := s.Get(key)
if err != nil {
Expand All @@ -200,11 +247,11 @@ func (s *StringStructure) IncrBy(key []byte, amount int, ttl time.Duration) erro
newValue := []byte(strconv.Itoa(newIntValue))

// Set the value
return s.Set(key, newValue, ttl)
return s.Set(key, string(newValue), ttl)
}

// IncrByFloat increments the float value of a key by the given amount
func (s *StringStructure) IncrByFloat(key []byte, amount float64, ttl time.Duration) error {
func (s *StringStructure) IncrByFloat(key string, amount float64, ttl time.Duration) error {
// Get the old value
oldValue, err := s.Get(key)
if err != nil {
Expand All @@ -224,11 +271,11 @@ func (s *StringStructure) IncrByFloat(key []byte, amount float64, ttl time.Durat
newValue := []byte(strconv.FormatFloat(newFloatValue, 'f', -1, 64))

// Set the value
return s.Set(key, newValue, ttl)
return s.Set(key, string(newValue), ttl)
}

// Decr decrements the integer value of a key by 1
func (s *StringStructure) Decr(key []byte, ttl time.Duration) error {
func (s *StringStructure) Decr(key string, ttl time.Duration) error {
// Get the old value
oldValue, err := s.Get(key)
if err != nil {
Expand All @@ -248,11 +295,11 @@ func (s *StringStructure) Decr(key []byte, ttl time.Duration) error {
newValue := []byte(strconv.Itoa(newIntValue))

// Set the value
return s.Set(key, newValue, ttl)
return s.Set(key, string(newValue), ttl)
}

// DecrBy decrements the integer value of a key by the given amount
func (s *StringStructure) DecrBy(key []byte, amount int, ttl time.Duration) error {
func (s *StringStructure) DecrBy(key string, amount int, ttl time.Duration) error {
// Get the old value
oldValue, err := s.Get(key)
if err != nil {
Expand All @@ -272,11 +319,11 @@ func (s *StringStructure) DecrBy(key []byte, amount int, ttl time.Duration) erro
newValue := []byte(strconv.Itoa(newIntValue))

// Set the value
return s.Set(key, newValue, ttl)
return s.Set(key, string(newValue), ttl)
}

// Exists checks if a key exists
func (s *StringStructure) Exists(key []byte) (bool, error) {
func (s *StringStructure) Exists(key string) (bool, error) {
// Get the value
_, err := s.Get(key)
if err != nil {
Expand All @@ -290,27 +337,27 @@ func (s *StringStructure) Exists(key []byte) (bool, error) {
}

// Expire sets the expiration time of a key
func (s *StringStructure) Expire(key []byte, ttl time.Duration) error {
func (s *StringStructure) Expire(key string, ttl time.Duration) error {
// Get the value
value, err := s.Get(key)
if err != nil {
return err
}

// Set the value
return s.Set(key, value, ttl)
return s.Set(key, string(value), ttl)
}

// Persist removes the expiration time of a key
func (s *StringStructure) Persist(key []byte) error {
func (s *StringStructure) Persist(key string) error {
// Get the value
value, err := s.Get(key)
if err != nil {
return err
}

// Set the value
return s.Set(key, value, 0)
return s.Set(key, string(value), 0)
}

// encodeStringValue encodes the value
Expand Down
Loading

0 comments on commit a751a63

Please sign in to comment.