Avoid too many sqlite3_prepare_v2 calls, which take many cpu cycles to complete.
SQLite statements may be prepared once when DB object is created.
Change-Id: I99e4fd3fea63fd61396c9f7b2c3b13539f312d48
Signed-off-by: Marcin Lis <m.lis@samsung.com>
+ * Container for initialized DataCommands, prepared for binding.
+ */
+ std::vector<DB::SqlConnection::DataCommandAutoPtr> m_commands;
+
+ /**
+ * Fills empty m_commands map with sql commands prepared for binding.
+ *
+ * Because the "sqlite3_prepare_v2" function takes many cpu cycles, the PrivilegeDb
+ * is optimized to call it only once for one query type.
+ * Designed to be used in the singleton contructor.
+ */
+ void initDataCommands();
+
+ /**
* Check if pkgId is already registered in database
*
* @param pkgId - package identifier
* Check if pkgId is already registered in database
*
* @param pkgId - package identifier
mSqlConnection = new DB::SqlConnection(path,
DB::SqlConnection::Flag::None,
DB::SqlConnection::Flag::RW);
mSqlConnection = new DB::SqlConnection(path,
DB::SqlConnection::Flag::None,
DB::SqlConnection::Flag::RW);
} catch (DB::SqlConnection::Exception::Base &e) {
LogError("Database initialization error: " << e.DumpToString());
ThrowMsg(PrivilegeDb::Exception::IOError,
} catch (DB::SqlConnection::Exception::Base &e) {
LogError("Database initialization error: " << e.DumpToString());
ThrowMsg(PrivilegeDb::Exception::IOError,
+void PrivilegeDb::initDataCommands()
+{
+ for (auto &it : Queries) {
+ m_commands.push_back(mSqlConnection->PrepareDataCommand(it.second));
+ }
+}
+
PrivilegeDb::~PrivilegeDb()
{
delete mSqlConnection;
PrivilegeDb::~PrivilegeDb()
{
delete mSqlConnection;
bool PrivilegeDb::PkgIdExists(const std::string &pkgId)
{
return try_catch<bool>([&] {
bool PrivilegeDb::PkgIdExists(const std::string &pkgId)
{
return try_catch<bool>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(
- Queries.at(QueryType::EPkgIdExists));
- command->BindString(1, pkgId.c_str());
- if (command->Step()) {
- // pkgId found in the database
- command->Reset();
- return true;
- };
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::EPkgIdExists));
- // pkgId not found in the database
- return false;
+ command->Reset();
+ command->BindString(1, pkgId.c_str());
+ return command->Step();
});
}
bool PrivilegeDb::GetAppPkgId(const std::string &appId, std::string &pkgId)
{
return try_catch<bool>([&] {
});
}
bool PrivilegeDb::GetAppPkgId(const std::string &appId, std::string &pkgId)
{
return try_catch<bool>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(Queries.at(QueryType::EGetPkgId));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::EGetPkgId));
+
+ command->Reset();
command->BindString(1, appId.c_str());
if (!command->Step()) {
command->BindString(1, appId.c_str());
if (!command->Step()) {
pkgIdIsNew = !(this->PkgIdExists(pkgId));
try_catch<void>([&] {
pkgIdIsNew = !(this->PkgIdExists(pkgId));
try_catch<void>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(
- Queries.at(QueryType::EAddApplication));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::EAddApplication));
command->BindString(1, appId.c_str());
command->BindString(2, pkgId.c_str());
command->BindInteger(3, static_cast<unsigned int>(uid));
command->BindString(1, appId.c_str());
command->BindString(2, pkgId.c_str());
command->BindInteger(3, static_cast<unsigned int>(uid));
Queries.at(QueryType::EAddApplication));
};
Queries.at(QueryType::EAddApplication));
};
LogDebug("Added appId: " << appId << ", pkgId: " << pkgId);
});
}
LogDebug("Added appId: " << appId << ", pkgId: " << pkgId);
});
}
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(
- Queries.at(QueryType::ERemoveApplication));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::ERemoveApplication));
command->BindString(1, appId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
command->BindString(1, appId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
Queries.at(QueryType::ERemoveApplication));
};
Queries.at(QueryType::ERemoveApplication));
};
LogDebug("Removed appId: " << appId);
pkgIdIsNoMore = !(this->PkgIdExists(pkgId));
LogDebug("Removed appId: " << appId);
pkgIdIsNoMore = !(this->PkgIdExists(pkgId));
std::vector<std::string> ¤tPrivileges)
{
try_catch<void>([&] {
std::vector<std::string> ¤tPrivileges)
{
try_catch<void>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(
- Queries.at(QueryType::EGetPkgPrivileges));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::EGetPkgPrivileges));
+
+ command->Reset();
command->BindString(1, pkgId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
command->BindString(1, pkgId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
void PrivilegeDb::RemoveAppPrivileges(const std::string &appId, uid_t uid)
{
try_catch<void>([&] {
void PrivilegeDb::RemoveAppPrivileges(const std::string &appId, uid_t uid)
{
try_catch<void>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(Queries.at(QueryType::ERemoveAppPrivileges));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::ERemoveAppPrivileges));
command->BindString(1, appId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
if (command->Step()) {
command->BindString(1, appId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
if (command->Step()) {
const std::vector<std::string> &privileges)
{
try_catch<void>([&] {
const std::vector<std::string> &privileges)
{
try_catch<void>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(Queries.at(QueryType::EAddAppPrivileges));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::EAddAppPrivileges));
+
+ command->Reset();
command->BindString(1, appId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
command->BindString(1, appId.c_str());
command->BindInteger(2, static_cast<unsigned int>(uid));
std::vector<std::string> &groups)
{
try_catch<void>([&] {
std::vector<std::string> &groups)
{
try_catch<void>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(
- Queries.at(QueryType::EGetPrivilegeGroups));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::EGetPrivilegeGroups));
+
+ command->Reset();
command->BindString(1, privilege.c_str());
while (command->Step()) {
command->BindString(1, privilege.c_str());
while (command->Step()) {
void PrivilegeDb::GetUserApps(uid_t uid, std::vector<std::string> &apps)
{
try_catch<void>([&] {
void PrivilegeDb::GetUserApps(uid_t uid, std::vector<std::string> &apps)
{
try_catch<void>([&] {
- DB::SqlConnection::DataCommandAutoPtr command =
- mSqlConnection->PrepareDataCommand(
- Queries.at(QueryType::EGetUserApps));
+ DB::SqlConnection::DataCommandAutoPtr &command =
+ m_commands.at(static_cast<size_t>(QueryType::EGetUserApps));
+
+ command->Reset();
command->BindInteger(1, static_cast<unsigned int>(uid));
apps.clear();
while (command->Step()) {
command->BindInteger(1, static_cast<unsigned int>(uid));
apps.clear();
while (command->Step()) {
};
// Move on copy semantics
};
// Move on copy semantics
- typedef std::auto_ptr<DataCommand> DataCommandAutoPtr;
+ typedef std::unique_ptr<DataCommand> DataCommandAutoPtr;