add msglen column to msgs table to be used to avoid calculating size

later.  Stores size of string as it was passed to db, not as stored.
This commit is contained in:
Andy2 2011-01-21 06:46:49 -08:00
parent b863931db5
commit 96fead3164
2 changed files with 8 additions and 4 deletions

View file

@ -434,14 +434,15 @@ DBMgr::StoreMessage( const char* const connName, int hid,
const unsigned char* buf, int len ) const unsigned char* buf, int len )
{ {
size_t newLen; size_t newLen;
const char* fmt = "INSERT INTO " MSGS_TABLE " (connname, hid, msg)" const char* fmt = "INSERT INTO " MSGS_TABLE " (connname, hid, msg, msglen)"
" VALUES( '%s', %d, E'%s')"; " VALUES( '%s', %d, E'%s', %d)";
unsigned char* bytes = PQescapeByteaConn( getThreadConn(), buf, len, &newLen ); unsigned char* bytes = PQescapeByteaConn( getThreadConn(), buf, len, &newLen );
assert( NULL != bytes ); assert( NULL != bytes );
char query[newLen+128]; char query[newLen+128];
unsigned int siz = snprintf( query, sizeof(query), fmt, connName, hid, bytes ); unsigned int siz = snprintf( query, sizeof(query), fmt, connName, hid,
bytes, len );
logf( XW_LOGINFO, "%s: query: %s", __func__, query ); logf( XW_LOGINFO, "%s: query: %s", __func__, query );
PQfreemem( bytes ); PQfreemem( bytes );
assert( siz < sizeof(query) ); assert( siz < sizeof(query) );
@ -453,7 +454,7 @@ bool
DBMgr::GetStoredMessage( const char* const connName, int hid, DBMgr::GetStoredMessage( const char* const connName, int hid,
unsigned char* buf, size_t* buflen, int* msgID ) unsigned char* buf, size_t* buflen, int* msgID )
{ {
const char* fmt = "SELECT id, msg FROM " MSGS_TABLE const char* fmt = "SELECT id, msg, msglen FROM " MSGS_TABLE
" WHERE connName = '%s' AND hid = %d ORDER BY id LIMIT 1"; " WHERE connName = '%s' AND hid = %d ORDER BY id LIMIT 1";
char query[256]; char query[256];
snprintf( query, sizeof(query), fmt, connName, hid ); snprintf( query, sizeof(query), fmt, connName, hid );
@ -466,6 +467,7 @@ DBMgr::GetStoredMessage( const char* const connName, int hid,
bool found = nTuples == 1; bool found = nTuples == 1;
if ( found ) { if ( found ) {
*msgID = atoi( PQgetvalue( result, 0, 0 ) ); *msgID = atoi( PQgetvalue( result, 0, 0 ) );
size_t msglen = atoi( PQgetvalue( result, 0, 2 ) );
/* int len = PQgetlength( result, 0, 1 ); */ /* int len = PQgetlength( result, 0, 1 ); */
const unsigned char* from = const unsigned char* from =
@ -476,6 +478,7 @@ DBMgr::GetStoredMessage( const char* const connName, int hid,
memcpy( buf, bytes, to_length ); memcpy( buf, bytes, to_length );
PQfreemem( bytes ); PQfreemem( bytes );
*buflen = to_length; *buflen = to_length;
assert( to_length == msglen );
} }
PQclear( result ); PQclear( result );
return found; return found;

View file

@ -54,6 +54,7 @@ id SERIAL
,hid INTEGER ,hid INTEGER
,ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
,msg BYTEA ,msg BYTEA
,msglen INTEGER
,UNIQUE ( connName, hid, msg ) ,UNIQUE ( connName, hid, msg )
); );
EOF EOF