Skip to content

Commit

Permalink
added get in set
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvyadav1111 committed Nov 6, 2024
1 parent 3866213 commit d2b11b7
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 200 deletions.
30 changes: 29 additions & 1 deletion docs/src/content/docs/commands/SET.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The `SET` command in DiceDB is used to set the value of a key. If the key alread
## Syntax

```bash
SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL] [NX | XX]
SET key value [NX | XX] [GET] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
```

## Parameters
Expand All @@ -24,6 +24,7 @@ SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix
| `NX` | Only set the key if it does not already exist. | None | No |
| `XX` | Only set the key if it already exists. | None | No |
| `KEEPTTL` | Retain the time-to-live associated with the key. | None | No |
| `GET` | Return the value of the key before setting it. | None | No |

## Return values

Expand All @@ -32,6 +33,8 @@ SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix
| Command is successful | `OK` |
| `NX` or `XX` conditions are not met | `nil` |
| Syntax or specified constraints are invalid | error |
| If the `GET` option is provided | The value of the key before setting it or error if value cannot be returned as a string |


## Behaviour

Expand All @@ -41,6 +44,7 @@ SET key value [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix
- Using the `EX`, `EXAT`, `PX` or `PXAT` options together with `KEEPTTL` is not allowed and will result in an error.
- When provided, `EX` sets the expiry time in seconds and `PX` sets the expiry time in milliseconds.
- The `KEEPTTL` option ensures that the key's existing TTL is retained.
- The `GET` option can be used to return the value of the key before setting it. If the key does not exist, `nil` is returned. If the key exists but does not contain a value which can be returned as a string, an error is returned. The set operation is not performed in this case.

## Errors

Expand Down Expand Up @@ -131,3 +135,27 @@ Trying to set key `foo` with both `EX` and `KEEPTTL` will result in an error
127.0.0.1:7379> SET foo bar EX 10 KEEPTTL
(error) ERR syntax error
```

### Set with GET option

```bash
127.0.0.1:7379> set foo bar
OK
127.0.0.1:7379> set foo bazz get
"bar"
```
### Set with GET option when key does not exist

```bash
127.0.0.1:7379> set foo bazz get
(nil)
127.0.0.1:7379> get foo
(nil)
```

### Set with Get with wrong type of value
```bash
127.0.0.1:7379> sadd foo item1
(integer) 1
127.0.0.1:7379> set foo bazz get
(error) WRONGTYPE Operation against a key holding the wrong kind of value
37 changes: 30 additions & 7 deletions integration_tests/commands/http/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,29 @@ func TestSetWithOptions(t *testing.T) {
},
expected: []interface{}{nil, nil, "OK", nil, nil, nil},
},
{
name: "GET with Existing Value",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v"}},
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "vv", "get": true}},
},
expected: []interface{}{"OK", "v"},
},
{
name: "GET with Non-Existing Value",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "vv", "get": true}},
},
expected: []interface{}{nil},
},
{
name: "GET with wrong type of value",
commands: []HTTPCommand{
{Command: "SADD", Body: map[string]interface{}{"key": "k", "value": "b"}},
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v", "get": true}},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
},
}

for _, tc := range testCases {
Expand All @@ -207,7 +230,7 @@ func TestWithKeepTTLFlag(t *testing.T) {
exec := NewHTTPCommandExecutor()
expiryTime := strconv.FormatInt(time.Now().Add(1*time.Minute).UnixMilli(), 10)

testCases := []TestCase {
testCases := []TestCase{
{
name: "SET WITH KEEP TTL",
commands: []HTTPCommand{
Expand All @@ -228,39 +251,39 @@ func TestWithKeepTTLFlag(t *testing.T) {
},
{
name: "SET WITH KEEPTTL with PX",
commands: []HTTPCommand {
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v", "px": 2000, "keepttl": true}},
{Command: "GET", Body: map[string]interface{}{"key": "k"}},
},
expected: []interface{}{"ERR syntax error", nil},
},
{
name: "SET WITH KEEPTTL with EX",
commands: []HTTPCommand {
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v", "ex": 3, "keepttl": true}},
{Command: "GET", Body: map[string]interface{}{"key": "k"}},
},
expected: []interface{}{"ERR syntax error", nil},
},
{
name: "SET WITH KEEPTTL with NX",
commands: []HTTPCommand {
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v", "nx": true, "keepttl": true}},
{Command: "GET", Body: map[string]interface{}{"key": "k"}},
},
expected: []interface{}{"OK", "v"},
},
{
name: "SET WITH KEEPTTL with XX",
commands: []HTTPCommand {
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v", "xx": true, "keepttl": true}},
{Command: "GET", Body: map[string]interface{}{"key": "k"}},
},
expected: []interface{}{nil, nil},
},
{
name: "SET WITH KEEPTTL with PXAT",
commands: []HTTPCommand {
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v", "pxat": expiryTime, "keepttl": true}},
{Command: "GET", Body: map[string]interface{}{"key": "k"}},
},
Expand All @@ -269,7 +292,7 @@ func TestWithKeepTTLFlag(t *testing.T) {
{

name: "SET WITH KEEPTTL with EXAT",
commands: []HTTPCommand {
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k", "value": "v", "exat": expiryTime, "keepttl": true}},
{Command: "GET", Body: map[string]interface{}{"key": "k"}},
},
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/commands/resp/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestSet(t *testing.T) {
func TestSetWithOptions(t *testing.T) {
conn := getLocalConnection()
expiryTime := strconv.FormatInt(time.Now().Add(1*time.Minute).UnixMilli(), 10)
defer FireCommand(conn, "FLUSHDB")
defer conn.Close()

testCases := []TestCase{
Expand Down Expand Up @@ -120,6 +121,21 @@ func TestSetWithOptions(t *testing.T) {
commands: []string{"SET k v XX EX 1", "GET k", "SLEEP 2", "GET k", "SET k v XX EX 1", "GET k"},
expected: []interface{}{"(nil)", "(nil)", "OK", "(nil)", "(nil)", "(nil)"},
},
{
name: "GET with Existing Value",
commands: []string{"SET k v", "SET k vv GET"},
expected: []interface{}{"OK", "v"},
},
{
name: "GET with Non-Existing Value",
commands: []string{"SET k vv GET"},
expected: []interface{}{"(nil)"},
},
{
name: "GET with wrong type of value",
commands: []string{"sadd k v", "SET k vv GET"},
expected: []interface{}{int64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
},
}

for _, tc := range testCases {
Expand Down
15 changes: 15 additions & 0 deletions integration_tests/commands/websocket/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ func TestSetWithOptions(t *testing.T) {
commands: []string{"SET k v XX EX 1", "GET k", "SLEEP 2", "GET k", "SET k v XX EX 1", "GET k"},
expected: []interface{}{nil, nil, "OK", nil, nil, nil},
},
{
name: "GET with Existing Value",
commands: []string{"SET k v", "SET k vv GET"},
expected: []interface{}{"OK", "v"},
},
{
name: "GET with Non-Existing Value",
commands: []string{"SET k vv GET"},
expected: []interface{}{nil},
},
{
name: "GET with wrong type of value",
commands: []string{"sadd k v", "SET k vv GET"},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
},
}

for _, tc := range testCases {
Expand Down
Loading

0 comments on commit d2b11b7

Please sign in to comment.