Fawkes API  Fawkes Development Version
robot_memory.h
1 /***************************************************************************
2  * robot_memory.h - Class for storing and querying information in the RobotMemory
3  *
4  * Created: Aug 23, 2016 1:34:32 PM 2016
5  * Copyright 2016 Frederik Zwilling
6  * 2017 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 #ifndef _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_
22 #define _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_
23 
24 #include "computables/computables_manager.h"
25 #include "event_trigger_manager.h"
26 
27 #include <aspect/blackboard.h>
28 #include <aspect/clock.h>
29 #include <aspect/configurable.h>
30 #include <aspect/logging.h>
31 #include <core/threading/mutex.h>
32 #include <plugins/mongodb/aspect/mongodb_conncreator.h>
33 
34 #include <bsoncxx/json.hpp>
35 #include <memory>
36 #include <utility>
37 #include <vector>
38 
39 namespace fawkes {
40 class RobotMemoryInterface;
41 #ifdef USE_TIMETRACKER
42 class TimeTracker;
43 #endif
44 } // namespace fawkes
45 
46 class RobotMemory
47 {
48  /// Friend the RobotMemoryThread so that only it can access the loop and init functions
49  friend class RobotMemoryThread;
50 
51 public:
55  fawkes::MongoDBConnCreator *mongo_connection_manager,
57  virtual ~RobotMemory();
58 
59  //robot memory functions
60  mongocxx::cursor query(bsoncxx::document::view query,
61  const std::string & collection_name = "",
62  mongocxx::options::find query_options = mongocxx::options::find());
63  bsoncxx::document::value aggregate(const std::vector<bsoncxx::document::view> &pipeline,
64  const std::string & collection = "");
65  // TODO fix int return codes, should be booleans
66  int insert(bsoncxx::document::view, const std::string &collection = "");
67  int insert(std::vector<bsoncxx::document::view> v_obj, const std::string &collection = "");
68  int insert(const std::string &obj_str, const std::string &collection = "");
69  int update(const bsoncxx::document::view &query,
70  const bsoncxx::document::view &update,
71  const std::string & collection = "",
72  bool upsert = false);
73  int update(const bsoncxx::document::view &query,
74  const std::string & update_str,
75  const std::string & collection = "",
76  bool upsert = false);
77  bsoncxx::document::value find_one_and_update(const bsoncxx::document::view &filter,
78  const bsoncxx::document::view &update,
79  const std::string & collection,
80  bool upsert = false,
81  bool return_new = true);
82  int remove(const bsoncxx::document::view &query, const std::string &collection = "");
83  bsoncxx::document::value mapreduce(const bsoncxx::document::view &query,
84  const std::string & collection,
85  const std::string & js_map_fun,
86  const std::string & js_reduce_fun);
87  mongocxx::cursor aggregate(bsoncxx::document::view pipeline, const std::string &collection = "");
88  int drop_collection(const std::string &collection);
89  int clear_memory();
90  int restore_collection(const std::string &collection,
91  const std::string &directory = "@CONFDIR@/robot-memory");
92  int dump_collection(const std::string &collection,
93  const std::string &directory = "@CONFDIR@/robot-memory");
94  int create_index(bsoncxx::document::view keys,
95  const std::string & collection = "",
96  bool unique = false);
97 
98  //bool semaphore_create(const std::string& name, unsigned int value);
99  //bool semaphore_acquire(const std::string& name, unsigned int v = 1);
100  //bool semaphore_release(const std::string& name, unsigned int v = 1);
101  bool mutex_setup_ttl(float max_age_sec);
102  bool mutex_create(const std::string &name);
103  bool mutex_destroy(const std::string &name);
104  bool mutex_try_lock(const std::string &name, bool force = false);
105  bool mutex_try_lock(const std::string &name, const std::string &identity, bool force = false);
106  bool mutex_unlock(const std::string &name, const std::string &identity);
107  bool mutex_renew_lock(const std::string &name, const std::string &identity);
108  bool mutex_expire_locks(float max_age_sec);
109 
110  /**
111  * Register a trigger to be notified when the robot memory is updated and the updated document matches the query
112  * @param query Query the updated document has to match
113  * @param collection db.collection to use
114  * @param callback Callback function (e.g. &Class::callback)
115  * @param _obj Pointer to class the callback is a function of (usaually this)
116  * @return Trigger object pointer, save it to remove the trigger later
117  */
118  template <typename T>
120  register_trigger(const bsoncxx::document::view &query,
121  const std::string & collection,
122  void (T::*callback)(const bsoncxx::document::view &),
123  T *_obj)
124  {
125  return trigger_manager_->register_trigger(query, collection, callback, _obj);
126  }
127  /**
128  * Register a trigger to be notified when the robot memory is updated and the updated document matches the query
129  * @param query_str Query as JSON string
130  * @param collection db.collection to use
131  * @param callback Callback function (e.g. &Class::callback)
132  * @param _obj Pointer to class the callback is a function of (usaually this)
133  * @return Trigger object pointer, save it to remove the trigger later
134  */
135  template <typename T>
137  register_trigger(const std::string &query_str,
138  const std::string &collection,
139  void (T::*callback)(bsoncxx::document::value),
140  T *_obj)
141  {
142  return register_trigger(bsoncxx::from_json(query_str), collection, callback, _obj);
143  }
144  void remove_trigger(EventTrigger *trigger);
145 
146  /**
147  * Registers a Computable which provides information in the robot memory that is computed on demand.
148  *
149  * @param query_to_compute Query describing what the function computes. Yor computable is called when an new query matches the key value fields in the identifiyer.
150  * @param collection db.collection to fill with computed information
151  * @param compute_func Callback function that computes the information and retruns a list of computed documents
152  * @param obj Pointer to class the callback is a function of (usaually this)
153  * @param caching_time How long should computed results for a query be cached and be used for identical queries in that time?
154  * @param priority Computable priority ordering the evaluation
155  * @return Computable Object pointer used for removing it
156  */
157  template <typename T>
159  register_computable(bsoncxx::document::value &&query_to_compute,
160  const std::string & collection,
161  std::list<bsoncxx::document::value> (
162  T::*compute_func)(const bsoncxx::document::view &, const std::string &),
163  T * obj,
164  double caching_time = 0.0,
165  int priority = 0)
166  {
167  return computables_manager_->register_computable(
168  std::move(query_to_compute), collection, compute_func, obj, caching_time, priority);
169  }
170  void remove_computable(Computable *computable);
171 
172 private:
173  fawkes::MongoDBConnCreator *mongo_connection_manager_;
174  mongocxx::client * mongodb_client_local_;
175  mongocxx::client * mongodb_client_distributed_;
176  bool distributed_;
177  fawkes::Configuration * config_;
178  fawkes::Logger * logger_;
179  fawkes::Clock * clock_;
180  fawkes::BlackBoard * blackboard_;
181 
182  const char * name_ = "RobotMemory";
183  std::string database_name_;
184  std::string default_collection_;
185  bool debug_;
186  fawkes::Mutex mutex_;
187  fawkes::RobotMemoryInterface *rm_if_;
188  EventTriggerManager * trigger_manager_;
189  ComputablesManager * computables_manager_;
190  std::vector<std::string> distributed_dbs_;
191 
192  unsigned int cfg_startup_grace_period_;
193  std::string cfg_coord_database_;
194  std::string cfg_coord_mutex_collection_;
195 
196  void init();
197  void loop();
198 
199  // TODO make log level an enum (if we need it at all)
200  void log(const std::string &what, const std::string &level = "info");
201  void log_deb(const std::string &what, const std::string &level = "info");
202  void log(const bsoncxx::document::view &query,
203  const std::string & what,
204  const std::string & level = "info");
205  void log_deb(const bsoncxx::document::view &query,
206  const std::string & what,
207  const std::string & level = "info");
208 
209  bool is_distributed_database(const std::string &dbcollection);
210  mongocxx::client * get_mongodb_client(const std::string &collection);
211  mongocxx::collection get_collection(const std::string &dbcollection);
212 
213 #ifdef USE_TIMETRACKER
214  fawkes::TimeTracker *tt_;
215  unsigned int tt_loopcount_;
216  unsigned int ttc_events_;
217  unsigned int ttc_cleanup_;
218 #endif
219 };
220 
221 #endif /* FAWKES_SRC_PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_ */
RobotMemory::register_trigger
EventTrigger * register_trigger(const bsoncxx::document::view &query, const std::string &collection, void(T::*callback)(const bsoncxx::document::view &), T *_obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Definition: robot_memory.h:119
ComputablesManager::register_computable
Computable * register_computable(bsoncxx::document::value &&query_to_compute, const std::string &collection, std::list< bsoncxx::document::value >(T::*compute_func)(const bsoncxx::document::view &, const std::string &), T *obj, double caching_time=0.0, int priority=0)
Registers a Computable which provides information in the robot memory that is computed on demand.
Definition: computables_manager.h:67
RobotMemory::create_index
int create_index(bsoncxx::document::view keys, const std::string &collection="", bool unique=false)
Create an index on a collection.
Definition: robot_memory.cpp:300
RobotMemory::mutex_expire_locks
bool mutex_expire_locks(float max_age_sec)
Expire old locks on mutexes.
Definition: robot_memory.cpp:1103
RobotMemory::remove_trigger
void remove_trigger(EventTrigger *trigger)
Remove a previously registered trigger.
Definition: robot_memory.cpp:773
RobotMemory::insert
int insert(bsoncxx::document::view, const std::string &collection="")
Inserts a document into the robot memory.
Definition: robot_memory.cpp:275
fawkes::Mutex
Definition: mutex.h:36
ComputablesManager
Definition: computables_manager.h:45
fawkes::MongoDBConnCreator
Definition: mongodb_conncreator.h:40
RobotMemory
Definition: robot_memory.h:45
fawkes::BlackBoard
Definition: blackboard.h:48
EventTrigger
Definition: event_trigger.h:30
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
fawkes::ClockAspect::clock
Clock * clock
Definition: clock.h:53
RobotMemory::mutex_renew_lock
bool mutex_renew_lock(const std::string &name, const std::string &identity)
Renew a mutex.
Definition: robot_memory.cpp:1017
RobotMemory::aggregate
bsoncxx::document::value aggregate(const std::vector< bsoncxx::document::view > &pipeline, const std::string &collection="")
Aggregation call on the robot memory.
Definition: robot_memory.cpp:232
fawkes::Configuration
Definition: config.h:68
RobotMemory::RobotMemory
RobotMemory(fawkes::Configuration *config, fawkes::Logger *logger, fawkes::Clock *clock, fawkes::MongoDBConnCreator *mongo_connection_manager, fawkes::BlackBoard *blackboard)
Robot Memory Constructor with objects of the thread.
Definition: robot_memory.cpp:66
RobotMemoryThread::init
virtual void init()
Initialize the thread.
Definition: robot_memory_thread.cpp:59
RobotMemory::remove_computable
void remove_computable(Computable *computable)
Remove previously registered computable.
Definition: robot_memory.cpp:783
RobotMemory::restore_collection
int restore_collection(const std::string &collection, const std::string &directory="@CONFDIR@/robot-memory")
Restore a previously dumped collection from a directory.
Definition: robot_memory.cpp:585
RobotMemoryThread::loop
virtual void loop()
Code to execute in the thread.
Definition: robot_memory_thread.cpp:109
RobotMemory::mapreduce
bsoncxx::document::value mapreduce(const bsoncxx::document::view &query, const std::string &collection, const std::string &js_map_fun, const std::string &js_reduce_fun)
Performs a MapReduce operation on the robot memory (https://docs.mongodb.com/manual/core/map-reduce/)
Definition: robot_memory.cpp:504
RobotMemory::mutex_create
bool mutex_create(const std::string &name)
Explicitly create a mutex.
Definition: robot_memory.cpp:797
fawkes::LoggingAspect::logger
Logger * logger
Definition: logging.h:50
fawkes::Logger
Definition: logger.h:40
RobotMemory::register_computable
Computable * register_computable(bsoncxx::document::value &&query_to_compute, const std::string &collection, std::list< bsoncxx::document::value >(T::*compute_func)(const bsoncxx::document::view &, const std::string &), T *obj, double caching_time=0.0, int priority=0)
Registers a Computable which provides information in the robot memory that is computed on demand.
Definition: robot_memory.h:158
fawkes
RobotMemory::mutex_try_lock
bool mutex_try_lock(const std::string &name, bool force=false)
Try to acquire a lock for a mutex.
Definition: robot_memory.cpp:950
EventTriggerManager
Definition: event_trigger_manager.h:44
RobotMemory::remove
int remove(const bsoncxx::document::view &query, const std::string &collection="")
Remove documents from the robot memory.
Definition: robot_memory.cpp:477
RobotMemory::drop_collection
int drop_collection(const std::string &collection)
Drop (= remove) a whole collection and all documents inside it.
Definition: robot_memory.cpp:554
RobotMemory::clear_memory
int clear_memory()
Remove the whole database of the robot memory and all documents inside.
Definition: robot_memory.cpp:568
fawkes::ConfigurableAspect::config
Configuration * config
Definition: configurable.h:50
RobotMemory::mutex_setup_ttl
bool mutex_setup_ttl(float max_age_sec)
Setup time-to-live index for mutexes.
Definition: robot_memory.cpp:1076
EventTriggerManager::register_trigger
EventTrigger * register_trigger(const bsoncxx::document::view &query, std::string dbcollection, void(T::*callback)(const bsoncxx::document::view &), T *obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Definition: event_trigger_manager.h:65
RobotMemory::mutex_destroy
bool mutex_destroy(const std::string &name)
Destroy a mutex.
Definition: robot_memory.cpp:827
RobotMemory::find_one_and_update
bsoncxx::document::value find_one_and_update(const bsoncxx::document::view &filter, const bsoncxx::document::view &update, const std::string &collection, bool upsert=false, bool return_new=true)
Atomically update and retrieve document.
Definition: robot_memory.cpp:434
fawkes::TimeTracker
Definition: tracker.h:40
RobotMemory::query
mongocxx::cursor query(bsoncxx::document::view query, const std::string &collection_name="", mongocxx::options::find query_options=mongocxx::options::find())
Query information from the robot memory.
Definition: robot_memory.cpp:201
fawkes::BlackBoardAspect::blackboard
BlackBoard * blackboard
Definition: blackboard.h:47
Computable
Definition: computable.h:30
RobotMemory::dump_collection
int dump_collection(const std::string &collection, const std::string &directory="@CONFDIR@/robot-memory")
Dump (= save) a collection to the filesystem to restore it later.
Definition: robot_memory.cpp:637
RobotMemory::update
int update(const bsoncxx::document::view &query, const bsoncxx::document::view &update, const std::string &collection="", bool upsert=false)
Updates documents in the robot memory.
Definition: robot_memory.cpp:379
RobotMemoryThread
Definition: robot_memory_thread.h:51
RobotMemory::mutex_unlock
bool mutex_unlock(const std::string &name, const std::string &identity)
Release lock on mutex.
Definition: robot_memory.cpp:961
fawkes::Clock
Definition: clock.h:39