Skip to content

Commit

Permalink
Add a float array to byte array utility method (#3374)
Browse files Browse the repository at this point in the history
* Add a float array to byte array utility method

* test
  • Loading branch information
sazzad16 authored Apr 18, 2023
1 parent 42d3509 commit 15068f3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/main/java/redis/clients/jedis/search/RediSearchUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package redis.clients.jedis.search;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.util.HashMap;
import java.util.Map;

import redis.clients.jedis.util.SafeEncoder;

public class RediSearchUtil {

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

/**
* x86 systems are little-endian and Java defaults to big-endian. This causes mismatching query
* results when RediSearch is running in a x86 system. This method helps to convert concerned
* arrays.
* @param input float array
* @return byte array
*/
public static byte[] ToByteArray(float[] input) {
byte[] bytes = new byte[Float.BYTES * input.length];
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().put(input);
return bytes;
}

private RediSearchUtil() {
throw new InstantiationError("Must not instantiate this class");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.util.Collections;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.Test;
import redis.clients.jedis.exceptions.JedisDataException;

import redis.clients.jedis.modules.RedisModuleCommandsTestBase;

public class SearchConfigTest extends RedisModuleCommandsTestBase {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/redis/clients/jedis/modules/search/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package redis.clients.jedis.modules.search;

import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.search.RediSearchUtil;

public class UtilTest {

@Test
public void floatArrayToByteArray() {
float[] floats = new float[]{0.2f};
byte[] bytes = RediSearchUtil.ToByteArray(floats);
byte[] expected = new byte[]{-51, -52, 76, 62};
Assert.assertArrayEquals(expected, bytes);
}
}

0 comments on commit 15068f3

Please sign in to comment.