From bba70e521559c63026f368c69cd7cb6e21b4d474 Mon Sep 17 00:00:00 2001 From: sguay Date: Fri, 21 Jun 2019 22:44:46 -0400 Subject: [PATCH] Add hkeys support (#49) * Add hexists support Add hexists support * add hkeys command --- .../operations/OperationFactory.java | 1 + .../fppt/jedismock/operations/RO_hkeys.java | 33 +++++++++++++++++++ .../comparisontests/SimpleOperationsTest.java | 19 +++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java diff --git a/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java b/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java index 4e7142bbf..64d17e3d4 100644 --- a/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java +++ b/src/main/java/com/github/fppt/jedismock/operations/OperationFactory.java @@ -67,6 +67,7 @@ public class OperationFactory { TRANSACTIONAL_OPERATIONS.put("hget", RO_hget::new); TRANSACTIONAL_OPERATIONS.put("hset", RO_hset::new); TRANSACTIONAL_OPERATIONS.put("hdel", RO_hdel::new); + TRANSACTIONAL_OPERATIONS.put("hkeys", RO_hkeys::new); TRANSACTIONAL_OPERATIONS.put("hgetall", RO_hgetall::new); TRANSACTIONAL_OPERATIONS.put("sinter", RO_sinter::new); TRANSACTIONAL_OPERATIONS.put("hmget", RO_hmget::new); diff --git a/src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java b/src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java new file mode 100644 index 000000000..b42574f8d --- /dev/null +++ b/src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java @@ -0,0 +1,33 @@ +package com.github.fppt.jedismock.operations; + +import com.github.fppt.jedismock.server.Response; +import com.github.fppt.jedismock.server.Slice; +import com.github.fppt.jedismock.storage.RedisBase; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +public class RO_hkeys extends AbstractRedisOperation { + public RO_hkeys(RedisBase base, List params) { + super(base, params); + } + + @Override + Slice response() { + Slice hash = params().get(0); + + Map fieldAndValueMap = base().getFieldsAndValues(hash); + int arraySize = fieldAndValueMap.size(); + Slice [] fkeys = new Slice[arraySize]; + + int currentIndex = 0; + for (Map.Entry entry: fieldAndValueMap.entrySet()){ + fkeys[currentIndex] = Response.bulkString(entry.getKey()); + currentIndex++; + } + + List values = Arrays.asList(fkeys); + return Response.array(values); + } +} diff --git a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java index f73054208..ed088b59f 100644 --- a/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java +++ b/src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java @@ -343,6 +343,25 @@ public void whenHGetAll_EnsureAllKeysAndValuesReturned(Jedis jedis) { assertEquals(0, result.size()); } + @Theory + public void whenHKeys_EnsureAllKeysReturned(Jedis jedis){ + jedis.hset(HASH, FIELD_1, VALUE_1); + jedis.hset(HASH, FIELD_2, VALUE_2); + + Set toCompare = new HashSet(); + toCompare.add(FIELD_1); + toCompare.add(FIELD_2); + + Set result = jedis.hkeys(HASH); + assertTrue(result.equals(toCompare)); + + toCompare.add(FIELD_3); + jedis.hset(HASH, FIELD_3, VALUE_3); + + result = jedis.hkeys(HASH); + assertTrue(result.equals(toCompare)); + } + @Theory public void whenUsingHsinter_EnsureSetIntersectionIsReturned(Jedis jedis) { String key1 = "my-set-key-1";