Skip to content

Commit

Permalink
[add] Exposed td_reset() api
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecosta90 committed Feb 28, 2021
1 parent 0ab3871 commit fc17a70
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 345 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ INFER?=./deps/infer
INFER_DOCKER?=redisbench/infer-linux64:1.0.0
ROOT=$(shell pwd)
SRCDIR := $(ROOT)/src
TESTDIR := $(ROOT)/tests


ifndef CMAKE_LIBRARY_SHARED_OPTIONS
Expand Down Expand Up @@ -101,10 +102,14 @@ coverage:
format:
clang-format -style=file -i $(SRCDIR)/*.c
clang-format -style=file -i $(SRCDIR)/*.h
clang-format -style=file -i $(TESTDIR)/*.c
clang-format -style=file -i $(TESTDIR)/*.h

lint:
clang-format -style=file -Werror -n $(SRCDIR)/*.c
clang-format -style=file -Werror -n $(SRCDIR)/*.h
clang-format -style=file -Werror -n $(TESTDIR)/*.c
clang-format -style=file -Werror -n $(TESTDIR)/*.h

# build all
full:
Expand Down
37 changes: 21 additions & 16 deletions src/tdigest.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,15 @@ static size_t td_required_buf_size(double compression) {

int td_init(double compression, td_histogram_t **result) {
size_t buf_size = td_required_buf_size(compression);
td_histogram_t *h = (td_histogram_t *)((char *)(__td_malloc(buf_size)));
if (!h) {
return NULL;
td_histogram_t *histogram = (td_histogram_t *)((char *)(__td_malloc(buf_size)));
if (!histogram) {
return 1;
}
bbzero((void *)(h), buf_size);
*h = (td_histogram_t){
.compression = compression,
.cap = (buf_size - sizeof(td_histogram_t)) / sizeof(node_t),
.min = __DBL_MAX__,
.max = __DBL_MIN__,
.total_compressions = 0,
.merged_nodes = 0,
.merged_weight = 0,
.unmerged_nodes = 0,
.unmerged_weight = 0,
};
*result = h;
bbzero((void *)(histogram), buf_size);
histogram->cap = (buf_size - sizeof(td_histogram_t)) / sizeof(node_t);
histogram->compression = compression;
td_reset(histogram);
*result = histogram;
return 0;
}

Expand All @@ -75,6 +67,19 @@ void td_merge(td_histogram_t *into, td_histogram_t *from) {

int td_centroid_count(td_histogram_t *h) { return next_node(h); }

void td_reset(td_histogram_t *h) {
if (h == NULL) {
return;
}
h->min = __DBL_MAX__;
h->max = __DBL_MIN__;
h->merged_nodes = 0;
h->merged_weight = 0;
h->unmerged_nodes = 0;
h->unmerged_weight = 0;
h->total_compressions = 0;
}

double td_size(td_histogram_t *h) { return h->merged_weight + h->unmerged_weight; }

double td_cdf(td_histogram_t *h, double val) {
Expand Down
Loading

0 comments on commit fc17a70

Please sign in to comment.