jedis icon indicating copy to clipboard operation
jedis copied to clipboard

Return List instead of Set from smembers method

Open mudit-saxena opened this issue 3 years ago • 6 comments

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

mudit-saxena avatar Nov 23 '20 19:11 mudit-saxena

@gkorland PING!

sazzad16 avatar Nov 24 '20 15:11 sazzad16

Agree with @mudit-saxena . The native SET is more suitable for this scenario.

dengliming avatar Mar 15 '21 01:03 dengliming

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.

yangbodong22011 avatar Jul 13 '21 09:07 yangbodong22011

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

mudit-saxena avatar Jul 14 '21 16:07 mudit-saxena

Related #2628

sazzad16 avatar Aug 19 '21 13:08 sazzad16

This issue is marked stale. It will be closed in 30 days if it is not updated.

github-actions[bot] avatar Jan 14 '24 00:01 github-actions[bot]