add new RecordSent to take list of msg ids -- and use stringstream

instead of static buffer and snprintf in one case.  May make sense
elsewhere too.
This commit is contained in:
Andy2 2011-08-17 18:07:21 -07:00
parent 8148c7699a
commit b511b4c455
2 changed files with 56 additions and 18 deletions

View file

@ -22,6 +22,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <iostream>
#include <sstream>
#include <string>
#include "dbmgr.h" #include "dbmgr.h"
#include "mlock.h" #include "mlock.h"
@ -393,6 +396,39 @@ DBMgr::RecordSent( const char* const connName, HostID hid, int nBytes )
execSql( query ); execSql( query );
} }
void
DBMgr::RecordSent( const int* msgIDs, int nMsgIDs )
{
if ( nMsgIDs > 0 ) {
stringstream buf;
buf << "SELECT connname, hid, sum(msglen) FROM " MSGS_TABLE
" WHERE id IN (";
for ( int ii = 0; ; ) {
buf << msgIDs[ii];
if ( ++ii == nMsgIDs ) {
break;
}
buf << ',';
}
buf << ") GROUP BY connname,hid";
const char* query = buf.str().c_str();
logf( XW_LOGINFO, "%s: query: %s", __func__, query );
PGresult* result = PQexec( getThreadConn(), query );
if ( PGRES_COMMAND_OK == PQresultStatus( result ) ) {
int ntuples = PQntuples( result );
for ( int ii = 0; ii < ntuples; ++ii ) {
RecordSent( PQgetvalue( result, ii, 0 ),
atoi( PQgetvalue( result, ii, 1 ) ),
atoi( PQgetvalue( result, ii, 2 ) ) );
}
}
PQclear( result );
}
}
void void
DBMgr::GetPlayerCounts( const char* const connName, int* nTotal, int* nHere ) DBMgr::GetPlayerCounts( const char* const connName, int* nTotal, int* nHere )
{ {
@ -475,7 +511,7 @@ DBMgr::PendingMsgCount( const char* connName, int hid )
} }
bool bool
DBMgr::execSql( const char* query ) DBMgr::execSql( const char* const query )
{ {
PGresult* result = PQexec( getThreadConn(), query ); PGresult* result = PQexec( getThreadConn(), query );
bool ok = PGRES_COMMAND_OK == PQresultStatus(result); bool ok = PGRES_COMMAND_OK == PQresultStatus(result);
@ -606,24 +642,25 @@ DBMgr::GetStoredMessage( const char* const connName, int hid,
void void
DBMgr::RemoveStoredMessages( const int* msgIDs, int nMsgIDs ) DBMgr::RemoveStoredMessages( const int* msgIDs, int nMsgIDs )
{ {
char ids[1024]; if ( nMsgIDs > 0 ) {
int len = 0; stringstream buf;
int ii;
assert( nMsgIDs > 0 ); buf << "DELETE FROM " MSGS_TABLE " WHERE id IN (";
for ( ii = 0; ; ) {
len += snprintf( ids + len, sizeof(ids) - len, "%d,", msgIDs[ii] ); for ( int ii = 0; ; ) {
buf << msgIDs[ii];
if ( ++ii == nMsgIDs ) { if ( ++ii == nMsgIDs ) {
ids[len-1] = '\0'; /* overwrite last comma */
break; break;
} }
buf << ',';
} }
const char* fmt = "DELETE from " MSGS_TABLE " WHERE id in (%s)"; buf << ')';
char query[1024];
snprintf( query, sizeof(query), fmt, ids );
logf( XW_LOGINFO, "%s: query: %s", __func__, query );
const char* query = buf.str().c_str();
logf( XW_LOGINFO, "%s: query: %s", __func__, query );
execSql( query ); execSql( query );
} }
}
static void static void
formatParams( char* paramValues[], int nParams, const char* fmt, char* buf, formatParams( char* paramValues[], int nParams, const char* fmt, char* buf,

View file

@ -64,6 +64,7 @@ class DBMgr {
bool AddCID( const char* const connName, CookieID cid ); bool AddCID( const char* const connName, CookieID cid );
void ClearCID( const char* connName ); void ClearCID( const char* connName );
void RecordSent( const char* const connName, HostID hid, int nBytes ); void RecordSent( const char* const connName, HostID hid, int nBytes );
void RecordSent( const int* msgID, int nMsgIDs );
void GetPlayerCounts( const char* const connName, int* nTotal, void GetPlayerCounts( const char* const connName, int* nTotal,
int* nHere ); int* nHere );
@ -91,7 +92,7 @@ class DBMgr {
private: private:
DBMgr(); DBMgr();
bool execSql( const char* query ); /* no-results query */ bool execSql( const char* const query ); /* no-results query */
void readArray( const char* const connName, int arr[] ); void readArray( const char* const connName, int arr[] );
PGconn* getThreadConn( void ); PGconn* getThreadConn( void );