Skip to content

Commit

Permalink
Add hkeys support (#49)
Browse files Browse the repository at this point in the history
* Add hexists support

Add hexists support

* add hkeys command
  • Loading branch information
sebastien-guay authored and fppt committed Jun 22, 2019
1 parent c61749d commit bba70e5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/github/fppt/jedismock/operations/RO_hkeys.java
Original file line number Diff line number Diff line change
@@ -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<Slice> params) {
super(base, params);
}

@Override
Slice response() {
Slice hash = params().get(0);

Map<Slice, Slice> fieldAndValueMap = base().getFieldsAndValues(hash);
int arraySize = fieldAndValueMap.size();
Slice [] fkeys = new Slice[arraySize];

int currentIndex = 0;
for (Map.Entry<Slice, Slice> entry: fieldAndValueMap.entrySet()){
fkeys[currentIndex] = Response.bulkString(entry.getKey());
currentIndex++;
}

List<Slice> values = Arrays.asList(fkeys);
return Response.array(values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> toCompare = new HashSet<String>();
toCompare.add(FIELD_1);
toCompare.add(FIELD_2);

Set<String> 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";
Expand Down

0 comments on commit bba70e5

Please sign in to comment.