-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from scoquelin/jl-hll
support hyperloglog commands
- Loading branch information
Showing
6 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
modules/api/src/main/scala/com/github/scoquelin/arugula/commands/RedisHLLAsyncCommands.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.scoquelin.arugula.commands | ||
|
||
import scala.concurrent.Future | ||
|
||
/** | ||
* This trait provides Redis HyperLogLog commands. | ||
* HyperLogLog is a probabilistic data structure used to estimate the cardinality of a set. | ||
* @see https://redis.io/docs/latest/develop/data-types/probabilistic/hyperloglogs/ | ||
* @tparam K The key type (usually String) | ||
* @tparam V The value type (usually String) | ||
*/ | ||
trait RedisHLLAsyncCommands[K, V] { | ||
/** | ||
* Adds the specified elements to the specified HyperLogLog | ||
* @param key The key of the HyperLogLog | ||
* @param values The values to add | ||
* @return The number of elements that were added to the HyperLogLog | ||
*/ | ||
def pfAdd(key: K, values: V*): Future[Long] | ||
|
||
/** | ||
* Merge multiple HyperLogLogs into a single one | ||
* @param destinationKey The key of the HyperLogLog to merge into | ||
* @param sourceKeys The keys of the HyperLogLogs to merge | ||
*/ | ||
def pfMerge(destinationKey: K, sourceKeys: K*): Future[Unit] | ||
|
||
/** | ||
* Get the number of elements in the HyperLogLog | ||
* @param keys The keys of the HyperLogLogs to count | ||
* @return The number of elements in the HyperLogLog | ||
*/ | ||
def pfCount(keys: K*): Future[Long] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...e/src/main/scala/com/github/scoquelin/arugula/commands/LettuceRedisHLLAsyncCommands.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.github.scoquelin.arugula.commands | ||
|
||
import scala.concurrent.Future | ||
|
||
import com.github.scoquelin.arugula.internal.LettuceRedisCommandDelegation | ||
|
||
trait LettuceRedisHLLAsyncCommands[K, V] extends RedisHLLAsyncCommands[K, V] with LettuceRedisCommandDelegation[K, V] { | ||
override def pfAdd(key: K, values: V*): Future[Long] = | ||
delegateRedisClusterCommandAndLift(_.pfadd(key, values: _*)).map(Long2long) | ||
|
||
override def pfMerge(destinationKey: K, sourceKeys: K*): Future[Unit] = | ||
delegateRedisClusterCommandAndLift(_.pfmerge(destinationKey, sourceKeys: _*)).map(_ => ()) | ||
|
||
override def pfCount(keys: K*): Future[Long] = | ||
delegateRedisClusterCommandAndLift(_.pfcount(keys: _*)).map(Long2long) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...s/test/src/test/scala/com/github/scoquelin/arugula/LettuceRedisHLLAsyncCommandsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.github.scoquelin.arugula | ||
|
||
import io.lettuce.core.RedisFuture | ||
import org.mockito.Mockito.{verify, when} | ||
import org.scalatest.matchers.must.Matchers | ||
import org.scalatest.{FutureOutcome, wordspec} | ||
|
||
class LettuceRedisHLLAsyncCommandsSpec extends wordspec.FixtureAsyncWordSpec with Matchers { | ||
|
||
override type FixtureParam = LettuceRedisCommandsClientFixture.TestContext | ||
|
||
override def withFixture(test: OneArgAsyncTest): FutureOutcome = | ||
withFixture(test.toNoArgAsyncTest(new LettuceRedisCommandsClientFixture.TestContext)) | ||
|
||
"LettuceRedisHLLAsyncCommands" should { | ||
"delegate PFADD command to Lettuce and lift result into a Future" in { testContext => | ||
import testContext._ | ||
|
||
val expectedValue = 1L | ||
val mockRedisFuture: RedisFuture[java.lang.Long] = mockRedisFutureToReturn(expectedValue) | ||
when(lettuceAsyncCommands.pfadd("key", "value")).thenReturn(mockRedisFuture) | ||
|
||
testClass.pfAdd("key", "value").map { result => | ||
result mustBe expectedValue | ||
verify(lettuceAsyncCommands).pfadd("key", "value") | ||
succeed | ||
} | ||
} | ||
|
||
"delegate PFMERGE command to Lettuce and lift result into a Future" in { testContext => | ||
import testContext._ | ||
|
||
val mockRedisFuture: RedisFuture[String] = mockRedisFutureToReturn("OK") | ||
when(lettuceAsyncCommands.pfmerge("destinationKey", "sourceKey")).thenReturn(mockRedisFuture) | ||
|
||
testClass.pfMerge("destinationKey", "sourceKey").map { result => | ||
result mustBe () | ||
verify(lettuceAsyncCommands).pfmerge("destinationKey", "sourceKey") | ||
succeed | ||
} | ||
} | ||
|
||
"delegate PFCOUNT command to Lettuce and lift result into a Future" in { testContext => | ||
import testContext._ | ||
|
||
val expectedValue = 1L | ||
val mockRedisFuture: RedisFuture[java.lang.Long] = mockRedisFutureToReturn(expectedValue) | ||
when(lettuceAsyncCommands.pfcount("key")).thenReturn(mockRedisFuture) | ||
|
||
testClass.pfCount("key").map { result => | ||
result mustBe expectedValue | ||
verify(lettuceAsyncCommands).pfcount("key") | ||
succeed | ||
} | ||
} | ||
} | ||
} |