Skip to content

Commit 15068f3

Browse files
authored
Add a float array to byte array utility method (#3374)
* Add a float array to byte array utility method * test
1 parent 42d3509 commit 15068f3

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/main/java/redis/clients/jedis/search/RediSearchUtil.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package redis.clients.jedis.search;
22

3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
5+
36
import java.util.HashMap;
47
import java.util.Map;
8+
59
import redis.clients.jedis.util.SafeEncoder;
610

711
public class RediSearchUtil {
812

13+
/**
14+
* Jedis' {@code hset} methods do not support {@link Object}s as values. This method eases process
15+
* of converting a {@link Map} with Objects as values so that the returning Map can be set to a
16+
* {@code hset} method.
17+
* @param input map with object value
18+
* @return map with string value
19+
*/
920
public static Map<String, String> toStringMap(Map<String, Object> input) {
1021
Map<String, String> output = new HashMap<>(input.size());
1122
for (Map.Entry<String, Object> entry : input.entrySet()) {
@@ -30,6 +41,19 @@ public static Map<String, String> toStringMap(Map<String, Object> input) {
3041
return output;
3142
}
3243

44+
/**
45+
* x86 systems are little-endian and Java defaults to big-endian. This causes mismatching query
46+
* results when RediSearch is running in a x86 system. This method helps to convert concerned
47+
* arrays.
48+
* @param input float array
49+
* @return byte array
50+
*/
51+
public static byte[] ToByteArray(float[] input) {
52+
byte[] bytes = new byte[Float.BYTES * input.length];
53+
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().put(input);
54+
return bytes;
55+
}
56+
3357
private RediSearchUtil() {
3458
throw new InstantiationError("Must not instantiate this class");
3559
}

src/test/java/redis/clients/jedis/modules/search/SearchConfigTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
5-
import static org.junit.Assert.fail;
65

76
import java.util.Collections;
87
import java.util.Map;
8+
99
import org.junit.BeforeClass;
1010
import org.junit.Test;
11-
import redis.clients.jedis.exceptions.JedisDataException;
11+
1212
import redis.clients.jedis.modules.RedisModuleCommandsTestBase;
1313

1414
public class SearchConfigTest extends RedisModuleCommandsTestBase {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package redis.clients.jedis.modules.search;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import redis.clients.jedis.search.RediSearchUtil;
6+
7+
public class UtilTest {
8+
9+
@Test
10+
public void floatArrayToByteArray() {
11+
float[] floats = new float[]{0.2f};
12+
byte[] bytes = RediSearchUtil.ToByteArray(floats);
13+
byte[] expected = new byte[]{-51, -52, 76, 62};
14+
Assert.assertArrayEquals(expected, bytes);
15+
}
16+
}

0 commit comments

Comments
 (0)