jedis
jedis copied to clipboard
Return List instead of Set from smembers method
Expected behavior
Redis SMEMBERS
returns an array of values for the given set. While in Jedis smembers
method returns a SET
of values going by interface signature. So, as a result, the client of the library expects that smembers
will honor all the SET
related constraints in terms of time-complexity i.e contains
, add
, etc will be O(1).
Actual behavior
https://github.com/redis/jedis/blob/818dc9db08f87004dca5ab7e4a8e8cf06c0ea15a/src/main/java/redis/clients/jedis/BinaryJedis.java#L1411
https://github.com/redis/jedis/blob/818dc9db08f87004dca5ab7e4a8e8cf06c0ea15a/src/main/java/redis/clients/jedis/BinaryJedis.java#L3978
On the contrary, looking at the implementation of smemebers()
method & SetFromList
class it becomes clear that the above constraints won't hold as smemebers()
actually returns a List wrapped in a Set interface. This use of decorator pattern is quite misleading as the operations contains
, add
, etc on the resultant Set
from smemebers()
will be O(n). Also, this violates the general Java Set constraints in terms of time-complexity expectation
Steps to reproduce:
Redis / Jedis Configuration
Jedis version:
3.0.1
Redis version:
6.0.5
Java version:
11
@gkorland PING!
Agree with @mudit-saxena . The native SET
is more suitable for this scenario.
Some API need to ensure the return order, such as zrange
, can only use SetFromList
. some do not need, such as zrandmember
, spop
, can use Java Set to get O(1) contains
.
I think we need to distinguish between the two cases, return to Java's Set
or List
, and then delete SetFromList
.
Or we can use List
directly and avoid introducing Set
.
For context given that under the hood implementation for SetFromList
is List
, directly using List
might be a better choice if we want to optimise for impact on existing usage from performance POV. Also, List
is in line with Redis smembers
https://redis.io/commands/smembers
Related #2628
This issue is marked stale. It will be closed in 30 days if it is not updated.