TDengine/include/util/tdigest.h

75 lines
2 KiB
C
Raw Normal View History

2022-05-23 08:03:39 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* include/tdigest.c
*
* Copyright (c) 2016, Usman Masood <usmanm at fastmail dot fm>
*/
#ifndef TDIGEST_H
#define TDIGEST_H
2022-10-13 03:49:46 +00:00
#include "os.h"
2022-05-23 08:03:39 +00:00
#ifndef M_PI
2022-10-13 03:09:43 +00:00
#define M_PI 3.14159265358979323846264338327950288 /* pi */
2022-05-23 08:03:39 +00:00
#endif
#define DOUBLE_MAX 1.79e+308
2022-10-13 03:09:43 +00:00
#define ADDITION_CENTROID_NUM 2
#define COMPRESSION 300
2022-05-23 08:03:39 +00:00
#define GET_CENTROID(compression) (ceil(compression * M_PI / 2) + 1 + ADDITION_CENTROID_NUM)
#define GET_THRESHOLD(compression) (7.5 + 0.37 * compression - 2e-4 * pow(compression, 2))
2022-10-13 03:09:43 +00:00
#define TDIGEST_SIZE(compression) \
(sizeof(TDigest) + sizeof(SCentroid) * GET_CENTROID(compression) + sizeof(SPt) * GET_THRESHOLD(compression))
2022-05-23 08:03:39 +00:00
typedef struct SCentroid {
2022-10-13 03:09:43 +00:00
double mean;
int64_t weight;
} SCentroid;
2022-05-23 08:03:39 +00:00
typedef struct SPt {
2022-10-13 03:09:43 +00:00
double value;
int64_t weight;
} SPt;
2022-05-23 08:03:39 +00:00
typedef struct TDigest {
2022-10-13 03:09:43 +00:00
double compression;
int32_t threshold;
int64_t size;
2022-05-23 08:03:39 +00:00
2022-10-13 03:09:43 +00:00
int64_t total_weight;
double min;
double max;
2022-05-23 08:03:39 +00:00
2022-10-13 03:09:43 +00:00
int32_t num_buffered_pts;
SPt *buffered_pts;
2022-05-23 08:03:39 +00:00
2022-10-13 03:09:43 +00:00
int32_t num_centroids;
SCentroid *centroids;
} TDigest;
2022-05-23 08:03:39 +00:00
2022-10-13 03:09:43 +00:00
TDigest *tdigestNewFrom(void *pBuf, int32_t compression);
2024-07-24 03:24:22 +00:00
int32_t tdigestAdd(TDigest *t, double x, int64_t w);
int32_t tdigestMerge(TDigest *t1, TDigest *t2);
2022-10-13 03:09:43 +00:00
double tdigestQuantile(TDigest *t, double q);
2024-07-24 03:24:22 +00:00
int32_t tdigestCompress(TDigest *t);
2022-10-13 03:09:43 +00:00
void tdigestFreeFrom(TDigest *t);
void tdigestAutoFill(TDigest *t, int32_t compression);
2022-05-23 08:03:39 +00:00
#endif /* TDIGEST_H */