MySQL 9.1.0
Source Code Documentation
option_usage_data.h
Go to the documentation of this file.
1/* Copyright (c) 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef OPTION_USAGE_DATA_H
25#define OPTION_USAGE_DATA_H
26
27#include <atomic>
30
31/**
32 @brief A helper class to handle option usage population
33
34 Adds a top level JSON object (if missing), then adds (if missing, otherwise
35 updates) the following two JSON elements to the top level JSON object:
36 * used (boolean)
37 * usedDate (ISO 8601 string)
38
39
40 Create an instance of the class at component/plugin init time and
41 dispose of it at deinit time as follows:
42
43 @code
44 ...
45 #include "mysql/components/library_mysys/option_usage_data.h"
46 ...
47 Option_usage_data *handlerton{nullptr};
48 ...
49 init() {
50 ...
51 usage_data = new Option_usage_data("feature name",
52 SERVICE_PLACEHOLDER(registry)); if (!usage_data) return 1;
53 ...
54 }
55 ...
56 deinit() {
57 ..
58 delete usage_data;
59 usage_data = nullptr;
60 ..
61 }
62 @endcode
63
64 Now, when the functionality is used (careful, an expensive call), do:
65
66 @code
67 usage_data->set_sampled(true);
68 @endcode
69
70 Do not register usage at the time the plugin component is initilized.
71 Try to register it at the time it is actually being used.
72 But do not do it too often: it parses JSON and writes to an InnoDB table!
73*/
75 public:
76 /**
77 @brief Use this constructor at init time
78 @param option_name The name of the option to register usage for
79 @param registry a reference to the registry service
80 */
81 Option_usage_data(const char *option_name, SERVICE_TYPE(registry) * registry)
82 : m_option_name(option_name), m_registry(registry), m_counter(0) {}
85
86 /**
87 @brief Records usage.
88
89 @param is_used True if the feature is used.
90 @retval true Error
91 @retval false Success
92 */
93 bool set(bool is_used);
94 /**
95 @brief Records usage (calls @ref Option_usage_data::set()) every Nth call
96
97 Very useful for high volume of calls to the usage function.
98
99 @param is_used True if the feature is used.
100 @param log_usage_every_nth_time Log usage for every Nth call to this
101 function.
102 @retval true Error
103 @retval false Success
104 */
105 bool set_sampled(bool is_used, unsigned long log_usage_every_nth_time);
106
107 protected:
108 const char *m_option_name;
110 std::atomic<unsigned> m_counter;
111};
112
113#endif /* OPTION_USAGE_DATA_H */
A helper class to handle option usage population.
Definition: option_usage_data.h:74
Option_usage_data(const char *option_name, const mysql_service_registry_t *registry)
Use this constructor at init time.
Definition: option_usage_data.h:81
std::atomic< unsigned > m_counter
Definition: option_usage_data.h:110
Option_usage_data(Option_usage_data &)=delete
const mysql_service_registry_t * m_registry
Definition: option_usage_data.h:109
const char * m_option_name
Definition: option_usage_data.h:108
~Option_usage_data()
Definition: option_usage_data.h:84
bool set(bool is_used)
Records usage.
Definition: option_usage_data.cc:41
bool set_sampled(bool is_used, unsigned long log_usage_every_nth_time)
Records usage (calls Option_usage_data::set()) every Nth call.
Definition: option_usage_data.cc:95
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:76