Skip to content

[#1177] Create new class AtomDBFactory - #1220

Open
marcocapozzoli wants to merge 9 commits into
masterfrom
masc/1177-atomdb-auth-a
Open

[#1177] Create new class AtomDBFactory#1220
marcocapozzoli wants to merge 9 commits into
masterfrom
masc/1177-atomdb-auth-a

Conversation

@marcocapozzoli

@marcocapozzoli marcocapozzoli commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Introduce AtomDBFactory to create leaf AtomDB backends (redismongodb, morkdb, inmemorydb) from config.
  • Replace direct RedisMongoDB / MorkDB / InMemoryDB construction with factory calls in AtomDBSingleton, AdapterDB, RemoteAtomDB, and db_loader.
  • Make the RedisMongoDB constructor private so backends are created only via the factory (and MorkDB).
  • Add a wrap_if_protected() hook (stub for now) for future authorization wrapping.

@marcocapozzoli marcocapozzoli self-assigned this Aug 6, 2026
@marcocapozzoli

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
  • Adds AtomDBFactory and AtomDBType parsing for five backend types. Centralizes backend creation and supports an optional, currently no-op wrap_if_protected() path.
  • Replaces direct construction in production code, benchmarks, and tests. Makes RedisMongoDB construction private and disables configuration-based constructors for AdapterDB and RemoteAtomDB.
  • Adds factory tests for backend selection, wrapping behavior, remote peer validation, empty peers, and adapter configuration. Updated backend tests cover the migrated construction paths in src/tests/cpp.
  • Composite creation can increase startup allocations by constructing nested backends and peer maps. No new per-request allocation is evident.
  • Review exception context, static singleton initialization, and disabled constructor validation. The factory rejects missing or unknown types and missing peer UIDs, while protection wrapping remains unimplemented.

Walkthrough

Adds AtomDBFactory as the central backend construction path. It supports basic and composite backends, optional protection wrapping, runtime integration, build wiring, benchmark migration, and factory-focused tests.

Changes

AtomDB factory integration

Layer / File(s) Summary
Factory contract and backend creation
src/atomdb/AtomDB.h, src/atomdb/AtomDBFactory.*, src/atomdb/redis_mongodb/RedisMongoDB.h
Defines supported backend types and creates basic or composite backends from configuration. Remote peers and optional local persistence are assembled by the factory.
Runtime construction and build wiring
src/atomdb/AtomDBSingleton.cc, src/main/db_loader.cc, src/atomdb/adapterdb/*, src/atomdb/remotedb/*, src/atomdb/BUILD, src/main/BUILD
Routes runtime initialization through AtomDBFactory and removes direct backend construction from AdapterDB and RemoteAtomDB.
Tests and benchmark migration
src/tests/cpp/*, src/tests/benchmark/atomdb/atomdb_main.cc
Adds factory tests for supported, composite, wrapped, invalid, and incomplete configurations. Existing tests and benchmarks use factory-created backends.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant db_loader
  participant AtomDBFactory
  participant RemoteAtomDB
  participant AtomDBSingleton

  db_loader->>AtomDBFactory: create(config, context, should_wrap)
  AtomDBFactory->>RemoteAtomDB: assemble configured peers and local persistence
  RemoteAtomDB-->>AtomDBFactory: return remote backend
  AtomDBFactory-->>db_loader: return shared_ptr<AtomDB>
  db_loader->>AtomDBSingleton: provide(backend)
Loading

Possibly related PRs

  • singnet/das#1206: Modifies protected-backend integration and backend construction.
  • singnet/das#1216: Modifies the same AtomDBFactory, AtomDBSingleton, and backend wrapping paths.
  • singnet/das#1155: Modifies remote peer and local-persistence construction.

Suggested reviewers: andre-senna, ccgsnet

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: introducing the AtomDBFactory class.
Description check ✅ Passed The description accurately summarizes the factory, construction changes, constructor visibility, and protection wrapper hook.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Tests For Behavior Changes ✅ Passed Production AtomDBFactory and call-site behavior changed, and src/tests/cpp/atomdb_factory_test.cc is registered in BUILD with coverage for basic, composite, wrapping, and invalid configurations.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch masc/1177-atomdb-auth-a

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/atomdb/AtomDBFactory.cc (1)

8-9: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add the required file-level std namespace directive.

AtomDBFactory.cc uses shared_ptr, string, and make_shared without a file-level using namespace std;. It currently relies on AtomDBFactory.h to export that namespace. Add the directive in this source file.

Proposed change
+using namespace std;
 using namespace atomdb;
 using namespace commons;

As per coding guidelines, src/**/*.cc must use file-level using namespace std and domain using namespace lines.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/atomdb/AtomDBFactory.cc` around lines 8 - 9, Add a file-level using
namespace std; directive in AtomDBFactory.cc alongside the existing atomdb and
commons namespace directives, so shared_ptr, string, and make_shared resolve
without relying on AtomDBFactory.h.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/atomdb/remotedb/RemoteAtomDB.cc`:
- Around line 27-43: Update the context initialization in the RemoteAtomDB peer
creation flow to derive the fallback context from each peer’s uid instead of
using the shared "remotedb_" value, while preserving explicitly configured
contexts. Ensure the derived context is reused for local_persistence and the
remote AtomDBFactory::create call, and add a regression test covering two peers
without configured contexts that verifies their persistence remains isolated.

In `@src/tests/cpp/atomdb_factory_test.cc`:
- Around line 25-65: Extend the AtomDBFactory construction coverage with a valid
morkdb configuration, asserting that create_backend returns a MorkDB instance,
and add the required direct Bazel dependency if MorkDB.h is included. Also add
behavior-focused coverage for direct factory creation and backend compatibility,
reusing the existing test helpers and assertion style.

---

Nitpick comments:
In `@src/atomdb/AtomDBFactory.cc`:
- Around line 8-9: Add a file-level using namespace std; directive in
AtomDBFactory.cc alongside the existing atomdb and commons namespace directives,
so shared_ptr, string, and make_shared resolve without relying on
AtomDBFactory.h.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f91c28d9-81c3-4aa7-a2a2-a301116dee17

📥 Commits

Reviewing files that changed from the base of the PR and between 95aece8 and dfbf5eb.

📒 Files selected for processing (17)
  • src/atomdb/AtomDBFactory.cc
  • src/atomdb/AtomDBFactory.h
  • src/atomdb/AtomDBSingleton.cc
  • src/atomdb/BUILD
  • src/atomdb/adapterdb/AdapterDB.cc
  • src/atomdb/adapterdb/BUILD
  • src/atomdb/redis_mongodb/RedisMongoDB.h
  • src/atomdb/remotedb/BUILD
  • src/atomdb/remotedb/RemoteAtomDB.cc
  • src/main/BUILD
  • src/main/db_loader.cc
  • src/tests/benchmark/atomdb/atomdb_main.cc
  • src/tests/cpp/BUILD
  • src/tests/cpp/adapterdb_test.cc
  • src/tests/cpp/atomdb_factory_test.cc
  • src/tests/cpp/redis_mongodb_test.cc
  • src/tests/cpp/redis_mongodb_test_2.cc

Comment thread src/atomdb/remotedb/RemoteAtomDB.cc Outdated
Comment thread src/tests/cpp/atomdb_factory_test.cc Outdated
@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/atomdb/remotedb/RemoteAtomDB.cc (2)

73-75: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use this->remote_db_ in RemoteAtomDB::is_protected().

Line 74 accesses a class member without this->. Match the existing composite_type_enabled() implementation.

Proposed change
-    for (auto& [uid, peer] : remote_db_) {
+    for (auto& [uid, peer] : this->remote_db_) {

As per coding guidelines, use this->field consistently in C++. As per path instructions, use this-> for member access in src/**/*.{h,cc}.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/atomdb/remotedb/RemoteAtomDB.cc` around lines 73 - 75, Update
RemoteAtomDB::is_protected() to access the remote_db_ member through
this->remote_db_, matching the style used by composite_type_enabled() and the
repository’s C++ member-access convention.

Sources: Coding guidelines, Path instructions


73-81: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Add focused tests for RemoteAtomDB::is_protected().

Test empty peers, unprotected peers, one protected peer, and mixed protected and unprotected peers. Also verify the result used by AtomDBFactory::wrap_if_protected().

As per path instructions, behavior changes in src/ require matching tests under src/tests/cpp/.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/atomdb/remotedb/RemoteAtomDB.cc` around lines 73 - 81, ​Add focused C++
tests under src/tests/cpp/ for RemoteAtomDB::is_protected(), covering empty
peers, all-unprotected peers, a single protected peer, and mixed peer protection
states. Also verify AtomDBFactory::wrap_if_protected() uses the reported
protection result correctly, reusing existing test fixtures and helpers where
available.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/db_loader.cc`:
- Around line 76-83: Update the AtomDB loading branch around
AtomDBSingleton::provide to preserve AtomDBFactory::create() protection
semantics for remotedb, adapterdb, and composite backends, ensuring configured
storage is wrapped or inspected before registration. Add C++ coverage for
protected and unprotected configurations, or, if the loader is intentionally
privileged, document and test the raw-storage contract.

---

Nitpick comments:
In `@src/atomdb/remotedb/RemoteAtomDB.cc`:
- Around line 73-75: Update RemoteAtomDB::is_protected() to access the
remote_db_ member through this->remote_db_, matching the style used by
composite_type_enabled() and the repository’s C++ member-access convention.
- Around line 73-81: ​Add focused C++ tests under src/tests/cpp/ for
RemoteAtomDB::is_protected(), covering empty peers, all-unprotected peers, a
single protected peer, and mixed peer protection states. Also verify
AtomDBFactory::wrap_if_protected() uses the reported protection result
correctly, reusing existing test fixtures and helpers where available.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 9cf26528-d8ee-4ff3-9760-055b0ff73942

📥 Commits

Reviewing files that changed from the base of the PR and between 653c4bd and 622d81c.

📒 Files selected for processing (3)
  • src/atomdb/adapterdb/AdapterDB.cc
  • src/atomdb/remotedb/RemoteAtomDB.cc
  • src/main/db_loader.cc
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/atomdb/adapterdb/AdapterDB.cc

Comment thread src/main/db_loader.cc Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/atomdb/adapterdb/AdapterDB.cc (1)

38-40: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Preserve the populated-backend invariant for factory construction.

The only active config constructor calls initialize(true). This bypasses the check that rejects a non-empty backend for an unpersisted mapping context. A factory-created AdapterDB can then write adapter output into existing backend data.

Use initialize(false) for production construction. Keep an explicit test-only opt-out if tests require it. Add a regression test with an unpersisted context and a populated injected backend.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/atomdb/adapterdb/AdapterDB.cc` around lines 38 - 40, Change the active
AdapterDB constructor to call initialize(false) so factory-created instances
reject populated backends for unpersisted mapping contexts. Preserve any
explicit test-only opt-out, and add a regression test covering an unpersisted
context with a populated injected backend.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/atomdb/AtomDBFactory.cc`:
- Around line 81-84: Update the remote peer loop in AtomDBFactory so an empty
UID from JsonConfig::at_path("uid") causes RAISE_ERROR(...) instead of silently
continuing. Add a test covering a configured remote peer without uid and assert
that construction or initialization throws, while preserving normal handling for
peers with valid UIDs.

In `@src/atomdb/remotedb/RemoteAtomDB.h`:
- Line 22: Update the RemoteAtomDB class documentation to remove the claim that
construction expects a JSON config. Document AtomDBFactory as the configuration
entry point, and identify the remaining peer-map constructor as the
dependency-injection entry point.

In `@src/tests/cpp/atomdb_factory_test.cc`:
- Around line 104-110: Extend
AtomDBFactoryTest.CreateAdapterDBRequiresBackendType with a valid
adapterdb.atomdb_backend configuration and assert AtomDBFactory::create
succeeds. Exercise a representative operation on the returned AdapterDB to
verify delegation, and add the direct AdapterDB Bazel dependency only if the
test includes its header.

---

Outside diff comments:
In `@src/atomdb/adapterdb/AdapterDB.cc`:
- Around line 38-40: Change the active AdapterDB constructor to call
initialize(false) so factory-created instances reject populated backends for
unpersisted mapping contexts. Preserve any explicit test-only opt-out, and add a
regression test covering an unpersisted context with a populated injected
backend.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 30bc8930-48fb-4904-bd47-97cbb2bdae97

📥 Commits

Reviewing files that changed from the base of the PR and between 622d81c and f415feb.

📒 Files selected for processing (18)
  • src/atomdb/AtomDB.h
  • src/atomdb/AtomDBFactory.cc
  • src/atomdb/AtomDBFactory.h
  • src/atomdb/AtomDBSingleton.cc
  • src/atomdb/BUILD
  • src/atomdb/adapterdb/AdapterDB.cc
  • src/atomdb/adapterdb/AdapterDB.h
  • src/atomdb/adapterdb/BUILD
  • src/atomdb/remotedb/RemoteAtomDB.cc
  • src/atomdb/remotedb/RemoteAtomDB.h
  • src/main/db_loader.cc
  • src/tests/benchmark/atomdb/atomdb_main.cc
  • src/tests/cpp/BUILD
  • src/tests/cpp/adapterdb_test.cc
  • src/tests/cpp/atomdb_factory_test.cc
  • src/tests/cpp/redis_mongodb_test.cc
  • src/tests/cpp/redis_mongodb_test_2.cc
  • src/tests/cpp/remote_atomdb_test.cc
💤 Files with no reviewable changes (1)
  • src/atomdb/adapterdb/BUILD
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/tests/cpp/redis_mongodb_test_2.cc
  • src/tests/cpp/adapterdb_test.cc
  • src/tests/cpp/redis_mongodb_test.cc
  • src/atomdb/BUILD

Comment thread src/atomdb/AtomDBFactory.cc Outdated
Comment thread src/atomdb/remotedb/RemoteAtomDB.h Outdated
Comment thread src/tests/cpp/atomdb_factory_test.cc Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/tests/cpp/atomdb_factory_test.cc`:
- Around line 130-168: Update the AdapterDB integration test setup around
mapping_path, the AtomDBFactory::create context, and the test Node name to
incorporate unique_marker in each resource identifier. Replace the fixed mapping
filename with a marker-based path and use marker-derived factory context and
node names so concurrent or previous runs cannot share mapping files or
persisted MorkDB state.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 20987971-14c3-4cf3-8a7b-db917cf0d4b1

📥 Commits

Reviewing files that changed from the base of the PR and between f415feb and edd7624.

📒 Files selected for processing (3)
  • src/atomdb/AtomDBFactory.cc
  • src/tests/cpp/BUILD
  • src/tests/cpp/atomdb_factory_test.cc
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/tests/cpp/BUILD

Comment thread src/tests/cpp/atomdb_factory_test.cc
Comment thread src/atomdb/AtomDBFactory.h Outdated
Comment thread src/atomdb/AtomDBFactory.h Outdated
Comment thread src/atomdb/AtomDBFactory.cc Outdated
Comment thread src/atomdb/AtomDBFactory.cc Outdated
Comment thread src/atomdb/AtomDBFactory.cc Outdated
AtomDBType type = parse_atomdb_type(atomdb_type);

if (type == AtomDBType::RedisMongoDB) {
return shared_ptr<AtomDB>(new RedisMongoDB(context, false, config));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid instantiating shared_ptr and unique_ptr using constructor. Use make_shared whenever possible.

Please review this in all the code, I'll not mark this issue anymore in this revision.


private:
friend class AtomDBFactory;
friend class MorkDB;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does MorkDB need to be a friend class?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this code snippet

// --> MorkDB : RedisMongoDB(context, skip_redis = true)
MorkDB::MorkDB(const string& context, const JsonConfig& config) : RedisMongoDB(context, true, config) {
    mork_setup(config);
}

This causes a compilation error if it is not a friend class

Comment thread src/atomdb/BUILD Outdated
Comment thread src/main/BUILD Outdated
Comment thread src/main/db_loader.cc Outdated
Comment thread src/tests/cpp/adapterdb_test.cc Outdated
…ith static helpers

Remove the should_wrap flag so create() always goes through wrap_if_protected, and expose AtomDB::string_to_type/type_to_string. Update callers and tests accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants