From 2df53efcc69a1cbc1d8b93d7ef8ed50022c4c745 Mon Sep 17 00:00:00 2001 From: Angelo Probst Date: Mon, 23 Sep 2024 15:29:02 -0300 Subject: [PATCH] wip --- .../src/adapters/ram_only.cpp | 56 ++++++++++--------- .../src/adapters/ram_only.hpp | 21 ++++--- hyperon_das_atomdb_cpp/src/database.hpp | 17 +++--- hyperon_das_atomdb_cpp/src/nanobind.cpp | 35 ++++-------- 4 files changed, 60 insertions(+), 69 deletions(-) diff --git a/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp b/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp index e9311f8e..535f0505 100644 --- a/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp +++ b/hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp @@ -154,22 +154,23 @@ bool InMemoryDB::is_ordered(const string& link_handle) const { } //------------------------------------------------------------------------------ -const pair InMemoryDB::get_incoming_links_handles( - const string& atom_handle, const KwArgs& kwargs) const { +const StringList InMemoryDB::get_incoming_links_handles(const string& atom_handle, + const KwArgs& kwargs) const { if (not kwargs.handles_only) { throw runtime_error( "'handles_only' is not true in kwargs - " "'InMemoryDB::get_incoming_links_atoms' should be used instead"); } auto it = this->db.incoming_set.find(atom_handle); - if (it != this->db.incoming_set.end()) - return {kwargs.cursor, move(StringList(it->second.begin(), it->second.end()))}; - return {kwargs.cursor, {}}; + if (it != this->db.incoming_set.end()) { + return move(StringList(it->second.begin(), it->second.end())); + } + return {}; } //------------------------------------------------------------------------------ -const pair>> InMemoryDB::get_incoming_links_atoms( - const string& atom_handle, const KwArgs& kwargs) const { +const vector> InMemoryDB::get_incoming_links_atoms(const string& atom_handle, + const KwArgs& kwargs) const { if (kwargs.handles_only) { throw runtime_error( "'handles_only' is true in kwargs - " @@ -182,17 +183,22 @@ const pair>> InMemoryDB::ge for (const auto& link_handle : it->second) { atoms.push_back(this->get_atom(link_handle, kwargs)); } - return {kwargs.cursor, move(atoms)}; + return move(atoms); } - return {kwargs.cursor, {}}; + return {}; } //------------------------------------------------------------------------------ -const pair InMemoryDB::get_matched_links( - const string& link_type, const StringList& target_handles, const KwArgs& kwargs) const { +const Pattern_or_Template_List InMemoryDB::get_matched_links(const string& link_type, + const StringList& target_handles, + const KwArgs& kwargs) const { if (link_type != WILDCARD && find(target_handles.begin(), target_handles.end(), WILDCARD) == target_handles.end()) { - return {kwargs.cursor, {make_pair(this->get_link_handle(link_type, target_handles), nullopt)}}; + try { + return {make_pair(this->get_link_handle(link_type, target_handles), nullopt)}; + } catch (const AtomDoesNotExist&) { + return {}; + } } auto link_type_hash = @@ -212,15 +218,15 @@ const pair InMemoryDB::get_matc } if (kwargs.toplevel_only) { - return {kwargs.cursor, this->_filter_non_toplevel(patterns_matched)}; + return this->_filter_non_toplevel(patterns_matched); } - return {kwargs.cursor, move(patterns_matched)}; + return move(patterns_matched); } //------------------------------------------------------------------------------ -const pair InMemoryDB::get_matched_type_template( - const ListOfAny& _template, const KwArgs& kwargs) const { +const Pattern_or_Template_List InMemoryDB::get_matched_type_template(const ListOfAny& _template, + const KwArgs& kwargs) const { /** * NOTE: * Next two lines are spending a lot of time in handling ListOfAny, however @@ -237,16 +243,16 @@ const pair InMemoryDB::get_matc templates_matched.reserve(it->second.size()); templates_matched.insert(templates_matched.end(), it->second.begin(), it->second.end()); if (kwargs.toplevel_only) { - return {kwargs.cursor, this->_filter_non_toplevel(templates_matched)}; + return this->_filter_non_toplevel(templates_matched); } - return {kwargs.cursor, move(templates_matched)}; + return move(templates_matched); } - return {kwargs.cursor, {}}; + return {}; } //------------------------------------------------------------------------------ -const pair InMemoryDB::get_matched_type( - const string& link_type, const KwArgs& kwargs) const { +const Pattern_or_Template_List InMemoryDB::get_matched_type(const string& link_type, + const KwArgs& kwargs) const { auto link_type_hash = ExpressionHasher::named_type_hash(link_type); auto it = this->db.templates.find(link_type_hash); if (it != this->db.templates.end()) { @@ -254,11 +260,11 @@ const pair InMemoryDB::get_matc templates_matched.reserve(it->second.size()); templates_matched.insert(templates_matched.end(), it->second.begin(), it->second.end()); if (kwargs.toplevel_only) { - return {kwargs.cursor, this->_filter_non_toplevel(templates_matched)}; + return this->_filter_non_toplevel(templates_matched); } - return {kwargs.cursor, move(templates_matched)}; + return move(templates_matched); } - return {kwargs.cursor, {}}; + return {}; } //------------------------------------------------------------------------------ @@ -427,7 +433,7 @@ const shared_ptr InMemoryDB::_get_link(const string& handle) const { const shared_ptr InMemoryDB::_get_and_delete_link(const string& link_handle) { auto it = this->db.link.find(link_handle); if (it != this->db.link.end()) { - auto link_document = make_shared(*it->second); + auto link_document = make_shared(*it->second); this->db.link.erase(it); return move(link_document); } diff --git a/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp b/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp index c1b6709a..352b935a 100644 --- a/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp +++ b/hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp @@ -118,22 +118,21 @@ class InMemoryDB : public AtomDB { bool is_ordered(const string& link_handle) const override; - const pair get_incoming_links_handles( - const string& atom_handle, const KwArgs& kwargs = {}) const override; + const StringList get_incoming_links_handles(const string& atom_handle, + const KwArgs& kwargs = {}) const override; - const pair>> get_incoming_links_atoms( + const vector> get_incoming_links_atoms( const string& atom_handle, const KwArgs& kwargs = {}) const override; - const pair get_matched_links( - const string& link_type, - const StringList& target_handles, - const KwArgs& kwargs = {}) const override; + const Pattern_or_Template_List get_matched_links(const string& link_type, + const StringList& target_handles, + const KwArgs& kwargs = {}) const override; - const pair get_matched_type_template( - const ListOfAny& _template, const KwArgs& kwargs = {}) const override; + const Pattern_or_Template_List get_matched_type_template(const ListOfAny& _template, + const KwArgs& kwargs = {}) const override; - const pair get_matched_type( - const string& link_type, const KwArgs& kwargs = {}) const override; + const Pattern_or_Template_List get_matched_type(const string& link_type, + const KwArgs& kwargs = {}) const override; const opt get_atom_type(const string& handle) const override; diff --git a/hyperon_das_atomdb_cpp/src/database.hpp b/hyperon_das_atomdb_cpp/src/database.hpp index ab32d4af..7c9e4daa 100644 --- a/hyperon_das_atomdb_cpp/src/database.hpp +++ b/hyperon_das_atomdb_cpp/src/database.hpp @@ -198,8 +198,8 @@ class AtomDB { * @return A pair containing an optional cursor and a list of strings representing the incoming * link handles. */ - virtual const pair get_incoming_links_handles( - const string& atom_handle, const KwArgs& kwargs = {}) const = 0; + virtual const StringList get_incoming_links_handles(const string& atom_handle, + const KwArgs& kwargs = {}) const = 0; /** * @brief Retrieves incoming link atoms for the specified atom. @@ -208,7 +208,7 @@ class AtomDB { * @return A pair containing an optional cursor and a list of Atom objects representing the * incoming links. */ - virtual const pair>> get_incoming_links_atoms( + virtual const vector> get_incoming_links_atoms( const string& atom_handle, const KwArgs& kwargs = {}) const = 0; /** @@ -219,8 +219,9 @@ class AtomDB { * @return A pair containing an optional cursor and a list of patterns or templates representing * the matched links. */ - virtual const pair get_matched_links( - const string& link_type, const StringList& target_handles, const KwArgs& kwargs = {}) const = 0; + virtual const Pattern_or_Template_List get_matched_links(const string& link_type, + const StringList& target_handles, + const KwArgs& kwargs = {}) const = 0; /** * @brief Retrieves matched type templates based on the specified template. @@ -229,7 +230,7 @@ class AtomDB { * @return A pair containing an optional cursor and a list of patterns or templates representing * the matched type templates. */ - virtual const pair get_matched_type_template( + virtual const Pattern_or_Template_List get_matched_type_template( const ListOfAny& _template, const KwArgs& kwargs = {}) const = 0; /** @@ -239,8 +240,8 @@ class AtomDB { * @return A pair containing an optional cursor and a list of patterns or templates representing * the matched types. */ - virtual const pair get_matched_type( - const string& link_type, const KwArgs& kwargs = {}) const = 0; + virtual const Pattern_or_Template_List get_matched_type(const string& link_type, + const KwArgs& kwargs = {}) const = 0; /** * @brief Retrieves the type of the atom with the specified handle. diff --git a/hyperon_das_atomdb_cpp/src/nanobind.cpp b/hyperon_das_atomdb_cpp/src/nanobind.cpp index f6d1746a..67e0bd33 100644 --- a/hyperon_das_atomdb_cpp/src/nanobind.cpp +++ b/hyperon_das_atomdb_cpp/src/nanobind.cpp @@ -114,39 +114,32 @@ NB_MODULE(hyperon_das_atomdb, m) { "get_incoming_links_handles", [](InMemoryDB& self, const string& atom_handle, - const OptCursor cursor = nullopt, bool handles_only = true, - const nb::kwargs& _ = {}) -> const pair { - return self.get_incoming_links_handles(atom_handle, - {handles_only : handles_only, cursor : cursor}); + const nb::kwargs& _ = {}) -> const StringList { + return self.get_incoming_links_handles(atom_handle, {handles_only : handles_only}); }, "atom_handle"_a, nb::kw_only(), - "cursor"_a = nullopt, "handles_only"_a = true, "_"_a = nb::kwargs()) .def( "get_incoming_links_atoms", [](InMemoryDB& self, const string& atom_handle, - const OptCursor cursor = nullopt, bool no_target_format = false, bool targets_documents = false, bool deep_representation = false, bool handles_only = false, - const nb::kwargs& _ = {}) - -> const pair>> { + const nb::kwargs& _ = {}) -> const vector> { return self.get_incoming_links_atoms(atom_handle, { no_target_format : no_target_format, targets_documents : targets_documents, deep_representation : deep_representation, - handles_only : handles_only, - cursor : cursor + handles_only : handles_only }); }, "atom_handle"_a, nb::kw_only(), - "cursor"_a = nullopt, "no_target_format"_a = false, "targets_documents"_a = false, "deep_representation"_a = false, @@ -157,46 +150,38 @@ NB_MODULE(hyperon_das_atomdb, m) { [](InMemoryDB& self, const string& link_type, const StringList& target_handles, - const OptCursor cursor = nullopt, bool toplevel_only = false, - const nb::kwargs& _ = {}) -> const pair { + const nb::kwargs& _ = {}) -> const Pattern_or_Template_List { return self.get_matched_links( - link_type, target_handles, {toplevel_only : toplevel_only, cursor : cursor}); + link_type, target_handles, {toplevel_only : toplevel_only}); }, "link_type"_a, "target_handles"_a, nb::kw_only(), - "cursor"_a = nullopt, "toplevel_only"_a = false, "_"_a = nb::kwargs()) .def( "get_matched_type_template", [](InMemoryDB& self, const ListOfAny& _template, - const OptCursor cursor = nullopt, bool toplevel_only = false, - const nb::kwargs& _ = {}) -> const pair { - return self.get_matched_type_template(_template, - {toplevel_only : toplevel_only, cursor : cursor}); + const nb::kwargs& _ = {}) -> const Pattern_or_Template_List { + return self.get_matched_type_template(_template, {toplevel_only : toplevel_only}); }, "_template"_a, nb::kw_only(), - "cursor"_a = nullopt, "toplevel_only"_a = false, "_"_a = nb::kwargs()) .def( "get_matched_type", [](InMemoryDB& self, const string& link_type, - const OptCursor cursor = nullopt, bool toplevel_only = false, - const nb::kwargs& _ = {}) -> const pair { - return self.get_matched_type(link_type, - {toplevel_only : toplevel_only, cursor : cursor}); + const nb::kwargs& _ = {}) -> const Pattern_or_Template_List { + return self.get_matched_type(link_type, {toplevel_only : toplevel_only}); }, "link_type"_a, nb::kw_only(), - "cursor"_a = nullopt, "toplevel_only"_a = false, "_"_a = nb::kwargs()) .def("get_atom_type", &AtomDB::get_atom_type)