From 9a5f620c51675ad6aafbf71db1208ff3d61cdbf4 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 28 Nov 2017 17:05:27 +0100 Subject: [PATCH] feat(MainDb): refactor main db event key --- src/CMakeLists.txt | 4 +- src/db/main-db-event-key.cpp | 36 ++-------- src/db/main-db-event-key.h | 19 +----- ...{main-db-event-key-p.h => main-db-key-p.h} | 12 ++-- src/db/main-db-key.cpp | 67 +++++++++++++++++++ src/db/main-db-key.h | 54 +++++++++++++++ src/db/main-db.cpp | 11 ++- 7 files changed, 142 insertions(+), 61 deletions(-) rename src/db/{main-db-event-key-p.h => main-db-key-p.h} (82%) create mode 100644 src/db/main-db-key.cpp create mode 100644 src/db/main-db-key.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 30575da1e..f9d78b8fc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -96,8 +96,9 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/platform-helpers/platform-helpers.h db/abstract/abstract-db-p.h db/abstract/abstract-db.h - db/main-db-event-key-p.h db/main-db-event-key.h + db/main-db-key-p.h + db/main-db-key.h db/main-db-p.h db/main-db.h db/session/db-session-p.h @@ -200,6 +201,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES core/platform-helpers/platform-helpers.cpp db/abstract/abstract-db.cpp db/main-db-event-key.cpp + db/main-db-key.cpp db/main-db.cpp db/session/db-session-provider.cpp db/session/db-session.cpp diff --git a/src/db/main-db-event-key.cpp b/src/db/main-db-event-key.cpp index fdc354be9..c9c350d28 100644 --- a/src/db/main-db-event-key.cpp +++ b/src/db/main-db-event-key.cpp @@ -18,7 +18,8 @@ */ #include "core/core-p.h" -#include "main-db-event-key-p.h" +#include "main-db-event-key.h" +#include "main-db-key-p.h" #include "main-db-p.h" // ============================================================================= @@ -29,21 +30,9 @@ LINPHONE_BEGIN_NAMESPACE // ----------------------------------------------------------------------------- -MainDbEventKey::MainDbEventKey () : ClonableObject(*new MainDbEventKeyPrivate) {} +MainDbEventKey::MainDbEventKey () : MainDbKey() {}; -MainDbEventKey::MainDbEventKey (const shared_ptr &core, long long storageId) : MainDbEventKey() { - L_D(); - d->core = core; - d->storageId = storageId; -} - -MainDbEventKey::MainDbEventKey (const MainDbEventKey &src) : MainDbEventKey() { - L_D(); - const MainDbEventKeyPrivate *dSrc = src.getPrivate(); - - d->core = dSrc->core; - d->storageId = dSrc->storageId; -} +MainDbEventKey::MainDbEventKey (const shared_ptr &core, long long storageId) : MainDbKey(core, storageId) {} MainDbEventKey::~MainDbEventKey () { L_D(); @@ -52,21 +41,4 @@ MainDbEventKey::~MainDbEventKey () { d->core.lock()->getPrivate()->mainDb->getPrivate()->storageIdToEvent.erase(d->storageId); } -MainDbEventKey &MainDbEventKey::operator= (const MainDbEventKey &src) { - L_D(); - - if (this != &src) { - const MainDbEventKeyPrivate *dSrc = src.getPrivate(); - d->core = dSrc->core; - d->storageId = dSrc->storageId; - } - - return *this; -} - -bool MainDbEventKey::isValid () const { - L_D(); - return !d->core.expired() && d->storageId >= 0; -} - LINPHONE_END_NAMESPACE diff --git a/src/db/main-db-event-key.h b/src/db/main-db-event-key.h index 5b9c90dbf..8eb7b990b 100644 --- a/src/db/main-db-event-key.h +++ b/src/db/main-db-event-key.h @@ -20,33 +20,20 @@ #ifndef _MAIN_DB_EVENT_KEY_H_ #define _MAIN_DB_EVENT_KEY_H_ -#include - -#include "object/clonable-object.h" +#include "main-db-key.h" // ============================================================================= LINPHONE_BEGIN_NAMESPACE -class Core; -class MainDbEventKeyPrivate; - -class MainDbEventKey : public ClonableObject { - friend class MainDb; - friend class MainDbPrivate; - +class MainDbEventKey : public MainDbKey { public: MainDbEventKey (); MainDbEventKey (const std::shared_ptr &core, long long storageId); - MainDbEventKey (const MainDbEventKey &src); ~MainDbEventKey (); - MainDbEventKey &operator= (const MainDbEventKey &src); - - bool isValid () const; - private: - L_DECLARE_PRIVATE(MainDbEventKey); + L_DECLARE_PRIVATE(MainDbKey); }; LINPHONE_END_NAMESPACE diff --git a/src/db/main-db-event-key-p.h b/src/db/main-db-key-p.h similarity index 82% rename from src/db/main-db-event-key-p.h rename to src/db/main-db-key-p.h index 62e4f6eaa..dea7d1180 100644 --- a/src/db/main-db-event-key-p.h +++ b/src/db/main-db-key-p.h @@ -1,5 +1,5 @@ /* - * main-db-event-key-p.h + * main-db-key-p.h * Copyright (C) 2010-2017 Belledonne Communications SARL * * This program is free software; you can redistribute it and/or @@ -17,17 +17,17 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef _MAIN_DB_EVENT_KEY_P_H_ -#define _MAIN_DB_EVENT_KEY_P_H_ +#ifndef _MAIN_DB_KEY_P_H_ +#define _MAIN_DB_KEY_P_H_ -#include "main-db-event-key.h" +#include "main-db-key.h" #include "object/clonable-object-p.h" // ============================================================================= LINPHONE_BEGIN_NAMESPACE -class MainDbEventKeyPrivate : public ClonableObjectPrivate { +class MainDbKeyPrivate : public ClonableObjectPrivate { public: std::weak_ptr core; long long storageId = -1; @@ -35,4 +35,4 @@ public: LINPHONE_END_NAMESPACE -#endif // ifndef _MAIN_DB_EVENT_KEY_P_H_ +#endif // ifndef _MAIN_DB_KEY_P_H_ diff --git a/src/db/main-db-key.cpp b/src/db/main-db-key.cpp new file mode 100644 index 000000000..2aef638e9 --- /dev/null +++ b/src/db/main-db-key.cpp @@ -0,0 +1,67 @@ +/* + * main-db-key.cpp + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "core/core-p.h" +#include "main-db-key-p.h" +#include "main-db-p.h" + +// ============================================================================= + +using namespace std; + +LINPHONE_BEGIN_NAMESPACE + +// ----------------------------------------------------------------------------- + +MainDbKey::MainDbKey () : ClonableObject(*new MainDbKeyPrivate) {} + +MainDbKey::MainDbKey (const shared_ptr &core, long long storageId) : MainDbKey() { + L_D(); + d->core = core; + d->storageId = storageId; +} + +MainDbKey::MainDbKey (const MainDbKey &src) : MainDbKey() { + L_D(); + const MainDbKeyPrivate *dSrc = src.getPrivate(); + + d->core = dSrc->core; + d->storageId = dSrc->storageId; +} + +MainDbKey::~MainDbKey () {} + +MainDbKey &MainDbKey::operator= (const MainDbKey &src) { + L_D(); + + if (this != &src) { + const MainDbKeyPrivate *dSrc = src.getPrivate(); + d->core = dSrc->core; + d->storageId = dSrc->storageId; + } + + return *this; +} + +bool MainDbKey::isValid () const { + L_D(); + return !d->core.expired() && d->storageId >= 0; +} + +LINPHONE_END_NAMESPACE diff --git a/src/db/main-db-key.h b/src/db/main-db-key.h new file mode 100644 index 000000000..2311a882c --- /dev/null +++ b/src/db/main-db-key.h @@ -0,0 +1,54 @@ +/* + * main-db-key.h + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _MAIN_DB_KEY_H_ +#define _MAIN_DB_KEY_H_ + +#include + +#include "object/clonable-object.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class Core; +class MainDbKeyPrivate; + +class MainDbKey : public ClonableObject { + friend class MainDb; + friend class MainDbPrivate; + +public: + MainDbKey (); + MainDbKey (const std::shared_ptr &core, long long storageId); + MainDbKey (const MainDbKey &src); + virtual ~MainDbKey () = 0; + + MainDbKey &operator= (const MainDbKey &src); + + bool isValid () const; + +private: + L_DECLARE_PRIVATE(MainDbKey); +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _MAIN_DB_KEY_H_ diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index de08c61b3..02708c15f 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -37,7 +37,7 @@ #include "event-log/event-log-p.h" #include "event-log/events.h" #include "logger/logger.h" -#include "main-db-event-key-p.h" +#include "main-db-key-p.h" #include "main-db-p.h" // ============================================================================= @@ -676,7 +676,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), } const EventLogPrivate *dEventLog = eventLog->getPrivate(); - MainDbEventKeyPrivate *dEventKey = dEventLog->dbKey.getPrivate(); + MainDbKeyPrivate *dEventKey = static_cast(dEventLog->dbKey).getPrivate(); const long long &eventId = dEventKey->storageId; soci::session *session = dbSession.getBackendSession(); @@ -1143,9 +1143,8 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), break; } - tr.commit(); - - soFarSoGood = storageId >= 0; + if ((soFarSoGood = storageId >= 0)) + tr.commit(); L_END_LOG_EXCEPTION @@ -1211,7 +1210,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return false; } - MainDbEventKeyPrivate *dEventKey = dEventLog->dbKey.getPrivate(); + MainDbKeyPrivate *dEventKey = static_cast(dEventLog->dbKey).getPrivate(); shared_ptr core = dEventKey->core.lock(); L_ASSERT(core);