add in-memory cacheing of messageless-connname/hid combos.

This commit is contained in:
Eric House 2013-09-14 21:39:18 -07:00
parent 657d0d2b1f
commit c212c0e367
2 changed files with 65 additions and 8 deletions

View file

@ -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<DBMgr::MsgInfo>& 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 ) );
}

View file

@ -25,6 +25,7 @@
#include <string>
#include <set>
#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<DevIDRelay> m_haveNoMessages;
set<DevIDRelay> m_haveNoMessagesDevID;
set<StrWPF> m_haveNoMessagesConnname;
}; /* DBMgr */