Skip to content

Commit

Permalink
fix for http
Browse files Browse the repository at this point in the history
  • Loading branch information
AshwinKul28 committed Oct 31, 2024
1 parent c24abe1 commit a01d8c1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
14 changes: 13 additions & 1 deletion internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,24 @@ var (
Info: "PERSIST removes the expiration from a key",
Eval: evalPersist,
}

//TODO: supports only http protocol, needs to be removed once http is migrated to multishard
copyCmdMeta = DiceCmdMeta{
Name: "COPY",
Name: "COPY",
Info: `COPY command copies the value stored at the source key to the destination key.`,
Eval: evalCOPY,
Arity: -2,
}

//TODO: supports only http protocol, needs to be removed once http is migrated to multishard
objectCopyCmdMeta = DiceCmdMeta{
Name: "OBJECTCOPY",
Info: `COPY command copies the value stored at the source key to the destination key.`,
StoreObjectEval: evalCOPYObject,
IsMigrated: true,
Arity: -2,
}

decrCmdMeta = DiceCmdMeta{
Name: "DECR",
Info: `DECR decrements the value of the specified key in args by 1,
Expand Down Expand Up @@ -1332,6 +1343,7 @@ func init() {
DiceCmds["COMMAND|DOCS"] = commandDocsCmdMeta
DiceCmds["COMMAND|GETKEYSANDFLAGS"] = commandGetKeysAndFlagsCmdMeta
DiceCmds["COPY"] = copyCmdMeta
DiceCmds["OBJECTCOPY"] = objectCopyCmdMeta
DiceCmds["DBSIZE"] = dbSizeCmdMeta
DiceCmds["DECR"] = decrCmdMeta
DiceCmds["DECRBY"] = decrByCmdMeta
Expand Down
49 changes: 49 additions & 0 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,55 @@ func evalPersist(args []string, store *dstore.Store) []byte {
return clientio.RespOne
}

func evalCOPY(args []string, store *dstore.Store) []byte {
if len(args) < 2 {
return diceerrors.NewErrArity("COPY")
}

isReplace := false

sourceKey := args[0]
destinationKey := args[1]
sourceObj := store.Get(sourceKey)
if sourceObj == nil {
return clientio.RespZero
}

for i := 2; i < len(args); i++ {
arg := strings.ToUpper(args[i])
if arg == "REPLACE" {

Check failure on line 2160 in internal/eval/eval.go

View workflow job for this annotation

GitHub Actions / lint

string `REPLACE` has 2 occurrences, make it a constant (goconst)
isReplace = true
}
}

if isReplace {
store.Del(destinationKey)
}

destinationObj := store.Get(destinationKey)
if destinationObj != nil {
return clientio.RespZero
}

copyObj := sourceObj.DeepCopy()
if copyObj == nil {
return clientio.RespZero
}

exp, ok := dstore.GetExpiry(sourceObj, store)
var exDurationMs int64 = -1
if ok {
exDurationMs = int64(exp - uint64(utils.GetCurrentTime().UnixMilli()))
}

store.Put(destinationKey, copyObj)

if exDurationMs > 0 {
store.SetExpiry(copyObj, exDurationMs)
}
return clientio.RespOne
}

// GETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds |
// PXAT unix-time-milliseconds | PERSIST]
// Get the value of key and optionally set its expiration.
Expand Down
2 changes: 1 addition & 1 deletion internal/worker/cmd_decompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func decomposeCopy(ctx context.Context, w *BaseWorker, cd *cmd.DiceDBCmd) ([]*cm

decomposedCmds := []*cmd.DiceDBCmd{
{
Cmd: "COPY",
Cmd: "OBJECTCOPY",
Args: cd.Args[1:],
Obj: newObj,
},
Expand Down

0 comments on commit a01d8c1

Please sign in to comment.