From 005f1a62bdab6099dc1f67c9e2560d6fac51eab8 Mon Sep 17 00:00:00 2001 From: marcocapozzoli Date: Tue, 3 Nov 2026 13:49:54 -0300 Subject: [PATCH 1/4] add get_atoms(), get_nodes and get_links() --- src/atomdb/AtomDB.h | 4 + src/atomdb/KeySensitiveAtomDB.h | 7 + src/atomdb/ProtectedAtomDB.cc | 45 +++++ src/atomdb/ProtectedAtomDB.h | 11 ++ src/atomdb/adapterdb/AdapterDB.cc | 15 ++ src/atomdb/adapterdb/AdapterDB.h | 4 + src/atomdb/inmemorydb/InMemoryDB.cc | 37 +++++ src/atomdb/inmemorydb/InMemoryDB.h | 4 + src/atomdb/redis_mongodb/RedisMongoDB.cc | 97 +++++++---- src/atomdb/redis_mongodb/RedisMongoDB.h | 7 + src/atomdb/remotedb/RemoteAtomDB.cc | 94 +++++++++++ src/atomdb/remotedb/RemoteAtomDB.h | 12 ++ src/atomdb/remotedb/RemoteAtomDBPeer.cc | 111 +++++++++++++ src/atomdb/remotedb/RemoteAtomDBPeer.h | 11 ++ src/tests/cpp/BUILD | 2 +- src/tests/cpp/adapterdb_test.cc | 40 +++++ src/tests/cpp/inmemorydb_test.cc | 29 ++++ src/tests/cpp/protected_atomdb_test.cc | 72 ++++++++ src/tests/cpp/redis_mongodb_test.cc | 36 ++++ .../cpp/remote_atomdb_key_sensitive_test.cc | 155 ++++++++++++++++++ src/tests/cpp/remote_atomdb_test.cc | 83 ++++++++++ src/tests/cpp/test_commons/mocks/MockAtomDB.h | 3 + 22 files changed, 848 insertions(+), 31 deletions(-) diff --git a/src/atomdb/AtomDB.h b/src/atomdb/AtomDB.h index 99b561d58..977a05f4e 100644 --- a/src/atomdb/AtomDB.h +++ b/src/atomdb/AtomDB.h @@ -65,6 +65,10 @@ class AtomDB : public HandleDecoder { virtual shared_ptr get_node(const string& handle) = 0; virtual shared_ptr get_link(const string& handle) = 0; + virtual vector> get_atoms(const vector& handles) = 0; + virtual vector> get_nodes(const vector& handles) = 0; + virtual vector> get_links(const vector& handles) = 0; + virtual vector> get_matching_atoms(bool is_toplevel, Atom& key) = 0; virtual shared_ptr query_for_pattern(const LinkSchema& link_schema) = 0; diff --git a/src/atomdb/KeySensitiveAtomDB.h b/src/atomdb/KeySensitiveAtomDB.h index 1c09d1e50..a2042d4ec 100644 --- a/src/atomdb/KeySensitiveAtomDB.h +++ b/src/atomdb/KeySensitiveAtomDB.h @@ -31,6 +31,13 @@ class KeySensitiveAtomDB { virtual shared_ptr get_node(const string& handle, shared_ptr keychain) = 0; virtual shared_ptr get_link(const string& handle, shared_ptr keychain) = 0; + virtual vector> get_atoms(const vector& handles, + shared_ptr keychain) = 0; + virtual vector> get_nodes(const vector& handles, + shared_ptr keychain) = 0; + virtual vector> get_links(const vector& handles, + shared_ptr keychain) = 0; + virtual vector> get_matching_atoms(bool is_toplevel, Atom& key, shared_ptr keychain) = 0; diff --git a/src/atomdb/ProtectedAtomDB.cc b/src/atomdb/ProtectedAtomDB.cc index eb8634004..068cf2979 100644 --- a/src/atomdb/ProtectedAtomDB.cc +++ b/src/atomdb/ProtectedAtomDB.cc @@ -56,6 +56,39 @@ shared_ptr ProtectedAtomDB::get_link(const string& handle, shared_ptr(this->get_atom(handle, keychain)); } +vector> ProtectedAtomDB::get_atoms(const vector& handles, + shared_ptr 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> ProtectedAtomDB::get_nodes(const vector& handles, + shared_ptr keychain) { + auto atoms = this->get_atoms(handles, keychain); + vector> nodes; + for (const auto& atom : atoms) { + if (auto node = dynamic_pointer_cast(atom)) { + nodes.push_back(node); + } + } + return nodes; +} + +vector> ProtectedAtomDB::get_links(const vector& handles, + shared_ptr keychain) { + auto atoms = this->get_atoms(handles, keychain); + vector> links; + for (const auto& atom : atoms) { + if (auto link = dynamic_pointer_cast(atom)) { + links.push_back(link); + } + } + return links; +} + vector> ProtectedAtomDB::get_matching_atoms(bool is_toplevel, Atom& key, shared_ptr keychain) { @@ -236,6 +269,18 @@ shared_ptr ProtectedAtomDB::get_node(const string& handle) { raise_keychai shared_ptr ProtectedAtomDB::get_link(const string& handle) { raise_keychain_required("get_link"); } +vector> ProtectedAtomDB::get_atoms(const vector& handles) { + raise_keychain_required("get_atoms"); +} + +vector> ProtectedAtomDB::get_nodes(const vector& handles) { + raise_keychain_required("get_nodes"); +} + +vector> ProtectedAtomDB::get_links(const vector& handles) { + raise_keychain_required("get_links"); +} + vector> ProtectedAtomDB::get_matching_atoms(bool is_toplevel, Atom& key) { raise_keychain_required("get_matching_atoms"); } diff --git a/src/atomdb/ProtectedAtomDB.h b/src/atomdb/ProtectedAtomDB.h index ed5d91fbf..a85f7931a 100644 --- a/src/atomdb/ProtectedAtomDB.h +++ b/src/atomdb/ProtectedAtomDB.h @@ -56,6 +56,17 @@ class ProtectedAtomDB : public AtomDB, public KeySensitiveAtomDB { shared_ptr get_link(const string& handle) override; shared_ptr get_link(const string& handle, shared_ptr keychain) override; + vector> get_atoms(const vector& handles) override; + vector> get_nodes(const vector& handles) override; + vector> get_links(const vector& handles) override; + + vector> get_atoms(const vector& handles, + shared_ptr keychain) override; + vector> get_nodes(const vector& handles, + shared_ptr keychain) override; + vector> get_links(const vector& handles, + shared_ptr keychain) override; + vector> get_matching_atoms(bool is_toplevel, Atom& key) override; vector> get_matching_atoms(bool is_toplevel, Atom& key, diff --git a/src/atomdb/adapterdb/AdapterDB.cc b/src/atomdb/adapterdb/AdapterDB.cc index f11aa082c..65efd9d92 100644 --- a/src/atomdb/adapterdb/AdapterDB.cc +++ b/src/atomdb/adapterdb/AdapterDB.cc @@ -86,6 +86,21 @@ shared_ptr AdapterDB::get_link(const string& handle) { return this->atomdb_backend->get_link(handle); } +vector> AdapterDB::get_atoms(const vector& handles) { + this->ensure_backend_ready(); + return this->atomdb_backend->get_atoms(handles); +} + +vector> AdapterDB::get_nodes(const vector& handles) { + this->ensure_backend_ready(); + return this->atomdb_backend->get_nodes(handles); +} + +vector> AdapterDB::get_links(const vector& handles) { + this->ensure_backend_ready(); + return this->atomdb_backend->get_links(handles); +} + vector> AdapterDB::get_matching_atoms(bool is_toplevel, Atom& key) { this->ensure_backend_ready(); return this->atomdb_backend->get_matching_atoms(is_toplevel, key); diff --git a/src/atomdb/adapterdb/AdapterDB.h b/src/atomdb/adapterdb/AdapterDB.h index 9c648d038..52810890e 100644 --- a/src/atomdb/adapterdb/AdapterDB.h +++ b/src/atomdb/adapterdb/AdapterDB.h @@ -66,6 +66,10 @@ class AdapterDB : public AtomDB { shared_ptr get_node(const string& handle) override; shared_ptr get_link(const string& handle) override; + vector> get_atoms(const vector& handles) override; + vector> get_nodes(const vector& handles) override; + vector> get_links(const vector& handles) override; + vector> get_matching_atoms(bool is_toplevel, Atom& key) override; shared_ptr query_for_pattern(const LinkSchema& link_schema) override; diff --git a/src/atomdb/inmemorydb/InMemoryDB.cc b/src/atomdb/inmemorydb/InMemoryDB.cc index deb91d5e2..7ddbbb9b1 100644 --- a/src/atomdb/inmemorydb/InMemoryDB.cc +++ b/src/atomdb/inmemorydb/InMemoryDB.cc @@ -177,6 +177,43 @@ shared_ptr InMemoryDB::get_link(const string& handle) { return dynamic_pointer_cast(atom); } +vector> InMemoryDB::get_atoms(const vector& handles) { + auto trie = load_tries()->atoms; + vector> atoms; + atoms.reserve(handles.size()); + for (const auto& handle : handles) { + auto atom = lookup_atom(*trie, handle); + if (atom == nullptr) continue; + auto cloned = clone_atom(atom.get()); + if (cloned != nullptr) { + atoms.push_back(cloned); + } + } + return atoms; +} + +vector> InMemoryDB::get_nodes(const vector& handles) { + auto atoms = this->get_atoms(handles); + vector> nodes; + for (const auto& atom : atoms) { + if (auto node = dynamic_pointer_cast(atom)) { + nodes.push_back(node); + } + } + return nodes; +} + +vector> InMemoryDB::get_links(const vector& handles) { + auto atoms = this->get_atoms(handles); + vector> links; + for (const auto& atom : atoms) { + if (auto link = dynamic_pointer_cast(atom)) { + links.push_back(link); + } + } + return links; +} + shared_ptr InMemoryDB::query_for_pattern(const LinkSchema& link_schema) { auto handle_set = make_shared(); auto handles = lookup_handle_set(*load_tries()->patterns, link_schema.handle()); diff --git a/src/atomdb/inmemorydb/InMemoryDB.h b/src/atomdb/inmemorydb/InMemoryDB.h index a6ef11351..593ab07dd 100644 --- a/src/atomdb/inmemorydb/InMemoryDB.h +++ b/src/atomdb/inmemorydb/InMemoryDB.h @@ -58,6 +58,10 @@ class InMemoryDB : public AtomDB { shared_ptr get_node(const string& handle) override; shared_ptr get_link(const string& handle) override; + vector> get_atoms(const vector& handles) override; + vector> get_nodes(const vector& handles) override; + vector> get_links(const vector& handles) override; + vector> get_matching_atoms(bool is_toplevel, Atom& key) override; shared_ptr query_for_pattern(const LinkSchema& link_schema) override; diff --git a/src/atomdb/redis_mongodb/RedisMongoDB.cc b/src/atomdb/redis_mongodb/RedisMongoDB.cc index 4487342ca..fda62c5f6 100644 --- a/src/atomdb/redis_mongodb/RedisMongoDB.cc +++ b/src/atomdb/redis_mongodb/RedisMongoDB.cc @@ -153,36 +153,8 @@ void RedisMongoDB::mongodb_setup(const JsonConfig& config) { } shared_ptr RedisMongoDB::get_atom(const string& handle) { - auto atom_document = - dynamic_pointer_cast(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 (atom_document->contains(MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS])) { - unsigned int arity = atom_document->get_size(MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS]); - vector 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( - atom_document->get("named_type"), targets, is_toplevel, custom_attributes); - } else { - return make_shared(atom_document->get("named_type"), - atom_document->get("name"), - is_toplevel, - custom_attributes); - } - } else { - return shared_ptr(NULL); - } + return atom_from_mongodb_document( + dynamic_pointer_cast(get_atom_document(handle))); } shared_ptr RedisMongoDB::get_node(const string& handle) { @@ -193,6 +165,71 @@ shared_ptr RedisMongoDB::get_link(const string& handle) { return dynamic_pointer_cast(get_atom(handle)); } +vector> RedisMongoDB::get_atoms(const vector& handles) { + auto atom_documents = this->get_atom_documents(handles, {}); + + vector> atoms; + for (const auto& atom_document : atom_documents) { + auto atom = atom_from_mongodb_document( + dynamic_pointer_cast(atom_document)); + if (atom != NULL) { + atoms.push_back(atom); + } + } + return atoms; +} + +vector> RedisMongoDB::get_nodes(const vector& handles) { + auto atoms = this->get_atoms(handles); + + vector> nodes; + for (const auto& atom : atoms) { + if (auto node = dynamic_pointer_cast(atom)) { + nodes.push_back(node); + } + } + return nodes; +} + +vector> RedisMongoDB::get_links(const vector& handles) { + auto atoms = this->get_atoms(handles); + + vector> links; + for (const auto& atom : atoms) { + if (auto link = dynamic_pointer_cast(atom)) { + links.push_back(link); + } + } + return links; +} + +shared_ptr RedisMongoDB::atom_from_mongodb_document( + const shared_ptr& atom_document) { + if (atom_document == NULL) { + return shared_ptr(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 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( + atom_document->get("named_type"), targets, is_toplevel, custom_attributes); + } + return make_shared( + atom_document->get("named_type"), atom_document->get("name"), is_toplevel, custom_attributes); +} + shared_ptr RedisMongoDB::query_for_pattern(const LinkSchema& link_schema) { if (skip_redis_) return nullptr; diff --git a/src/atomdb/redis_mongodb/RedisMongoDB.h b/src/atomdb/redis_mongodb/RedisMongoDB.h index 642b97dcd..73474d9e8 100644 --- a/src/atomdb/redis_mongodb/RedisMongoDB.h +++ b/src/atomdb/redis_mongodb/RedisMongoDB.h @@ -60,6 +60,10 @@ class RedisMongoDB : public AtomDB { shared_ptr get_node(const string& handle); shared_ptr get_link(const string& handle); + vector> get_atoms(const vector& handles); + vector> get_nodes(const vector& handles); + vector> get_links(const vector& handles); + vector> get_matching_atoms(bool is_toplevel, Atom& key); shared_ptr query_for_pattern(const LinkSchema& link_schema); @@ -199,6 +203,9 @@ class RedisMongoDB : public AtomDB { void redis_setup(const JsonConfig& config); void mongodb_setup(const JsonConfig& config); + + shared_ptr atom_from_mongodb_document( + const shared_ptr& atom_document); }; } // namespace atomdb diff --git a/src/atomdb/remotedb/RemoteAtomDB.cc b/src/atomdb/remotedb/RemoteAtomDB.cc index 1b3818fc2..58a5f4fd1 100644 --- a/src/atomdb/remotedb/RemoteAtomDB.cc +++ b/src/atomdb/remotedb/RemoteAtomDB.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -79,6 +80,18 @@ shared_ptr RemoteAtomDB::get_link(const string& handle) { return dynamic_pointer_cast(atom); } +vector> RemoteAtomDB::get_atoms(const vector& handles) { + return this->get_atoms(handles, nullptr); +} + +vector> RemoteAtomDB::get_nodes(const vector& handles) { + return this->get_nodes(handles, nullptr); +} + +vector> RemoteAtomDB::get_links(const vector& handles) { + return this->get_links(handles, nullptr); +} + vector> RemoteAtomDB::get_matching_atoms(bool is_toplevel, Atom& key) { return this->get_matching_atoms(is_toplevel, key, nullptr); } @@ -326,6 +339,87 @@ shared_ptr RemoteAtomDB::get_link(const string& handle, shared_ptr(this->get_atom(handle, keychain)); } +vector> RemoteAtomDB::get_atoms(const vector& handles, + shared_ptr keychain) { + if (handles.empty()) { + return {}; + } + + vector> atoms; + unordered_set found; + vector pending = handles; + + // Adds the atom to the result only if it hasn't been found yet + auto add_if_new = [&](const shared_ptr& atom, const string& uid) { + if (!atom || !found.insert(atom->handle()).second) return; + LOG_DEBUG("get_atoms(" << atom->handle() << ") fetched from peer [" << uid << "]"); + atoms.push_back(atom); + }; + + // Recomputes which handles are still missing + auto update_pending = [&]() { + pending.clear(); + for (const auto& h : handles) { + if (found.find(h) == found.end()) { + pending.push_back(h); + } + } + }; + + for (auto& [uid, peer] : writable_peers_) { + for (const auto& atom : peer->get_atoms(pending, keychain)) { + add_if_new(atom, uid); + } + update_pending(); + } + if (pending.empty()) { + return atoms; + } + + for (auto& [uid, peer] : readonly_peers_) { + for (const auto& h : pending) { + add_if_new(peer->get_cached_atom(h, keychain), uid); + } + update_pending(); + } + if (pending.empty()) { + return atoms; + } + + for (auto& [uid, peer] : readonly_peers_) { + for (const auto& atom : peer->get_atoms(pending, keychain)) { + add_if_new(atom, uid); + } + update_pending(); + } + + return atoms; +} + +vector> RemoteAtomDB::get_nodes(const vector& handles, + shared_ptr keychain) { + auto atoms = this->get_atoms(handles, keychain); + vector> nodes; + for (const auto& atom : atoms) { + if (auto node = dynamic_pointer_cast(atom)) { + nodes.push_back(node); + } + } + return nodes; +} + +vector> RemoteAtomDB::get_links(const vector& handles, + shared_ptr keychain) { + auto atoms = this->get_atoms(handles, keychain); + vector> links; + for (const auto& atom : atoms) { + if (auto link = dynamic_pointer_cast(atom)) { + links.push_back(link); + } + } + return links; +} + vector> RemoteAtomDB::get_matching_atoms(bool is_toplevel, Atom& key, shared_ptr keychain) { diff --git a/src/atomdb/remotedb/RemoteAtomDB.h b/src/atomdb/remotedb/RemoteAtomDB.h index b6b362c7a..90add486c 100644 --- a/src/atomdb/remotedb/RemoteAtomDB.h +++ b/src/atomdb/remotedb/RemoteAtomDB.h @@ -39,6 +39,18 @@ class RemoteAtomDB : public AtomDB, public KeySensitiveAtomDB { shared_ptr get_link(const string& handle) override; shared_ptr get_link(const string& handle, shared_ptr keychain) override; + vector> get_atoms(const vector& handles) override; + vector> get_atoms(const vector& handles, + shared_ptr keychain) override; + + vector> get_nodes(const vector& handles) override; + vector> get_nodes(const vector& handles, + shared_ptr keychain) override; + + vector> get_links(const vector& handles) override; + vector> get_links(const vector& handles, + shared_ptr keychain) override; + vector> get_matching_atoms(bool is_toplevel, Atom& key) override; vector> get_matching_atoms(bool is_toplevel, Atom& key, diff --git a/src/atomdb/remotedb/RemoteAtomDBPeer.cc b/src/atomdb/remotedb/RemoteAtomDBPeer.cc index 81cce879c..7e6ce7ee6 100644 --- a/src/atomdb/remotedb/RemoteAtomDBPeer.cc +++ b/src/atomdb/remotedb/RemoteAtomDBPeer.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -111,6 +112,18 @@ shared_ptr RemoteAtomDBPeer::get_link(const string& handle) { return dynamic_pointer_cast(atom); } +vector> RemoteAtomDBPeer::get_atoms(const vector& handles) { + return this->get_atoms(handles, nullptr); +} + +vector> RemoteAtomDBPeer::get_nodes(const vector& handles) { + return this->get_nodes(handles, nullptr); +} + +vector> RemoteAtomDBPeer::get_links(const vector& handles) { + return this->get_links(handles, nullptr); +} + shared_ptr RemoteAtomDBPeer::get_cached_atom(const string& handle, shared_ptr keychain) { auto pwb = protected_write_buffer(); if (auto atom = pwb ? pwb->get_atom(handle, keychain) : write_buffer()->get_atom(handle)) { @@ -737,6 +750,104 @@ shared_ptr RemoteAtomDBPeer::get_link(const string& handle, shared_ptr(this->get_atom(handle, keychain)); } +vector> RemoteAtomDBPeer::get_atoms(const vector& handles, + shared_ptr keychain) { + if (handles.empty()) { + return {}; + } + + vector> atoms; + unordered_set found; + vector pending = handles; + + auto update_pending = [&]() { + pending.clear(); + for (const auto& h : handles) { + if (found.find(h) == found.end()) { + pending.push_back(h); + } + } + }; + + auto pwb = protected_write_buffer(); + auto from_write_buffer = + pwb ? pwb->get_atoms(pending, keychain) : write_buffer()->get_atoms(pending); + for (const auto& atom : from_write_buffer) { + if (found.insert(atom->handle()).second) { + atoms.push_back(atom); + } + } + update_pending(); + if (pending.empty()) { + return atoms; + } + + auto rc = read_cache(); + if (local_persistence_) { + for (const auto& atom : local_persistence_->get_atoms(pending)) { + LOG_DEBUG("[RemoteDB(" << uid_ << ")] get_atoms(" << atom->handle() + << ") <- local_persistence (warmed into read_cache)"); + rc->add_atom(atom.get()); + if (found.insert(atom->handle()).second) { + atoms.push_back(atom); + } + } + update_pending(); + if (pending.empty()) { + return atoms; + } + } + + auto prc = protected_read_cache(); + auto from_read_cache = prc ? prc->get_atoms(pending, keychain) : rc->get_atoms(pending); + for (const auto& atom : from_read_cache) { + if (found.insert(atom->handle()).second) { + atoms.push_back(atom); + } + } + update_pending(); + if (pending.empty()) { + return atoms; + } + + auto from_remote = this->protected_atomdb_ ? this->protected_atomdb_->get_atoms(pending, keychain) + : this->atomdb_->get_atoms(pending); + for (const auto& atom : from_remote) { + LOG_DEBUG("[RemoteDB(" << uid_ << ")] get_atoms(" << atom->handle() + << ") <- remote atomdb (warmed)"); + rc->add_atom(atom.get()); + if (found.insert(atom->handle()).second) { + atoms.push_back(atom); + } + } + + return atoms; +} + +vector> RemoteAtomDBPeer::get_nodes(const vector& handles, + shared_ptr keychain) { + auto atoms = this->get_atoms(handles, keychain); + vector> nodes; + for (const auto& atom : atoms) { + if (auto node = dynamic_pointer_cast(atom)) { + nodes.push_back(node); + } + } + return nodes; +} + +vector> RemoteAtomDBPeer::get_links(const vector& handles, + shared_ptr keychain) { + auto atoms = this->get_atoms(handles, keychain); + vector> links; + for (const auto& atom : atoms) { + if (auto link = dynamic_pointer_cast(atom)) { + links.push_back(link); + } + } + return links; +} + vector> RemoteAtomDBPeer::get_matching_atoms(bool is_toplevel, Atom& key, shared_ptr keychain) { diff --git a/src/atomdb/remotedb/RemoteAtomDBPeer.h b/src/atomdb/remotedb/RemoteAtomDBPeer.h index 410e88937..e7f6a3ba4 100644 --- a/src/atomdb/remotedb/RemoteAtomDBPeer.h +++ b/src/atomdb/remotedb/RemoteAtomDBPeer.h @@ -55,6 +55,17 @@ class RemoteAtomDBPeer : public AtomDB, public processor::ThreadMethod, public K shared_ptr get_link(const string& handle) override; shared_ptr get_link(const string& handle, shared_ptr keychain) override; + vector> get_atoms(const vector& handles) override; + vector> get_nodes(const vector& handles) override; + vector> get_links(const vector& handles) override; + + vector> get_atoms(const vector& handles, + shared_ptr keychain) override; + vector> get_nodes(const vector& handles, + shared_ptr keychain) override; + vector> get_links(const vector& handles, + shared_ptr keychain) override; + // In-memory lookups only (write_buffer + read_cache). Used by the RemoteAtomDB facade // to probe every peer's cache before escalating any peer to its backend. shared_ptr get_cached_atom(const string& handle, shared_ptr keychain = nullptr); diff --git a/src/tests/cpp/BUILD b/src/tests/cpp/BUILD index f12f93266..1122921c0 100644 --- a/src/tests/cpp/BUILD +++ b/src/tests/cpp/BUILD @@ -950,7 +950,7 @@ cc_test( "//tests/cpp/test_commons:in_memory_access_permission_types", "//tests/cpp/test_commons:mock_animals_data_lib", "//tests/cpp/test_commons:test_atomdb_json_config", - "@com_github_google_googletest//:gtest_main", + "@com_github_google_googletest//:gtest", "@mbedtls", ], ) diff --git a/src/tests/cpp/adapterdb_test.cc b/src/tests/cpp/adapterdb_test.cc index 939949e31..f9ef01906 100644 --- a/src/tests/cpp/adapterdb_test.cc +++ b/src/tests/cpp/adapterdb_test.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -343,6 +344,44 @@ TEST_P(AdapterDBTest, AddGetAndQueryTargetsLink) { delete link; } +TEST_P(AdapterDBTest, GetAtomsNodesAndLinks) { + auto db = create_current_adapter(); + ASSERT_NE(db, nullptr); + + auto n1 = new Node("Symbol", "AdapterGetAtomsNode1"); + auto n2 = new Node("Symbol", "AdapterGetAtomsNode2"); + auto n3 = new Node("Symbol", "AdapterGetAtomsNode3"); + auto h1 = db->add_node(n1); + auto h2 = db->add_node(n2); + auto h3 = db->add_node(n3); + auto link = new Link("Expression", {h1, h2, h3}); + auto link_handle = db->add_link(link); + string missing_handle = "missing" + Utils::random_string(25); + + vector handles = {h1, link_handle, missing_handle, h2}; + + set atoms; + for (const auto& atom : db->get_atoms(handles)) atoms.insert(atom->handle()); + EXPECT_EQ(atoms, set({h1, h2, link_handle})); + + set nodes; + for (const auto& node : db->get_nodes(handles)) nodes.insert(node->handle()); + EXPECT_EQ(nodes, set({h1, h2})); + + set links; + for (const auto& fetched_link : db->get_links(handles)) links.insert(fetched_link->handle()); + EXPECT_EQ(links, set({link_handle})); + + EXPECT_TRUE(db->delete_link(link_handle)); + EXPECT_TRUE(db->delete_node(h1)); + EXPECT_TRUE(db->delete_node(h2)); + EXPECT_TRUE(db->delete_node(h3)); + delete n1; + delete n2; + delete n3; + delete link; +} + TEST_P(AdapterDBTest, AddLinkFailsWhenTargetsDoNotExist) { auto db = create_current_adapter(); ASSERT_NE(db, nullptr); @@ -541,5 +580,6 @@ TEST_F(AdapterDBConstructorFailureTest, ConstructorFailsWhenMappingFileDoesNotEx int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); + Utils::init_random(0); return RUN_ALL_TESTS(); } diff --git a/src/tests/cpp/inmemorydb_test.cc b/src/tests/cpp/inmemorydb_test.cc index 02b18e2ae..ed5d86031 100644 --- a/src/tests/cpp/inmemorydb_test.cc +++ b/src/tests/cpp/inmemorydb_test.cc @@ -19,6 +19,7 @@ #include "Merger.h" #include "Node.h" #include "Properties.h" +#include "Utils.h" using namespace atomdb; using namespace atomdb::atomdb_api_types; @@ -85,6 +86,33 @@ TEST_F(InMemoryDBTest, AddNodesAndLinks) { EXPECT_EQ(retrieved_link1->handle(), link1_handle); } +TEST_F(InMemoryDBTest, GetAtomsNodesAndLinks) { + auto human = new Node("Symbol", "\"human\""); + auto monkey = new Node("Symbol", "\"monkey\""); + auto similarity = new Node("Symbol", "Similarity"); + + string human_handle = db->add_node(human); + string monkey_handle = db->add_node(monkey); + string similarity_handle = db->add_node(similarity); + auto link = new Link("Expression", {similarity_handle, human_handle, monkey_handle}); + string link_handle = db->add_link(link); + string missing_handle = "missing" + Utils::random_string(25); + + vector handles = {human_handle, link_handle, missing_handle, monkey_handle}; + + set atoms; + for (const auto& atom : db->get_atoms(handles)) atoms.insert(atom->handle()); + EXPECT_EQ(atoms, set({human_handle, monkey_handle, link_handle})); + + set nodes; + for (const auto& node : db->get_nodes(handles)) nodes.insert(node->handle()); + EXPECT_EQ(nodes, set({human_handle, monkey_handle})); + + set links; + for (const auto& fetched_link : db->get_links(handles)) links.insert(fetched_link->handle()); + EXPECT_EQ(links, set({link_handle})); +} + TEST_F(InMemoryDBTest, QueryForPattern) { auto human = new Node("Symbol", "\"human\""); auto monkey = new Node("Symbol", "\"monkey\""); @@ -1447,5 +1475,6 @@ TEST_F(InMemoryDBTest, IsUnprotected) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); + Utils::init_random(0); return RUN_ALL_TESTS(); } diff --git a/src/tests/cpp/protected_atomdb_test.cc b/src/tests/cpp/protected_atomdb_test.cc index 8f619a724..1e96e1d6b 100644 --- a/src/tests/cpp/protected_atomdb_test.cc +++ b/src/tests/cpp/protected_atomdb_test.cc @@ -21,6 +21,7 @@ #include "ProtectedAtomDB.h" #include "RedisMongoDB.h" #include "TestAtomDBJsonConfig.h" +#include "Utils.h" using namespace atomdb; using namespace atomdb_api_types; @@ -221,6 +222,9 @@ TEST(ProtectedAtomDBTest, ProtectedMethodsRequireKeychain) { LinkSchema schema("Expression", 2); EXPECT_THROW(protected_atomdb->db->get_atom("handle"), runtime_error); + EXPECT_THROW(protected_atomdb->db->get_atoms({"handle"}), runtime_error); + EXPECT_THROW(protected_atomdb->db->get_nodes({"handle"}), runtime_error); + EXPECT_THROW(protected_atomdb->db->get_links({"handle"}), runtime_error); EXPECT_THROW(protected_atomdb->db->query_for_pattern(schema), runtime_error); EXPECT_THROW(protected_atomdb->db->atom_exists("handle"), runtime_error); EXPECT_THROW(protected_atomdb->db->atoms_exist({"handle"}), runtime_error); @@ -361,6 +365,68 @@ TEST(ProtectedAtomDBTest, ReadOperationsReturnOnlyAuthorizedAtoms) { set({animals.human})); } +TEST(ProtectedAtomDBTest, GetAtomsNodesAndLinksReturnOnlyAuthorizedAtoms) { + shared_ptr protected_atomdb = make_shared(true); + protected_atomdb->grant_full_access(PKAdmin); + protected_atomdb->grant_link_template(PKSimilarityHuman, similarity_human_tokens()); + auto admin_keys = protected_atomdb->keys(PKAdmin); + auto similarity_human_keys = protected_atomdb->keys(PKSimilarityHuman); + Animals animals; + string missing_handle = "missing" + Utils::random_string(25); + vector handles = {animals.similarity_human_monkey, + animals.similarity_human_chimp, + animals.similarity_snake_vine, + animals.inheritance_human_mammal, + animals.human, + missing_handle}; + + EXPECT_TRUE(protected_atomdb->db->get_atoms(handles, nullptr).empty()); + EXPECT_TRUE(protected_atomdb->db->get_nodes(handles, nullptr).empty()); + EXPECT_TRUE(protected_atomdb->db->get_links(handles, nullptr).empty()); + + set similarity_atoms; + for (const auto& atom : protected_atomdb->db->get_atoms(handles, similarity_human_keys)) { + similarity_atoms.insert(atom->handle()); + } + EXPECT_EQ(similarity_atoms, + set({animals.similarity_human_monkey, animals.similarity_human_chimp})); + EXPECT_TRUE(protected_atomdb->db->get_nodes(handles, similarity_human_keys).empty()); + + set similarity_links; + for (const auto& link : protected_atomdb->db->get_links(handles, similarity_human_keys)) { + similarity_links.insert(link->handle()); + } + EXPECT_EQ(similarity_links, + set({animals.similarity_human_monkey, animals.similarity_human_chimp})); + + set admin_atoms; + for (const auto& atom : protected_atomdb->db->get_atoms(handles, admin_keys)) { + admin_atoms.insert(atom->handle()); + } + EXPECT_EQ(admin_atoms, + set({animals.similarity_human_monkey, + animals.similarity_human_chimp, + animals.similarity_snake_vine, + animals.inheritance_human_mammal, + animals.human})); + + set admin_nodes; + for (const auto& node : protected_atomdb->db->get_nodes(handles, admin_keys)) { + admin_nodes.insert(node->handle()); + } + EXPECT_EQ(admin_nodes, set({animals.human})); + + set admin_links; + for (const auto& link : protected_atomdb->db->get_links(handles, admin_keys)) { + admin_links.insert(link->handle()); + } + EXPECT_EQ(admin_links, + set({animals.similarity_human_monkey, + animals.similarity_human_chimp, + animals.similarity_snake_vine, + animals.inheritance_human_mammal})); +} + TEST(ProtectedAtomDBTest, ExistenceChecksPreserveNodeAndLinkTypes) { shared_ptr protected_atomdb = make_shared(true); protected_atomdb->grant_full_access(PKAdmin); @@ -619,3 +685,9 @@ TEST(ProtectedAtomDBTest, NestedRelatedGrantDoesNotImplyInnerSimilarity) { similarity_human_keys)) .empty()); } + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + Utils::init_random(0); + return RUN_ALL_TESTS(); +} diff --git a/src/tests/cpp/redis_mongodb_test.cc b/src/tests/cpp/redis_mongodb_test.cc index db3b988e6..d70e3ef1d 100644 --- a/src/tests/cpp/redis_mongodb_test.cc +++ b/src/tests/cpp/redis_mongodb_test.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -24,12 +25,14 @@ #include "RedisMongoDB.h" #include "TestAtomDBJsonConfig.h" #include "UntypedVariable.h" +#include "Utils.h" #include "Wildcard.h" #include "expression_hasher.h" using namespace atomdb; using namespace atomdb::atomdb_api_types; using namespace atoms; +using namespace commons; using namespace std; namespace { @@ -469,6 +472,38 @@ TEST_F(RedisMongoDBTest, AddGetAndDeleteLink) { EXPECT_TRUE(db->delete_atom(test_2_node_handle)); } +TEST_F(RedisMongoDBTest, GetAtomsNodesAndLinks) { + auto human = new Node("Symbol", "\"get-atoms-human\""); + auto monkey = new Node("Symbol", "\"get-atoms-monkey\""); + auto similarity = new Node("Symbol", "GetAtomsSimilarity"); + + string human_handle = db->add_node(human); + string monkey_handle = db->add_node(monkey); + string similarity_handle = db->add_node(similarity); + auto link = new Link("Expression", {similarity_handle, human_handle, monkey_handle}); + string link_handle = db->add_link(link); + string missing_handle = "missing" + Utils::random_string(25); + + vector handles = {human_handle, link_handle, missing_handle, monkey_handle}; + + set atoms; + for (const auto& atom : db->get_atoms(handles)) atoms.insert(atom->handle()); + EXPECT_EQ(atoms, set({human_handle, monkey_handle, link_handle})); + + set nodes; + for (const auto& node : db->get_nodes(handles)) nodes.insert(node->handle()); + EXPECT_EQ(nodes, set({human_handle, monkey_handle})); + + set links; + for (const auto& fetched_link : db->get_links(handles)) links.insert(fetched_link->handle()); + EXPECT_EQ(links, set({link_handle})); + + EXPECT_TRUE(db->delete_atom(link_handle)); + EXPECT_TRUE(db->delete_atom(human_handle)); + EXPECT_TRUE(db->delete_atom(monkey_handle)); + EXPECT_TRUE(db->delete_atom(similarity_handle)); +} + TEST_F(RedisMongoDBTest, AddAndDeleteLinks) { vector links; vector test_node_handles; @@ -1774,5 +1809,6 @@ TEST(RedisMongoDBPrefixIsolation, InstancesKeepIndependentNamespaces) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::testing::AddGlobalTestEnvironment(new RedisMongoDBTestEnvironment()); + Utils::init_random(0); return RUN_ALL_TESTS(); } diff --git a/src/tests/cpp/remote_atomdb_key_sensitive_test.cc b/src/tests/cpp/remote_atomdb_key_sensitive_test.cc index d76743c8e..8f281f510 100644 --- a/src/tests/cpp/remote_atomdb_key_sensitive_test.cc +++ b/src/tests/cpp/remote_atomdb_key_sensitive_test.cc @@ -246,6 +246,161 @@ TEST_F(RemoteAtomDBKeySensitiveTest, NullptrKeychainReadsOnlyTheUnprotectedPeer) set({this->similarity_human_chimp_handle})); } +TEST_F(RemoteAtomDBKeySensitiveTest, GetAtomsNodesAndLinksWithoutKeychain) { + vector handles = {this->similarity_human_monkey_handle, + this->similarity_human_chimp_handle, + this->similarity_snake_vine_handle, + this->inheritance_human_mammal_handle, + this->human_handle, + this->mammal_handle}; + + set atoms; + for (const auto& atom : this->db->get_atoms(handles)) atoms.insert(atom->handle()); + set atoms_with_null_keychain; + for (const auto& atom : this->db->get_atoms(handles, nullptr)) { + atoms_with_null_keychain.insert(atom->handle()); + } + EXPECT_EQ(atoms, + set({this->similarity_human_chimp_handle, + this->similarity_snake_vine_handle, + this->human_handle})); + EXPECT_EQ(atoms_with_null_keychain, atoms); + + set nodes; + for (const auto& node : this->db->get_nodes(handles)) nodes.insert(node->handle()); + set nodes_with_null_keychain; + for (const auto& node : this->db->get_nodes(handles, nullptr)) nodes_with_null_keychain.insert(node->handle()); + EXPECT_EQ(nodes, set({this->human_handle})); + EXPECT_EQ(nodes_with_null_keychain, nodes); + + set links; + for (const auto& link : this->db->get_links(handles)) links.insert(link->handle()); + set links_with_null_keychain; + for (const auto& link : this->db->get_links(handles, nullptr)) links_with_null_keychain.insert(link->handle()); + EXPECT_EQ(links, set({this->similarity_human_chimp_handle, this->similarity_snake_vine_handle})); + EXPECT_EQ(links_with_null_keychain, links); +} + +TEST_F(RemoteAtomDBKeySensitiveTest, GetAtomsNodesAndLinksWithKeychain) { + auto similarity_key = this->similarity_keychain(); + auto full_access_key = this->full_access_keychain(); + vector handles = {this->similarity_human_monkey_handle, + this->similarity_human_chimp_handle, + this->similarity_snake_vine_handle, + this->inheritance_human_mammal_handle, + this->human_handle, + this->mammal_handle}; + + set similarity_atoms; + for (const auto& atom : this->db->get_atoms(handles, similarity_key)) { + similarity_atoms.insert(atom->handle()); + } + EXPECT_EQ(similarity_atoms, + set({this->similarity_human_monkey_handle, + this->similarity_human_chimp_handle, + this->similarity_snake_vine_handle, + this->human_handle})); + + set similarity_nodes; + for (const auto& node : this->db->get_nodes(handles, similarity_key)) { + similarity_nodes.insert(node->handle()); + } + EXPECT_EQ(similarity_nodes, set({this->human_handle})); + + set similarity_links; + for (const auto& link : this->db->get_links(handles, similarity_key)) { + similarity_links.insert(link->handle()); + } + EXPECT_EQ(similarity_links, + set({this->similarity_human_monkey_handle, + this->similarity_human_chimp_handle, + this->similarity_snake_vine_handle})); + + set full_access_atoms; + for (const auto& atom : this->db->get_atoms(handles, full_access_key)) { + full_access_atoms.insert(atom->handle()); + } + EXPECT_EQ(full_access_atoms, + set({this->similarity_human_chimp_handle, + this->similarity_snake_vine_handle, + this->inheritance_human_mammal_handle, + this->human_handle, + this->mammal_handle})); + + set full_access_nodes; + for (const auto& node : this->db->get_nodes(handles, full_access_key)) { + full_access_nodes.insert(node->handle()); + } + EXPECT_EQ(full_access_nodes, set({this->human_handle, this->mammal_handle})); + + set full_access_links; + for (const auto& link : this->db->get_links(handles, full_access_key)) { + full_access_links.insert(link->handle()); + } + EXPECT_EQ(full_access_links, + set({this->similarity_human_chimp_handle, + this->similarity_snake_vine_handle, + this->inheritance_human_mammal_handle})); +} + +TEST_F(RemoteAtomDBKeySensitiveTest, PeerGetAtomsNodesAndLinksRespectKeychain) { + auto* similarity_peer = this->db->get_peer("similarity_peer"); + auto* full_access_peer = this->db->get_peer("full_access_peer"); + auto* unprotected_peer = this->db->get_peer("unprotected_peer"); + auto similarity_key = this->similarity_keychain(); + auto full_access_key = this->full_access_keychain(); + + vector handles = {this->similarity_human_monkey_handle, + this->similarity_snake_vine_handle, + this->human_handle, + this->similarity_human_chimp_handle}; + + EXPECT_TRUE(similarity_peer->get_atoms(handles).empty()); + EXPECT_TRUE(similarity_peer->get_atoms(handles, nullptr).empty()); + EXPECT_TRUE(similarity_peer->get_nodes(handles).empty()); + EXPECT_TRUE(similarity_peer->get_links(handles).empty()); + + set granted; + for (const auto& atom : similarity_peer->get_atoms(handles, similarity_key)) { + granted.insert(atom->handle()); + } + EXPECT_EQ(granted, set({this->similarity_human_monkey_handle})); + EXPECT_TRUE(similarity_peer->get_nodes(handles, similarity_key).empty()); + ASSERT_EQ(similarity_peer->get_links(handles, similarity_key).size(), 1u); + EXPECT_EQ(similarity_peer->get_links(handles, similarity_key)[0]->handle(), + this->similarity_human_monkey_handle); + + set unprotected_atoms; + for (const auto& atom : unprotected_peer->get_atoms(handles)) unprotected_atoms.insert(atom->handle()); + set unprotected_atoms_with_keychain; + for (const auto& atom : unprotected_peer->get_atoms(handles, similarity_key)) { + unprotected_atoms_with_keychain.insert(atom->handle()); + } + EXPECT_EQ(unprotected_atoms, + set({this->similarity_snake_vine_handle, + this->human_handle, + this->similarity_human_chimp_handle})); + EXPECT_EQ(unprotected_atoms_with_keychain, unprotected_atoms); + + vector full_handles = {this->inheritance_human_mammal_handle, + this->mammal_handle, + this->similarity_human_monkey_handle}; + EXPECT_TRUE(full_access_peer->get_atoms(full_handles, similarity_key).empty()); + EXPECT_TRUE(full_access_peer->get_nodes(full_handles).empty()); + EXPECT_TRUE(full_access_peer->get_links(full_handles, nullptr).empty()); + + set full_atoms; + for (const auto& atom : full_access_peer->get_atoms(full_handles, full_access_key)) { + full_atoms.insert(atom->handle()); + } + EXPECT_EQ(full_atoms, set({this->inheritance_human_mammal_handle, this->mammal_handle})); + ASSERT_EQ(full_access_peer->get_nodes(full_handles, full_access_key).size(), 1u); + EXPECT_EQ(full_access_peer->get_nodes(full_handles, full_access_key)[0]->handle(), this->mammal_handle); + ASSERT_EQ(full_access_peer->get_links(full_handles, full_access_key).size(), 1u); + EXPECT_EQ(full_access_peer->get_links(full_handles, full_access_key)[0]->handle(), + this->inheritance_human_mammal_handle); +} + TEST_F(RemoteAtomDBKeySensitiveTest, SimilarityKeyDoesNotOpenTheFullAccessDatabase) { auto keychain = this->similarity_keychain(); diff --git a/src/tests/cpp/remote_atomdb_test.cc b/src/tests/cpp/remote_atomdb_test.cc index f1a5a8637..99b97eead 100644 --- a/src/tests/cpp/remote_atomdb_test.cc +++ b/src/tests/cpp/remote_atomdb_test.cc @@ -28,6 +28,7 @@ #include "RemoteAtomDB.h" #include "RemoteAtomDBPeer.h" #include "TestAtomDBJsonConfig.h" +#include "Utils.h" using namespace atomdb; using namespace atomdb::atomdb_api_types; @@ -126,6 +127,49 @@ TEST_F(RemoteAtomDBPeerTest, AddAndGetLinks) { EXPECT_EQ(retrieved->handle(), link_handle); } +TEST_F(RemoteAtomDBPeerTest, GetAtomsNodesAndLinks) { + auto remote_node = new Node("Symbol", "\"remote-node\""); + string remote_handle = remote_->add_node(remote_node); + + auto local_human = new Node("Symbol", "\"local-human\""); + auto local_monkey = new Node("Symbol", "\"local-monkey\""); + auto similarity = new Node("Symbol", "Similarity"); + string local_human_handle = local_->add_node(local_human); + string local_monkey_handle = local_->add_node(local_monkey); + string similarity_handle = local_->add_node(similarity); + auto local_link = new Link("Expression", {similarity_handle, local_human_handle, local_monkey_handle}); + string local_link_handle = local_->add_link(local_link); + + auto dirty = new Node("Symbol", "\"dirty-node\""); + string dirty_handle = peer_->add_node(dirty); + string missing_handle = "missing" + Utils::random_string(25); + + vector handles = {dirty_handle, local_link_handle, remote_handle, missing_handle, local_human_handle}; + + set atoms; + for (const auto& atom : peer_->get_atoms(handles)) atoms.insert(atom->handle()); + set atoms_with_null_keychain; + for (const auto& atom : peer_->get_atoms(handles, nullptr)) atoms_with_null_keychain.insert(atom->handle()); + EXPECT_EQ(atoms, set({dirty_handle, local_link_handle, remote_handle, local_human_handle})); + EXPECT_EQ(atoms_with_null_keychain, atoms); + + set nodes; + for (const auto& node : peer_->get_nodes(handles)) nodes.insert(node->handle()); + set nodes_with_null_keychain; + for (const auto& node : peer_->get_nodes(handles, nullptr)) nodes_with_null_keychain.insert(node->handle()); + EXPECT_EQ(nodes, set({dirty_handle, remote_handle, local_human_handle})); + EXPECT_EQ(nodes_with_null_keychain, nodes); + + set links; + for (const auto& fetched_link : peer_->get_links(handles)) links.insert(fetched_link->handle()); + set links_with_null_keychain; + for (const auto& fetched_link : peer_->get_links(handles, nullptr)) { + links_with_null_keychain.insert(fetched_link->handle()); + } + EXPECT_EQ(links, set({local_link_handle})); + EXPECT_EQ(links_with_null_keychain, links); +} + TEST_F(RemoteAtomDBPeerTest, GetFromCacheThenRemote) { // Add to remote only (bypass peer) auto human = new Node("Symbol", "\"human\""); @@ -656,6 +700,44 @@ TEST_F(RemoteAtomDBTest, AddLinksAndRetrieve) { EXPECT_EQ(retrieved2->handle(), link2_handle); } +TEST_F(RemoteAtomDBTest, GetAtomsNodesAndLinks) { + auto human = new Node("Symbol", "\"batch-human\""); + auto monkey = new Node("Symbol", "\"batch-monkey\""); + auto similarity = new Node("Symbol", "BatchSimilarity"); + + string human_handle = db_->add_node(human); + string monkey_handle = db_->add_node(monkey); + string similarity_handle = db_->add_node(similarity); + auto link = new Link("Expression", {similarity_handle, human_handle, monkey_handle}); + string link_handle = db_->add_link(link); + string missing_handle = "missing" + Utils::random_string(25); + + vector handles = {human_handle, link_handle, missing_handle, monkey_handle}; + + set atoms; + for (const auto& atom : db_->get_atoms(handles)) atoms.insert(atom->handle()); + set atoms_with_null_keychain; + for (const auto& atom : db_->get_atoms(handles, nullptr)) atoms_with_null_keychain.insert(atom->handle()); + EXPECT_EQ(atoms, set({human_handle, monkey_handle, link_handle})); + EXPECT_EQ(atoms_with_null_keychain, atoms); + + set nodes; + for (const auto& node : db_->get_nodes(handles)) nodes.insert(node->handle()); + set nodes_with_null_keychain; + for (const auto& node : db_->get_nodes(handles, nullptr)) nodes_with_null_keychain.insert(node->handle()); + EXPECT_EQ(nodes, set({human_handle, monkey_handle})); + EXPECT_EQ(nodes_with_null_keychain, nodes); + + set links; + for (const auto& fetched_link : db_->get_links(handles)) links.insert(fetched_link->handle()); + set links_with_null_keychain; + for (const auto& fetched_link : db_->get_links(handles, nullptr)) { + links_with_null_keychain.insert(fetched_link->handle()); + } + EXPECT_EQ(links, set({link_handle})); + EXPECT_EQ(links_with_null_keychain, links); +} + TEST_F(RemoteAtomDBTest, DeleteOperations) { auto human = new Node("Symbol", "\"human\""); auto monkey = new Node("Symbol", "\"monkey\""); @@ -1447,5 +1529,6 @@ TEST(RemoteAtomDBFederationTest, UnprotectedRemoteHandleToMetta) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); + Utils::init_random(0); return RUN_ALL_TESTS(); } diff --git a/src/tests/cpp/test_commons/mocks/MockAtomDB.h b/src/tests/cpp/test_commons/mocks/MockAtomDB.h index 4ba4e4603..90f01bf1a 100644 --- a/src/tests/cpp/test_commons/mocks/MockAtomDB.h +++ b/src/tests/cpp/test_commons/mocks/MockAtomDB.h @@ -34,6 +34,9 @@ class AtomDBMock : public AtomDB { MOCK_METHOD(shared_ptr, get_atom, (const string& handle), (override)); MOCK_METHOD(shared_ptr, get_node, (const string& handle), (override)); MOCK_METHOD(shared_ptr, get_link, (const string& handle), (override)); + MOCK_METHOD(vector>, get_atoms, (const vector& handles), (override)); + MOCK_METHOD(vector>, get_nodes, (const vector& handles), (override)); + MOCK_METHOD(vector>, get_links, (const vector& handles), (override)); MOCK_METHOD(shared_ptr, query_for_pattern, (const LinkSchema& link_template), From 69dac45e459375df181e93df32f24e56ecfeef20 Mon Sep 17 00:00:00 2001 From: marcocapozzoli Date: Tue, 3 Nov 2026 18:38:30 -0300 Subject: [PATCH 2/4] lint --- .../cpp/remote_atomdb_key_sensitive_test.cc | 15 ++++++++++----- src/tests/cpp/remote_atomdb_test.cc | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/tests/cpp/remote_atomdb_key_sensitive_test.cc b/src/tests/cpp/remote_atomdb_key_sensitive_test.cc index 8f281f510..39cdf9a8b 100644 --- a/src/tests/cpp/remote_atomdb_key_sensitive_test.cc +++ b/src/tests/cpp/remote_atomdb_key_sensitive_test.cc @@ -269,15 +269,18 @@ TEST_F(RemoteAtomDBKeySensitiveTest, GetAtomsNodesAndLinksWithoutKeychain) { set nodes; for (const auto& node : this->db->get_nodes(handles)) nodes.insert(node->handle()); set nodes_with_null_keychain; - for (const auto& node : this->db->get_nodes(handles, nullptr)) nodes_with_null_keychain.insert(node->handle()); + for (const auto& node : this->db->get_nodes(handles, nullptr)) + nodes_with_null_keychain.insert(node->handle()); EXPECT_EQ(nodes, set({this->human_handle})); EXPECT_EQ(nodes_with_null_keychain, nodes); set links; for (const auto& link : this->db->get_links(handles)) links.insert(link->handle()); set links_with_null_keychain; - for (const auto& link : this->db->get_links(handles, nullptr)) links_with_null_keychain.insert(link->handle()); - EXPECT_EQ(links, set({this->similarity_human_chimp_handle, this->similarity_snake_vine_handle})); + for (const auto& link : this->db->get_links(handles, nullptr)) + links_with_null_keychain.insert(link->handle()); + EXPECT_EQ(links, + set({this->similarity_human_chimp_handle, this->similarity_snake_vine_handle})); EXPECT_EQ(links_with_null_keychain, links); } @@ -371,7 +374,8 @@ TEST_F(RemoteAtomDBKeySensitiveTest, PeerGetAtomsNodesAndLinksRespectKeychain) { this->similarity_human_monkey_handle); set unprotected_atoms; - for (const auto& atom : unprotected_peer->get_atoms(handles)) unprotected_atoms.insert(atom->handle()); + for (const auto& atom : unprotected_peer->get_atoms(handles)) + unprotected_atoms.insert(atom->handle()); set unprotected_atoms_with_keychain; for (const auto& atom : unprotected_peer->get_atoms(handles, similarity_key)) { unprotected_atoms_with_keychain.insert(atom->handle()); @@ -395,7 +399,8 @@ TEST_F(RemoteAtomDBKeySensitiveTest, PeerGetAtomsNodesAndLinksRespectKeychain) { } EXPECT_EQ(full_atoms, set({this->inheritance_human_mammal_handle, this->mammal_handle})); ASSERT_EQ(full_access_peer->get_nodes(full_handles, full_access_key).size(), 1u); - EXPECT_EQ(full_access_peer->get_nodes(full_handles, full_access_key)[0]->handle(), this->mammal_handle); + EXPECT_EQ(full_access_peer->get_nodes(full_handles, full_access_key)[0]->handle(), + this->mammal_handle); ASSERT_EQ(full_access_peer->get_links(full_handles, full_access_key).size(), 1u); EXPECT_EQ(full_access_peer->get_links(full_handles, full_access_key)[0]->handle(), this->inheritance_human_mammal_handle); diff --git a/src/tests/cpp/remote_atomdb_test.cc b/src/tests/cpp/remote_atomdb_test.cc index 99b97eead..ca4d60044 100644 --- a/src/tests/cpp/remote_atomdb_test.cc +++ b/src/tests/cpp/remote_atomdb_test.cc @@ -137,26 +137,30 @@ TEST_F(RemoteAtomDBPeerTest, GetAtomsNodesAndLinks) { string local_human_handle = local_->add_node(local_human); string local_monkey_handle = local_->add_node(local_monkey); string similarity_handle = local_->add_node(similarity); - auto local_link = new Link("Expression", {similarity_handle, local_human_handle, local_monkey_handle}); + auto local_link = + new Link("Expression", {similarity_handle, local_human_handle, local_monkey_handle}); string local_link_handle = local_->add_link(local_link); auto dirty = new Node("Symbol", "\"dirty-node\""); string dirty_handle = peer_->add_node(dirty); string missing_handle = "missing" + Utils::random_string(25); - vector handles = {dirty_handle, local_link_handle, remote_handle, missing_handle, local_human_handle}; + vector handles = { + dirty_handle, local_link_handle, remote_handle, missing_handle, local_human_handle}; set atoms; for (const auto& atom : peer_->get_atoms(handles)) atoms.insert(atom->handle()); set atoms_with_null_keychain; - for (const auto& atom : peer_->get_atoms(handles, nullptr)) atoms_with_null_keychain.insert(atom->handle()); + for (const auto& atom : peer_->get_atoms(handles, nullptr)) + atoms_with_null_keychain.insert(atom->handle()); EXPECT_EQ(atoms, set({dirty_handle, local_link_handle, remote_handle, local_human_handle})); EXPECT_EQ(atoms_with_null_keychain, atoms); set nodes; for (const auto& node : peer_->get_nodes(handles)) nodes.insert(node->handle()); set nodes_with_null_keychain; - for (const auto& node : peer_->get_nodes(handles, nullptr)) nodes_with_null_keychain.insert(node->handle()); + for (const auto& node : peer_->get_nodes(handles, nullptr)) + nodes_with_null_keychain.insert(node->handle()); EXPECT_EQ(nodes, set({dirty_handle, remote_handle, local_human_handle})); EXPECT_EQ(nodes_with_null_keychain, nodes); @@ -717,14 +721,16 @@ TEST_F(RemoteAtomDBTest, GetAtomsNodesAndLinks) { set atoms; for (const auto& atom : db_->get_atoms(handles)) atoms.insert(atom->handle()); set atoms_with_null_keychain; - for (const auto& atom : db_->get_atoms(handles, nullptr)) atoms_with_null_keychain.insert(atom->handle()); + for (const auto& atom : db_->get_atoms(handles, nullptr)) + atoms_with_null_keychain.insert(atom->handle()); EXPECT_EQ(atoms, set({human_handle, monkey_handle, link_handle})); EXPECT_EQ(atoms_with_null_keychain, atoms); set nodes; for (const auto& node : db_->get_nodes(handles)) nodes.insert(node->handle()); set nodes_with_null_keychain; - for (const auto& node : db_->get_nodes(handles, nullptr)) nodes_with_null_keychain.insert(node->handle()); + for (const auto& node : db_->get_nodes(handles, nullptr)) + nodes_with_null_keychain.insert(node->handle()); EXPECT_EQ(nodes, set({human_handle, monkey_handle})); EXPECT_EQ(nodes_with_null_keychain, nodes); From d5d3823c77b3ae27adddc3617eae0b2c45c06b52 Mon Sep 17 00:00:00 2001 From: marcocapozzoli Date: Wed, 4 Nov 2026 10:24:15 -0300 Subject: [PATCH 3/4] Add check --- src/atomdb/inmemorydb/InMemoryDB.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/atomdb/inmemorydb/InMemoryDB.cc b/src/atomdb/inmemorydb/InMemoryDB.cc index 2f34e3f2a..815f8ec20 100644 --- a/src/atomdb/inmemorydb/InMemoryDB.cc +++ b/src/atomdb/inmemorydb/InMemoryDB.cc @@ -183,6 +183,7 @@ vector> InMemoryDB::get_atoms(const vector& handles) { vector> 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; auto cloned = clone_atom(atom.get()); From 756d61b4f8c6710103619b5cfe7d6f9d45ce8477 Mon Sep 17 00:00:00 2001 From: marcocapozzoli Date: Tue, 10 Nov 2026 14:38:12 -0300 Subject: [PATCH 4/4] Fix tests --- src/tests/cpp/protected_atomdb_test.cc | 12 +++++++++--- src/tests/cpp/remote_atomdb_key_sensitive_test.cc | 5 +++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/tests/cpp/protected_atomdb_test.cc b/src/tests/cpp/protected_atomdb_test.cc index e01e1b7e1..1542859c7 100644 --- a/src/tests/cpp/protected_atomdb_test.cc +++ b/src/tests/cpp/protected_atomdb_test.cc @@ -424,9 +424,15 @@ TEST(ProtectedAtomDBTest, GetAtomsNodesAndLinksReturnOnlyAuthorizedAtoms) { for (const auto& atom : protected_atomdb->db->get_atoms(handles, similarity_human_keys)) { similarity_atoms.insert(atom->handle()); } - EXPECT_EQ(similarity_atoms, - set({animals.similarity_human_monkey, animals.similarity_human_chimp})); - EXPECT_TRUE(protected_atomdb->db->get_nodes(handles, similarity_human_keys).empty()); + EXPECT_EQ( + similarity_atoms, + set({animals.similarity_human_monkey, animals.similarity_human_chimp, animals.human})); + + set similarity_nodes; + for (const auto& node : protected_atomdb->db->get_nodes(handles, similarity_human_keys)) { + similarity_nodes.insert(node->handle()); + } + EXPECT_EQ(similarity_nodes, set({animals.human})); set similarity_links; for (const auto& link : protected_atomdb->db->get_links(handles, similarity_human_keys)) { diff --git a/src/tests/cpp/remote_atomdb_key_sensitive_test.cc b/src/tests/cpp/remote_atomdb_key_sensitive_test.cc index 850e3f3b4..d0ed7d55b 100644 --- a/src/tests/cpp/remote_atomdb_key_sensitive_test.cc +++ b/src/tests/cpp/remote_atomdb_key_sensitive_test.cc @@ -372,8 +372,9 @@ TEST_F(RemoteAtomDBKeySensitiveTest, PeerGetAtomsNodesAndLinksRespectKeychain) { for (const auto& atom : similarity_peer->get_atoms(handles, similarity_key)) { granted.insert(atom->handle()); } - EXPECT_EQ(granted, set({this->similarity_human_monkey_handle})); - EXPECT_TRUE(similarity_peer->get_nodes(handles, similarity_key).empty()); + EXPECT_EQ(granted, set({this->similarity_human_monkey_handle, this->human_handle})); + ASSERT_EQ(similarity_peer->get_nodes(handles, similarity_key).size(), 1u); + EXPECT_EQ(similarity_peer->get_nodes(handles, similarity_key)[0]->handle(), this->human_handle); ASSERT_EQ(similarity_peer->get_links(handles, similarity_key).size(), 1u); EXPECT_EQ(similarity_peer->get_links(handles, similarity_key)[0]->handle(), this->similarity_human_monkey_handle);