22#include "computables_manager.h"
24#include <core/exception.h>
26# include <utils/time/tracker.h>
28#include <plugins/robot-memory/robot_memory.h>
29#include <utils/time/tracker_macros.h>
32#include <mongocxx/exception/operation_exception.hpp>
41using namespace mongocxx;
42using namespace bsoncxx;
51 robot_memory_(robot_memory),
52 matching_test_collection_(
"robmem.computables_matching")
55 matching_test_collection_ =
56 config_->
get_string(
"/plugins/robot-memory/database") +
".computables_matching";
64 ttc_cleanup_ = tt_->add_class(
"RobotMemory Cleanup Function Call");
65 ttc_cleanup_inner_loop_ = tt_->add_class(
"RobotMemory Cleanup Inner Loop");
66 ttc_cleanup_remove_query_ = tt_->add_class(
"RobotMemory Cleanup Database Remove Query");
83ComputablesManager::~ComputablesManager()
97 for (std::list<Computable *>::iterator it = computables.begin(); it != computables.end(); ++it) {
98 if ((*it) == computable) {
100 computables.erase(it);
117 for (std::map<std::tuple<std::string, std::string>,
long long>::iterator it =
118 cached_querries_.begin();
119 it != cached_querries_.end();
121 if (collection == std::get<0>(it->first) && to_json(query) == std::get<1>(it->first)) {
125 if (collection.find(matching_test_collection_) != std::string::npos)
127 bool added_computed_docs =
false;
130 std::string current_test_collection = matching_test_collection_ + std::to_string(rand());
132 robot_memory_->
insert(query, current_test_collection);
133 }
catch (mongocxx::operation_exception &e) {
138 for (std::list<Computable *>::iterator it = computables.begin(); it != computables.end(); ++it) {
139 auto cursor = robot_memory_->
query((*it)->get_query(), current_test_collection);
140 if (collection == (*it)->get_collection() && cursor.begin() != cursor.end()) {
141 std::list<document::value> computed_docs_list = (*it)->compute(query);
142 if (!computed_docs_list.empty()) {
144 std::vector<document::view> computed_docs_vector{
145 std::make_move_iterator(std::begin(computed_docs_list)),
146 std::make_move_iterator(std::end(computed_docs_list))};
148 long long cached_until =
149 computed_docs_vector[0][
"_robmem_info"][
"cached_until"].get_int64();
150 cached_querries_[std::make_tuple(collection, to_json(query))] = cached_until;
152 robot_memory_->
insert(computed_docs_vector, (*it)->get_collection());
153 added_computed_docs =
true;
158 return added_computed_docs;
167 TIMETRACK_START(ttc_cleanup_);
168 long long current_time_ms =
169 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
170 for (std::map<std::tuple<std::string, std::string>,
long long>::iterator it =
171 cached_querries_.begin();
172 it != cached_querries_.end();
174 TIMETRACK_START(ttc_cleanup_inner_loop_);
175 if (current_time_ms > it->second) {
176 using namespace bsoncxx::builder;
178 doc.append(basic::kvp(
"_robmem_info.computed",
true));
180 basic::kvp(
"_robmem_info.cached_until", [current_time_ms](basic::sub_document subdoc) {
181 subdoc.append(basic::kvp(
"$lt",
static_cast<std::int64_t
>(current_time_ms)));
183 TIMETRACK_START(ttc_cleanup_remove_query_);
184 robot_memory_->
remove(doc, std::get<0>(it->first));
185 TIMETRACK_END(ttc_cleanup_remove_query_);
186 cached_querries_.erase(it->first);
188 TIMETRACK_END(ttc_cleanup_inner_loop_);
190 TIMETRACK_END(ttc_cleanup_);
191#ifdef USE_TIMETRACKER
192 if (++tt_loopcount_ % 5 == 0) {
193 tt_->print_to_stdout();
Class holding information for a single computable this class also enhances computed documents by addi...
This class manages registering computables and can check if any computables are invoced by a query.
void cleanup_computed_docs()
Clean up all collections containing documents computed on demand.
void remove_computable(Computable *computable)
Remove previously registered computable.
ComputablesManager(fawkes::Configuration *config, RobotMemory *robot_memory)
Constructor for class managing computables with refereces to plugin objects.
bool check_and_compute(const bsoncxx::document::view &query, std::string collection)
Checks if computable knowledge is queried and calls the compute functions in this case.
Access to the robot memory based on mongodb.
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.
int remove(const bsoncxx::document::view &query, const std::string &collection="")
Remove documents from the robot memory.
int insert(bsoncxx::document::view, 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.
Interface for configuration handling.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Base class for exceptions in Fawkes.
Fawkes library namespace.