Skip to content

Commit

Permalink
Added HSET variants to ease complex objects (#3605)
Browse files Browse the repository at this point in the history
focusing RediSearch.
  • Loading branch information
sazzad16 authored Nov 7, 2023
1 parent 5a70626 commit dbf475c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/redis/clients/jedis/CommandArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import redis.clients.jedis.args.Rawable;
import redis.clients.jedis.args.RawableFactory;
import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.params.IParams;
import redis.clients.jedis.search.RediSearchUtil;

public class CommandArguments implements Iterable<Rawable> {

Expand Down Expand Up @@ -34,10 +36,19 @@ public CommandArguments add(Object arg) {
args.add((Rawable) arg);
} else if (arg instanceof byte[]) {
args.add(RawableFactory.from((byte[]) arg));
} else if (arg instanceof Integer) {
args.add(RawableFactory.from((Integer) arg));
} else if (arg instanceof Double) {
args.add(RawableFactory.from((Double) arg));
} else if (arg instanceof Boolean) {
args.add(RawableFactory.from((Boolean) arg ? 1 : 0));
} else if (arg instanceof float[]) {
args.add(RawableFactory.from(RediSearchUtil.toByteArray((float[]) arg)));
} else if (arg instanceof String) {
args.add(RawableFactory.from((String) arg));
} else if (arg instanceof Boolean) {
args.add(RawableFactory.from(Integer.toString((Boolean) arg ? 1 : 0)));
} else if (arg instanceof GeoCoordinate) {
GeoCoordinate geo = (GeoCoordinate) arg;
args.add(RawableFactory.from(geo.getLongitude() + "," + geo.getLatitude()));
} else {
args.add(RawableFactory.from(String.valueOf(arg)));
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3110,6 +3110,14 @@ public final CommandObject<Long> spublish(byte[] channel, byte[] message) {
// Miscellaneous commands

// RediSearch commands
public final CommandObject<Long> hsetObject(String key, String field, Object value) {
return new CommandObject<>(commandArguments(HSET).key(key).add(field).add(value), BuilderFactory.LONG);
}

public final CommandObject<Long> hsetObject(String key, Map<String, Object> hash) {
return new CommandObject<>(addFlatMapArgs(commandArguments(HSET).key(key), hash), BuilderFactory.LONG);
}

private boolean isRoundRobinSearchCommand() {
if (broadcastAndRoundRobinConfig == null) {
return true;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3619,6 +3619,14 @@ public void psubscribe(BinaryJedisPubSub jedisPubSub, final byte[]... patterns)
// Random node commands

// RediSearch commands
public long hsetObject(String key, String field, Object value) {
return executeCommand(commandObjects.hsetObject(key, field, value));
}

public long hsetObject(String key, Map<String, Object> hash) {
return executeCommand(commandObjects.hsetObject(key, hash));
}

@Override
public String ftCreate(String indexName, IndexOptions indexOptions, Schema schema) {
return checkAndBroadcastCommand(commandObjects.ftCreate(indexName, indexOptions, schema));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,4 +1333,22 @@ public void escapeMapUtil() {
assertNotEquals("hello-world", client.hget("doc2", "txt"));
assertEquals("hello-world", RediSearchUtil.unescape(client.hget("doc2", "txt")));
}

@Test
public void hsetObject() {
float[] floats = new float[]{0.2f};
assertEquals(1L, client.hsetObject("obj", "floats", floats));
assertArrayEquals(RediSearchUtil.toByteArray(floats),
client.hget("obj".getBytes(), "floats".getBytes()));

GeoCoordinate geo = new GeoCoordinate(-0.441, 51.458);
Map<String, Object> fields = new HashMap<>();
fields.put("title", "hello world");
fields.put("loc", geo);
assertEquals(2L, client.hsetObject("obj", fields));
Map<String, String> stringMap = client.hgetAll("obj");
assertEquals(3, stringMap.size());
assertEquals("hello world", stringMap.get("title"));
assertEquals(geo.getLongitude() + "," + geo.getLatitude(), stringMap.get("loc"));
}
}

0 comments on commit dbf475c

Please sign in to comment.