CARMA C++
TagIDAuthority.h
Go to the documentation of this file.
1 #ifndef CARMA_DBMS_TAGIDAUTHORITY_H
2 #define CARMA_DBMS_TAGIDAUTHORITY_H
3 
4 
17 #include <string>
18 #include <map>
19 #include <vector>
20 
21 #include "carma/util/PthreadRWLock.h"
22 #include "carma/monitor/types.h"
23 
24 
25 namespace log4cpp {
26 
27 class Category;
28 
29 } // namespace log4cpp
30 
31 
32 namespace carma {
33 namespace dbms {
34 
35 class DBConfigurator;
36 
52 public:
53 
57  static TagIDAuthority & getAuthority( );
58 
59  virtual ~TagIDAuthority( );
60 
61  static ::std::string getDefaultCanonicalNamesToTagIDsConfFile( );
62 
82  static void configureAuthority(
83  const bool useDb,
84  const ::std::string & dbConfFile,
85  const bool useConfFileIfDBIsDown = true,
86  const ::std::string & canonicalNamesToTagIDsConfFile =
87  getDefaultCanonicalNamesToTagIDsConfFile( ) );
88 
95  static void closeAuthority() ;
96 
102  carma::monitor::tagIDType lookupID( const ::std::string & name ) const;
103 
112  unsigned short lookupSubsystemID( const ::std::string & name ) const;
113 
114  bool tagIdAssignedOnTheFly( const ::std::string & name ) const;
115 
116  bool tagIdAssignedOnTheFly( carma::monitor::tagIDType tagId ) const;
117 
123  ::std::string lookupName( carma::monitor::tagIDType tagId ) const;
124 
132  static ::std::string
133  getSubsystemName( const ::std::string & canonicalName );
134 
151  static ::std::string getSubsystemName( const unsigned subsysID );
152 
158  static int getSubsystemCount();
159 
169  static carma::monitor::tagIDType composeTagID(
170  unsigned short subsystemId, unsigned short monitorPointId );
171 
179  static unsigned short getSubsystemID( carma::monitor::tagIDType tagId );
180 
188  static unsigned short getPointID( carma::monitor::tagIDType tagId );
189 
190  static unsigned short maxPossiblePointID( unsigned short subsystemId );
191 
196  ::std::string tagIDNameMapSha1Sum() const;
197 
198  struct TagIdOriginStats {
199  size_t total;
200  size_t totalDb;
201  size_t totalConfFile;
202  size_t totalOtf;
203 
204  // These maps are sparse. They only have an entry for a subsystem ID
205  // if the value for that subsystem ID is non-zero.
206  ::std::map< unsigned short, size_t > subsystemTotal;
207  ::std::map< unsigned short, size_t > subsystemDb;
208  ::std::map< unsigned short, size_t > subsystemConfFile;
209  ::std::map< unsigned short, size_t > subsystemOtf;
210  };
211 
212  TagIdOriginStats collectTagIdOriginStats( ) const;
213 
214  void logTagIdOriginStats() const;
215 
216  void logNotableOnTheFlyCreationsThusFarAndInTheFuture();
217 
218  void
219  logNotableOnTheFlyCreationsThusFarAndInTheFuture( size_t targetLineCount );
220 
221 private:
222  struct StrictlyWeakStringOrdering {
223  bool operator()( const ::std::string & lhs,
224  const ::std::string & rhs ) const;
225  };
226 
227 public:
228 
229  typedef enum {
230  SUBSYS_TABLE_ORIGIN,
231  DB_ORIGIN,
232  CONF_FILE_ORIGIN,
233  ON_THE_FLY_ORIGIN
234  } OriginType;
235 
236 private:
237 
238  typedef enum {
239  SUBSYS_DOES_NOT_EXIST,
240  SUBSYS_ID_MISMATCH,
241  SUBSYS_MATCHES
242  } SubsysMatchType;
243 
244 public:
245 
246  struct NameMapInfo {
247  carma::monitor::tagIDType tagId_;
248  OriginType origin_;
249 
250  NameMapInfo( carma::monitor::tagIDType id, OriginType origin );
251  };
252 
259  bool retrieveTagInfo( const std::string & canonicalName,
260  NameMapInfo & nameMapInfo ) const;
261 
262 private:
263 
264  typedef ::std::map< ::std::string, NameMapInfo, StrictlyWeakStringOrdering >
265  Name2InfoMap;
266 
267  typedef ::std::map< ::std::string, carma::monitor::tagIDType, StrictlyWeakStringOrdering >
268  Name2IdMap;
269 
270  typedef ::std::map< carma::monitor::tagIDType, ::std::string > Id2NameMap;
271 
272  TagIDAuthority( bool canUseDb,
273  bool canUseConfFile,
274  const ::std::string & dbConfFile,
275  const ::std::string & tagsConfFile );
276 
281  void registerNameHoldingWriteLock( const ::std::string & name,
282  carma::monitor::tagIDType tagId,
283  OriginType origin );
284 
291  void registerSubsystemNamesHoldingWriteLock( );
292 
297  void getNamesToTagIDsFromConfFile( const ::std::string & fileName );
298  void getNamesToTagIDsFromBinaryConfFile( const ::std::string & fileName );
299  void getNamesToTagIDsFromTextConfFile( const ::std::string & fileName );
300 
301  void registerNameIDsUsingDB( const dbms::DBConfigurator * dbconf );
302 
303  carma::monitor::tagIDType
304  convertNameToTagIdHoldingLock( const ::std::string & name ) const;
305 
306 public:
307 
308  carma::monitor::tagIDType
309  findIdOrAssignOtf( const ::std::string & name,
310  bool & assignedOTF,
311  const ::std::string & subsysName );
312 
313 private:
314 
315  TagIdOriginStats collectTagIdOriginStatsHoldingLock( ) const;
316 
317  SubsysMatchType checkSubsysMatchHoldingLock(
318  const ::std::string & canonicalName,
319  carma::monitor::tagIDType nonSubsysTagId,
320  Name2InfoMap::const_iterator & hint ) const;
321 
322  size_t estimateMemHoldingLock() const;
323 
324 
325  // subsysnames_ is initialized in subsystemnames.cc
326  static const char * const subsysnames_[ ];
327 
328  // subsysnamesCount_ is initialized in subsystemnames.cc
329  static const int subsysnamesCount_;
330 
331 
332  mutable util::PthreadRWLock guard_;
333 
334  OriginType masterOrigin_;
335  ::std::string sha1sum_;
336 
337  Name2InfoMap name2Info_;
338  Id2NameMap id2Name_;
339  Name2IdMap subsys2NextOtfId_;
340 
341  bool logNotableOtfCreations_;
342  bool haveUnloggedNotableOtfCreations_;
343 };
344 
345 
346 } // namespace carma::dbms
347 } // namespace carma
348 
349 
350 inline carma::monitor::tagIDType
351 carma::dbms::TagIDAuthority::composeTagID( const unsigned short subsystemId,
352  const unsigned short monitorPointId )
353 {
354  return ((static_cast< carma::monitor::tagIDType >( subsystemId ) << 16) |
355  static_cast< carma::monitor::tagIDType >( monitorPointId ));
356 }
357 
358 inline unsigned short
359 carma::dbms::TagIDAuthority::getSubsystemID( const carma::monitor::tagIDType tagId )
360 {
361  return (tagId >> 16);
362 }
363 
364 
365 inline unsigned short
366 carma::dbms::TagIDAuthority::getPointID( const carma::monitor::tagIDType tagId )
367 {
368  return (tagId & 0x0000FFFF);
369 }
370 
371 inline unsigned short
372 carma::dbms::TagIDAuthority::maxPossiblePointID( const unsigned short subsystemId )
373 {
374  return 0x0000FFFF;
375 }
376 
377 
378 #endif // CARMA_DBMS_TAGIDAUTHORITY_H
Class used for configuring a DBConnection.
::std::string getSubsystemName(const ::std::string &canonicalName)
return the subsystem name from a monitor component&#39;s canonical name the singleton does not need to ex...
A simple wrapper class that makes use of ::pthread_rwlock_t easier in a C++ world.
Definition: PthreadRWLock.h:46
::std::string tagIDNameMapSha1Sum() const
get the sha1 sum of the tagID to canonical name map.
::std::string lookupName(carma::monitor::tagIDType tagId) const
return the full hierarchical name for for an ID
carma::monitor::tagIDType lookupID(const ::std::string &name) const
static int getSubsystemCount()
return the number of subsystems, the singleton object does not need to be instantiated ...
static TagIDAuthority & getAuthority()
get the singleton instance of the TagIDAuthority.
static carma::monitor::tagIDType composeTagID(unsigned short subsystemId, unsigned short monitorPointId)
Crafts a tagID given the subsystemID and the ID of the monitor point (pointID) and returns the tagID...
static unsigned short getPointID(carma::monitor::tagIDType tagId)
Converse of composeTagID.
A lookup class for mapping names and IDs.
static unsigned short getSubsystemID(carma::monitor::tagIDType tagId)
Converse of composeTagID.
type definitions for monitor system
static void configureAuthority(const bool useDb, const ::std::string &dbConfFile, const bool useConfFileIfDBIsDown=true, const ::std::string &canonicalNamesToTagIDsConfFile=getDefaultCanonicalNamesToTagIDsConfFile())
configure the singleton, this should be called before the initial call to getAuthority; the singleton...
static void closeAuthority()
indicate that one&#39;s use of the authority is complete.
bool retrieveTagInfo(const std::string &canonicalName, NameMapInfo &nameMapInfo) const
Retrieve tag id and origin information for a given canonical name.
unsigned short lookupSubsystemID(const ::std::string &name) const
return the subsystem ID for a given subsystem name.