mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-06 08:27:04 +08:00
feat: added DECR command support
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -983,3 +983,67 @@ func TestEchoVault_INCR(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEchoVault_DECR(t *testing.T) {
|
||||||
|
server := createEchoVault()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
key string
|
||||||
|
presetValues map[string]internal.KeyData
|
||||||
|
want int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "1. Decrement non-existent key",
|
||||||
|
key: "DecrKey1",
|
||||||
|
presetValues: nil,
|
||||||
|
want: 0,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "2. Decrement existing key with integer value",
|
||||||
|
key: "DecrKey2",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"DecrKey2": {Value: "5"},
|
||||||
|
},
|
||||||
|
want: 4,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "3. Decrement existing key with non-integer value",
|
||||||
|
key: "DecrKey3",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"DecrKey3": {Value: "not_an_int"},
|
||||||
|
},
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "4. Decrement existing key with int64 value",
|
||||||
|
key: "DecrKey4",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"DecrKey4": {Value: int64(10)},
|
||||||
|
},
|
||||||
|
want: 9,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if tt.presetValues != nil {
|
||||||
|
for k, d := range tt.presetValues {
|
||||||
|
presetKeyData(server, context.Background(), k, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
got, err := server.Incr(tt.key)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("TTL() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("TTL() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -430,6 +430,53 @@ func handleIncr(params internal.HandlerFuncParams) ([]byte, error) {
|
|||||||
return []byte(fmt.Sprintf(":%d\r\n", newValue)), nil
|
return []byte(fmt.Sprintf(":%d\r\n", newValue)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleDecr(params internal.HandlerFuncParams) ([]byte, error) {
|
||||||
|
// Extract key from command
|
||||||
|
keys, err := decrKeyFunc(params.Command)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
key := keys.WriteKeys[0]
|
||||||
|
values := params.GetValues(params.Context, []string{key}) // Get the current values for the specified keys
|
||||||
|
currentValue, ok := values[key] // Check if the key exists
|
||||||
|
|
||||||
|
var newValue int64
|
||||||
|
var currentValueInt int64
|
||||||
|
|
||||||
|
// Check if the key exists and its current value
|
||||||
|
if !ok || currentValue == nil {
|
||||||
|
// If key does not exist, initialize it with 0
|
||||||
|
newValue = -1
|
||||||
|
} else {
|
||||||
|
// Use type switch to handle different types of currentValue
|
||||||
|
switch v := currentValue.(type) {
|
||||||
|
case string:
|
||||||
|
var err error
|
||||||
|
currentValueInt, err = strconv.ParseInt(v, 10, 64) // Parse the string to int64
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("value is not an integer or out of range")
|
||||||
|
}
|
||||||
|
case int:
|
||||||
|
currentValueInt = int64(v) // Convert int to int64
|
||||||
|
case int64:
|
||||||
|
currentValueInt = v // Use int64 value directly
|
||||||
|
default:
|
||||||
|
fmt.Printf("unexpected type for currentValue: %T\n", currentValue)
|
||||||
|
return nil, errors.New("unexpected type for currentValue") // Handle unexpected types
|
||||||
|
}
|
||||||
|
newValue = currentValueInt - 1 // Decrement the value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the new incremented value
|
||||||
|
if err := params.SetValues(params.Context, map[string]interface{}{key: fmt.Sprintf("%d", newValue)}); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare response with the actual new value
|
||||||
|
return []byte(fmt.Sprintf(":%d\r\n", newValue)), nil
|
||||||
|
}
|
||||||
|
|
||||||
func Commands() []internal.Command {
|
func Commands() []internal.Command {
|
||||||
return []internal.Command{
|
return []internal.Command{
|
||||||
{
|
{
|
||||||
@@ -607,5 +654,14 @@ LT - Only set the expiry time if the new expiry time is less than the current on
|
|||||||
KeyExtractionFunc: incrKeyFunc,
|
KeyExtractionFunc: incrKeyFunc,
|
||||||
HandlerFunc: handleIncr,
|
HandlerFunc: handleIncr,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Command: "decr",
|
||||||
|
Module: constants.GenericModule,
|
||||||
|
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
|
||||||
|
Description: `(DECR key) Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that cannot be represented as integer. This operation is limited to 64 bit signed integers.`,
|
||||||
|
Sync: true,
|
||||||
|
KeyExtractionFunc: decrKeyFunc,
|
||||||
|
HandlerFunc: handleDecr,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2016,4 +2016,125 @@ func Test_Generic(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("Test_HandlerDECR", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
conn, err := internal.GetConnection("localhost", port)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = conn.Close()
|
||||||
|
}()
|
||||||
|
client := resp.NewConn(conn)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
key string
|
||||||
|
presetValue interface{}
|
||||||
|
command []resp.Value
|
||||||
|
expectedResponse int64
|
||||||
|
expectedError error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "1. Increment non-existent key",
|
||||||
|
key: "DecrKey1",
|
||||||
|
presetValue: nil,
|
||||||
|
command: []resp.Value{resp.StringValue("DECR"), resp.StringValue("DecrKey1")},
|
||||||
|
expectedResponse: -1,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "2. Decrement existing key with integer value",
|
||||||
|
key: "DecrKey2",
|
||||||
|
presetValue: "5",
|
||||||
|
command: []resp.Value{resp.StringValue("DECR"), resp.StringValue("DecrKey2")},
|
||||||
|
expectedResponse: 4,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "3. Decrement existing key with non-integer value",
|
||||||
|
key: "DecrKey3",
|
||||||
|
presetValue: "not_an_int",
|
||||||
|
command: []resp.Value{resp.StringValue("DECR"), resp.StringValue("DecrKey3")},
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New("value is not an integer or out of range"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "4. Decrement existing key with int64 value",
|
||||||
|
key: "DecrKey4",
|
||||||
|
presetValue: int64(10),
|
||||||
|
command: []resp.Value{resp.StringValue("DECR"), resp.StringValue("DecrKey4")},
|
||||||
|
expectedResponse: 11,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "5. Command too short",
|
||||||
|
key: "DencrKey5",
|
||||||
|
presetValue: nil,
|
||||||
|
command: []resp.Value{resp.StringValue("DECR")},
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(constants.WrongArgsResponse),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "6. Command too long",
|
||||||
|
key: "DecrKey6",
|
||||||
|
presetValue: nil,
|
||||||
|
command: []resp.Value{
|
||||||
|
resp.StringValue("DECR"),
|
||||||
|
resp.StringValue("DecrKey6"),
|
||||||
|
resp.StringValue("DecrKey6"),
|
||||||
|
},
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(constants.WrongArgsResponse),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
if test.presetValue != nil {
|
||||||
|
command := []resp.Value{resp.StringValue("SET"), resp.StringValue(test.key), resp.StringValue(fmt.Sprintf("%v", test.presetValue))}
|
||||||
|
if err = client.WriteArray(command); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
res, _, err := client.ReadValue()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if !strings.EqualFold(res.String(), "ok") {
|
||||||
|
t.Errorf("expected preset response to be OK, got %s", res.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = client.WriteArray(test.command); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, _, err := client.ReadValue()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if test.expectedError != nil {
|
||||||
|
if !strings.Contains(res.Error().Error(), test.expectedError.Error()) {
|
||||||
|
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
responseInt, err := strconv.ParseInt(res.String(), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error parsing response to int64: %s", err)
|
||||||
|
}
|
||||||
|
if responseInt != test.expectedResponse {
|
||||||
|
t.Errorf("expected response %d, got %d", test.expectedResponse, responseInt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@@ -145,3 +145,12 @@ func incrKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
|
|||||||
WriteKeys: cmd[1:2],
|
WriteKeys: cmd[1:2],
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func decrKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
|
||||||
|
if len(cmd) != 2 {
|
||||||
|
return internal.KeyExtractionFuncResult{}, errors.New("wrong number of arguments for INCR")
|
||||||
|
}
|
||||||
|
return internal.KeyExtractionFuncResult{
|
||||||
|
WriteKeys: cmd[1:2],
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user