From 697bd2bdcac6667817ee3b221ea116d2f31ad645 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 8 Apr 2022 09:57:10 -0400 Subject: [PATCH] TT#170203 support filter function when fetching records This allows us to fetch only specific records that we're interested in, based on context, instead of all of them and then having to do a second pass over them. Change-Id: I5e314fa633f57c79db85476e347a3305b5f585e9 --- mediator.c | 8 ++++---- mediator.h | 2 ++ medmysql.c | 8 ++++++-- medmysql.h | 2 +- medredis.c | 14 +++++++++++--- medredis.h | 2 +- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/mediator.c b/mediator.c index ed41ad6..b461340 100644 --- a/mediator.c +++ b/mediator.c @@ -352,12 +352,12 @@ int main(int argc, char **argv) gettimeofday(&tv_start, NULL); #endif - int ret = medmysql_fetch_records(mysql_callid, &acc_records, 1); + int ret = medmysql_fetch_records(mysql_callid, &acc_records, 1, NULL, NULL); if (ret < 0) goto out; int must_sort = (ret > 0); - ret = medredis_fetch_records(mysql_callid, &acc_records); + ret = medredis_fetch_records(mysql_callid, &acc_records, NULL, NULL); if (ret > 0) must_sort = 1; @@ -402,10 +402,10 @@ int main(int argc, char **argv) gettimeofday(&tv_start, NULL); #endif - if(medredis_fetch_records(redis_callid, &acc_records) < 0) + if(medredis_fetch_records(redis_callid, &acc_records, NULL, NULL) < 0) goto out; - medmysql_fetch_records(redis_callid, &acc_records, 0); + medmysql_fetch_records(redis_callid, &acc_records, 0, NULL, NULL); // always sort records from Redis, regardless of whether records from MySQL were merged records_sort(&acc_records); diff --git a/mediator.h b/mediator.h index fa94215..0d70bbd 100644 --- a/mediator.h +++ b/mediator.h @@ -100,6 +100,8 @@ typedef struct { time_t created; } med_cache_entry_t; +typedef gboolean (*records_filter_func)(med_entry_t *, void *data); + extern GHashTable *med_peer_host_table; extern GHashTable *med_peer_ip_table; extern GHashTable *med_peer_id_table; diff --git a/medmysql.c b/medmysql.c index 4af05cd..a176355 100644 --- a/medmysql.c +++ b/medmysql.c @@ -673,7 +673,8 @@ gboolean medmysql_fetch_callids(GQueue *output) /**********************************************************************/ int medmysql_fetch_records(char *callid, - GQueue *entries, int warn_empty) + GQueue *entries, int warn_empty, + records_filter_func filter, void *filter_data) { MYSQL_RES *res; MYSQL_ROW row; @@ -734,7 +735,10 @@ int medmysql_fetch_records(char *callid, cdr_parse_entry(e); - g_queue_push_tail(entries, e); + if (!filter || filter(e, filter_data)) + g_queue_push_tail(entries, e); + else + med_entry_free(e); if (check_shutdown()) return -1; diff --git a/medmysql.h b/medmysql.h index 157873f..740fbed 100644 --- a/medmysql.h +++ b/medmysql.h @@ -53,7 +53,7 @@ struct medmysql_call_stat_info_t { int medmysql_init(void); void medmysql_cleanup(void); gboolean medmysql_fetch_callids(GQueue *output); -int medmysql_fetch_records(char *callid, GQueue *entries, int warn_empty); +int medmysql_fetch_records(char *callid, GQueue *entries, int warn_empty, records_filter_func, void *filter_data); int medmysql_trash_entries(const char *callid, struct medmysql_batches *); int medmysql_backup_entries(const char *callid, struct medmysql_batches *); int medmysql_delete_entries(const char *callid, struct medmysql_batches *); diff --git a/medredis.c b/medredis.c index 9f7b360..a7cdf87 100644 --- a/medredis.c +++ b/medredis.c @@ -643,7 +643,9 @@ static void medredis_free_keys_list(gpointer data) { /**********************************************************************/ int medredis_fetch_records(char *callid, - GQueue *entries) { + GQueue *entries, + records_filter_func filter, void *filter_data) +{ /* @@ -746,8 +748,14 @@ int medredis_fetch_records(char *callid, continue; } medredis_free_reply(&reply); - g_queue_push_tail(entries, e); - ret = 1; + + if (!filter || filter(e, filter_data)) + { + g_queue_push_tail(entries, e); + ret = 1; + } + else + med_entry_free(e); } g_list_free_full(keys, medredis_free_keys_list); diff --git a/medredis.h b/medredis.h index a92d974..93df908 100644 --- a/medredis.h +++ b/medredis.h @@ -9,7 +9,7 @@ int medredis_init(void); void medredis_cleanup(void); gboolean medredis_fetch_callids(GQueue *output); -int medredis_fetch_records(char *callid, GQueue *entries); +int medredis_fetch_records(char *callid, GQueue *entries, records_filter_func, void *filter_data); int medredis_trash_entries(GQueue *records); int medredis_backup_entries(GQueue *records);