1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::{
  commands,
  error::RedisError,
  interfaces::{ClientLike, RedisResult},
  types::{FromRedis, MultipleKeys, MultipleValues, RedisKey},
};
use std::convert::TryInto;

/// Functions that implement the [HyperLogLog](https://redis.io/commands#hyperloglog) interface.
#[async_trait]
pub trait HyperloglogInterface: ClientLike + Sized {
  /// Adds all the element arguments to the HyperLogLog data structure stored at the variable name specified as first
  /// argument.
  ///
  /// <https://redis.io/commands/pfadd>
  async fn pfadd<R, K, V>(&self, key: K, elements: V) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<MultipleValues> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(elements);
    commands::hyperloglog::pfadd(self, key, elements).await?.convert()
  }

  /// When called with a single key, returns the approximated cardinality computed by the HyperLogLog data structure
  /// stored at the specified variable, which is 0 if the variable does not exist.
  ///
  /// When called with multiple keys, returns the approximated cardinality of the union of the HyperLogLogs passed, by
  /// internally merging the HyperLogLogs stored at the provided keys into a temporary HyperLogLog.
  ///
  /// <https://redis.io/commands/pfcount>
  async fn pfcount<R, K>(&self, keys: K) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<MultipleKeys> + Send,
  {
    into!(keys);
    commands::hyperloglog::pfcount(self, keys).await?.convert()
  }

  /// Merge multiple HyperLogLog values into an unique value that will approximate the cardinality of the union of the
  /// observed sets of the source HyperLogLog structures.
  ///
  /// <https://redis.io/commands/pfmerge>
  async fn pfmerge<R, D, S>(&self, dest: D, sources: S) -> RedisResult<R>
  where
    R: FromRedis,
    D: Into<RedisKey> + Send,
    S: Into<MultipleKeys> + Send,
  {
    into!(dest, sources);
    commands::hyperloglog::pfmerge(self, dest, sources).await?.convert()
  }
}