Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#238] everything under submodules, moving FieldNames from python to c++ #239

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions hyperon_das_atomdb_cpp/src/constants.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "constants.h"

namespace atomdb {

const char* FieldNames::ID_HASH = "_id";
const char* FieldNames::HANDLE = "handle";
const char* FieldNames::COMPOSITE_TYPE = "composite_type";
const char* FieldNames::COMPOSITE_TYPE_HASH = "composite_type_hash";
const char* FieldNames::NODE_NAME = "name";
const char* FieldNames::TYPE_NAME = "named_type";
const char* FieldNames::TYPE_NAME_HASH = "named_type_hash";
const char* FieldNames::KEY_PREFIX = "key";
const char* FieldNames::KEYS = "keys";
const char* FieldNames::IS_TOPLEVEL = "is_toplevel";
const char* FieldNames::TARGETS = "targets";
const char* FieldNames::TARGETS_DOCUMENTS = "targets_documents";
const char* FieldNames::CUSTOM_ATTRIBUTES = "custom_attributes";

} // namespace atomdb
16 changes: 16 additions & 0 deletions hyperon_das_atomdb_cpp/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@ static const string TYPEDEF_MARK_HASH = ExpressionHasher::named_type_hash(":");

enum FieldIndexType { BINARY_TREE = 0, TOKEN_INVERTED_LIST };

struct FieldNames {
static const char* ID_HASH;
static const char* HANDLE;
static const char* COMPOSITE_TYPE;
static const char* COMPOSITE_TYPE_HASH;
static const char* NODE_NAME;
static const char* TYPE_NAME;
static const char* TYPE_NAME_HASH;
static const char* KEY_PREFIX;
static const char* KEYS;
static const char* IS_TOPLEVEL;
static const char* TARGETS;
static const char* TARGETS_DOCUMENTS;
static const char* CUSTOM_ATTRIBUTES;
};

} // namespace atomdb
2 changes: 1 addition & 1 deletion hyperon_das_atomdb_cpp/src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ shared_ptr<Link> AtomDB::_build_link(const Link& link_params, bool is_toplevel)
const auto& targets = link_params.targets_documents;
if (link_type.empty() or targets.empty()) {
// TODO: log error ???
throw AddLinkException("'type' and 'targets_documents' are required.",
throw AddLinkException("'type' and 'targets' are required.",
"link_params: " + link_params.to_string() +
", is_toplevel: " + (is_toplevel ? "true" : "false"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ..ext.constants import * # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ..ext.database import * # type: ignore
22 changes: 11 additions & 11 deletions hyperon_das_atomdb_cpp/src/nanobind/bind_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ static ListOfAny pylist_to_composite_type(const nb::list& py_list) {
*/
static nb::dict atom_to_dict(const Atom& self) {
nb::dict dict;
dict["_id"] = self._id;
dict["handle"] = self.handle;
dict["composite_type_hash"] = self.composite_type_hash;
dict["named_type"] = self.named_type;
dict["custom_attributes"] = self.custom_attributes;
dict[FieldNames::ID_HASH] = self._id;
dict[FieldNames::HANDLE] = self.handle;
dict[FieldNames::COMPOSITE_TYPE_HASH] = self.composite_type_hash;
dict[FieldNames::TYPE_NAME] = self.named_type;
dict[FieldNames::CUSTOM_ATTRIBUTES] = self.custom_attributes;
return move(dict);
};

Expand All @@ -114,7 +114,7 @@ static nb::dict atom_to_dict(const Atom& self) {
*/
static nb::dict node_to_dict(const Node& self) {
nb::dict dict = atom_to_dict(self);
dict["name"] = self.name;
dict[FieldNames::NODE_NAME] = self.name;
return move(dict);
};

Expand All @@ -125,10 +125,10 @@ static nb::dict node_to_dict(const Node& self) {
*/
static nb::dict link_to_dict(const Link& self) {
nb::dict dict = atom_to_dict(self);
dict["composite_type"] = composite_type_to_pylist(self.composite_type);
dict["named_type_hash"] = self.named_type_hash;
dict["targets"] = self.targets;
dict["is_toplevel"] = self.is_toplevel;
dict[FieldNames::COMPOSITE_TYPE] = composite_type_to_pylist(self.composite_type);
dict[FieldNames::TYPE_NAME_HASH] = self.named_type_hash;
dict[FieldNames::TARGETS] = self.targets;
dict[FieldNames::IS_TOPLEVEL] = self.is_toplevel;
nb::list targets_documents;
for (const auto& target : self.targets_documents) {
if (auto node = std::get_if<Node>(&target)) {
Expand All @@ -137,7 +137,7 @@ static nb::dict link_to_dict(const Link& self) {
targets_documents.append(link_to_dict(*link));
}
}
dict["targets_documents"] = move(targets_documents);
dict[FieldNames::TARGETS_DOCUMENTS] = move(targets_documents);
return move(dict);
};

Expand Down
33 changes: 26 additions & 7 deletions hyperon_das_atomdb_cpp/src/nanobind/hyperon_das_atomdb_cpp_bind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,35 @@ namespace nb = nanobind;
using namespace nb::literals;

NB_MODULE(ext, m) {
// root module ---------------------------------------------------------------------------------
m.attr("WILDCARD") = WILDCARD;
m.attr("WILDCARD_HASH") = WILDCARD_HASH;
m.attr("TYPE_HASH") = TYPE_HASH;
m.attr("TYPEDEF_MARK_HASH") = TYPEDEF_MARK_HASH;
nb::enum_<FieldIndexType>(m, "FieldIndexType", nb::is_arithmetic())
// ---------------------------------------------------------------------------------------------
// constants submodule -------------------------------------------------------------------------
nb::module_ constants = m.def_submodule("constants");
constants.attr("WILDCARD") = WILDCARD;
constants.attr("WILDCARD_HASH") = WILDCARD_HASH;
constants.attr("TYPE_HASH") = TYPE_HASH;
constants.attr("TYPEDEF_MARK_HASH") = TYPEDEF_MARK_HASH;
nb::enum_<FieldIndexType>(constants, "FieldIndexType", nb::is_arithmetic())
.value("BINARY_TREE", FieldIndexType::BINARY_TREE)
.value("TOKEN_INVERTED_LIST", FieldIndexType::TOKEN_INVERTED_LIST)
.export_values();
nb::class_<AtomDB, AtomDBTrampoline>(m, "AtomDB")
nb::class_<FieldNames>(constants, "FieldNames")
.def_ro_static("ID_HASH", &FieldNames::ID_HASH)
.def_ro_static("HANDLE", &FieldNames::HANDLE)
.def_ro_static("COMPOSITE_TYPE", &FieldNames::COMPOSITE_TYPE)
.def_ro_static("COMPOSITE_TYPE_HASH", &FieldNames::COMPOSITE_TYPE_HASH)
.def_ro_static("NODE_NAME", &FieldNames::NODE_NAME)
.def_ro_static("TYPE_NAME", &FieldNames::TYPE_NAME)
.def_ro_static("TYPE_NAME_HASH", &FieldNames::TYPE_NAME_HASH)
.def_ro_static("KEY_PREFIX", &FieldNames::KEY_PREFIX)
.def_ro_static("KEYS", &FieldNames::KEYS)
.def_ro_static("IS_TOPLEVEL", &FieldNames::IS_TOPLEVEL)
.def_ro_static("TARGETS", &FieldNames::TARGETS)
.def_ro_static("TARGETS_DOCUMENTS", &FieldNames::TARGETS_DOCUMENTS)
.def_ro_static("CUSTOM_ATTRIBUTES", &FieldNames::CUSTOM_ATTRIBUTES);
// ---------------------------------------------------------------------------------------------
// database submodule --------------------------------------------------------------------------
nb::module_ database = m.def_submodule("database");
nb::class_<AtomDB, AtomDBTrampoline>(database, "AtomDB")
.def(nb::init<>())
.def_static("build_node_handle", &AtomDB::build_node_handle, "node_type"_a, "node_name"_a)
.def_static("node_handle", // retrocompatibility
Expand Down