Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/atomdb/AtomDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class AtomDB {
virtual shared_ptr<Node> get_node(const string& handle) = 0;
virtual shared_ptr<Link> get_link(const string& handle) = 0;

virtual vector<shared_ptr<Atom>> get_atoms(const vector<string>& handles) = 0;
virtual vector<shared_ptr<Node>> get_nodes(const vector<string>& handles) = 0;
virtual vector<shared_ptr<Link>> get_links(const vector<string>& handles) = 0;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

virtual vector<shared_ptr<Atom>> get_matching_atoms(bool is_toplevel, Atom& key) = 0;

virtual shared_ptr<atomdb_api_types::HandleSet> query_for_pattern(const LinkSchema& link_schema) = 0;
Expand Down
7 changes: 7 additions & 0 deletions src/atomdb/KeySensitiveAtomDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class KeySensitiveAtomDB {
virtual shared_ptr<Node> get_node(const string& handle, shared_ptr<Keychain> keychain) = 0;
virtual shared_ptr<Link> get_link(const string& handle, shared_ptr<Keychain> keychain) = 0;

virtual vector<shared_ptr<Atom>> get_atoms(const vector<string>& handles,
shared_ptr<Keychain> keychain) = 0;
virtual vector<shared_ptr<Node>> get_nodes(const vector<string>& handles,
shared_ptr<Keychain> keychain) = 0;
virtual vector<shared_ptr<Link>> get_links(const vector<string>& handles,
shared_ptr<Keychain> keychain) = 0;

virtual vector<shared_ptr<Atom>> get_matching_atoms(bool is_toplevel,
Atom& key,
shared_ptr<Keychain> keychain) = 0;
Expand Down
45 changes: 45 additions & 0 deletions src/atomdb/ProtectedAtomDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@ shared_ptr<Link> ProtectedAtomDB::get_link(const string& handle, shared_ptr<Keyc
return dynamic_pointer_cast<Link>(this->get_atom(handle, keychain));
}

vector<shared_ptr<Atom>> ProtectedAtomDB::get_atoms(const vector<string>& handles,
shared_ptr<Keychain> keychain) {
auto public_key = this->try_get_public_key(keychain);
if (!public_key) {
return {};
}
return this->filter_atoms(this->backend->get_atoms(handles), *public_key);
}

vector<shared_ptr<Node>> ProtectedAtomDB::get_nodes(const vector<string>& handles,
shared_ptr<Keychain> keychain) {
auto atoms = this->get_atoms(handles, keychain);
vector<shared_ptr<Node>> nodes;
for (const auto& atom : atoms) {
if (auto node = dynamic_pointer_cast<Node>(atom)) {
nodes.push_back(node);
}
}
return nodes;
}

vector<shared_ptr<Link>> ProtectedAtomDB::get_links(const vector<string>& handles,
shared_ptr<Keychain> keychain) {
auto atoms = this->get_atoms(handles, keychain);
vector<shared_ptr<Link>> links;
for (const auto& atom : atoms) {
if (auto link = dynamic_pointer_cast<Link>(atom)) {
links.push_back(link);
}
}
return links;
}

vector<shared_ptr<Atom>> ProtectedAtomDB::get_matching_atoms(bool is_toplevel,
Atom& key,
shared_ptr<Keychain> keychain) {
Expand Down Expand Up @@ -245,6 +278,18 @@ shared_ptr<Node> ProtectedAtomDB::get_node(const string& handle) { raise_keychai

shared_ptr<Link> ProtectedAtomDB::get_link(const string& handle) { raise_keychain_required("get_link"); }

vector<shared_ptr<Atom>> ProtectedAtomDB::get_atoms(const vector<string>& handles) {
raise_keychain_required("get_atoms");
}

vector<shared_ptr<Node>> ProtectedAtomDB::get_nodes(const vector<string>& handles) {
raise_keychain_required("get_nodes");
}

vector<shared_ptr<Link>> ProtectedAtomDB::get_links(const vector<string>& handles) {
raise_keychain_required("get_links");
}

vector<shared_ptr<Atom>> ProtectedAtomDB::get_matching_atoms(bool is_toplevel, Atom& key) {
raise_keychain_required("get_matching_atoms");
}
Expand Down
11 changes: 11 additions & 0 deletions src/atomdb/ProtectedAtomDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ class ProtectedAtomDB : public AtomDB, public KeySensitiveAtomDB {
shared_ptr<Link> get_link(const string& handle) override;
shared_ptr<Link> get_link(const string& handle, shared_ptr<Keychain> keychain) override;

vector<shared_ptr<Atom>> get_atoms(const vector<string>& handles) override;
vector<shared_ptr<Node>> get_nodes(const vector<string>& handles) override;
vector<shared_ptr<Link>> get_links(const vector<string>& handles) override;

vector<shared_ptr<Atom>> get_atoms(const vector<string>& handles,
shared_ptr<Keychain> keychain) override;
vector<shared_ptr<Node>> get_nodes(const vector<string>& handles,
shared_ptr<Keychain> keychain) override;
vector<shared_ptr<Link>> get_links(const vector<string>& handles,
shared_ptr<Keychain> keychain) override;

vector<shared_ptr<Atom>> get_matching_atoms(bool is_toplevel, Atom& key) override;
vector<shared_ptr<Atom>> get_matching_atoms(bool is_toplevel,
Atom& key,
Expand Down
15 changes: 15 additions & 0 deletions src/atomdb/adapterdb/AdapterDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ shared_ptr<Link> AdapterDB::get_link(const string& handle) {
return this->atomdb_backend->get_link(handle);
}

vector<shared_ptr<Atom>> AdapterDB::get_atoms(const vector<string>& handles) {
this->ensure_backend_ready();
return this->atomdb_backend->get_atoms(handles);
}

vector<shared_ptr<Node>> AdapterDB::get_nodes(const vector<string>& handles) {
this->ensure_backend_ready();
return this->atomdb_backend->get_nodes(handles);
}

vector<shared_ptr<Link>> AdapterDB::get_links(const vector<string>& handles) {
this->ensure_backend_ready();
return this->atomdb_backend->get_links(handles);
}

vector<shared_ptr<Atom>> AdapterDB::get_matching_atoms(bool is_toplevel, Atom& key) {
this->ensure_backend_ready();
return this->atomdb_backend->get_matching_atoms(is_toplevel, key);
Expand Down
4 changes: 4 additions & 0 deletions src/atomdb/adapterdb/AdapterDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class AdapterDB : public AtomDB {
shared_ptr<Node> get_node(const string& handle) override;
shared_ptr<Link> get_link(const string& handle) override;

vector<shared_ptr<Atom>> get_atoms(const vector<string>& handles) override;
vector<shared_ptr<Node>> get_nodes(const vector<string>& handles) override;
vector<shared_ptr<Link>> get_links(const vector<string>& handles) override;

vector<shared_ptr<Atom>> get_matching_atoms(bool is_toplevel, Atom& key) override;

shared_ptr<atomdb_api_types::HandleSet> query_for_pattern(const LinkSchema& link_schema) override;
Expand Down
38 changes: 38 additions & 0 deletions src/atomdb/inmemorydb/InMemoryDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,44 @@ shared_ptr<Link> InMemoryDB::get_link(const string& handle) {
return dynamic_pointer_cast<Link>(get_atom(handle));
}

vector<shared_ptr<Atom>> InMemoryDB::get_atoms(const vector<string>& handles) {
auto trie = load_tries()->atoms;
vector<shared_ptr<Atom>> atoms;
atoms.reserve(handles.size());
for (const auto& handle : handles) {
if (!Hasher::is_atom_handle(handle)) continue;
auto atom = lookup_atom(*trie, handle);
if (atom == nullptr) continue;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
auto cloned = clone_atom(atom.get());
if (cloned != nullptr) {
atoms.push_back(cloned);
}
}
return atoms;
}

vector<shared_ptr<Node>> InMemoryDB::get_nodes(const vector<string>& handles) {
auto atoms = this->get_atoms(handles);
vector<shared_ptr<Node>> nodes;
for (const auto& atom : atoms) {
if (auto node = dynamic_pointer_cast<Node>(atom)) {
nodes.push_back(node);
}
}
return nodes;
}

vector<shared_ptr<Link>> InMemoryDB::get_links(const vector<string>& handles) {
auto atoms = this->get_atoms(handles);
vector<shared_ptr<Link>> links;
for (const auto& atom : atoms) {
if (auto link = dynamic_pointer_cast<Link>(atom)) {
links.push_back(link);
}
}
return links;
}

shared_ptr<HandleSet> InMemoryDB::query_for_pattern(const LinkSchema& link_schema) {
auto handle_set = make_shared<HandleSetInMemory>();
auto handles = lookup_handle_set(*load_tries()->patterns, link_schema.handle());
Expand Down
4 changes: 4 additions & 0 deletions src/atomdb/inmemorydb/InMemoryDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class InMemoryDB : public AtomDB, public HandleDecoder {
shared_ptr<Node> get_node(const string& handle) override;
shared_ptr<Link> get_link(const string& handle) override;

vector<shared_ptr<Atom>> get_atoms(const vector<string>& handles) override;
vector<shared_ptr<Node>> get_nodes(const vector<string>& handles) override;
vector<shared_ptr<Link>> get_links(const vector<string>& handles) override;

vector<shared_ptr<Atom>> get_matching_atoms(bool is_toplevel, Atom& key) override;

shared_ptr<atomdb_api_types::HandleSet> query_for_pattern(const LinkSchema& link_schema) override;
Expand Down
97 changes: 67 additions & 30 deletions src/atomdb/redis_mongodb/RedisMongoDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,36 +153,8 @@ void RedisMongoDB::mongodb_setup(const JsonConfig& config) {
}

shared_ptr<Atom> RedisMongoDB::get_atom(const string& handle) {
auto atom_document =
dynamic_pointer_cast<atomdb_api_types::MongodbDocument>(get_atom_document(handle));
if (atom_document != NULL) {
Properties custom_attributes;
if (atom_document->contains("custom_attributes")) {
custom_attributes =
atom_document->extract_custom_attributes(atom_document->get_object("custom_attributes"));
}
bool is_toplevel = false;
if (atom_document->contains("is_toplevel")) {
is_toplevel = atom_document->get_bool("is_toplevel");
}
if (Hasher::is_link_handle(handle)) {
unsigned int arity = atom_document->get_size(MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS]);
vector<string> targets;
for (unsigned int i = 0; i < arity; i++) {
targets.push_back(
string(atom_document->get(MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS], i)));
}
return make_shared<Link>(
atom_document->get("named_type"), targets, is_toplevel, custom_attributes);
} else {
return make_shared<Node>(atom_document->get("named_type"),
atom_document->get("name"),
is_toplevel,
custom_attributes);
}
} else {
return shared_ptr<Atom>(NULL);
}
return atom_from_mongodb_document(
dynamic_pointer_cast<atomdb_api_types::MongodbDocument>(get_atom_document(handle)));
}

shared_ptr<Node> RedisMongoDB::get_node(const string& handle) {
Expand All @@ -195,6 +167,71 @@ shared_ptr<Link> RedisMongoDB::get_link(const string& handle) {
return dynamic_pointer_cast<Link>(get_atom(handle));
}

vector<shared_ptr<Atom>> RedisMongoDB::get_atoms(const vector<string>& handles) {
auto atom_documents = this->get_atom_documents(handles, {});

vector<shared_ptr<Atom>> atoms;
for (const auto& atom_document : atom_documents) {
auto atom = atom_from_mongodb_document(
dynamic_pointer_cast<atomdb_api_types::MongodbDocument>(atom_document));
if (atom != NULL) {
atoms.push_back(atom);
}
}
return atoms;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

vector<shared_ptr<Node>> RedisMongoDB::get_nodes(const vector<string>& handles) {
auto atoms = this->get_atoms(handles);

vector<shared_ptr<Node>> nodes;
for (const auto& atom : atoms) {
if (auto node = dynamic_pointer_cast<Node>(atom)) {
nodes.push_back(node);
}
}
return nodes;
}

vector<shared_ptr<Link>> RedisMongoDB::get_links(const vector<string>& handles) {
auto atoms = this->get_atoms(handles);

vector<shared_ptr<Link>> links;
for (const auto& atom : atoms) {
if (auto link = dynamic_pointer_cast<Link>(atom)) {
links.push_back(link);
}
}
return links;
}

shared_ptr<Atom> RedisMongoDB::atom_from_mongodb_document(
const shared_ptr<atomdb_api_types::MongodbDocument>& atom_document) {
if (atom_document == NULL) {
return shared_ptr<Atom>(NULL);
}
Properties custom_attributes;
if (atom_document->contains("custom_attributes")) {
custom_attributes =
atom_document->extract_custom_attributes(atom_document->get_object("custom_attributes"));
}
bool is_toplevel = false;
if (atom_document->contains("is_toplevel")) {
is_toplevel = atom_document->get_bool("is_toplevel");
}
if (atom_document->contains(MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS])) {
unsigned int arity = atom_document->get_size(MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS]);
vector<string> targets;
for (unsigned int i = 0; i < arity; i++) {
targets.push_back(string(atom_document->get(MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS], i)));
}
return make_shared<Link>(
atom_document->get("named_type"), targets, is_toplevel, custom_attributes);
}
return make_shared<Node>(
atom_document->get("named_type"), atom_document->get("name"), is_toplevel, custom_attributes);
}

shared_ptr<atomdb_api_types::HandleSet> RedisMongoDB::query_for_pattern(const LinkSchema& link_schema) {
if (skip_redis_) return nullptr;

Expand Down
7 changes: 7 additions & 0 deletions src/atomdb/redis_mongodb/RedisMongoDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class RedisMongoDB : public AtomDB {
shared_ptr<Node> get_node(const string& handle);
shared_ptr<Link> get_link(const string& handle);

vector<shared_ptr<Atom>> get_atoms(const vector<string>& handles);
vector<shared_ptr<Node>> get_nodes(const vector<string>& handles);
vector<shared_ptr<Link>> get_links(const vector<string>& handles);

vector<shared_ptr<Atom>> get_matching_atoms(bool is_toplevel, Atom& key);

shared_ptr<atomdb_api_types::HandleSet> query_for_pattern(const LinkSchema& link_schema);
Expand Down Expand Up @@ -200,6 +204,9 @@ class RedisMongoDB : public AtomDB {

void redis_setup(const JsonConfig& config);
void mongodb_setup(const JsonConfig& config);

shared_ptr<Atom> atom_from_mongodb_document(
const shared_ptr<atomdb_api_types::MongodbDocument>& atom_document);
};

} // namespace atomdb
Loading
Loading