Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst committed Sep 23, 2024
1 parent 4ed0462 commit 2df53ef
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 69 deletions.
56 changes: 31 additions & 25 deletions hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,23 @@ bool InMemoryDB::is_ordered(const string& link_handle) const {
}

//------------------------------------------------------------------------------
const pair<const OptCursor, const StringList> 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<const OptCursor, const vector<shared_ptr<const Atom>>> InMemoryDB::get_incoming_links_atoms(
const string& atom_handle, const KwArgs& kwargs) const {
const vector<shared_ptr<const Atom>> 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 - "
Expand All @@ -182,17 +183,22 @@ const pair<const OptCursor, const vector<shared_ptr<const Atom>>> 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<const OptCursor, const Pattern_or_Template_List> 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 =
Expand All @@ -212,15 +218,15 @@ const pair<const OptCursor, const Pattern_or_Template_List> 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<const OptCursor, const Pattern_or_Template_List> 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
Expand All @@ -237,28 +243,28 @@ const pair<const OptCursor, const Pattern_or_Template_List> 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<const OptCursor, const Pattern_or_Template_List> 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()) {
Pattern_or_Template_List templates_matched;
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 {};
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -427,7 +433,7 @@ const shared_ptr<const Link> InMemoryDB::_get_link(const string& handle) const {
const shared_ptr<const Link> 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<Link>(*it->second);
auto link_document = make_shared<const Link>(*it->second);
this->db.link.erase(it);
return move(link_document);
}
Expand Down
21 changes: 10 additions & 11 deletions hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,21 @@ class InMemoryDB : public AtomDB {

bool is_ordered(const string& link_handle) const override;

const pair<const OptCursor, const StringList> 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<const OptCursor, const vector<shared_ptr<const Atom>>> get_incoming_links_atoms(
const vector<shared_ptr<const Atom>> get_incoming_links_atoms(
const string& atom_handle, const KwArgs& kwargs = {}) const override;

const pair<const OptCursor, const Pattern_or_Template_List> 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<const OptCursor, const Pattern_or_Template_List> 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<const OptCursor, const Pattern_or_Template_List> 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<const string> get_atom_type(const string& handle) const override;

Expand Down
17 changes: 9 additions & 8 deletions hyperon_das_atomdb_cpp/src/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const OptCursor, const StringList> 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.
Expand All @@ -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<const OptCursor, const vector<shared_ptr<const Atom>>> get_incoming_links_atoms(
virtual const vector<shared_ptr<const Atom>> get_incoming_links_atoms(
const string& atom_handle, const KwArgs& kwargs = {}) const = 0;

/**
Expand All @@ -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<const OptCursor, const Pattern_or_Template_List> 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.
Expand All @@ -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<const OptCursor, const Pattern_or_Template_List> get_matched_type_template(
virtual const Pattern_or_Template_List get_matched_type_template(
const ListOfAny& _template, const KwArgs& kwargs = {}) const = 0;

/**
Expand All @@ -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<const OptCursor, const Pattern_or_Template_List> 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.
Expand Down
35 changes: 10 additions & 25 deletions hyperon_das_atomdb_cpp/src/nanobind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const OptCursor, const StringList> {
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 OptCursor, const vector<shared_ptr<const Atom>>> {
const nb::kwargs& _ = {}) -> const vector<shared_ptr<const Atom>> {
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,
Expand All @@ -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 OptCursor, const Pattern_or_Template_List> {
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<const OptCursor, const Pattern_or_Template_List> {
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<const OptCursor, const Pattern_or_Template_List> {
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)
Expand Down

0 comments on commit 2df53ef

Please sign in to comment.