PMDA++  0.4.4
Header-only C++ library for writing PCP PMDAs
cache.hpp
Go to the documentation of this file.
1 // Copyright Paul Colby 2013 - 2015.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 /**
7  * @file
8  * @brief Defines PMDA cache helper functions.
9  */
10 
11 #ifndef __PCP_CPP_CACHE_HPP__
12 #define __PCP_CPP_CACHE_HPP__
13 
14 #include "config.hpp"
15 #include "exception.hpp"
16 #include "types.hpp"
17 
18 #include <sstream>
19 
20 PCP_CPP_BEGIN_NAMESPACE
21 
22 namespace pcp {
23 namespace cache {
24 
25 /**
26  * @brief Flags that may be applied to cache lookups.
27  */
29  require_active = 0x1 ///< Throw if the found cache entry is not active.
30 };
31 
32 /**
33  * @brief Pipe operator for combining lookup_flags values.
34  *
35  * This function performs a logical OR of two lookup_flags sets. This is a
36  * convenience function, allowing lookup_flags enum values to be used both
37  * standalone, and in combination.
38  *
39  * @param a First set of flags.
40  * @param b Second set of flags.
41  *
42  * @return Combined set of both \a a and \a b flags.
43  */
45 {
46  return static_cast<lookup_flags>(
47  static_cast<int>(a) | static_cast<int>(b)
48  );
49 }
50 
51 /**
52  * @brief Structure for return results from pcp::cache::lookup functions.
53  *
54  * @tparam Type Type to cast opaque pointers to.
55  */
56 template <typename Type>
58  char * name; ///< Instance name.
59  instance_id_type instance_id; ///< Instance identifier.
60  Type opaque; ///< Opaque pointer, cast to type \a Type.
61  int status; ///< Entry status, such as PMDA_CACHE_ACTIVE.
62 };
63 
64 /**
65  * @brief Lookup a cache entry, by instance ID.
66  *
67  * If \a flags includes \a require_active, and an inactive cache entry is found,
68  * a pcp::exception will be thrown instead of the inactive entry returned.
69  *
70  * @tparam Type Type to cast opaque pointers to.
71  *
72  * @param indom Instance domain to lookup.
73  * @param instance_id Instance ID to lookup.
74  * @param flags Optional flags that alter the function's behaviour.
75  *
76  * @throw pcp::exception If no cache entry entry found.
77  *
78  * @return A struct containing the found cache entry details.
79  *
80  * @see pmdaCacheLookup
81  */
82 template <typename Type>
83 lookup_result_type<Type> lookup(const pmInDom indom,
85  const lookup_flags flags = require_active)
86 {
88  void * opaque;
89  result.name = NULL;
90  result.status = pmdaCacheLookup(indom, instance_id, &result.name, &opaque);
91  if (result.status < 0) {
92  throw pcp::exception(result.status);
93  }
94  if ((flags & require_active) && (result.status != PMDA_CACHE_ACTIVE)) {
95  std::ostringstream message;
96  message << "Cache entry " << indom << ':' << instance_id << " (";
97  if (result.name == NULL) {
98  message << "NULL";
99  } else {
100  message << '"' << result.name << '"';
101  }
102  message << ") inactive";
103  throw pcp::exception(result.status, message.str());
104  }
105  result.instance_id = instance_id;
106  result.opaque = static_cast<Type>(opaque);
107  return result;
108 }
109 
110 /**
111  * @brief Lookup a cache entry, by instance name.
112  *
113  * If \a flags includes \a require_active, and an inactive cache entry is found,
114  * a pcp::exception will be thrown instead of the inactive entry returned.
115  *
116  * @tparam Type Type to cast opaque pointers to.
117  *
118  * @param indom Instance domain to lookup.
119  * @param name Instance name to lookup.
120  * @param flags Optional flags that alter the function's behaviour.
121  *
122  * @throw pcp::exception If no cache entry entry found.
123  *
124  * @return A struct containing the found cache entry details.
125  *
126  * @see pmdaCacheLookupName
127  */
128 template <typename Type>
129 lookup_result_type<Type> lookup(const pmInDom indom, const std::string &name,
130  const lookup_flags flags = require_active)
131 {
133  void * opaque;
134  int instance_id;
135  result.status = pmdaCacheLookupName(indom, name.c_str(), &instance_id, &opaque);
136  if (result.status < 0) {
137  throw pcp::exception(result.status);
138  }
139  if ((flags & require_active) && (result.status != PMDA_CACHE_ACTIVE)) {
140  std::ostringstream message;
141  message << "Cache entry " << indom << ':' << instance_id
142  << " (\"" << name << "\") inactive";
143  throw pcp::exception(result.status, message.str());
144  }
145  result.name = NULL;
146  result.instance_id = instance_id;
147  result.opaque = static_cast<Type>(opaque);
148  return result;
149 }
150 
151 /**
152  * @brief Lookup a cache entry, by instance name and key.
153  *
154  * If \a flags includes \a require_active, and an inactive cache entry is found,
155  * a pcp::exception will be thrown instead of the inactive entry returned.
156  *
157  * @tparam Type Type to cast opaque pointers to.
158  *
159  * @param indom Instance domain to lookup.
160  * @param name Instance name to lookup.
161  * @param key Instance key to lookup.
162  * @param flags Optional flags that alter the function's behaviour.
163  *
164  * @throw pcp::exception If no cache entry entry found.
165  *
166  * @return A struct containing the found cache entry details.
167  *
168  * @see pmdaCacheLookupKey
169  */
170 template <typename Type>
171 lookup_result_type<Type> lookup(const pmInDom indom, const std::string &name,
172  const std::string &key,
173  const lookup_flags flags = require_active)
174 {
176  void * opaque;
177  int instance_id;
178  result.status = pmdaCacheLookupKey(indom, name.c_str(), key.size(),
179  key.c_str(), &result.name,
180  &instance_id, &opaque);
181  if (result.status < 0) {
182  throw pcp::exception(result.status);
183  }
184  if ((flags & require_active) && (result.status != PMDA_CACHE_ACTIVE)) {
185  std::ostringstream message;
186  message << "Cache entry " << indom << ':' << instance_id
187  << " (\"" << name << "\":\"" << key << "\") inactive";
188  throw pcp::exception(result.status, message.str());
189  }
190  result.instance_id = instance_id;
191  result.opaque = static_cast<Type>(opaque);
192  return result;
193 }
194 
195 /**
196  * @brief Perform additional operations on the cache.
197  *
198  * This is a very simple wrapper for PCP's pmdaCacheOp function.
199  *
200  * @param indom Instance domain to operate on.
201  * @param operation Operation to perform. Should be one of the PMDA_CACHE_*
202  * constants.
203  *
204  * @throw pcp::exception On errors.
205  *
206  * @return The result of the pmdaCacheOp function. See pmdaCacheOp for details.
207  *
208  * @see pmdaCacheOp
209  */
210 inline int perform(const pmInDom indom, const int operation)
211 {
212  const int result = pmdaCacheOp(indom, operation);
213  if (result < 0) {
214  throw pcp::exception(result);
215  }
216  return result;
217 }
218 
219 /**
220  * @brief Purge cache entries that have not been active for some time.
221  *
222  * @param indom Instance domain to purge entries for.
223  * @param recent All entries that have not been active within this many
224  * seconds will be purged.
225  *
226  * @throw pcp::exception On error.
227  *
228  * @return The number of items purged.
229  *
230  * @see pmdaCachePurge
231  */
232 inline size_t purge(const pmInDom indom, const time_t recent)
233 {
234  const int result = pmdaCachePurge(indom, recent);
235  if (result < 0) {
236  throw pcp::exception(result);
237  }
238  return result;
239 }
240 
241 #ifndef PCP_CPP_NO_BOOST
242 /**
243  * @brief Purge cache entries that have not been active for some time.
244  *
245  * @param indom Instance domain to purge entries for.
246  * @param recent All entries that have not been active within this interval
247  * will be purged.
248  *
249  * @throw pcp::exception On error.
250  *
251  * @return The number of items purged.
252  *
253  * @see pmdaCachePurge
254  */
255 inline size_t purge(const pmInDom indom, const boost::posix_time::time_duration &recent)
256 {
257  return purge(indom, recent.total_seconds());
258 }
259 #endif
260 
261 /**
262  * @brief Add a item to the cache.
263  *
264  * @param indom Instance domain to add an entry for.
265  * @param name Instance name to add to the cache.
266  * @param flags Optional flags to be passed to pmdaCacheStore.
267  * @param opaque Optional opaque pointer to be include in the cache entry.
268  *
269  * @throw pcp::exception On error.
270  *
271  * @return The instance ID of the stored cache entry.
272  *
273  * @see pmdaCacheStore
274  */
275 inline instance_id_type store(const pmInDom indom, const std::string &name,
276  const int flags = PMDA_CACHE_ADD,
277  void * const opaque = NULL)
278 {
279  const int result = pmdaCacheStore(indom, flags, name.c_str(), opaque);
280  if (result < 0) {
281  throw pcp::exception(result);
282  }
283  return result;
284 }
285 
286 /**
287  * @brief Add a item to the cache.
288  *
289  * @param indom Instance domain to add an entry for.
290  * @param name Instance name to add to the cache.
291  * @param opaque Optional opaque pointer to be include in the cache entry.
292  * @param flags Optional flags to be passed to pmdaCacheStore.
293  *
294  * @throw pcp::exception On error.
295  *
296  * @return The instance ID of the stored cache entry.
297  *
298  * @see pmdaCacheStore
299  */
300 inline instance_id_type store(const pmInDom indom, const std::string &name,
301  void * const opaque, const int flags = PMDA_CACHE_ADD)
302 {
303  const int result = pmdaCacheStore(indom, flags, name.c_str(), opaque);
304  if (result < 0) {
305  throw pcp::exception(result);
306  }
307  return result;
308 }
309 
310 /**
311  * @brief Add a item to the cache.
312  *
313  * @param indom Instance domain to add an entry for.
314  * @param key Hint to pass to pmdaCacheStoreKey. See pmdaCacheStoreKey for
315  * details.
316  * @param name Instance name to add to the cache.
317  * @param flags Optional flags to be passed to pmdaCacheStore.
318  * @param opaque Optional opaque pointer to be include in the cache entry.
319  *
320  * @throw pcp::exception On error.
321  *
322  * @return The instance ID of the stored cache entry.
323  *
324  * @see pmdaCacheStoreKey
325  */
326 inline instance_id_type store(const pmInDom indom, const std::string &name,
327  const std::string &key, const int flags = 0,
328  void * const opaque = NULL)
329 {
330  const int result = pmdaCacheStoreKey(indom, flags, name.c_str(), key.size(),
331  key.c_str(), opaque);
332  if (result < 0) {
333  throw pcp::exception(result);
334  }
335  return result;
336 }
337 
338 /**
339  * @brief Add a item to the cache.
340  *
341  * @param indom Instance domain to add an entry for.
342  * @param key Hint to pass to pmdaCacheStoreKey. See pmdaCacheStoreKey for
343  * details.
344  * @param name Instance name to add to the cache.
345  * @param opaque Optional opaque pointer to be include in the cache entry.
346  * @param flags Optional flags to be passed to pmdaCacheStore.
347  *
348  * @throw pcp::exception On error.
349  *
350  * @return The instance ID of the stored cache entry.
351  *
352  * @see pmdaCacheStoreKey
353  */
354 inline instance_id_type store(const pmInDom indom, const std::string &name,
355  const std::string &key, void * const opaque,
356  const int flags = 0)
357 {
358  const int result = pmdaCacheStoreKey(indom, flags, name.c_str(), key.size(),
359  key.c_str(), opaque);
360  if (result < 0) {
361  throw pcp::exception(result);
362  }
363  return result;
364 }
365 
366 } } // pcp::cache namespace.
367 
368 PCP_CPP_END_NAMESPACE
369 
370 #endif
Throw if the found cache entry is not active.
Definition: cache.hpp:29
unsigned int instance_id_type
https://github.com/pcolby/pcp-pmda-cpp/issues/11
Definition: types.hpp:25
lookup_flags operator|(lookup_flags a, lookup_flags b)
Pipe operator for combining lookup_flags values.
Definition: cache.hpp:44
Definition: atom.hpp:20
int status
Entry status, such as PMDA_CACHE_ACTIVE.
Definition: cache.hpp:61
Structure for return results from pcp::cache::lookup functions.
Definition: cache.hpp:57
Sets up common PMDA++ library macros.
Declares various types used throughout the PMDA++ library.
Type opaque
Opaque pointer, cast to type Type.
Definition: cache.hpp:60
Base class for all PMDA++ exceptions.
Definition: exception.hpp:25
int perform(const pmInDom indom, const int operation)
Perform additional operations on the cache.
Definition: cache.hpp:210
instance_id_type instance_id
Instance identifier.
Definition: cache.hpp:59
Defines the pcp::exception class.
lookup_result_type< Type > lookup(const pmInDom indom, const instance_id_type instance_id, const lookup_flags flags=require_active)
Lookup a cache entry, by instance ID.
Definition: cache.hpp:83
lookup_flags
Flags that may be applied to cache lookups.
Definition: cache.hpp:28
size_t purge(const pmInDom indom, const time_t recent)
Purge cache entries that have not been active for some time.
Definition: cache.hpp:232
char * name
Instance name.
Definition: cache.hpp:58
instance_id_type store(const pmInDom indom, const std::string &name, const int flags=PMDA_CACHE_ADD, void *const opaque=NULL)
Add a item to the cache.
Definition: cache.hpp:275