]> arthur.barton.de Git - netdata.git/blob - src/rrdfamily.c
dns_query_time plugin: replace "." with "_" in dimensions
[netdata.git] / src / rrdfamily.c
1 #define NETDATA_RRD_INTERNALS 1
2 #include "common.h"
3
4 // ----------------------------------------------------------------------------
5 // RRDFAMILY index
6
7 int rrdfamily_compare(void *a, void *b) {
8     if(((RRDFAMILY *)a)->hash_family < ((RRDFAMILY *)b)->hash_family) return -1;
9     else if(((RRDFAMILY *)a)->hash_family > ((RRDFAMILY *)b)->hash_family) return 1;
10     else return strcmp(((RRDFAMILY *)a)->family, ((RRDFAMILY *)b)->family);
11 }
12
13 #define rrdfamily_index_add(host, rc) (RRDFAMILY *)avl_insert_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
14 #define rrdfamily_index_del(host, rc) (RRDFAMILY *)avl_remove_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
15
16 static RRDFAMILY *rrdfamily_index_find(RRDHOST *host, const char *id, uint32_t hash) {
17     RRDFAMILY tmp;
18     tmp.family = id;
19     tmp.hash_family = (hash)?hash:simple_hash(tmp.family);
20
21     return (RRDFAMILY *)avl_search_lock(&(host->rrdfamily_root_index), (avl *) &tmp);
22 }
23
24 RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id) {
25     RRDFAMILY *rc = rrdfamily_index_find(host, id, 0);
26     if(!rc) {
27         rc = callocz(1, sizeof(RRDFAMILY));
28
29         rc->family = strdupz(id);
30         rc->hash_family = simple_hash(rc->family);
31
32         // initialize the variables index
33         avl_init_lock(&rc->variables_root_index, rrdvar_compare);
34
35         RRDFAMILY *ret = rrdfamily_index_add(host, rc);
36         if(ret != rc)
37             fatal("RRDFAMILY: INTERNAL ERROR: Expected to INSERT RRDFAMILY '%s' into index, but inserted '%s'.", rc->family, (ret)?ret->family:"NONE");
38     }
39
40     rc->use_count++;
41     return rc;
42 }
43
44 void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc) {
45     rc->use_count--;
46     if(!rc->use_count) {
47         RRDFAMILY *ret = rrdfamily_index_del(host, rc);
48         if(ret != rc)
49             fatal("RRDFAMILY: INTERNAL ERROR: Expected to DELETE RRDFAMILY '%s' from index, but deleted '%s'.", rc->family, (ret)?ret->family:"NONE");
50
51         if(rc->variables_root_index.avl_tree.root != NULL)
52             fatal("RRDFAMILY: INTERNAL ERROR: Variables index of RRDFAMILY '%s' that is freed, is not empty.", rc->family);
53
54         freez((void *)rc->family);
55         freez(rc);
56     }
57 }
58