From c212c0e3674ccbb020ccc7ff93201fc4cf06e1f8 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 14 Sep 2013 21:39:18 -0700 Subject: [PATCH] add in-memory cacheing of messageless-connname/hid combos. --- xwords4/relay/dbmgr.cpp | 63 +++++++++++++++++++++++++++++++++++++---- xwords4/relay/dbmgr.h | 10 +++++-- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index bf05eeb23..66df622fc 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -981,6 +981,8 @@ void DBMgr::StoreMessage( const char* const connName, int hid, const uint8_t* buf, int len ) { + clearHasNoMessages( connName, hid ); + DevIDRelay devID = getDevID( connName, hid ); if ( DEVID_NONE == devID ) { logf( XW_LOGERROR, "%s: warning: devid not found for connName=%s, " @@ -1105,9 +1107,15 @@ void DBMgr::GetStoredMessages( const char* const connName, HostID hid, vector& msgs ) { - StrWPF query; - query.printf( "hid = %d AND connname = '%s'", hid, connName ); - storedMessagesImpl( query, msgs, false ); + if ( !hasNoMessages( connName, hid ) ) { + StrWPF query; + query.printf( "hid = %d AND connname = '%s'", hid, connName ); + storedMessagesImpl( query, msgs, false ); + + if ( 0 == msgs.size() ) { + setHasNoMessages( connName, hid ); + } + } } void @@ -1182,10 +1190,53 @@ DBMgr::getCountWhere( const char* table, string& test ) return count; } +void +DBMgr::formatKey( StrWPF& key, const char* const connName, HostID hid ) +{ + key.printf( "%s:%d", connName, hid ); +} + +bool +DBMgr::hasNoMessages( const char* const connName, HostID hid ) +{ + StrWPF key; + formatKey( key, connName, hid ); + MutexLock ml( &m_haveNoMessagesMutex ); + bool result = m_haveNoMessagesConnname.find(key) != m_haveNoMessagesConnname.end(); + logf( XW_LOGINFO, "%s(key=%s)=>%d", __func__, key.c_str(), result ); + return result; +} + +void +DBMgr::setHasNoMessages( const char* const connName, HostID hid ) +{ + StrWPF key; + formatKey( key, connName, hid ); + logf( XW_LOGINFO, "%s(key=%s)", __func__, key.c_str() ); + { + MutexLock ml( &m_haveNoMessagesMutex ); + m_haveNoMessagesConnname.insert( key ); + } + assert( hasNoMessages( connName, hid ) ); +} + +void +DBMgr::clearHasNoMessages( const char* const connName, HostID hid ) +{ + StrWPF key; + formatKey( key, connName, hid ); + logf( XW_LOGINFO, "%s(key=%s)", __func__, key.c_str() ); + { + MutexLock ml( &m_haveNoMessagesMutex ); + m_haveNoMessagesConnname.erase( key ); + } + assert( !hasNoMessages( connName, hid ) ); +} + bool DBMgr::hasNoMessages( DevIDRelay devid ) { MutexLock ml( &m_haveNoMessagesMutex ); - bool result = m_haveNoMessages.find(devid) != m_haveNoMessages.end(); + bool result = m_haveNoMessagesDevID.find(devid) != m_haveNoMessagesDevID.end(); logf( XW_LOGINFO, "%s(devid=%d)=>%d", __func__, devid, result ); return result; } @@ -1195,7 +1246,7 @@ void DBMgr::setHasNoMessages( DevIDRelay devid ) logf( XW_LOGINFO, "%s(devid=%d)", __func__, devid ); { MutexLock ml( &m_haveNoMessagesMutex ); - m_haveNoMessages.insert( devid ); + m_haveNoMessagesDevID.insert( devid ); } assert( hasNoMessages( devid ) ); } @@ -1205,7 +1256,7 @@ void DBMgr::clearHasNoMessages( DevIDRelay devid ) logf( XW_LOGINFO, "%s(devid=%d)", __func__, devid ); { MutexLock ml( &m_haveNoMessagesMutex ); - m_haveNoMessages.erase( devid ); + m_haveNoMessagesDevID.erase( devid ); } assert( !hasNoMessages( devid ) ); } diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index c505d7d10..ddaa1b9f2 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -25,6 +25,7 @@ #include #include +#include "strwpf.h" #include "xwrelay.h" #include "xwrelay_priv.h" #include "devid.h" @@ -165,6 +166,11 @@ class DBMgr { PGconn* getThreadConn( void ); void clearThreadConn(); + bool hasNoMessages( const char* const connName, HostID hid ); + void setHasNoMessages( const char* const connName, HostID hid ); + void clearHasNoMessages( const char* const connName, HostID hid ); + void formatKey( StrWPF& key, const char* const connName, HostID hid ); + bool hasNoMessages( DevIDRelay devid ); void setHasNoMessages( DevIDRelay devid ); void clearHasNoMessages( DevIDRelay devid ); @@ -174,8 +180,8 @@ class DBMgr { bool m_useB64; pthread_mutex_t m_haveNoMessagesMutex; - set m_haveNoMessages; - + set m_haveNoMessagesDevID; + set m_haveNoMessagesConnname; }; /* DBMgr */