0

I put in redis a key-value, where key is UUID converted to a byte array, for space optimization.

"3DEBB752-654A-4206-89BA-D3517237312E" -> [-119, -70, -45, 81, 114, 55, 49, 46, 61, -21, -73, 82, 101, 74, 66, 6].

I'm using Spring Jedis to get data from Redis server and when I try to get key via KEYS function

jedisConnection.keys("*".getBytes());

I get

[-119, -70, -45, 81, 114, 55, 49, 46, 61, -21, -73, 82, 101, 74, 66, 6]

But, when I try to fetch keys via SCAN function

jedisConnection.scan(ScanOptions.NONE);

key somehow change to this

[-17, -65, -67, -17, -65, -67, -17, -65, -67, 81, 114, 55, 49, 46, 61, -17, -65, -67, 82, 101, 74, 66, 6]

I'm confused, please tell me why are the keys different for KEYS and SCAN

  • Have you traversed through all the keys using SCAN ? SCAN returns only a few keys at a time. – Basit Anwer Sep 21 '15 at 12:25
  • Yeap... And for the test reason i used empty DB is Redis server, with just few keys.. So I'm not mixed up with another key, if you are talking about that.. – Oleg Kalinichenko Sep 21 '15 at 12:41
  • Have your tried to skip `"*".getBytes()` to just `"*"` – Basit Anwer Sep 21 '15 at 12:51
  • Can you share some code? I'm not able to restore the UUID from the bytes or to understand what's going on. This behavior _might_ be serializer/byte-conversion related but I'm not fully sure about that. – mp911de Sep 21 '15 at 12:56
  • No. Method `keys` receive only `byte[]`, not `String`. And i'm not sure the problem is there, because it returns everything fine.. – Oleg Kalinichenko Sep 21 '15 at 12:58
  • @mp911de Sorry, it was first byte setted manually, for internal needs, i removed it.. Try restore again, if you'd fail - i'll share code – Oleg Kalinichenko Sep 21 '15 at 13:07

1 Answers1

1

Well... Writing the answer in case if someone needs it.

First of all. Problem reproduced only when you got custom bytes array inside redis. If you'd save byte[] derived from string - everything would be ok.
After we calling scan method, Jedis inside itself receive bytes arrays bucket, creating new string instances from our bytes arrays, and there is a problem. Negative bytes transferring into 65533 value, and after transferring back to bytes arrays our primordial arrays are changed.

So, one of solutions - extend Jedis, JedisConnection and JedisConnectionFactory classes, where in Jedis make an analogue of method 'scan' without transferring byte[] to string