Skip to content

Commit 246238c

Browse files
raphaeldeliobsbodden
authored andcommitted
feat: Ops for T Digest
1 parent 54b4e4b commit 246238c

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed

redis-om-spring/src/main/java/com/redis/om/spring/client/RedisModulesClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import redis.clients.jedis.bloom.commands.TopKFilterCommands;
1818
import redis.clients.jedis.json.commands.RedisJsonCommands;
1919
import redis.clients.jedis.search.RediSearchCommands;
20+
import redis.clients.jedis.bloom.commands.TDigestSketchCommands;
2021

2122
import java.util.Objects;
2223
import java.util.Optional;
@@ -62,6 +63,10 @@ public TopKFilterCommands clientForTopK() {
6263
return unifiedJedis;
6364
}
6465

66+
public TDigestSketchCommands clientForTDigest() {
67+
return unifiedJedis;
68+
}
69+
6570
private UnifiedJedis getUnifiedJedis() {
6671

6772
var sentinelConfiguration = jedisConnectionFactory.getSentinelConfiguration();
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.redis.om.spring.ops.pds;
2+
3+
import redis.clients.jedis.bloom.TDigestMergeParams;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public interface TDigestOperations<K> {
9+
/**
10+
* Create a new T-Digest sketch.
11+
*
12+
* @param key The key of the sketch
13+
* @return Status string reply
14+
*/
15+
String create(K key);
16+
17+
/**
18+
* Create a new T-Digest sketch with specified compression.
19+
*
20+
* @param key The key of the sketch
21+
* @param compression The compression parameter
22+
* @return Status string reply
23+
*/
24+
String create(K key, int compression);
25+
26+
/**
27+
* Reset the sketch.
28+
*
29+
* @param key The key of the sketch
30+
* @return Status string reply
31+
*/
32+
String reset(K key);
33+
34+
/**
35+
* Merge multiple sketches into one.
36+
*
37+
* @param key The key of the destination sketch
38+
* @param sourceKeys The keys of the source sketches
39+
* @return Status string reply
40+
*/
41+
String merge(K key, K... sourceKeys);
42+
43+
/**
44+
* Merge multiple sketches into one with parameters.
45+
*
46+
* @param params The merge parameters
47+
* @param key The key of the destination sketch
48+
* @param sourceKeys The keys of the source sketches
49+
* @return Status string reply
50+
*/
51+
String merge(TDigestMergeParams params, K key, K... sourceKeys);
52+
53+
/**
54+
* Get information about the sketch.
55+
*
56+
* @param key The key of the sketch
57+
* @return Map of information about the sketch
58+
*/
59+
Map<String, Object> info(K key);
60+
61+
/**
62+
* Add values to the sketch.
63+
*
64+
* @param key The key of the sketch
65+
* @param values The values to add
66+
* @return Status string reply
67+
*/
68+
String add(K key, double... values);
69+
70+
/**
71+
* Get the CDF (Cumulative Distribution Function) for values.
72+
*
73+
* @param key The key of the sketch
74+
* @param values The values to get CDF for
75+
* @return List of CDF values
76+
*/
77+
List<Double> cdf(K key, double... values);
78+
79+
/**
80+
* Get the quantile for values.
81+
*
82+
* @param key The key of the sketch
83+
* @param values The values to get quantile for
84+
* @return List of quantile values
85+
*/
86+
List<Double> quantile(K key, double... values);
87+
88+
/**
89+
* Get the minimum value in the sketch.
90+
*
91+
* @param key The key of the sketch
92+
* @return The minimum value
93+
*/
94+
double min(K key);
95+
96+
/**
97+
* Get the maximum value in the sketch.
98+
*
99+
* @param key The key of the sketch
100+
* @return The maximum value
101+
*/
102+
double max(K key);
103+
104+
/**
105+
* Get the trimmed mean of the sketch.
106+
*
107+
* @param key The key of the sketch
108+
* @param lowCut The low cut quantile
109+
* @param highCut The high cut quantile
110+
* @return The trimmed mean
111+
*/
112+
double trimmedMean(K key, double lowCut, double highCut);
113+
114+
/**
115+
* Get the rank of values.
116+
*
117+
* @param key The key of the sketch
118+
* @param values The values to get rank for
119+
* @return List of ranks
120+
*/
121+
List<Long> rank(K key, double... values);
122+
123+
/**
124+
* Get the reverse rank of values.
125+
*
126+
* @param key The key of the sketch
127+
* @param values The values to get reverse rank for
128+
* @return List of reverse ranks
129+
*/
130+
List<Long> revRank(K key, double... values);
131+
132+
/**
133+
* Get the value by rank.
134+
*
135+
* @param key The key of the sketch
136+
* @param ranks The ranks to get values for
137+
* @return List of values
138+
*/
139+
List<Double> byRank(K key, long... ranks);
140+
141+
/**
142+
* Get the value by reverse rank.
143+
*
144+
* @param key The key of the sketch
145+
* @param ranks The reverse ranks to get values for
146+
* @return List of values
147+
*/
148+
List<Double> byRevRank(K key, long... ranks);
149+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.redis.om.spring.ops.pds;
2+
3+
import com.redis.om.spring.client.RedisModulesClient;
4+
import redis.clients.jedis.bloom.TDigestMergeParams;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class TDigestOperationsImpl<K> implements TDigestOperations<K> {
10+
final RedisModulesClient client;
11+
12+
public TDigestOperationsImpl(RedisModulesClient client) {
13+
this.client = client;
14+
}
15+
16+
@Override
17+
public String create(K key) {
18+
return client.clientForTDigest().tdigestCreate(key.toString());
19+
}
20+
21+
@Override
22+
public String create(K key, int compression) {
23+
return client.clientForTDigest().tdigestCreate(key.toString(), compression);
24+
}
25+
26+
@Override
27+
public String reset(K key) {
28+
return client.clientForTDigest().tdigestReset(key.toString());
29+
}
30+
31+
@Override
32+
public String merge(K key, K... sourceKeys) {
33+
String[] sourceKeyStrings = new String[sourceKeys.length];
34+
for (int i = 0; i < sourceKeys.length; i++) {
35+
sourceKeyStrings[i] = sourceKeys[i].toString();
36+
}
37+
return client.clientForTDigest().tdigestMerge(key.toString(), sourceKeyStrings);
38+
}
39+
40+
@Override
41+
public String merge(TDigestMergeParams params, K key, K... sourceKeys) {
42+
String[] sourceKeyStrings = new String[sourceKeys.length];
43+
for (int i = 0; i < sourceKeys.length; i++) {
44+
sourceKeyStrings[i] = sourceKeys[i].toString();
45+
}
46+
return client.clientForTDigest().tdigestMerge(params, key.toString(), sourceKeyStrings);
47+
}
48+
49+
@Override
50+
public Map<String, Object> info(K key) {
51+
return client.clientForTDigest().tdigestInfo(key.toString());
52+
}
53+
54+
@Override
55+
public String add(K key, double... values) {
56+
return client.clientForTDigest().tdigestAdd(key.toString(), values);
57+
}
58+
59+
@Override
60+
public List<Double> cdf(K key, double... values) {
61+
return client.clientForTDigest().tdigestCDF(key.toString(), values);
62+
}
63+
64+
@Override
65+
public List<Double> quantile(K key, double... values) {
66+
return client.clientForTDigest().tdigestQuantile(key.toString(), values);
67+
}
68+
69+
@Override
70+
public double min(K key) {
71+
return client.clientForTDigest().tdigestMin(key.toString());
72+
}
73+
74+
@Override
75+
public double max(K key) {
76+
return client.clientForTDigest().tdigestMax(key.toString());
77+
}
78+
79+
@Override
80+
public double trimmedMean(K key, double lowCut, double highCut) {
81+
return client.clientForTDigest().tdigestTrimmedMean(key.toString(), lowCut, highCut);
82+
}
83+
84+
@Override
85+
public List<Long> rank(K key, double... values) {
86+
return client.clientForTDigest().tdigestRank(key.toString(), values);
87+
}
88+
89+
@Override
90+
public List<Long> revRank(K key, double... values) {
91+
return client.clientForTDigest().tdigestRevRank(key.toString(), values);
92+
}
93+
94+
@Override
95+
public List<Double> byRank(K key, long... ranks) {
96+
return client.clientForTDigest().tdigestByRank(key.toString(), ranks);
97+
}
98+
99+
@Override
100+
public List<Double> byRevRank(K key, long... ranks) {
101+
return client.clientForTDigest().tdigestByRevRank(key.toString(), ranks);
102+
}
103+
}

0 commit comments

Comments
 (0)