Skip to content

Commit 77fbbd5

Browse files
authored
implement msetnx (#299)
* implement msetnx * fix mget
1 parent ef1a950 commit 77fbbd5

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

src/main/java/com/github/fppt/jedismock/operations/strings/MGet.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.fppt.jedismock.operations.strings;
22

3+
import com.github.fppt.jedismock.datastructures.RMString;
34
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
45
import com.github.fppt.jedismock.operations.RedisCommand;
56
import com.github.fppt.jedismock.server.Response;
@@ -17,8 +18,10 @@ class MGet extends AbstractRedisOperation {
1718
}
1819

1920
protected Slice response() {
21+
RedisBase base = base();
2022
return Response.array(params().stream()
21-
.map(key-> base().getRMString(key) != null ? (base().getRMString(key).getAsSlice()) : null)
23+
.map(base::getValue)
24+
.map(s -> s instanceof RMString ? (s.getAsSlice()) : null)
2225
.map(Response::bulkString)
2326
.collect(toList()));
2427
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.fppt.jedismock.operations.strings;
2+
3+
import com.github.fppt.jedismock.datastructures.Slice;
4+
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
5+
import com.github.fppt.jedismock.operations.RedisCommand;
6+
import com.github.fppt.jedismock.server.Response;
7+
import com.github.fppt.jedismock.storage.RedisBase;
8+
9+
import java.util.List;
10+
11+
@RedisCommand("msetnx")
12+
public class MSetNX extends AbstractRedisOperation {
13+
public MSetNX(RedisBase base, List<Slice> params) {
14+
super(base, params);
15+
}
16+
17+
@Override
18+
protected Slice response() {
19+
RedisBase base = base();
20+
for (int i = 0; i < params().size(); i += 2) {
21+
if (base.exists(params().get(i))) {
22+
return Response.integer(0);
23+
}
24+
}
25+
for (int i = 0; i < params().size(); i += 2) {
26+
base.putValue(params().get(i), params().get(i + 1).extract());
27+
}
28+
return Response.integer(1);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.fppt.jedismock.comparisontests.strings;
2+
3+
import com.github.fppt.jedismock.comparisontests.ComparisonBase;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.TestTemplate;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import redis.clients.jedis.Jedis;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
@ExtendWith(ComparisonBase.class)
14+
public class TestMGet {
15+
@BeforeEach
16+
public void setUp(Jedis jedis) {
17+
jedis.flushAll();
18+
}
19+
20+
@TestTemplate
21+
public void mget(Jedis jedis) {
22+
jedis.set("key1", "Hello");
23+
jedis.set("key2", "World");
24+
assertEquals(Arrays.asList("Hello", "World", null), jedis.mget("key1", "key2", "key3"));
25+
}
26+
27+
28+
@TestTemplate
29+
public void mgetWithNonText(Jedis jedis) {
30+
jedis.set("key1", "Hello");
31+
jedis.hset("key2", "field", "World");
32+
assertEquals(Arrays.asList("Hello", null), jedis.mget("key1", "key2"));
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.fppt.jedismock.comparisontests.strings;
2+
3+
import com.github.fppt.jedismock.comparisontests.ComparisonBase;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.TestTemplate;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import redis.clients.jedis.Jedis;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
@ExtendWith(ComparisonBase.class)
14+
public class TestMSetNX {
15+
@BeforeEach
16+
public void setUp(Jedis jedis) {
17+
jedis.flushAll();
18+
}
19+
20+
@TestTemplate
21+
public void msetnx(Jedis jedis) {
22+
assertEquals(1, jedis.msetnx("key1", "Hello", "key2", "there"));
23+
assertEquals(0, jedis.msetnx("key2", "new", "key3", "world"));
24+
assertEquals(Arrays.asList("Hello", "there", null), jedis.mget("key1", "key2", "key3"));
25+
}
26+
}

0 commit comments

Comments
 (0)