21 #ifndef _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_ 22 #define _PLUGINS_ROBOT_MEMORY_ROBOT_MEMORY_H_ 24 #include "computables/computables_manager.h" 25 #include "event_trigger_manager.h" 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> 38 class RobotMemoryInterface;
46 typedef std::unique_ptr<mongo::DBClientCursor> QResCursor;
62 QResCursor
query(mongo::Query
query,
const std::string &collection =
"");
63 mongo::BSONObj
aggregate(
const std::vector<mongo::BSONObj> &pipeline,
64 const std::string & collection =
"");
65 int insert(mongo::BSONObj obj,
const std::string &collection =
"");
66 int insert(std::vector<mongo::BSONObj> v_obj,
const std::string &collection =
"");
67 int insert(
const std::string &obj_str,
const std::string &collection =
"");
70 const std::string &collection =
"",
73 const std::string &update_str,
74 const std::string &collection =
"",
77 const mongo::BSONObj &
update,
78 const std::string & collection,
80 bool return_new =
true);
81 int remove(mongo::Query
query,
const std::string &collection =
"");
83 const std::string &collection,
84 const std::string &js_map_fun,
85 const std::string &js_reduce_fun);
86 QResCursor
aggregate(mongo::BSONObj pipeline,
const std::string &collection =
"");
90 const std::string &directory =
"@CONFDIR@/robot-memory");
92 const std::string &directory =
"@CONFDIR@/robot-memory");
93 int create_index(mongo::BSONObj keys,
const std::string &collection =
"",
bool unique =
false);
102 bool mutex_try_lock(
const std::string &
name,
const std::string &identity,
bool force =
false);
115 template <
typename T>
118 const std::string &collection,
119 void (T::*callback)(mongo::BSONObj),
132 template <
typename T>
135 const std::string &collection,
136 void (T::*callback)(mongo::BSONObj),
139 return register_trigger(mongo::fromjson(query_str), collection, callback, _obj);
154 template <
typename T>
157 const std::string & collection,
158 std::list<mongo::BSONObj> (T::*compute_func)(
const mongo::BSONObj &,
159 const std::string &),
161 double caching_time = 0.0,
165 query_to_compute, collection, compute_func, obj, caching_time, priority);
171 mongo::DBClientBase * mongodb_client_local_;
172 mongo::DBClientBase * mongodb_client_distributed_;
179 const char * name_ =
"RobotMemory";
180 std::string database_name_;
181 std::string default_collection_;
184 fawkes::RobotMemoryInterface *rm_if_;
187 std::vector<std::string> distributed_dbs_;
189 unsigned int cfg_startup_grace_period_;
190 std::string cfg_coord_database_;
191 std::string cfg_coord_mutex_collection_;
196 void log(
const std::string &what,
const std::string &level =
"info");
197 void log_deb(
const std::string &what,
const std::string &level =
"info");
198 void log(
const mongo::Query &
query,
const std::string &what,
const std::string &level =
"info");
199 void log(
const mongo::BSONObj &obj,
const std::string &what,
const std::string &level =
"info");
201 log_deb(
const mongo::Query &
query,
const std::string &what,
const std::string &level =
"info");
203 log_deb(
const mongo::BSONObj &obj,
const std::string &what,
const std::string &level =
"info");
205 void set_fields(mongo::BSONObj &obj,
const std::string &what);
206 void set_fields(mongo::Query &q,
const std::string &what);
207 void remove_field(mongo::Query &q,
const std::string &what);
209 mongo::DBClientBase *get_mongodb_client(
const std::string &collection);
Computable * register_computable(const mongo::Query &query_to_compute, const std::string &collection, std::list< mongo::BSONObj >(T::*compute_func)(const mongo::BSONObj &, 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.
QResCursor query(mongo::Query query, const std::string &collection="")
Query information from the robot memory.
Manager to realize triggers on events in the robot memory.
This class manages registering computables and can check if any computables are invoced by a query.
bool mutex_unlock(const std::string &name, const std::string &identity)
Release lock on mutex.
int create_index(mongo::BSONObj keys, const std::string &collection="", bool unique=false)
Create an index on a collection.
int remove(mongo::Query query, const std::string &collection="")
Remove documents from the robot memory.
Fawkes library namespace.
int update(mongo::Query query, mongo::BSONObj update, const std::string &collection="", bool upsert=false)
Updates documents in the robot memory.
mongo::BSONObj aggregate(const std::vector< mongo::BSONObj > &pipeline, const std::string &collection="")
Aggregation call on the robot memory.
This is supposed to be the central clock in Fawkes.
Class holding information for a single computable this class also enhances computed documents by addi...
mongo::BSONObj find_one_and_update(const mongo::BSONObj &filter, const mongo::BSONObj &update, const std::string &collection, bool upsert=false, bool return_new=true)
Atomically update and retrieve document.
void remove_computable(Computable *computable)
Remove previously registered computable.
void remove_trigger(EventTrigger *trigger)
Remove a previously registered trigger.
int restore_collection(const std::string &collection, const std::string &directory="@CONFDIR@/robot-memory")
Restore a previously dumped collection from a directory.
Class holding all information about an EventTrigger.
Logger * logger
This is the Logger member used to access the logger.
mongo::BSONObj mapreduce(mongo::Query 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/)
Clock * clock
By means of this member access to the clock is given.
virtual void init()
Initialize the thread.
int insert(mongo::BSONObj obj, const std::string &collection="")
Inserts a document into the robot memory.
int drop_collection(const std::string &collection)
Drop (= remove) a whole collection and all documents inside it.
bool mutex_try_lock(const std::string &name, bool force=false)
Try to acquire a lock for a mutex.
bool mutex_renew_lock(const std::string &name, const std::string &identity)
Renew a mutex.
Thread that provides a robot memory with MongoDB.
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.
bool mutex_setup_ttl(float max_age_sec)
Setup time-to-live index for mutexes.
virtual void loop()
Code to execute in the thread.
Computable * register_computable(const mongo::Query &query_to_compute, const std::string &collection, std::list< mongo::BSONObj >(T::*compute_func)(const mongo::BSONObj &, 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.
const char * name() const
Get name of thread.
Interface for a MongoDB connection creator.
bool mutex_create(const std::string &name)
Explicitly create a mutex.
EventTrigger * register_trigger(mongo::Query query, const std::string &collection, void(T::*callback)(mongo::BSONObj), T *_obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
bool mutex_destroy(const std::string &name)
Destroy a mutex.
EventTrigger * register_trigger(const std::string &query_str, const std::string &collection, void(T::*callback)(mongo::BSONObj), T *_obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
int clear_memory()
Remove the whole database of the robot memory and all documents inside.
EventTrigger * register_trigger(mongo::Query query, std::string collection, void(T::*callback)(mongo::BSONObj), T *obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Access to the robot memory based on mongodb.
The BlackBoard abstract class.
Mutex mutual exclusion lock.
bool mutex_expire_locks(float max_age_sec)
Expire old locks on mutexes.
Configuration * config
This is the Configuration member used to access the configuration.
Interface for configuration handling.
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.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.