Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/atomdb/AtomDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AtomDB : public HandleDecoder {

virtual bool allow_nested_indexing() = 0;
virtual bool composite_type_enabled() const = 0;
virtual bool is_protected() const = 0;

virtual shared_ptr<Atom> get_atom(const string& handle) = 0; // HandleDecoder interface
virtual shared_ptr<Node> get_node(const string& handle) = 0;
Expand Down
47 changes: 47 additions & 0 deletions src/atomdb/AtomDBFactory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "AtomDBFactory.h"

#include "InMemoryDB.h"
#include "MorkDB.h"
#include "ProtectedAtomDB.h"
#include "RedisMongoDB.h"
#include "Utils.h"

using namespace atomdb;
using namespace commons;
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// --------------------------------------------------------------------------------
// Public methods

shared_ptr<AtomDB> AtomDBFactory::create(const JsonConfig& config, const string& context) {
return wrap_if_protected(create_backend(config, context));
}

shared_ptr<AtomDB> AtomDBFactory::create_backend(const JsonConfig& config, const string& context) {
auto atomdb_type = config.at_path("type").get_or<string>("");

if (atomdb_type == "redismongodb") {
return shared_ptr<AtomDB>(new RedisMongoDB(context, false, config));
}
if (atomdb_type == "morkdb") {
return shared_ptr<AtomDB>(new MorkDB(context, config));
}
if (atomdb_type == "inmemorydb") {
return make_shared<InMemoryDB>(context.empty() ? "inmemorydb_" : context);
}

RAISE_ERROR("AtomDBFactory: unsupported AtomDB type: " + atomdb_type);
return shared_ptr<AtomDB>{};
}

shared_ptr<AtomDB> AtomDBFactory::wrap_if_protected(shared_ptr<AtomDB> backend) {
if (!backend) {
RAISE_ERROR("AtomDBFactory::wrap_if_protected() received null backend");
}
if (!backend->is_protected()) {
return backend;
}
if (dynamic_pointer_cast<ProtectedAtomDB>(backend)) {
return backend;
}
return shared_ptr<AtomDB>(new ProtectedAtomDB(backend));
}
39 changes: 39 additions & 0 deletions src/atomdb/AtomDBFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <memory>
#include <string>

#include "AtomDB.h"
#include "JsonConfig.h"

using namespace std;
using namespace commons;

namespace atomdb {

/**
* @brief Single entry point to construct concrete AtomDB backends.
*
* Use this instead of calling RedisMongoDB/MorkDB/InMemoryDB constructors directly.
*/
class AtomDBFactory {
public:
/**
* @brief Creates a backend and wraps it with ProtectedAtomDB when is_protected().
*/
static shared_ptr<AtomDB> create(const JsonConfig& config, const string& context = "");

/**
* @brief Creates a concrete backend without authorization wrapping.
*
* Supported types: redismongodb, morkdb, inmemorydb.
*/
static shared_ptr<AtomDB> create_backend(const JsonConfig& config, const string& context = "");

/**
* @brief Wraps backend with ProtectedAtomDB when protected and not already wrapped.
*/
static shared_ptr<AtomDB> wrap_if_protected(shared_ptr<AtomDB> backend);
};

} // namespace atomdb
36 changes: 18 additions & 18 deletions src/atomdb/AtomDBSingleton.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "AtomDBSingleton.h"

#include "AdapterDB.h"
#include "MorkDB.h"
#include "RedisMongoDB.h"
#include "AtomDBFactory.h"
#include "RemoteAtomDB.h"
#include "Utils.h"

Expand All @@ -19,24 +18,25 @@ void AtomDBSingleton::init(const JsonConfig& atomdb_config) {
if (AtomDBSingleton::initialized) {
RAISE_ERROR(
"AtomDBSingleton already initialized. AtomDBSingleton::init() should be called only once.");
}

shared_ptr<AtomDB> atomdb;
auto atomdb_type = atomdb_config.at_path("type").get_or<string>("");

if (atomdb_type == "remotedb") {
auto remote_peers_config =
atomdb_config.at_path("remote_peers").get_or<JsonConfig>(JsonConfig());
atomdb = shared_ptr<AtomDB>(new RemoteAtomDB(remote_peers_config));
atomdb = AtomDBFactory::wrap_if_protected(atomdb);
} else if (atomdb_type == "adapterdb") {
atomdb = shared_ptr<AtomDB>(new AdapterDB(atomdb_config));
atomdb = AtomDBFactory::wrap_if_protected(atomdb);
} else {
auto atomdb_type = atomdb_config.at_path("type").get_or<string>("");
if (atomdb_type == "morkdb") {
AtomDBSingleton::atom_db = shared_ptr<AtomDB>(new MorkDB("", atomdb_config));
} else if (atomdb_type == "redismongodb") {
AtomDBSingleton::atom_db = shared_ptr<AtomDB>(new RedisMongoDB("", false, atomdb_config));
} else if (atomdb_type == "remotedb") {
auto remote_peers_config =
atomdb_config.at_path("remote_peers").get_or<JsonConfig>(JsonConfig());
AtomDBSingleton::atom_db = shared_ptr<AtomDB>(new RemoteAtomDB(remote_peers_config));
} else if (atomdb_type == "adapterdb") {
AtomDBSingleton::atom_db = shared_ptr<AtomDB>(new AdapterDB(atomdb_config));
} else {
RAISE_ERROR("Invalid AtomDB type: " + atomdb_type);
}

AtomDBSingleton::initialized = true;
atomdb = AtomDBFactory::create(atomdb_config);
}

AtomDBSingleton::atom_db = atomdb;
AtomDBSingleton::initialized = true;
}

shared_ptr<AtomDB> AtomDBSingleton::get_instance() {
Expand Down
20 changes: 18 additions & 2 deletions src/atomdb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,33 @@ cc_library(
deps = [
":atomdb",
":atomdb_api_types",
":atomdb_factory",
":atomdb_singleton",
":atomdbutils",
"//atomdb/adapterdb:adapterdb_lib",
"//atomdb/auth:protected_atomdb_lib",
"//atomdb/inmemorydb:inmemorydb_lib",
"//atomdb/morkdb:morkdb_lib",
"//atomdb/redis_mongodb:redis_mongodb_lib",
"//atomdb/remotedb:remotedb_lib",
],
)

cc_library(
name = "atomdb_factory",
srcs = ["AtomDBFactory.cc"],
hdrs = ["AtomDBFactory.h"],
includes = ["."],
deps = [
":atomdb",
"//atomdb/auth:protected_atomdb_lib",
"//atomdb/inmemorydb",
"//atomdb/morkdb",
"//atomdb/redis_mongodb",
"//commons:commons_lib",
],
)

cc_library(
name = "atomdb",
hdrs = ["AtomDB.h"],
Expand Down Expand Up @@ -56,10 +73,9 @@ cc_library(
hdrs = ["AtomDBSingleton.h"],
includes = ["."],
deps = [
":atomdb_factory",
"//atomdb:atomdb_api_types",
"//atomdb/adapterdb",
"//atomdb/morkdb",
"//atomdb/redis_mongodb",
"//atomdb/remotedb:remotedb_lib",
"//commons:commons_lib",
],
Expand Down
20 changes: 12 additions & 8 deletions src/atomdb/adapterdb/AdapterDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
#include <chrono>
#include <thread>

#include "AtomDBFactory.h"
#include "AtomPersister.h"
#include "BoundedSharedQueue.h"
#include "DatabaseOrchestrator.h"
#include "DedicatedThread.h"
#include "MongoInitializer.h"
#include "MorkDB.h"
#include "MorkMappingStrategy.h"
#include "PostgresMappingStrategy.h"
#include "PostgresWrapper.h"
#include "Processor.h"
#include "RedisMongoDB.h"
#include "RemoteAtomDB.h"
#include "Utils.h"
#include "expression_hasher.h"
Expand Down Expand Up @@ -72,6 +71,11 @@ bool AdapterDB::composite_type_enabled() const {
return this->atomdb_backend->composite_type_enabled();
}

bool AdapterDB::is_protected() const {
this->ensure_backend_ready();
return this->atomdb_backend->is_protected();
}

shared_ptr<Atom> AdapterDB::get_atom(const string& handle) {
this->ensure_backend_ready();
return this->atomdb_backend->get_atom(handle);
Expand Down Expand Up @@ -314,12 +318,12 @@ void AdapterDB::atomdb_backend_setup() {
auto atomdb_backend_config =
this->config.at_path("adapterdb.atomdb_backend").get_or<JsonConfig>(JsonConfig());
string atomdb_backend_type = atomdb_backend_config.at_path("type").get_or<string>("");
if (atomdb_backend_type == "morkdb") {
this->atomdb_backend = shared_ptr<AtomDB>(new MorkDB("", atomdb_backend_config));
} else if (atomdb_backend_type == "redismongodb") {
this->atomdb_backend = shared_ptr<AtomDB>(new RedisMongoDB("", false, atomdb_backend_config));
} else if (atomdb_backend_type == "remotedb") {
this->atomdb_backend = shared_ptr<AtomDB>(new RemoteAtomDB(atomdb_backend_config));
if (atomdb_backend_type == "remotedb") {
this->atomdb_backend = AtomDBFactory::wrap_if_protected(
shared_ptr<AtomDB>(new RemoteAtomDB(atomdb_backend_config)));
} else if (atomdb_backend_type == "morkdb" || atomdb_backend_type == "redismongodb" ||
atomdb_backend_type == "inmemorydb") {
this->atomdb_backend = AtomDBFactory::create(atomdb_backend_config);
} else {
RAISE_ERROR("Invalid AtomDB type: " + atomdb_backend_type);
}
Expand Down
2 changes: 2 additions & 0 deletions src/atomdb/adapterdb/AdapterDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class AdapterDB : public AtomDB {
*/
bool composite_type_enabled() const override;

bool is_protected() const override;

shared_ptr<Atom> get_atom(const string& handle) override;
shared_ptr<Node> get_node(const string& handle) override;
shared_ptr<Link> get_link(const string& handle) override;
Expand Down
3 changes: 1 addition & 2 deletions src/atomdb/adapterdb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ cc_library(
includes = ["."],
deps = [
"//atomdb",
"//atomdb/morkdb",
"//atomdb/redis_mongodb",
"//atomdb:atomdb_factory",
"//atomdb/remotedb:remotedb_lib",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
Expand Down
24 changes: 24 additions & 0 deletions src/atomdb/auth/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "protected_atomdb_lib",
includes = ["."],
deps = [
":protected_atomdb",
],
)

cc_library(
name = "protected_atomdb",
srcs = ["ProtectedAtomDB.cc"],
hdrs = ["ProtectedAtomDB.h"],
includes = ["."],
deps = [
"//atomdb",
"//atomdb:atomdb_api_types",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
],
)
Loading
Loading