cache entities to avoid allocations

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
folling 2023-11-20 08:58:58 +01:00 committed by Folling
parent d3e93b6a72
commit f38ebeab14
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
6 changed files with 324 additions and 37 deletions

View file

@ -21,39 +21,56 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c
return nullptr;
}
LOG_VERBOSE("project={}; name={}", project->path.c_str(), name);
auto ctx = project->function_context();
if (name == nullptr) {
LOG_ERROR("name is nullptr");
ctx->set_error("name is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
if (cppbase::is_empty_or_blank(name)) {
LOG_ERROR("name is empty or blank");
ctx->set_error("name is empty or blank", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
VTRYRV(auto id, nullptr, project->db->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
LOG_VERBOSE("creating blueprint in objects table");
LOG_VERBOSE("project={}; name={}", project->path().c_str(), name);
TRY(db->execute(
"INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);", static_cast<int>(IkarusObjectType_Blueprint), name
));
VTRYRV(
auto id,
nullptr,
project->db()
->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
LOG_VERBOSE("creating blueprint in objects table");
auto id = db->last_insert_rowid();
TRY(db->execute(
"INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);",
static_cast<int>(IkarusObjectType_Blueprint),
name
));
LOG_VERBOSE("blueprint is {}", id);
auto id = db->last_insert_rowid();
LOG_VERBOSE("inserting blueprint into blueprints table");
LOG_VERBOSE("id is {}", id);
TRY(db->execute("INSERT INTO `blueprints`(`id`) VALUES(?);", id));
LOG_VERBOSE("inserting blueprint into blueprints table");
return cppbase::ok(id);
}));
TRY(db->execute("INSERT INTO `blueprints`(`id`) VALUES(?);", id));
return cppbase::ok(id);
})
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("unable to insert blueprint into database: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_VERBOSE("successfully created blueprint");
return new IkarusBlueprint{project, id};
return project->get_blueprint(id);
}
void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
@ -64,16 +81,27 @@ void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
return;
}
auto * ctx = blueprint->project->function_context();
LOG_VERBOSE("blueprint={}", blueprint->id);
if (auto res = blueprint->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->id); res.is_err()) {
LOG_ERROR("failed to delete blueprint {} from objects table: {}", blueprint->id, res.unwrap_error());
return;
}
TRYRV(
,
blueprint->project->db()
->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to delete blueprint from objects table: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_VERBOSE("blueprint was successfully deleted from database, freeing pointer");
LOG_VERBOSE("blueprint was successfully deleted from database, freeing blueprint");
delete blueprint;
blueprint->project->remove_blueprint(blueprint);
LOG_VERBOSE("successfully deleted blueprint");
}
@ -86,14 +114,23 @@ size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
return 0;
}
auto * ctx = blueprint->project->function_context();
LOG_VERBOSE("blueprint={}", blueprint->id);
VTRYRV(
auto count,
0,
blueprint->project->db->query_one<int>(
"SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint_id` = ?;", blueprint->id
)
blueprint->project->db()
->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint_id` = ?;", blueprint->id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint property count: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
return static_cast<size_t>(count);
@ -109,6 +146,8 @@ void ikarus_blueprint_get_properties(
return;
}
auto * ctx = blueprint->project->function_context();
if (properties_out == nullptr) {
LOG_ERROR("properties_out is nullptr");
return;
@ -118,17 +157,28 @@ void ikarus_blueprint_get_properties(
IkarusId ids[properties_out_size];
if (auto res = blueprint->project->db->query_many_buffered<IkarusId>(
"SELECT `id` FROM `properties` WHERE `source` = ?", static_cast<IkarusId *>(ids), properties_out_size, blueprint->id
);
res.is_err()) {
LOG_ERROR("failed to fetch blueprint property ids: {}", res.unwrap_error());
return;
}
TRYRV(
,
blueprint->project->db()
->query_many_buffered<IkarusId>(
"SELECT `id` FROM `properties` WHERE `source` = ?",
static_cast<IkarusId *>(ids),
properties_out_size,
blueprint->id
)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint property ids: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
for (size_t i = 0; i < properties_out_size; ++i) {
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
properties_out[i] = new IkarusProperty{blueprint->project, ids[i]};
properties_out[i] = blueprint->project->get_property(ids[i]);
}
LOG_VERBOSE("successfully fetched blueprint properties");

View file

@ -3,4 +3,7 @@
#include <objects/object.hpp>
/// \private
struct IkarusEntity : public IkarusObject {};
struct IkarusEntity : public IkarusObject {
inline IkarusEntity(struct IkarusProject * project, IkarusId id):
IkarusObject{project, id} {}
};