Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst committed Sep 13, 2024
1 parent ad2e1d5 commit 3501903
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
31 changes: 17 additions & 14 deletions hyperon_das_atomdb_cpp/src/adapters/ram_only.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,23 @@ const pair<const OptCursor, const Pattern_or_Template_List> InMemoryDB::get_matc
if (link_type != WILDCARD &&
find(target_handles.begin(), target_handles.end(), WILDCARD) == target_handles.end()) {
return {params.get<int>(ParamsKeys::CURSOR),
{make_tuple(this->get_link_handle(link_type, target_handles), nullopt)}};
{make_pair(this->get_link_handle(link_type, target_handles), nullopt)}};
}

auto link_type_hash =
link_type == WILDCARD ? WILDCARD : ExpressionHasher::named_type_hash(link_type);

StringList handles({link_type_hash});
StringList handles;
handles.reserve(target_handles.size() + 1);
handles.push_back(link_type_hash);
handles.insert(handles.end(), target_handles.begin(), target_handles.end());
auto pattern_hash = ExpressionHasher::composite_hash(handles);

Pattern_or_Template_List patterns_matched;
auto it = this->db.patterns.find(pattern_hash);
if (it != this->db.patterns.end()) {
for (const auto& pattern_tuple : it->second) {
patterns_matched.push_back(pattern_tuple);
}
patterns_matched.reserve(it->second.size());
patterns_matched.insert(patterns_matched.end(), it->second.begin(), it->second.end());
}

if (params.get<bool>(ParamsKeys::TOPLEVEL_ONLY).value_or(false)) {
Expand Down Expand Up @@ -509,17 +510,18 @@ void InMemoryDB::_add_templates(const string& composite_type_hash,
const StringList& targets_hash) {
auto it = this->db.templates.find(composite_type_hash);
if (it != this->db.templates.end()) {
it->second.insert(make_tuple(key, targets_hash));
it->second.insert(make_pair(key, targets_hash));
} else {
this->db.templates[composite_type_hash] =
Database::TemplatesSet({make_tuple(key, targets_hash)});
Database::Pattern_or_Template_Set({make_pair(key, targets_hash)});
}

it = this->db.templates.find(named_type_hash);
if (it != this->db.templates.end()) {
it->second.insert(make_tuple(key, targets_hash));
it->second.insert(make_pair(key, targets_hash));
} else {
this->db.templates[named_type_hash] = Database::PatternsSet({make_tuple(key, targets_hash)});
this->db.templates[named_type_hash] =
Database::Pattern_or_Template_Set({make_pair(key, targets_hash)});
}
}

Expand All @@ -531,12 +533,12 @@ void InMemoryDB::_delete_templates(const Link& link_document, const StringList&

auto it = this->db.templates.find(composite_type_hash);
if (it != this->db.templates.end()) {
it->second.erase(make_tuple(key, targets_hash));
it->second.erase(make_pair(key, targets_hash));
}

it = this->db.templates.find(named_type_hash);
if (it != this->db.templates.end()) {
it->second.erase(make_tuple(key, targets_hash));
it->second.erase(make_pair(key, targets_hash));
}
}

Expand All @@ -550,9 +552,10 @@ void InMemoryDB::_add_patterns(const string& named_type_hash,
for (const auto& pattern_key : pattern_keys) {
auto it = this->db.patterns.find(pattern_key);
if (it == this->db.patterns.end()) {
this->db.patterns[pattern_key] = Database::PatternsSet({make_tuple(key, targets_hash)});
this->db.patterns[pattern_key] =
Database::Pattern_or_Template_Set({make_pair(key, targets_hash)});
} else {
it->second.insert(make_tuple(key, targets_hash));
it->second.insert(make_pair(key, targets_hash));
}
}
}
Expand All @@ -567,7 +570,7 @@ void InMemoryDB::_delete_patterns(const Link& link_document, const StringList& t
for (const auto& pattern_key : pattern_keys) {
auto it = this->db.patterns.find(pattern_key);
if (it != this->db.patterns.end()) {
it->second.erase(make_tuple(key, targets_hash));
it->second.erase(make_pair(key, targets_hash));
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions hyperon_das_atomdb_cpp/src/adapters/ram_only.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,27 @@ namespace atomdb {
*/
class Database {
public:
struct TupleHash {
auto operator()(const tuple<string, StringList>& t) const -> size_t {
using Pattern_or_Template = pair<string, StringList>;

struct Pattern_or_Template_Hash {
auto operator()(const Pattern_or_Template& t) const -> size_t {
size_t list_hash = 0;
for (const auto& str : get<1>(t)) {
for (const auto& str : t.second) {
list_hash ^= hash<string>{}(str);
}
return hash<string>{}(get<0>(t)) ^ list_hash;
return hash<string>{}(t.first) ^ list_hash;
}
};

using Pattern = tuple<string, StringList>;
using PatternsSet = unordered_set<Pattern, TupleHash>;
using Template = tuple<string, StringList>;
using TemplatesSet = unordered_set<Template, TupleHash>;
using Pattern_or_Template_Set = unordered_set<Pattern_or_Template, Pattern_or_Template_Hash>;

unordered_map<string, shared_ptr<AtomType>> atom_type;
unordered_map<string, shared_ptr<Node>> node;
unordered_map<string, shared_ptr<Link>> link;
unordered_map<string, StringList> outgoing_set;
unordered_map<string, StringUnorderedSet> incoming_set;
unordered_map<string, PatternsSet> patterns;
unordered_map<string, TemplatesSet> templates;
unordered_map<string, Pattern_or_Template_Set> patterns;
unordered_map<string, Pattern_or_Template_Set> templates;

Database()
: atom_type({}),
Expand Down
35 changes: 22 additions & 13 deletions hyperon_das_atomdb_cpp/src/nanobind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,14 @@ NB_MODULE(hyperon_das_atomdb, m) {
.def(
"add_link",
[](
InMemoryDB& self, const LinkParams& link_params, bool toplevel
InMemoryDB& self,
const LinkParams& link_params,
bool toplevel
) -> shared_ptr<const Link> {
return self.add_link(link_params, toplevel);
},
"link_params"_a, "toplevel"_a = true
"link_params"_a,
"toplevel"_a = true
)
.def(
"get_atom",
Expand All @@ -160,12 +163,14 @@ NB_MODULE(hyperon_das_atomdb, m) {
bool targets_documents = false,
bool deep_representation = false
) -> shared_ptr<const Atom> {
Params params = Params({
{ParamsKeys::NO_TARGET_FORMAT, no_target_format},
{ParamsKeys::TARGETS_DOCUMENTS, targets_documents},
{ParamsKeys::DEEP_REPRESENTATION, deep_representation}
});
return self.get_atom(handle, params);
/* TODO: adds a lot of overhead, have to find a faster alternative
* Params params = Params({
* {ParamsKeys::NO_TARGET_FORMAT, no_target_format},
* {ParamsKeys::TARGETS_DOCUMENTS, targets_documents},
* {ParamsKeys::DEEP_REPRESENTATION, deep_representation}
* });
*/
return self.get_atom(handle); //, params);
},
"handle"_a,
nb::kw_only(),
Expand All @@ -176,7 +181,9 @@ NB_MODULE(hyperon_das_atomdb, m) {
.def(
"get_node_handle",
[](
InMemoryDB& self, const string& node_type, const string& node_name
InMemoryDB& self,
const string& node_type,
const string& node_name
) -> const string {
return self.get_node_handle(node_type, node_name);
}
Expand All @@ -190,10 +197,12 @@ NB_MODULE(hyperon_das_atomdb, m) {
opt<int> cursor = nullopt,
bool toplevel_only = false
) -> const pair<const OptCursor, const Pattern_or_Template_List> {
Params params = Params({{ParamsKeys::TOPLEVEL_ONLY, toplevel_only}});
if (cursor)
params.set(ParamsKeys::CURSOR, cursor.value());
return self.get_matched_links(link_type, target_handles, params);
/* TODO: adds a lot of overhead, have to find a faster alternative
* Params params = Params({{ParamsKeys::TOPLEVEL_ONLY, toplevel_only}});
* if (cursor)
* params.set(ParamsKeys::CURSOR, cursor.value());
*/
return self.get_matched_links(link_type, target_handles); //, params);
},
"link_type"_a,
"target_handles"_a,
Expand Down
3 changes: 1 addition & 2 deletions hyperon_das_atomdb_cpp/src/type_aliases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <optional>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand All @@ -25,7 +24,7 @@ using StringList = vector<string>;
using StringUnorderedSet = unordered_set<string>;
using ListOfAny = vector<any>;

using Pattern_or_Template = tuple<string, opt<StringList>>;
using Pattern_or_Template = pair<string, opt<StringList>>;
using Pattern_or_Template_List = vector<Pattern_or_Template>;

} // namespace atomdb
4 changes: 1 addition & 3 deletions hyperon_das_atomdb_cpp/src/utils/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class Params : private ParamsMap {
* Defaults to an empty map if not provided.
* @return A Params object initialized with the specified params.
*/
Params(const ParamsMap& params = {}) {
this->insert(params.begin(), params.end());
};
Params(const ParamsMap& params = {}) { this->insert(params.begin(), params.end()); };

/**
* @brief Checks if the specified parameter exists in the collection.
Expand Down

0 comments on commit 3501903

Please sign in to comment.