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.
This commit is contained in:
Eric House 2013-08-15 07:24:30 -07:00
parent 42bbc946ae
commit 61009cefc7
2 changed files with 44 additions and 3 deletions

View file

@ -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<DBMgr::MsgInfo>& msgs )
void
DBMgr::GetStoredMessages( DevIDRelay relayID, vector<MsgInfo>& 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, ... )

View file

@ -23,6 +23,7 @@
#define _DBMGR_H_
#include <string>
#include <set>
#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<DevIDRelay> m_haveNoMessages;
}; /* DBMgr */