Skip to content

Commit

Permalink
Merge pull request #1443 from DiceDB/arpit-readiness-cli-chat
Browse files Browse the repository at this point in the history
DECR functionally working
  • Loading branch information
arpitbbhayani authored Feb 3, 2025
2 parents 422489c + c33f288 commit a84565d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
39 changes: 23 additions & 16 deletions internal/cmd/cmd_decr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"math"

"github.com/dicedb/dice/internal/object"
dstore "github.com/dicedb/dice/internal/store"
"github.com/dicedb/dice/wire"
Expand All @@ -18,39 +16,48 @@ func init() {
commandRegistry.AddCommand(cDECR)
}

// evalDECR decrements an integer value stored at the specified key by 1.
//
// The function expects exactly one argument: the key to decrement.
// If the key does not exist, it is initialized with value -1.
// If the key exists but does not contain an integer, an error is returned.
//
// Parameters:
// - c *Cmd: The command context containing the arguments
// - s *dstore.Store: The data store instance
//
// Returns:
// - *CmdRes: Response containing the new integer value after decrement
// - error: Error if wrong number of arguments or wrong value type
func evalDECR(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) <= 1 {
if len(c.C.Args) != 1 {
return cmdResNil, errWrongArgumentCount("DECR")
}

incr := int64(-1)
delta := int64(-1)

key := c.C.Args[0]
obj := s.Get(key)
if obj == nil {
obj = s.NewObj(incr, INFINITE_EXPIRATION, object.ObjTypeInt)
obj = s.NewObj(delta, INFINITE_EXPIRATION, object.ObjTypeInt)
s.Put(key, obj)
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: incr},
Value: &wire.Response_VInt{VInt: delta},
}}, nil
}

switch obj.Type {
case object.ObjTypeString:
return cmdResNil, errIntegerOutOfRange
case object.ObjTypeInt:
break
default:
return cmdResNil, errWrongTypeOperation("DECR")
}

i, _ := obj.Value.(int64)
if (incr < 0 && i < 0 && incr < (math.MinInt64-i)) ||
(incr > 0 && i > 0 && incr > (math.MaxInt64-i)) {
return cmdResNil, errIntegerOutOfRange
}
val, _ := obj.Value.(int64)
val += delta

i += incr
obj.Value = i
obj.Value = val
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: i},
Value: &wire.Response_VInt{VInt: val},
}}, nil
}
3 changes: 1 addition & 2 deletions internal/cmd/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/dicedb/dice/wire"
)

//nolint: stylecheck
const INFINITE_EXPIRATION = int64(-1)

type Cmd struct {
Expand Down Expand Up @@ -150,7 +151,6 @@ func errWrongArgumentCount(command string) error {
}

var errUnknownObjectType = errors.New("unknown object type")
var errIntegerOutOfRange = errors.New("integer out of range")

//nolint:unparam
func errInvalidSyntax(command string) error {
Expand All @@ -162,7 +162,6 @@ func errInvalidValue(command, param string) error {
return fmt.Errorf("invalid value for a parameter in '%s' command for %s parameter", strings.ToUpper(command), strings.ToUpper(param))
}

//nolint:unparam
func errWrongTypeOperation(command string) error {
return fmt.Errorf("wrong type operation for '%s' command", strings.ToUpper(command))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ func init() {
DiceCmds["COMMAND|DOCS"] = commandDocsCmdMeta
DiceCmds["COMMAND|GETKEYSANDFLAGS"] = commandGetKeysAndFlagsCmdMeta
DiceCmds["OBJECTCOPY"] = objectCopyCmdMeta
DiceCmds["DECR"] = decrCmdMeta
DiceCmds["DECR"] = decrCmdMeta // moved to ironhawk
DiceCmds["DECRBY"] = decrByCmdMeta
DiceCmds["DEL"] = delCmdMeta
DiceCmds["DUMP"] = dumpkeyCMmdMeta
Expand Down

0 comments on commit a84565d

Please sign in to comment.