Skip to content

Commit

Permalink
added log of the SphinxQL query that successfully processed by buddy …
Browse files Browse the repository at this point in the history
…into the query log; fixed #2235
  • Loading branch information
tomatolog authored and donhardman committed Nov 14, 2024
1 parent 1ef3d00 commit 38cda75
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/searchd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3422,6 +3422,30 @@ void LogSphinxqlError ( const char * sStmt, const Str_t& sError )
sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() );
}

void LogSphinxqlBuddyQuery ( const Str_t sQuery, const CSphQueryResultMeta & tMeta )
{
if ( g_eLogFormat!=LOG_FORMAT_SPHINXQL || g_iQueryLogFile<0 || IsEmpty ( sQuery ) )
return;

StringBuilder_c tBuf;

// time, conn id, wall, found
int iQueryTime = Max ( tMeta.m_iQueryTime, 0 );
int iRealTime = Max ( tMeta.m_iRealQueryTime, 0 );

tBuf << "/* ";
FormatTimeConnClient ( tBuf );
tBuf << " real " << FixedFrac ( iRealTime ) << " wall " << FixedFrac ( iQueryTime );

if ( tMeta.m_iMultiplier>1 )
tBuf << " x" << tMeta.m_iMultiplier;
tBuf << " found " << tMeta.m_iTotalMatches << " */ ";

tBuf << sQuery << '\n';

sphSeek ( g_iQueryLogFile, 0, SEEK_END );
sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() );
}

void ReportIndexesName ( int iSpanStart, int iSpandEnd, const CSphVector<SearchFailure_t> & dLog, StringBuilder_c & sOut )
{
Expand Down
1 change: 1 addition & 0 deletions src/searchdaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,7 @@ namespace session
}

void LogSphinxqlError ( const char * sStmt, const Str_t& sError );
void LogSphinxqlBuddyQuery ( const Str_t sQuery, const CSphQueryResultMeta & tMeta );
int GetDaemonLogBufSize ();

// that is used from sphinxql command over API
Expand Down
60 changes: 60 additions & 0 deletions src/searchdbuddy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,65 @@ static bool ConvertErrorMessage ( const char * sStmt, std::pair<int, BYTE> tSave
return true;
}

template<typename T>
bool ConvertValue ( const char * sName, const JsonObj_c & tMeta, T & tVal )
{
JsonObj_c tSrcVal = tMeta.GetItem ( sName );
if ( !tSrcVal )
return false;

if ( !tSrcVal.IsStr() )
return false;

int64_t iVal = 0;
double fVal = 0.0;
ESphJsonType eType;
if ( !sphJsonStringToNumber ( tSrcVal.SzVal(), strlen ( tSrcVal.SzVal() ), eType, iVal, fVal ) )
return false;

if ( eType==JSON_INT64 )
tVal = (T)iVal;
else
tVal = (T)fVal;
return true;
}

static void LogBuddyQuery ( const Str_t sSrcQuery, const JsonObj_c & tBudyyReply )
{
CSphFixedVector<BYTE> dBuf ( sSrcQuery.second + CSphString::GetGap() );
const char * sCur = sSrcQuery.first;
const char * sEnd = sCur + sSrcQuery.second;
BYTE * sDst = dBuf.Begin();

while ( sCur<sEnd )
FixupSpace_t::FixupSpace ( sDst, *sCur++ );

int iDstLen = sDst - dBuf.Begin();
*sDst++ = '\0';
Str_t sLogQuery ( (const char *)dBuf.Begin(), iDstLen );

CSphString sTmpError;
CSphQueryResultMeta tLogMeta;
JsonObj_c tSrcMeta = tBudyyReply.GetObjItem ( "meta", sTmpError, true );
if ( tSrcMeta )
{
// total => m_iMatches
ConvertValue ( "total", tSrcMeta, tLogMeta.m_iMatches );

// total_found => m_iTotalMatches
ConvertValue ( "total_found", tSrcMeta, tLogMeta.m_iTotalMatches );

// time => m_iQueryTime \ m_iRealQueryTime
float fTime = 0.0f;
if ( ConvertValue ( "time", tSrcMeta, fTime ) )
tLogMeta.m_iRealQueryTime = tLogMeta.m_iQueryTime = (int)( fTime * 1000.0f );

// total_relation => null
}

LogSphinxqlBuddyQuery ( sLogQuery, tLogMeta );
}

void ProcessSqlQueryBuddy ( Str_t sSrcQuery, Str_t tError, std::pair<int, BYTE> tSavedPos, BYTE & uPacketID, GenericOutputBuffer_c & tOut )
{
auto tReplyRaw = BuddyQuery ( false, tError, Str_t(), sSrcQuery, HTTP_GET, VecTraits_T<BYTE>() );
Expand Down Expand Up @@ -809,6 +868,7 @@ void ProcessSqlQueryBuddy ( Str_t sSrcQuery, Str_t tError, std::pair<int, BYTE>
std::unique_ptr<RowBuffer_i> tBuddyRows ( CreateSqlRowBuffer ( &uPacketID, &tOut ) );

ConvertJsonDataset ( tReplyParsed.m_tMessage, sSrcQuery.first, *tBuddyRows );
LogBuddyQuery ( sSrcQuery, tReplyParsed.m_tRoot );
}

#ifdef _WIN32
Expand Down

0 comments on commit 38cda75

Please sign in to comment.