|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 One Identity |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As an additional exemption you are allowed to compile & link against the |
| 19 | + * OpenSSL libraries as published by the OpenSSL project. See the file |
| 20 | + * COPYING for details. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#include "stats/aggregator/stats-aggregator.h" |
| 25 | + |
| 26 | +gboolean |
| 27 | +stats_aggregator_is_orphaned(StatsAggregator *self) |
| 28 | +{ |
| 29 | + if (self && self->is_orphaned) |
| 30 | + return self->is_orphaned(self); |
| 31 | + |
| 32 | + return TRUE; |
| 33 | +} |
| 34 | + |
| 35 | +void |
| 36 | +stats_aggregator_track_counter(StatsAggregator *self) |
| 37 | +{ |
| 38 | + if (!self) |
| 39 | + return; |
| 40 | + |
| 41 | + if (stats_aggregator_is_orphaned(self) && self->register_aggr) |
| 42 | + self->register_aggr(self); |
| 43 | + |
| 44 | + ++self->use_count; |
| 45 | +} |
| 46 | + |
| 47 | +void |
| 48 | +stats_aggregator_untrack_counter(StatsAggregator *self) |
| 49 | +{ |
| 50 | + if (!self) |
| 51 | + return; |
| 52 | + |
| 53 | + if (self->use_count > 0) |
| 54 | + --self->use_count; |
| 55 | + |
| 56 | + if (self->use_count == 0) |
| 57 | + stats_aggregator_aggregate(self); |
| 58 | + |
| 59 | + if (stats_aggregator_is_orphaned(self)) |
| 60 | + stats_aggregator_unregister(self); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +void |
| 65 | +stats_aggregator_insert_data(StatsAggregator *self, gsize value) |
| 66 | +{ |
| 67 | + if (self && self->insert_data) |
| 68 | + self->insert_data(self, value); |
| 69 | +} |
| 70 | + |
| 71 | +void |
| 72 | +stats_aggregator_aggregate(StatsAggregator *self) |
| 73 | +{ |
| 74 | + if (self && self->aggregate) |
| 75 | + self->aggregate(self); |
| 76 | +} |
| 77 | + |
| 78 | +void |
| 79 | +stats_aggregator_reset(StatsAggregator *self) |
| 80 | +{ |
| 81 | + if (self && self->reset) |
| 82 | + self->reset(self); |
| 83 | +} |
| 84 | + |
| 85 | +void |
| 86 | +stats_aggregator_free(StatsAggregator *self) |
| 87 | +{ |
| 88 | + if (self && self->free) |
| 89 | + self->free(self); |
| 90 | + stats_cluster_key_cloned_free(&self->key); |
| 91 | + g_free(self); |
| 92 | +} |
| 93 | + |
| 94 | +static gboolean |
| 95 | +_is_orphaned(StatsAggregator *self) |
| 96 | +{ |
| 97 | + return self->use_count == 0; |
| 98 | +} |
| 99 | + |
| 100 | +static void |
| 101 | +_set_virtual_functions(StatsAggregator *self) |
| 102 | +{ |
| 103 | + self->is_orphaned = _is_orphaned; |
| 104 | +} |
| 105 | + |
| 106 | +void |
| 107 | +stats_aggregator_init_instance(StatsAggregator *self, StatsClusterKey *sc_key, gint stats_level) |
| 108 | +{ |
| 109 | + self->use_count = 0; |
| 110 | + self->stats_level = stats_level; |
| 111 | + stats_cluster_key_clone(&self->key, sc_key); |
| 112 | + _set_virtual_functions(self); |
| 113 | +} |
| 114 | + |
| 115 | +void |
| 116 | +stats_aggregator_unregister(StatsAggregator *self) |
| 117 | +{ |
| 118 | + if (self && self->unregister_aggr) |
| 119 | + self->unregister_aggr(self); |
| 120 | +} |
0 commit comments