Skip to content

Commit fe5c683

Browse files
add TDigest function to the client
1 parent 65c8c3a commit fe5c683

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

client.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,76 @@ func (client *Client) CfInfo(key string) (map[string]int64, error) {
443443
return ParseInfoReply(redis.Values(conn.Do("CF.INFO", key)))
444444
}
445445

446+
// Allocate the memory and initialize the t-digest
447+
func (client *Client) TdCreate(key string, compression int64) (string, error){
448+
conn := client.Pool.Get()
449+
defer conn.Close()
450+
return redis.String(conn.Do("TDIGEST.CREATE", key, compression))
451+
}
452+
453+
// Reset the sketch to zero - empty out the sketch and re-initialize it
454+
func (client *Client) TdReset(key string) (string, error){
455+
conn := client.Pool.Get()
456+
defer conn.Close()
457+
return redis.String(conn.Do("TDIGEST.RESET", key))
458+
}
459+
460+
// Adds one or more samples to a sketch
461+
func (client *Client) TdAdd(key string, samples map[string]float64) ([]string, error) ) {
462+
conn := client.Pool.Get()
463+
defer conn.Close()
464+
args := redis.Args{key}
465+
for k, v := range samples {
466+
args = args.Add(k, v)
467+
}
468+
reply, err := conn.Do("TDIGEST.ADD", args...)
469+
return redis.Strings(reply, err)
470+
}
471+
472+
// Merges all of the values from 'from' to 'this' sketch
473+
func (client *Client) TdMerge(toKey string, fromKey string) (string, error) {
474+
conn := client.Pool.Get()
475+
defer conn.Close()
476+
return redis.String(conn.Do("TDIGEST.MERGE", toKey, fromKey))
477+
}
478+
479+
// Get minimum value from the sketch. Will return DBL_MAX if the sketch is empty
480+
func (client *Client) TdMin(key string) (float64, error) {
481+
conn := client.Pool.Get()
482+
defer conn.Close()
483+
return redis.String(conn.Do("TDIGEST.MIN", key))
484+
}
485+
486+
// Get maximum value from the sketch. Will return DBL_MIN if the sketch is empty
487+
func (client *Client) TdMax(key string) (float64, error) {
488+
conn := client.Pool.Get()
489+
defer conn.Close()
490+
return redis.String(conn.Do("TDIGEST.MAX", key))
491+
}
492+
493+
// Returns an estimate of the cutoff such that a specified fraction of the data added
494+
// to this TDigest would be less than or equal to the cutoff
495+
func (client *Client) TdQuantile(key string, quantile float64) (float64, error) {
496+
conn := client.Pool.Get()
497+
defer conn.Close()
498+
return redis.String(conn.Do("TDIGEST.QUANTILE", key, quantile))
499+
}
500+
501+
// Returns the fraction of all points added which are <= value
502+
func (client *Client) TdCdf(key string, value float64) (float64, error) {
503+
conn := client.Pool.Get()
504+
defer conn.Close()
505+
return redis.String(conn.Do("TDIGEST.CDF", key, value))
506+
}
507+
508+
// Returns compression, capacity, total merged and unmerged nodes, the total
509+
// compressions made up to date on that key, and merged and unmerged weight.
510+
func (client *Client) TdInfo(key string) (map[string]int64, error) {
511+
conn := client.Pool.Get()
512+
defer conn.Close()
513+
return ParseInfoReply(redis.Values(conn.Do("TDIGEST.INFO", key)))
514+
}
515+
446516
func ParseInfoReply(values []interface{}, err error) (map[string]int64, error) {
447517
if err != nil {
448518
return nil, err

0 commit comments

Comments
 (0)