22 #include "computables_manager.h" 24 #include <core/exception.h> 25 #include <plugins/robot-memory/robot_memory.h> 36 using namespace mongo;
45 robot_memory_(robot_memory),
46 matching_test_collection_(
"robmem.computables_matching")
49 matching_test_collection_ =
50 config_->
get_string(
"/plugins/robot-memory/database") +
".computables_matching";
57 ComputablesManager::~ComputablesManager()
68 for (std::list<Computable *>::iterator it = computables.begin(); it != computables.end(); ++it) {
69 if ((*it) == computable) {
71 computables.erase(it);
88 for (std::map<std::tuple<std::string, std::string>,
long long>::iterator it =
89 cached_querries_.begin();
90 it != cached_querries_.end();
92 if (collection == std::get<0>(it->first) && query.toString() == std::get<1>(it->first)) {
96 if (collection.find(matching_test_collection_) != std::string::npos)
98 bool added_computed_docs =
false;
101 std::string current_test_collection = matching_test_collection_ + std::to_string(rand());
102 robot_memory_->
insert(query.obj, current_test_collection);
103 for (std::list<Computable *>::iterator it = computables.begin(); it != computables.end(); ++it) {
104 if (collection == (*it)->get_collection()
105 && robot_memory_->
query((*it)->get_query(), current_test_collection)->more()) {
106 std::list<BSONObj> computed_docs_list = (*it)->compute(query.obj);
107 if (!computed_docs_list.empty()) {
109 std::vector<BSONObj> computed_docs_vector{
110 std::make_move_iterator(std::begin(computed_docs_list)),
111 std::make_move_iterator(std::end(computed_docs_list))};
113 long long cached_until =
114 computed_docs_vector[0].getField(
"_robmem_info").Obj().getField(
"cached_until").Long();
115 cached_querries_[std::make_tuple(collection, query.toString())] = cached_until;
117 robot_memory_->
insert(computed_docs_vector, (*it)->get_collection());
118 added_computed_docs =
true;
123 return added_computed_docs;
132 long long current_time_ms =
133 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
134 for (std::map<std::tuple<std::string, std::string>,
long long>::iterator it =
135 cached_querries_.begin();
136 it != cached_querries_.end();
138 if (current_time_ms > it->second) {
139 robot_memory_->
remove(BSON(
"_robmem_info.computed" <<
true <<
"_robmem_info.cached_until" 140 << BSON(
"$lt" << current_time_ms)),
141 std::get<0>(it->first));
142 cached_querries_.erase(it->first);
QResCursor query(mongo::Query query, const std::string &collection="")
Query information from the robot memory.
int remove(mongo::Query query, const std::string &collection="")
Remove documents from the robot memory.
Fawkes library namespace.
void cleanup_computed_docs()
Clean up all collections containing documents computed on demand.
Class holding information for a single computable this class also enhances computed documents by addi...
ComputablesManager(fawkes::Configuration *config, RobotMemory *robot_memory)
Constructor for class managing computables with refereces to plugin objects.
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.
Base class for exceptions in Fawkes.
void remove_computable(Computable *computable)
Remove previously registered computable.
Access to the robot memory based on mongodb.
Interface for configuration handling.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
bool check_and_compute(mongo::Query query, std::string collection)
Checks if computable knowledge is queried and calls the compute functions in this case.