diff options
Diffstat (limited to '')
-rw-r--r-- | database/rrdlabels.c | 83 |
1 files changed, 77 insertions, 6 deletions
diff --git a/database/rrdlabels.c b/database/rrdlabels.c index 4a9a6dae6..f6abd6023 100644 --- a/database/rrdlabels.c +++ b/database/rrdlabels.c @@ -399,7 +399,7 @@ size_t text_sanitize(unsigned char *dst, const unsigned char *src, size_t dst_si // find how big this character is (2-4 bytes) size_t utf_character_size = 2; - while(utf_character_size <= 4 && src[utf_character_size] && IS_UTF8_BYTE(src[utf_character_size]) && !IS_UTF8_STARTBYTE(src[utf_character_size])) + while(utf_character_size < 4 && src[utf_character_size] && IS_UTF8_BYTE(src[utf_character_size]) && !IS_UTF8_STARTBYTE(src[utf_character_size])) utf_character_size++; if(utf) { @@ -651,10 +651,24 @@ void rrdlabels_get_value_to_buffer_or_null(DICTIONARY *labels, BUFFER *wb, const dictionary_acquired_item_release(labels, acquired_item); } +void rrdlabels_value_to_buffer_array_item_or_null(DICTIONARY *labels, BUFFER *wb, const char *key) { + if(!labels) return; + + const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key); + RRDLABEL *lb = dictionary_acquired_item_value(acquired_item); + + if(lb && lb->label_value) + buffer_json_add_array_item_string(wb, string2str(lb->label_value)); + else + buffer_json_add_array_item_string(wb, NULL); + + dictionary_acquired_item_release(labels, acquired_item); +} + // ---------------------------------------------------------------------------- // rrdlabels_get_value_to_char_or_null() -void rrdlabels_get_value_to_char_or_null(DICTIONARY *labels, char **value, const char *key) { +void rrdlabels_get_value_strdup_or_null(DICTIONARY *labels, char **value, const char *key) { const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key); RRDLABEL *lb = dictionary_acquired_item_value(acquired_item); @@ -663,6 +677,46 @@ void rrdlabels_get_value_to_char_or_null(DICTIONARY *labels, char **value, const dictionary_acquired_item_release(labels, acquired_item); } +void rrdlabels_get_value_strcpyz(DICTIONARY *labels, char *dst, size_t dst_len, const char *key) { + const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key); + RRDLABEL *lb = dictionary_acquired_item_value(acquired_item); + + if(lb && lb->label_value) + strncpyz(dst, string2str(lb->label_value), dst_len); + else + dst[0] = '\0'; + + dictionary_acquired_item_release(labels, acquired_item); +} + +STRING *rrdlabels_get_value_string_dup(DICTIONARY *labels, const char *key) { + const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key); + RRDLABEL *lb = dictionary_acquired_item_value(acquired_item); + + STRING *ret = NULL; + if(lb && lb->label_value) + ret = string_dup(lb->label_value); + + dictionary_acquired_item_release(labels, acquired_item); + + return ret; +} + +STRING *rrdlabels_get_value_to_buffer_or_unset(DICTIONARY *labels, BUFFER *wb, const char *key, const char *unset) { + const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key); + RRDLABEL *lb = dictionary_acquired_item_value(acquired_item); + + STRING *ret = NULL; + if(lb && lb->label_value) + buffer_strcat(wb, string2str(lb->label_value)); + else + buffer_strcat(wb, unset); + + dictionary_acquired_item_release(labels, acquired_item); + + return ret; +} + // ---------------------------------------------------------------------------- // rrdlabels_unmark_all() // remove labels RRDLABEL_FLAG_OLD and RRDLABEL_FLAG_NEW from all dictionary items @@ -778,6 +832,7 @@ void rrdlabels_copy(DICTIONARY *dst, DICTIONARY *src) { // returns true when there are keys in the dictionary matching a simple pattern struct simple_pattern_match_name_value { + size_t searches; SIMPLE_PATTERN *pattern; char equal; }; @@ -788,6 +843,7 @@ static int simple_pattern_match_name_only_callback(const DICTIONARY_ITEM *item, (void)value; // we return -1 to stop the walkthrough on first match + t->searches++; if(simple_pattern_matches(t->pattern, name)) return -1; return 0; @@ -799,6 +855,7 @@ static int simple_pattern_match_name_and_value_callback(const DICTIONARY_ITEM *i RRDLABEL *lb = (RRDLABEL *)value; // we return -1 to stop the walkthrough on first match + t->searches++; if(simple_pattern_matches(t->pattern, name)) return -1; size_t len = RRDLABELS_MAX_NAME_LENGTH + RRDLABELS_MAX_VALUE_LENGTH + 2; // +1 for =, +1 for \0 @@ -817,28 +874,34 @@ static int simple_pattern_match_name_and_value_callback(const DICTIONARY_ITEM *i // terminate it *dst = '\0'; - if(simple_pattern_matches(t->pattern, tmp)) return -1; + t->searches++; + if(simple_pattern_matches_length_extract(t->pattern, tmp, dst - tmp, NULL, 0) == SP_MATCHED_POSITIVE) + return -1; return 0; } -bool rrdlabels_match_simple_pattern_parsed(DICTIONARY *labels, SIMPLE_PATTERN *pattern, char equal) { +bool rrdlabels_match_simple_pattern_parsed(DICTIONARY *labels, SIMPLE_PATTERN *pattern, char equal, size_t *searches) { if (!labels) return false; struct simple_pattern_match_name_value t = { + .searches = 0, .pattern = pattern, .equal = equal }; int ret = dictionary_walkthrough_read(labels, equal?simple_pattern_match_name_and_value_callback:simple_pattern_match_name_only_callback, &t); + if(searches) + *searches = t.searches; + return (ret == -1)?true:false; } bool rrdlabels_match_simple_pattern(DICTIONARY *labels, const char *simple_pattern_txt) { if (!labels) return false; - SIMPLE_PATTERN *pattern = simple_pattern_create(simple_pattern_txt, " ,|\t\r\n\f\v", SIMPLE_PATTERN_EXACT); + SIMPLE_PATTERN *pattern = simple_pattern_create(simple_pattern_txt, " ,|\t\r\n\f\v", SIMPLE_PATTERN_EXACT, true); char equal = '\0'; const char *s; @@ -849,7 +912,7 @@ bool rrdlabels_match_simple_pattern(DICTIONARY *labels, const char *simple_patte } } - bool ret = rrdlabels_match_simple_pattern_parsed(labels, pattern, equal); + bool ret = rrdlabels_match_simple_pattern_parsed(labels, pattern, equal, NULL); simple_pattern_free(pattern); @@ -959,6 +1022,14 @@ int rrdlabels_to_buffer(DICTIONARY *labels, BUFFER *wb, const char *before_each, return dictionary_walkthrough_read(labels, label_to_buffer_callback, (void *)&tmp); } +void rrdlabels_to_buffer_json_members(DICTIONARY *labels, BUFFER *wb) { + RRDLABEL *lb; + dfe_start_read(labels, lb) { + buffer_json_member_add_string(wb, lb_dfe.name, string2str(lb->label_value)); + } + dfe_done(lb); +} + void rrdset_update_rrdlabels(RRDSET *st, DICTIONARY *new_rrdlabels) { if(!st->rrdlabels) st->rrdlabels = rrdlabels_create(); |