Skip to content

Commit

Permalink
Encode map in encoded object (#3555)
Browse files Browse the repository at this point in the history
* Encode map in encoded object

* Address changes in test
  • Loading branch information
sazzad16 authored Sep 23, 2023
1 parent eb6893a commit e0e2319
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 48 deletions.
28 changes: 14 additions & 14 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -2688,27 +2688,27 @@ public final CommandObject<List<byte[]>> xreadGroup(byte[] groupName, byte[] con

// Scripting commands
public final CommandObject<Object> eval(String script) {
return new CommandObject<>(commandArguments(EVAL).add(script).add(0), BuilderFactory.ENCODED_OBJECT);
return new CommandObject<>(commandArguments(EVAL).add(script).add(0), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> eval(String script, String sampleKey) {
return new CommandObject<>(commandArguments(EVAL).add(script).add(0).processKey(sampleKey), BuilderFactory.ENCODED_OBJECT);
return new CommandObject<>(commandArguments(EVAL).add(script).add(0).processKey(sampleKey), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> eval(String script, int keyCount, String... params) {
return new CommandObject<>(commandArguments(EVAL).add(script).add(keyCount)
.addObjects((Object[]) params).processKeys(Arrays.copyOf(params, keyCount)),
BuilderFactory.ENCODED_OBJECT);
BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> eval(String script, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(EVAL).add(script).add(keys.size())
.keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> evalReadonly(String script, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(EVAL_RO).add(script).add(keys.size())
.keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> eval(byte[] script) {
Expand Down Expand Up @@ -2736,27 +2736,27 @@ public final CommandObject<Object> evalReadonly(byte[] script, List<byte[]> keys
}

public final CommandObject<Object> evalsha(String sha1) {
return new CommandObject<>(commandArguments(EVALSHA).add(sha1).add(0), BuilderFactory.ENCODED_OBJECT);
return new CommandObject<>(commandArguments(EVALSHA).add(sha1).add(0), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> evalsha(String sha1, String sampleKey) {
return new CommandObject<>(commandArguments(EVALSHA).add(sha1).add(0).processKey(sampleKey), BuilderFactory.ENCODED_OBJECT);
return new CommandObject<>(commandArguments(EVALSHA).add(sha1).add(0).processKey(sampleKey), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> evalsha(String sha1, int keyCount, String... params) {
return new CommandObject<>(commandArguments(EVALSHA).add(sha1).add(keyCount)
.addObjects((Object[]) params).processKeys(Arrays.copyOf(params, keyCount)),
BuilderFactory.ENCODED_OBJECT);
BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> evalsha(String sha1, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(EVALSHA).add(sha1).add(keys.size())
.keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> evalshaReadonly(String sha1, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(EVALSHA_RO).add(sha1).add(keys.size())
.keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> evalsha(byte[] sha1) {
Expand Down Expand Up @@ -2853,12 +2853,12 @@ public final CommandObject<String> slowlogReset() {

public final CommandObject<Object> fcall(String name, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(FCALL).add(name).add(keys.size())
.keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> fcallReadonly(String name, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(FCALL_RO).add(name).add(keys.size())
.keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<String> functionDelete(String libraryName) {
Expand Down Expand Up @@ -4206,12 +4206,12 @@ public final CommandObject<List<GearsLibraryInfo>> tFunctionList(TFunctionListPa

public final CommandObject<Object> tFunctionCall(String library, String function, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(GearsCommand.TFCALL).add(library + "." + function)
.add(keys.size()).keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.add(keys.size()).keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}

public final CommandObject<Object> tFunctionCallAsync(String library, String function, List<String> keys, List<String> args) {
return new CommandObject<>(commandArguments(GearsCommand.TFCALLASYNC).add(library + "." + function)
.add(keys.size()).keys(keys).addObjects(args), BuilderFactory.ENCODED_OBJECT);
.add(keys.size()).keys(keys).addObjects(args), BuilderFactory.AGGRESSIVE_ENCODED_OBJECT);
}
// RedisGears commands

Expand Down
52 changes: 18 additions & 34 deletions src/test/java/redis/clients/jedis/modules/gears/GearsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import redis.clients.jedis.gears.TFunctionLoadParams;
import redis.clients.jedis.modules.RedisModuleCommandsTestBase;
import redis.clients.jedis.gears.resps.GearsLibraryInfo;
import redis.clients.jedis.util.KeyValue;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -429,44 +428,21 @@ public void testLibraryCallHashResult() throws IOException {

Object result = client.tFunctionCall("hashitout", "hashy", Collections.singletonList("hash1"),
Collections.emptyList());
assertEquals(ArrayList.class, result.getClass());
List<Object> list = (List) result;
assertFalse(list.isEmpty());
boolean isResp3 = list.get(0) instanceof KeyValue;

assertEquals(isResp3 ? 7 : 14, list.size());
final Map<String, String> asMap;

if (protocol != RedisProtocol.RESP3) {
List<String> asList = (List) result;
int indexOfJava = asList.indexOf("Java");
assertTrue(indexOfJava >= 0);
assertEquals("James Gosling", asList.get(indexOfJava + 1));
int indexOfJavaScript = asList.indexOf("JavaScript");
assertTrue(indexOfJavaScript >= 0);
assertEquals("Brendan Eich", asList.get(indexOfJavaScript + 1));
int indexOfC = asList.indexOf("C");
assertTrue(indexOfC >= 0);
assertEquals("Dennis Ritchie", asList.get(indexOfC + 1));
int indexOfRuby = asList.indexOf("Ruby");
assertTrue(indexOfRuby >= 0);
assertEquals("Yukihiro Matsumoto", asList.get(indexOfRuby + 1));
int indexOfPython = asList.indexOf("Python");
assertTrue(indexOfPython >= 0);
assertEquals("Guido van Rossum", asList.get(indexOfPython + 1));
int indexOfCPP = asList.indexOf("C++");
assertTrue(indexOfCPP >= 0);
assertEquals("Bjarne Stroustrup", asList.get(indexOfCPP + 1));
int indexOfLastUpdated = asList.indexOf("__last_updated__");
assertTrue(indexOfLastUpdated >= 0);
assertTrue(Integer.parseInt(asList.get(indexOfLastUpdated + 1)) > 0);
final List<String> asList = (List) result;
asMap = flatMapToMap(asList);

} else {
for (KeyValue kv : (List<KeyValue<String, Object>>) result) {
if (!kv.getKey().toString().equalsIgnoreCase("__last_updated__")) {
assertTrue(payload.containsKey(kv.getKey()));
assertEquals(payload.get(kv.getKey()), kv.getValue());
}
}
asMap = (Map) result;
}

payload.forEach((language, author) -> {
assertThat(asMap, Matchers.hasEntry(language, author));
});
assertThat(Long.parseLong(asMap.get("__last_updated__")), Matchers.greaterThan(0L));
}

@Test
Expand Down Expand Up @@ -540,4 +516,12 @@ private static Map<String, List<Predicate<GearsLibraryInfo>>> initializeTestLibr

return libraryConditions;
}

private static Map flatMapToMap(List list) {
Map map = new HashMap(list.size() / 2);
for (int i = 0; i < list.size(); i += 2) {
map.put(list.get(i), list.get(i + 1));
}
return map;
}
}

0 comments on commit e0e2319

Please sign in to comment.