Commit 76342c71 authored by Ronan's avatar Ronan

feat(MainDb): reworking, better code (performance and fixes)

parent 556fb1a6
...@@ -106,6 +106,8 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES ...@@ -106,6 +106,8 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES
core/platform-helpers/platform-helpers.h core/platform-helpers/platform-helpers.h
db/abstract/abstract-db-p.h db/abstract/abstract-db-p.h
db/abstract/abstract-db.h db/abstract/abstract-db.h
db/internal/db-transaction.h
db/internal/statements.h
db/main-db-chat-message-key.h db/main-db-chat-message-key.h
db/main-db-event-key.h db/main-db-event-key.h
db/main-db-key-p.h db/main-db-key-p.h
...@@ -225,6 +227,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES ...@@ -225,6 +227,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES
core/paths/paths.cpp core/paths/paths.cpp
core/platform-helpers/platform-helpers.cpp core/platform-helpers/platform-helpers.cpp
db/abstract/abstract-db.cpp db/abstract/abstract-db.cpp
db/internal/statements.cpp
db/main-db-chat-message-key.cpp db/main-db-chat-message-key.cpp
db/main-db-event-key.cpp db/main-db-event-key.cpp
db/main-db-key.cpp db/main-db-key.cpp
...@@ -284,24 +287,15 @@ elseif (UNIX) ...@@ -284,24 +287,15 @@ elseif (UNIX)
list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-linux.h) list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-linux.h)
endif () endif ()
set(LINPHONE_CXX_OBJECTS_INCLUDE_DIRS ${BELR_INCLUDE_DIRS} ${LIBXSD_INCLUDE_DIRS}) set(LINPHONE_CXX_OBJECTS_INCLUDE_DIRS
${BELR_INCLUDE_DIRS}
${LIBXSD_INCLUDE_DIRS}
${SOCI_INCLUDE_DIRS}
${SOCI_MYSQL_INCLUDES}
)
set(LINPHONE_CXX_OBJECTS_DEFINITIONS "-DLIBLINPHONE_EXPORTS") set(LINPHONE_CXX_OBJECTS_DEFINITIONS "-DLIBLINPHONE_EXPORTS")
set(LINPHONE_CXX_OBJECTS_INCLUDE_DIRS ${BELR_INCLUDE_DIRS}) set(LINPHONE_CXX_OBJECTS_INCLUDE_DIRS ${BELR_INCLUDE_DIRS})
if (SOCI_FOUND)
list(APPEND LINPHONE_CXX_OBJECTS_INCLUDE_DIRS ${SOCI_INCLUDE_DIRS} ${SOCI_MYSQL_INCLUDES})
add_definitions(-DSOCI_ENABLED)
list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES
db/internal/db-exception-handler.h
db/internal/statements.h
)
list(APPEND LINPHONE_CXX_OBJECTS_SOURCE_FILES
db/internal/statements.cpp
)
endif ()
set(LINPHONE_PRIVATE_HEADER_FILES) set(LINPHONE_PRIVATE_HEADER_FILES)
foreach (header ${LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES}) foreach (header ${LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES})
list(APPEND LINPHONE_PRIVATE_HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${header}") list(APPEND LINPHONE_PRIVATE_HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${header}")
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#ifndef _L_ADDRESS_H_ #ifndef _L_ADDRESS_H_
#define _L_ADDRESS_H_ #define _L_ADDRESS_H_
#include <ostream>
#include "enums.h" #include "enums.h"
#include "object/clonable-object.h" #include "object/clonable-object.h"
...@@ -107,6 +109,11 @@ private: ...@@ -107,6 +109,11 @@ private:
L_DECLARE_PRIVATE(Address); L_DECLARE_PRIVATE(Address);
}; };
inline std::ostream &operator<< (std::ostream &os, const Address &address) {
os << "Address(" << address.asString() << ")";
return os;
}
LINPHONE_END_NAMESPACE LINPHONE_END_NAMESPACE
#endif // ifndef _L_ADDRESS_H_ #endif // ifndef _L_ADDRESS_H_
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#ifndef _L_IDENTITY_ADDRESS_H_ #ifndef _L_IDENTITY_ADDRESS_H_
#define _L_IDENTITY_ADDRESS_H_ #define _L_IDENTITY_ADDRESS_H_
#include <ostream>
#include "object/clonable-object.h" #include "object/clonable-object.h"
// ============================================================================= // =============================================================================
...@@ -65,6 +67,11 @@ private: ...@@ -65,6 +67,11 @@ private:
L_DECLARE_PRIVATE(IdentityAddress); L_DECLARE_PRIVATE(IdentityAddress);
}; };
inline std::ostream &operator<< (std::ostream &os, const IdentityAddress &identityAddress) {
os << "IdentityAddress(" << identityAddress.asString() << ")";
return os;
}
LINPHONE_END_NAMESPACE LINPHONE_END_NAMESPACE
#endif // ifndef _L_IDENTITY_ADDRESS_H_ #endif // ifndef _L_IDENTITY_ADDRESS_H_
...@@ -50,6 +50,11 @@ private: ...@@ -50,6 +50,11 @@ private:
L_DECLARE_PRIVATE(ChatRoomId); L_DECLARE_PRIVATE(ChatRoomId);
}; };
inline std::ostream &operator<< (std::ostream &os, const ChatRoomId &chatRoomId) {
os << "ChatRoomId(" << chatRoomId.getPeerAddress() << ", local=" << chatRoomId.getLocalAddress() << ")";
return os;
}
LINPHONE_END_NAMESPACE LINPHONE_END_NAMESPACE
// Add map key support. // Add map key support.
......
...@@ -162,10 +162,8 @@ shared_ptr<AbstractChatRoom> Core::findChatRoom (const ChatRoomId &chatRoomId) c ...@@ -162,10 +162,8 @@ shared_ptr<AbstractChatRoom> Core::findChatRoom (const ChatRoomId &chatRoomId) c
if (it != d->chatRoomsById.cend()) if (it != d->chatRoomsById.cend())
return it->second; return it->second;
lInfo() << "Unable to find chat room in RAM: (peer=" << lInfo() << "Unable to find chat room in RAM: " << chatRoomId << ".";
chatRoomId.getPeerAddress().asString() << ", local=" << chatRoomId.getLocalAddress().asString() << ")."; return nullptr;
return shared_ptr<ChatRoom>();
} }
list<shared_ptr<AbstractChatRoom>> Core::findChatRooms (const IdentityAddress &peerAddress) const { list<shared_ptr<AbstractChatRoom>> Core::findChatRooms (const IdentityAddress &peerAddress) const {
......
...@@ -30,8 +30,6 @@ LINPHONE_BEGIN_NAMESPACE ...@@ -30,8 +30,6 @@ LINPHONE_BEGIN_NAMESPACE
class AbstractDbPrivate : public ObjectPrivate { class AbstractDbPrivate : public ObjectPrivate {
public: public:
AbstractDbPrivate () = default;
DbSession dbSession; DbSession dbSession;
private: private:
......
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
#include <TargetConditionals.h> #include <TargetConditionals.h>
#endif // ifdef __APPLE__ #endif // ifdef __APPLE__
#if defined(SOCI_ENABLED) && (TARGET_OS_IPHONE || defined(__ANDROID__)) #if (TARGET_OS_IPHONE || defined(__ANDROID__))
#include <sqlite3.h> #include <sqlite3.h>
#endif // if defined(SOCI_ENABLED) && (TARGET_OS_IPHONE || defined(__ANDROID__)) #endif // if (TARGET_OS_IPHONE || defined(__ANDROID__))
#include "abstract-db-p.h" #include "abstract-db-p.h"
#include "logger/logger.h" #include "logger/logger.h"
...@@ -34,22 +34,20 @@ using namespace std; ...@@ -34,22 +34,20 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE LINPHONE_BEGIN_NAMESPACE
#if defined(SOCI_ENABLED) && (TARGET_OS_IPHONE || defined(__ANDROID__)) #if (TARGET_OS_IPHONE || defined(__ANDROID__))
// Force static sqlite3 linking for IOS and Android. // Force static sqlite3 linking for IOS and Android.
extern "C" void register_factory_sqlite3(); extern "C" void register_factory_sqlite3();
static void sqlite3Log (void *, int iErrCode, const char *zMsg) { static void sqlite3Log (void *, int iErrCode, const char *zMsg) {
lInfo() << "[sqlite3][" << iErrCode << "]" << zMsg; lInfo() << "[sqlite3][" << iErrCode << "]" << zMsg;
} }
#endif // if defined(SOCI_ENABLED) && (TARGET_OS_IPHONE || defined(__ANDROID__)) #endif // if (TARGET_OS_IPHONE || defined(__ANDROID__))
void AbstractDbPrivate::safeInit () { void AbstractDbPrivate::safeInit () {
#ifdef SOCI_ENABLED L_Q();
L_Q(); dbSession.enableForeignKeys(false);
dbSession.enableForeignKeys(false); q->init();
q->init(); dbSession.enableForeignKeys(true);
dbSession.enableForeignKeys(true);
#endif // ifdef SOCI_ENABLED
} }
AbstractDb::AbstractDb (AbstractDbPrivate &p) : Object(p) {} AbstractDb::AbstractDb (AbstractDbPrivate &p) : Object(p) {}
...@@ -57,7 +55,7 @@ AbstractDb::AbstractDb (AbstractDbPrivate &p) : Object(p) {} ...@@ -57,7 +55,7 @@ AbstractDb::AbstractDb (AbstractDbPrivate &p) : Object(p) {}
bool AbstractDb::connect (Backend backend, const string &parameters) { bool AbstractDb::connect (Backend backend, const string &parameters) {
L_D(); L_D();
#if defined(SOCI_ENABLED) && (TARGET_OS_IPHONE || defined(__ANDROID__)) #if (TARGET_OS_IPHONE || defined(__ANDROID__))
if (backend == Sqlite3) { if (backend == Sqlite3) {
static bool registered = false; static bool registered = false;
if (!registered) { if (!registered) {
...@@ -66,7 +64,7 @@ bool AbstractDb::connect (Backend backend, const string &parameters) { ...@@ -66,7 +64,7 @@ bool AbstractDb::connect (Backend backend, const string &parameters) {
sqlite3_config(SQLITE_CONFIG_LOG, sqlite3Log, nullptr); sqlite3_config(SQLITE_CONFIG_LOG, sqlite3Log, nullptr);
} }
} }
#endif // if defined(SOCI_ENABLED) && (TARGET_OS_IPHONE || defined(__ANDROID__)) #endif // if (TARGET_OS_IPHONE || defined(__ANDROID__))
d->backend = backend; d->backend = backend;
d->dbSession = DbSession( d->dbSession = DbSession(
...@@ -99,30 +97,28 @@ bool AbstractDb::forceReconnect () { ...@@ -99,30 +97,28 @@ bool AbstractDb::forceReconnect () {
return false; return false;
} }
#ifdef SOCI_ENABLED constexpr int retryCount = 2;
constexpr int retryCount = 2; lInfo() << "Trying sql backend reconnect...";
lInfo() << "Trying sql backend reconnect...";
try {
try { for (int i = 0; i < retryCount; ++i) {
for (int i = 0; i < retryCount; ++i) { try {
try { lInfo() << "Reconnect... Try: " << i;
lInfo() << "Reconnect... Try: " << i; d->dbSession.getBackendSession()->reconnect(); // Equivalent to close and connect.
d->dbSession.getBackendSession()->reconnect(); // Equivalent to close and connect. d->safeInit();
d->safeInit(); lInfo() << "Database reconnection successful!";
lInfo() << "Database reconnection successful!"; return true;
return true; } catch (const soci::soci_error &e) {
} catch (const soci::soci_error &e) { if (e.get_error_category() != soci::soci_error::connection_error)
if (e.get_error_category() != soci::soci_error::connection_error) throw e;
throw e;
}
} }
} catch (const exception &e) {
lError() << "Unable to reconnect: `" << e.what() << "`.";
return false;
} }
} catch (const exception &e) {
lError() << "Unable to reconnect: `" << e.what() << "`.";
return false;
}
lError() << "Database reconnection failed!"; lError() << "Database reconnection failed!";
#endif // ifdef SOCI_ENABLED
return false; return false;
} }
......
...@@ -28,7 +28,7 @@ LINPHONE_BEGIN_NAMESPACE ...@@ -28,7 +28,7 @@ LINPHONE_BEGIN_NAMESPACE
class AbstractDbPrivate; class AbstractDbPrivate;
class LINPHONE_PUBLIC AbstractDb : public Object { class AbstractDb : public Object {
public: public:
enum Backend { enum Backend {
Mysql, Mysql,
......
/* /*
* db-exception-handler.h * db-transaction.h
* Copyright (C) 2010-2018 Belledonne Communications SARL * Copyright (C) 2010-2018 Belledonne Communications SARL
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -17,20 +17,18 @@ ...@@ -17,20 +17,18 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#ifndef _L_DB_EXCEPTION_HANDLER_H_ #ifndef _L_DB_TRANSACTION_H_
#define _L_DB_EXCEPTION_HANDLER_H_ #define _L_DB_TRANSACTION_H_
#include <soci/soci.h>
#include "db/main-db-p.h" #include "db/main-db-p.h"
#include "logger/logger.h" #include "logger/logger.h"
// ============================================================================= // =============================================================================
#define L_DB_EXCEPTION_HANDLER_C(CONTEXT) \ #define L_DB_TRANSACTION_C(CONTEXT) \
LinphonePrivate::DbExceptionHandlerInfo().set(__func__, CONTEXT) * [&](SmartTransaction &tr) LinphonePrivate::DbTransactionInfo().set(__func__, CONTEXT) * [&](SmartTransaction &tr)
#define L_DB_EXCEPTION_HANDLER L_DB_EXCEPTION_HANDLER_C(this) #define L_DB_TRANSACTION L_DB_TRANSACTION_C(this)
LINPHONE_BEGIN_NAMESPACE LINPHONE_BEGIN_NAMESPACE
...@@ -68,8 +66,8 @@ private: ...@@ -68,8 +66,8 @@ private:
L_DISABLE_COPY(SmartTransaction); L_DISABLE_COPY(SmartTransaction);
}; };
struct DbExceptionHandlerInfo { struct DbTransactionInfo {
DbExceptionHandlerInfo &set (const char *_name, const MainDb *_mainDb) { DbTransactionInfo &set (const char *_name, const MainDb *_mainDb) {
name = _name; name = _name;
mainDb = const_cast<MainDb *>(_mainDb); mainDb = const_cast<MainDb *>(_mainDb);
return *this; return *this;
...@@ -80,7 +78,7 @@ struct DbExceptionHandlerInfo { ...@@ -80,7 +78,7 @@ struct DbExceptionHandlerInfo {
}; };
template<typename Function> template<typename Function>
class DbExceptionHandler { class DbTransaction {
using InternalReturnType = typename std::remove_reference< using InternalReturnType = typename std::remove_reference<
decltype(std::declval<Function>()(std::declval<SmartTransaction &>())) decltype(std::declval<Function>()(std::declval<SmartTransaction &>()))
>::type; >::type;
...@@ -92,7 +90,7 @@ public: ...@@ -92,7 +90,7 @@ public:
InternalReturnType InternalReturnType
>::type; >::type;
DbExceptionHandler (DbExceptionHandlerInfo &info, Function &&function) : mFunction(std::move(function)) { DbTransaction (DbTransactionInfo &info, Function &&function) : mFunction(std::move(function)) {
MainDb *mainDb = info.mainDb; MainDb *mainDb = info.mainDb;
const char *name = info.name; const char *name = info.name;
soci::session *session = mainDb->getPrivate()->dbSession.getBackendSession(); soci::session *session = mainDb->getPrivate()->dbSession.getBackendSession();
...@@ -122,7 +120,7 @@ public: ...@@ -122,7 +120,7 @@ public:
} }
} }
DbExceptionHandler (DbExceptionHandler &&dbExceptionHandler) : mFunction(std::move(dbExceptionHandler.mFunction)) {} DbTransaction (DbTransaction &&DbTransaction) : mFunction(std::move(DbTransaction.mFunction)) {}
operator ReturnType () const { operator ReturnType () const {
return mResult; return mResult;
...@@ -170,14 +168,14 @@ private: ...@@ -170,14 +168,14 @@ private:
Function mFunction; Function mFunction;
ReturnType mResult{}; ReturnType mResult{};
L_DISABLE_COPY(DbExceptionHandler); L_DISABLE_COPY(DbTransaction);
}; };
template<typename Function> template<typename Function>
typename DbExceptionHandler<Function>::ReturnType operator* (DbExceptionHandlerInfo &info, Function &&function) { typename DbTransaction<Function>::ReturnType operator* (DbTransactionInfo &info, Function &&function) {
return DbExceptionHandler<Function>(info, std::forward<Function>(function)); return DbTransaction<Function>(info, std::forward<Function>(function));
} }
LINPHONE_END_NAMESPACE LINPHONE_END_NAMESPACE
#endif // ifndef _L_DB_EXCEPTION_HANDLER_H_ #endif // ifndef _L_DB_TRANSACTION_H_
...@@ -50,42 +50,11 @@ namespace Statements { ...@@ -50,42 +50,11 @@ namespace Statements {
const char *mSql[2]; const char *mSql[2];
}; };
// ---------------------------------------------------------------------------
// Create statements.
// ---------------------------------------------------------------------------
constexpr AbstractStatement create[CreateCount] = {
[CreateConferenceEventView] = { {
Backend::Sqlite3, R"(
CREATE VIEW IF NOT EXISTS conference_event_view AS
SELECT id, type, creation_time, chat_room_id, from_sip_address_id, to_sip_address_id, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address_id, participant_sip_address_id, subject
FROM event
LEFT JOIN conference_event ON conference_event.event_id = event.id
LEFT JOIN conference_chat_message_event ON conference_chat_message_event.event_id = event.id
LEFT JOIN conference_notified_event ON conference_notified_event.event_id = event.id
LEFT JOIN conference_participant_device_event ON conference_participant_device_event.event_id = event.id
LEFT JOIN conference_participant_event ON conference_participant_event.event_id = event.id
LEFT JOIN conference_subject_event ON conference_subject_event.event_id = event.id
)" }, {
Backend::Mysql, R"(
CREATE OR REPLACE VIEW conference_event_view AS
SELECT id, type, creation_time, chat_room_id, from_sip_address_id, to_sip_address_id, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address_id, participant_sip_address_id, subject
FROM event
LEFT JOIN conference_event ON conference_event.event_id = event.id
LEFT JOIN conference_chat_message_event ON conference_chat_message_event.event_id = event.id
LEFT JOIN conference_notified_event ON conference_notified_event.event_id = event.id
LEFT JOIN conference_participant_device_event ON conference_participant_device_event.event_id = event.id
LEFT JOIN conference_participant_event ON conference_participant_event.event_id = event.id
LEFT JOIN conference_subject_event ON conference_subject_event.event_id = event.id
)"
} }
};
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Select statements. // Select statements.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
constexpr AbstractStatement select[SelectCount] = { constexpr const char *select[SelectCount] = {
[SelectSipAddressId] = R"( [SelectSipAddressId] = R"(
SELECT id SELECT id
FROM sip_address FROM sip_address
...@@ -111,6 +80,19 @@ namespace Statements { ...@@ -111,6 +80,19 @@ namespace Statements {
AND participant_b_sip_address_id IN (:3, :4) AND participant_b_sip_address_id IN (:3, :4)
)", )",
[SelectConferenceEvent] = R"(
SELECT conference_event_view.id AS event_id, type, conference_event_view.creation_time, from_sip_address.value, to_sip_address.value, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address.value, participant_sip_address.value, conference_event_view.subject, peer_sip_address.value, local_sip_address.value
FROM conference_event_view
JOIN chat_room ON chat_room.id = chat_room_id
JOIN sip_address AS peer_sip_address ON peer_sip_address.id = peer_sip_address_id
JOIN sip_address AS local_sip_address ON local_sip_address.id = local_sip_address_id
LEFT JOIN sip_address AS from_sip_address ON from_sip_address.id = from_sip_address_id
LEFT JOIN sip_address AS to_sip_address ON to_sip_address.id = to_sip_address_id
LEFT JOIN sip_address AS device_sip_address ON device_sip_address.id = device_sip_address_id
LEFT JOIN sip_address AS participant_sip_address ON participant_sip_address.id = participant_sip_address_id
WHERE event_id = :1
)",
[SelectConferenceEvents] = R"( [SelectConferenceEvents] = R"(
SELECT conference_event_view.id AS event_id, type, creation_time, from_sip_address.value, to_sip_address.value, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address.value, participant_sip_address.value, subject SELECT conference_event_view.id AS event_id, type, creation_time, from_sip_address.value, to_sip_address.value, time, imdn_message_id, state, direction, is_secured, notify_id, device_sip_address.value, participant_sip_address.value, subject
FROM conference_event_view FROM conference_event_view
...@@ -138,12 +120,8 @@ namespace Statements { ...@@ -138,12 +120,8 @@ namespace Statements {
// Getters. // Getters.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const char *get (Create createStmt, AbstractDb::Backend backend) { const char *get (Select selectStmt) {
return createStmt >= Create::CreateCount ? nullptr : create[createStmt].get(backend); return selectStmt >= Select::SelectCount ? nullptr : select[selectStmt];
}
const char *get (Select selectStmt, AbstractDb::Backend backend) {
return selectStmt >= Select::SelectCount ? nullptr : select[selectStmt].get(backend);
} }
const char *get (Insert insertStmt, AbstractDb::Backend backend) { const char *get (Insert insertStmt, AbstractDb::Backend backend) {
......
...@@ -27,16 +27,12 @@ ...@@ -27,16 +27,12 @@
LINPHONE_BEGIN_NAMESPACE LINPHONE_BEGIN_NAMESPACE
namespace Statements { namespace Statements {
enum Create {
CreateConferenceEventView,
CreateCount
};
enum Select { enum Select {
SelectSipAddressId, SelectSipAddressId,
SelectChatRoomId, SelectChatRoomId,
SelectChatRoomParticipantId, SelectChatRoomParticipantId,
SelectOneToOneChatRoomId, SelectOneToOneChatRoomId,
SelectConferenceEvent,
SelectConferenceEvents, SelectConferenceEvents,
SelectCount SelectCount
}; };
...@@ -46,8 +42,7 @@ namespace Statements { ...@@ -46,8 +42,7 @@ namespace Statements {
InsertCount InsertCount
}; };
const char *get (Create createStmt, AbstractDb::Backend backend); const char *get (Select selectStmt);
const char *get (Select selectStmt, AbstractDb::Backend backend);
const char *get (Insert insertStmt, AbstractDb::Backend backend); const char *get (Insert insertStmt, AbstractDb::Backend backend);
} }
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include <unordered_map> #include <unordered_map>
#include "linphone/utils/utils.h"
#include "abstract/abstract-db-p.h" #include "abstract/abstract-db-p.h"
#include "event-log/event-log.h" #include "event-log/event-log.h"
#include "main-db.h" #include "main-db.h"
...@@ -38,12 +40,18 @@ public: ...@@ -38,12 +40,18 @@ public:
mutable std::unordered_map<long long, std::weak_ptr<ChatMessage>> storageIdToChatMessage; mutable std::unordered_map<long long, std::weak_ptr<ChatMessage>> storageIdToChatMessage;
private: private:
// ---------------------------------------------------------------------------
// Misc helpers.
// ---------------------------------------------------------------------------
std::shared_ptr<AbstractChatRoom> findChatRoom (const ChatRoomId &chatRoomId) const;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Low level API. // Low level API.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
long long insertSipAddress (const std::string &sipAddress); long long insertSipAddress (const std::string &sipAddress);
void insertContent (long long messageEventId, const Content &content); void insertContent (long long chatMessageId, const Content &content);
long long insertContentType (const std::string &contentType); long long insertContentType (const std::string &contentType);
long long insertOrUpdateImportedBasicChatRoom ( long long insertOrUpdateImportedBasicChatRoom (
long long peerSipAddressId, long long peerSipAddressId,
...@@ -53,7 +61,7 @@ private: ...@@ -53,7 +61,7 @@ private:
long long insertChatRoom (const std::shared_ptr<AbstractChatRoom> &chatRoom); long long insertChatRoom (const std::shared_ptr<AbstractChatRoom> &chatRoom);
long long insertChatRoomParticipant (long long chatRoomId, long long participantSipAddressId, bool isAdmin); long long insertChatRoomParticipant (long long chatRoomId, long long participantSipAddressId, bool isAdmin);
void insertChatRoomParticipantDevice (long long participantId, long long participantDeviceSipAddressId); void insertChatRoomParticipantDevice (long long participantId, long long participantDeviceSipAddressId);
void insertChatMessageParticipant (long long messageEventId, long long sipAddressId, int state); void insertChatMessageParticipant (long long chatMessageId, long long sipAddressId, int state);
long long selectSipAddressId (const std::string &sipAddress) const; long long selectSipAddressId (const std::string &sipAddress) const;
long long selectChatRoomId (long long peerSipAddressId, long long localSipAddressId) const; long long selectChatRoomId (long long peerSipAddressId, long long localSipAddressId) const;
...@@ -61,7 +69,7 @@ private: ...@@ -61,7 +69,7 @@ private:
long long selectChatRoomParticipantId (long long chatRoomId, long long participantSipAddressId) const; long long selectChatRoomParticipantId (long long chatRoomId, long long participantSipAddressId) const;
long long selectOneToOneChatRoomId (long long sipAddressIdA, long long sipAddressIdB) const; long long selectOneToOneChatRoomId (long long sipAddressIdA, long long sipAddressIdB) const;
void deleteContents (long long messageEventId); void deleteContents (long long chatMessageId);
void deleteChatRoomParticipant (long long chatRoomId, long long participantSipAddressId); void deleteChatRoomParticipant (long long chatRoomId, long long participantSipAddressId);
void deleteChatRoomParticipantDevice (long long participantId, long long participantDeviceSipAddressId); void deleteChatRoomParticipantDevice (long long participantId, long long participantDeviceSipAddressId);
...@@ -69,68 +77,65 @@ private: ...@@ -69,68 +77,65 @@ private:
// Events API. // Events API.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
long long getConferenceEventIdFromRow (const soci::row &row) const {
return dbSession.resolveId(row, -1);
}
time_t getConferenceEventCreationTimeFromRow (const soci::row &row) const {
return Utils::getTmAsTimeT(row.get<tm>(2));
}
unsigned int getConferenceEventNotifyIdFromRow (const soci::row &row) const {
L_Q();
return q->getBackend() == MainDb::Backend::Mysql
? row.get<unsigned int>(10, 0)
: static_cast<unsigned int>(row.get<int>(10, 0));
}
std::shared_ptr<EventLog> selectGenericConferenceEvent ( std::shared_ptr<EventLog> selectGenericConferenceEvent (
long long eventId, const std::shared_ptr<AbstractChatRoom> &chatRoom,
EventLog::Type type, const soci::row &row
time_t creationTime,
const ChatRoomId &chatRoomId
) const; ) const;
std::shared_ptr<EventLog> selectConferenceEvent ( std::shared_ptr<EventLog> selectGenericConferenceNotifiedEvent (
long long eventId, const ChatRoomId &chatRoomId,
EventLog::Type type, const soci::row &row
time_t creationTime,
const ChatRoomId &chatRoomId
) const; ) const;
std::shared_ptr<EventLog> selectConferenceCallEvent ( std::shared_ptr<EventLog> selectConferenceEvent (
long long eventId, const ChatRoomId &chatRoomId,
EventLog::Type type, EventLog::Type type,
time_t creationTime, const soci::row &row
const ChatRoomId &chatRoomId
) const; ) const;
std::shared_ptr<EventLog> selectConferenceChatMessageEvent ( std::shared_ptr<EventLog> selectConferenceCallEvent (
long long eventId, const ChatRoomId &chatRoomId,
EventLog::Type type, EventLog::Type type,
time_t creationTime, const soci::row &row
const ChatRoomId &chatRoomId
) const; ) const;
// TODO: Remove me. Workaround to increase fetch performance.
std::shared_ptr<EventLog> selectConferenceChatMessageEvent ( std::shared_ptr<EventLog> selectConferenceChatMessageEvent (
long long eventId, const std::shared_ptr<AbstractChatRoom> &chatRoom,
EventLog::Type type, EventLog::Type type,
time_t creationTime, const soci::row &row
std::shared_ptr<AbstractChatRoom> &chatRoom,
const std::string &fromSipAddress,
const std::string &toSipAddress,
const tm &messageTime,
const std::string &imdnMessageId,
int state,
int direction,
int isSecured
) const; ) const;