Bayeux  3.4.1
Core Foundation library for SuperNEMO
urn_db_service.h
Go to the documentation of this file.
1 /* Author(s): Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2017-03-06
4  * Last modified: 2017-03-31
5  *
6  * License:
7  *
8  * Copyright (C) 2017 Francois Mauger <mauger@lpccaen.in2p3.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or (at
13  * your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License 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 Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  *
25  * Description:
26  *
27  * URN information database.
28  *
29  */
30 
31 #ifndef DATATOOLS_URN_DB_SERVICE_H
32 #define DATATOOLS_URN_DB_SERVICE_H
33 
34 // Standard Library:
35 #include <string>
36 #include <map>
37 #include <set>
38 
39 // Third party:
40 // - Boost:
41 #include <boost/graph/graph_traits.hpp>
42 #include <boost/graph/adjacency_list.hpp>
43 
44 // This project:
45 #include <datatools/properties.h>
46 #include <datatools/base_service.h>
47 #include <datatools/bit_mask.h>
48 #include <datatools/urn_info.h>
49 
50 namespace datatools {
51 
52  class dependency_graph;
53 
104  /*
141  */
144  {
145  public:
146 
149  {
151  explicit dependee_db_entry(const urn_db_service & db_);
152  void make(const urn_db_service & db_);
153  const urn_db_service & get_db() const;
154  private:
155  const urn_db_service * _db_ = nullptr;
156  };
157 
159  typedef std::map<std::string, dependee_db_entry> dependee_db_dict_type;
160 
162  struct urn_record
163  {
164  bool is_local() const;
165  bool is_mounted() const;
166  bool is_valid() const;
167  void make_local(const std::string & urn_,
168  const std::string & category_,
169  const std::string & description_);
170  void make_mounted(const urn_db_service & mounted_db_,
171  const std::string & mounted_urn_);
172  const urn_info & get() const;
173  private:
174  urn_info & _grab_();
175  public:
177  const urn_db_service * _mounted_db_ref_ = nullptr;
178  const urn_info * _mounted_uinfo_ = nullptr;
180  };
181 
183  typedef std::map<std::string, urn_record> urn_record_dict_type;
184 
187  {
188  std::size_t get_number_of_links() const;
189  bool is_linked_to_urn(const std::string & urn_) const;
190  void add_link_to_urn(const std::string & urn_);
191  void remove_link_to_urn(const std::string & urn_);
192  void clear();
193  const std::set<std::string> & get_urns() const;
194  private:
195  std::set<std::string> _urns_;
196  };
197 
199  typedef std::map<std::string, backward_links> backward_links_dict_type;
200 
202  urn_db_service();
203 
205  virtual ~urn_db_service();
206 
208  bool is_allow_mounted() const;
209 
211  void set_allow_mounted(bool m_);
212 
214  bool has_explicit_allowed_categories() const;
215 
217  bool has_allowed_category(const std::string & category_) const;
218 
220  void add_allowed_category(const std::string & category_);
221 
223  void remove_allowed_category(const std::string & category_);
224 
226  const std::set<std::string> & get_allowed_categories() const;
227 
229  bool check_category(const std::string & category_) const;
230 
232  bool has_allowed_link_topic(const std::string & link_topic_) const;
233 
236 
238  void add_allowed_link_topic(const std::string & link_topic_);
239 
241  void remove_allowed_link_topic(const std::string & link_topic_);
242 
244  const std::set<std::string> & get_allowed_link_topics() const;
245 
247  bool check_link_topic(const std::string & link_topic_) const;
248 
250  void connect_db(const urn_db_service & db_);
251 
253  bool has_dependees() const;
254 
256  std::set<std::string> get_dependees() const;
257 
259  virtual bool is_initialized() const;
260 
262  virtual int initialize(const datatools::properties & config_,
263  datatools::service_dict_type & services_);
264 
266  virtual int reset();
267 
269  virtual void tree_dump(std::ostream& out = std::clog,
270  const std::string & title = "",
271  const std::string & indent = "",
272  bool inherit = false) const;
273 
275  void load(const std::string & filename_);
276 
278  void load_csv(const std::string & filename_);
279 
281  bool is_mounted(const std::string & urn_) const;
282 
284  bool is_local(const std::string & urn_) const;
285 
287  static bool parse_mount_rule(const std::string & rule_,
288  std::string & external_db_,
289  std::string & mounted_urn_);
290 
291  // //! Parse mount rule
292  // static bool parse_mount_rule(const std::string & rule_,
293  // std::string & external_db_,
294  // std::set<std::string> & mounted_urns_);
295 
297  void add_mounted(const std::string & external_db_,
298  const std::string & mounted_urn_);
299 
301  const urn_info & add(const std::string & urn_,
302  const std::string & category_,
303  const std::string & description_ = "");
304 
306  void remove(const std::string & urn_);
307 
313  bool add_link(const std::string & urn_from_,
314  const std::string & urn_to_,
315  const std::string & forward_topic_,
316  std::string & errmsg_);
317 
319  void add_link(const std::string & urn_from_,
320  const std::string & urn_to_,
321  const std::string & forward_topic_);
322 
324  bool remove_link(const std::string & urn_from_,
325  const std::string & urn_to_,
326  std::string & errmsg_);
327 
329  void remove_link(const std::string & urn_from_,
330  const std::string & urn_to_);
331 
333  void remove_all_links(const std::string & urn_from_);
334 
336  bool is_linked_to(const std::string & urn_,
337  const std::string & other_urn_,
338  const std::string & topic_ = "") const;
339 
341  bool has_links(const std::string & urn_) const;
342 
344  bool has(const std::string & urn_) const;
345 
347  const urn_info & get(const std::string &) const;
348 
350  std::size_t get_number_of_urn_records() const;
351 
359  };
360 
362  void build_list_of_urns(std::vector<std::string> & urns_,
363  const uint32_t flags_) const;
364 
366  bool has_backward_links(const std::string & urn_) const;
367 
369  bool is_removable(const std::string & urn_) const;
370 
372  bool is_locked() const;
373 
375  void lock();
376 
378  void unlock();
379 
381  void kernel_push(const std::string & name_ = "");
382 
384  void kernel_pop();
385 
387  bool is_kernel_pushed() const;
388 
391  {
393  void add_topic(const std::string & topic_);
394  void make_deps(dependency_graph & deps_) const;
395  private:
396  void _process_node_components_(const urn_record & urec_,
397  dependency_graph & deps_,
398  std::set<std::string> & processed_) const;
399 
400  private:
401 
402  const urn_db_service & _db_;
403  logger::priority _logging_;
404  std::set<std::string> _topics_;
405  };
406 
407  private:
408 
410  urn_info & _grab_(const std::string &);
411 
412  void _init_();
413 
414  void _reset_();
415 
416  void _at_lock_();
417 
418  void _at_unlock_();
419 
420  void _add_backward_link_(const std::string & urn_to_,
421  const std::string & urn_from_);
422 
423  void _remove_backward_link_(const std::string & urn_to_,
424  const std::string & urn_from_);
425 
426  void _process_links_(const std::string & urn_from_);
427 
428  private:
429 
430  // Management:
431  bool _initialized_ = false;
432  bool _locked_ = false;
433 
434  // Configuration:
435  std::set<std::string> _allowed_categories_;
436  std::set<std::string> _allowed_link_topics_;
437  bool _allow_mounted_ = false;
438  dependee_db_dict_type _dependee_dbs_;
439 
440  // Private data:
441  urn_record_dict_type _urn_records_;
442  backward_links_dict_type _backward_links_;
443 
445 
446  };
447 
448 } // end of namespace datatools
449 
450 /***************
451  * OCD support *
452  ***************/
453 #include <datatools/ocd_macros.h>
455 
456 #endif // DATATOOLS_URN_DB_SERVICE_H
457 
458 // Local Variables: --
459 // mode: c++ --
460 // c-file-style: "gnu" --
461 // tab-width: 2 --
462 // End: --
void kernel_pop()
Remove from system.
std::map< std::string, backward_links > backward_links_dict_type
Dictionary of backward links.
Definition: urn_db_service.h:199
void make(const urn_db_service &db_)
friend urn_db_service
Definition: urn_db_service.h:179
bool is_mounted(const std::string &urn_) const
Check if an URN is mounted.
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
bool is_linked_to(const std::string &urn_, const std::string &other_urn_, const std::string &topic_="") const
Check if an URN record is linked to another one through a certain topic.
bool has_backward_links(const std::string &urn_) const
Check if a URN record has backward links.
urn_select_flag
URN record selection flags.
Definition: urn_db_service.h:353
const urn_db_service * _mounted_db_ref_
Definition: urn_db_service.h:177
Definition: urn_db_service.h:356
void add_allowed_category(const std::string &category_)
Add an allowed category.
bool is_removable(const std::string &urn_) const
Check if a URN record can be removed.
virtual void tree_dump(std::ostream &out=std::clog, const std::string &title="", const std::string &indent="", bool inherit=false) const
Smart print.
A fatal error. The application will most likely terminate. This is the highest priority.
Definition: logger.h:85
static const uint32_t bit01
Definition: bit_mask.h:28
void build_list_of_urns(std::vector< std::string > &urns_, const uint32_t flags_) const
Build the list of registered URNs.
void make_mounted(const urn_db_service &mounted_db_, const std::string &mounted_urn_)
std::map< std::string, service_entry_ptr > service_dict_type
Type alias for a flat dictionary of service entries.
Definition: service_tools.h:196
bool check_category(const std::string &category_) const
Check if a category is allowed.
void set_allow_mounted(bool m_)
Set allowed mounted flag.
The base service class.
Definition: base_service.h:68
static const uint32_t bit03
Definition: bit_mask.h:30
urn_info _uinfo_
Definition: urn_db_service.h:176
Populated a dependency graph.
Definition: urn_db_service.h:390
void remove_all_links(const std::string &urn_from_)
Remove all links from a URN record.
std::map< std::string, urn_record > urn_record_dict_type
URN record dictionary.
Definition: urn_db_service.h:183
bool add_link(const std::string &urn_from_, const std::string &urn_to_, const std::string &forward_topic_, std::string &errmsg_)
bool is_allow_mounted() const
Check if mounted URN are allowed.
virtual int initialize(const datatools::properties &config_, datatools::service_dict_type &services_)
Initialize the service from a list of properties.
static const uint32_t bit04
Definition: bit_mask.h:31
void remove(const std::string &urn_)
Remove an URN info.
virtual bool is_initialized() const
Check initialization status.
#define DATATOOLS_SERVICE_REGISTRATION_INTERFACE(SERVICE_CLASS_NAME)
Definition: base_service.h:152
void load_csv(const std::string &filename_)
Load a list of URN records without components from a CSV file.
const std::set< std::string > & get_allowed_categories() const
Return allowed categories.
Definition: urn_db_service.h:354
bool is_local(const std::string &urn_) const
Check if an URN is local.
void make_deps(dependency_graph &deps_) const
bool is_kernel_pushed() const
Check if the service is pushed (registered) in the kernel.
void connect_db(const urn_db_service &db_)
Connect to an external URN database dependee service.
Definition: urn_db_service.h:357
const urn_db_service & get_db() const
void add_allowed_link_topic(const std::string &link_topic_)
Add an allowed link topic.
void lock()
Lock the service.
bool has_links(const std::string &urn_) const
Check if an URN record is linked to some other ones (forward links only)
std::map< std::string, dependee_db_entry > dependee_db_dict_type
Dictionary of external dependee URN database services.
Definition: urn_db_service.h:159
bool has_explicit_allowed_link_topics() const
Check if explicit allowed link topics are defined.
DOCD_CLASS_DECLARATION(my::algo)
Declaration of the OCD support for the my::algo class.
External dependee URN database service.
Definition: urn_db_service.h:148
static bool parse_mount_rule(const std::string &rule_, std::string &external_db_, std::string &mounted_urn_)
Parse mount rule.
dependee_db_entry()
const urn_info & add(const std::string &urn_, const std::string &category_, const std::string &description_="")
Add a new URN info.
static const uint32_t bit02
Definition: bit_mask.h:29
Dependency graph.
Definition: dependency_graph.h:64
Definition: urn_db_service.h:358
A service with a dictionary of URN informations.
Definition: urn_db_service.h:142
void kernel_push(const std::string &name_="")
Add to system.
std::set< std::string > get_dependees() const
Return the list of dependees.
void load(const std::string &filename_)
Load an URN info definition file.
bool is_locked() const
Check if the service is locked.
virtual int reset()
Reset.
dependency_graph_builder(const urn_db_service &, const logger::priority &=logger::PRIO_FATAL)
const urn_info & get() const
void make_local(const std::string &urn_, const std::string &category_, const std::string &description_)
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
std::size_t get_number_of_urn_records() const
Return the number of URN records.
void remove_allowed_category(const std::string &category_)
Remove an allowed category.
bool has_dependees() const
Check if the db has dependees.
urn_db_service()
Default constructor.
URN record.
Definition: urn_db_service.h:162
void add_mounted(const std::string &external_db_, const std::string &mounted_urn_)
Add a mounted URN.
bool has_allowed_link_topic(const std::string &link_topic_) const
Check if a link topic is a allowed.
Definition: urn_db_service.h:355
bool check_link_topic(const std::string &link_topic_) const
Check if a link topic is allowed.
virtual ~urn_db_service()
Destructor.
static const uint32_t bit00
Definition: bit_mask.h:27
bool has_allowed_category(const std::string &category_) const
Check if a category is a allowed.
bool has_explicit_allowed_categories() const
Check if explicit allowed categories are defined.
void unlock()
Unlock the service.
bool has(const std::string &urn_) const
Check if URN info exists.
A class documenting an object addressed by its unique URN and linked to other objects (also addressed...
Definition: urn_info.h:71
const urn_info * _mounted_uinfo_
Definition: urn_db_service.h:178
void remove_allowed_link_topic(const std::string &link_topic_)
Remove an allowed link topic.
const urn_info & get(const std::string &) const
Return the URN info.
bool remove_link(const std::string &urn_from_, const std::string &urn_to_, std::string &errmsg_)
Remove a link between two URN info records.
const std::set< std::string > & get_allowed_link_topics() const
Return allowed link topics.
A dictionary of arbitrary properties.
Definition: properties.h:125