From 61009cefc76660493b66d89721129d1b7f4c014d Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 15 Aug 2013 07:24:30 -0700 Subject: [PATCH] in advance of using memcache, keep track (in memory) of those devids for which no messages are pending, since that's a query that can be made by each device every minute. --- xwords4/relay/dbmgr.cpp | 39 ++++++++++++++++++++++++++++++++++++--- xwords4/relay/dbmgr.h | 8 ++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 5d3cf2524..15825c6ff 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -877,6 +877,8 @@ DBMgr::StoreMessage( const char* const connName, int hid, if ( DEVID_NONE == devID ) { logf( XW_LOGERROR, "%s: warning: devid not found for connName=%s, " "hid=%d", __func__, connName, hid ); + } else { + clearHasNoMessages( devID ); } size_t newLen; @@ -972,9 +974,15 @@ DBMgr::storedMessagesImpl( string test, vector& msgs ) void DBMgr::GetStoredMessages( DevIDRelay relayID, vector& msgs ) { - string query; - string_printf( query, "devid=%d", relayID ); - storedMessagesImpl( query, msgs ); + if ( !hasNoMessages( relayID ) ) { + string query; + string_printf( query, "devid=%d", relayID ); + storedMessagesImpl( query, msgs ); + + if ( 0 == msgs.size() ) { + setHasNoMessages( relayID ); + } + } } void @@ -1058,6 +1066,31 @@ DBMgr::getCountWhere( const char* table, string& test ) return count; } +bool DBMgr::hasNoMessages( DevIDRelay devid ) +{ + bool result; + { + MutexLock ml( &m_haveNoMessagesMutex ); + result = m_haveNoMessages.find(devid) != m_haveNoMessages.end(); + } + logf( XW_LOGINFO, "%s(devid=%d)=>%d", __func__, devid, result ); + return result; +} + +void DBMgr::setHasNoMessages( DevIDRelay devid ) +{ + logf( XW_LOGINFO, "%s(devid=%d)", __func__, devid ); + MutexLock ml( &m_haveNoMessagesMutex ); + m_haveNoMessages.insert( devid ); +} + +void DBMgr::clearHasNoMessages( DevIDRelay devid ) +{ + logf( XW_LOGINFO, "%s(devid=%d)", __func__, devid ); + MutexLock ml( &m_haveNoMessagesMutex ); + m_haveNoMessages.erase( devid ); +} + static void formatParams( char* paramValues[], int nParams, const char* fmt, char* buf, int bufLen, ... ) diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index ae79335c2..3b629999a 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -23,6 +23,7 @@ #define _DBMGR_H_ #include +#include #include "xwrelay.h" #include "xwrelay_priv.h" @@ -141,10 +142,17 @@ class DBMgr { PGconn* getThreadConn( void ); void clearThreadConn(); + bool hasNoMessages( DevIDRelay devid ); + void setHasNoMessages( DevIDRelay devid ); + void clearHasNoMessages( DevIDRelay devid ); + void conn_key_alloc(); pthread_key_t m_conn_key; bool m_useB64; + pthread_mutex_t m_haveNoMessagesMutex; + set m_haveNoMessages; + }; /* DBMgr */