Skip to content

Instantly share code, notes, and snippets.

@cute
Created April 1, 2012 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cute/2271978 to your computer and use it in GitHub Desktop.
Save cute/2271978 to your computer and use it in GitHub Desktop.
Sphinx 2.0.1 GetDocinfo Patch
diff -rup sphinx-2.0.1-beta/src/searchd.cpp 2.0.1/src/searchd.cpp
--- sphinx-2.0.1-beta/src/searchd.cpp 2011-04-22 02:16:02.000000000 +0800
+++ 2.0.1/src/searchd.cpp 2011-09-01 12:30:02.000000000 +0800
@@ -439,7 +439,8 @@ enum SearchdCommand_e
SEARCHD_COMMAND_PERSIST = 4,
SEARCHD_COMMAND_STATUS = 5,
SEARCHD_COMMAND_FLUSHATTRS = 7,
-
+ SEARCHD_COMMAND_SEGMENT = 8,
+ SEARCHD_COMMAND_DOCINFO = 9,
SEARCHD_COMMAND_TOTAL
};
@@ -453,7 +454,9 @@ enum
VER_COMMAND_KEYWORDS = 0x100,
VER_COMMAND_STATUS = 0x100,
VER_COMMAND_QUERY = 0x100,
- VER_COMMAND_FLUSHATTRS = 0x100
+ VER_COMMAND_FLUSHATTRS = 0x100,
+ VER_COMMAND_SEGMENT = 0x100,
+ VER_COMMAND_DOCINFO = 0x100,
};
@@ -7184,6 +7187,107 @@ void SendSearchResponse ( SearchHandler_
}
+void HandleCommandDocinfo ( int iSock, int iVer, InputBuffer_c & tReq )
+{
+ if ( !CheckCommandVersion ( iVer, VER_COMMAND_DOCINFO, tReq ) )
+ return;
+
+ SphDocID_t uDocID = tReq.GetUint64 ();
+ CSphString sIndex = tReq.GetString ();
+
+ const ServedIndex_t * pIndex = g_pIndexes->GetRlockedEntry ( sIndex );
+ if ( !pIndex )
+ {
+ tReq.SendErrorReply ( "unknown local index '%s' in search request", sIndex.cstr() );
+ return;
+ }
+
+ CSphQueryResult *pResult = new CSphQueryResult();
+
+ int iRespLen = 4;
+ NetOutputBuffer_c tOut ( iSock );
+ tOut.SendWord ( SEARCHD_OK );
+ tOut.SendWord ( VER_COMMAND_DOCINFO );
+
+ if (!pIndex->m_pIndex->GetDocinfo(pResult, uDocID))
+ {
+ tOut.SendInt ( iRespLen);
+ tOut.SendInt ( 0 );
+ tOut.Flush ();
+ pIndex->Unlock();
+ return;
+ }
+
+ const CSphMatch & tMatch = pResult->m_dMatches [0];
+ const BYTE * pStrings = pResult->m_pStrings;
+ const int tCount = pResult->m_tSchema.GetAttrsCount();
+
+ iRespLen += tCount * 12;
+
+ for ( int j = 0; j < tCount ; j++ )
+ {
+ const CSphColumnInfo & tCol = pResult->m_tSchema.GetAttr(j);
+ DWORD uOffset = (DWORD) tMatch.GetAttr(tCol.m_tLocator );
+ iRespLen += strlen ( tCol.m_sName.cstr());
+
+ if ( tCol.m_eAttrType == SPH_ATTR_UINT32SET ) {
+ const DWORD * pValues = tMatch.GetAttrMVA ( tCol.m_tLocator, pResult->m_pMva );
+ if (pValues ){
+ iRespLen += pValues[0] * 4;
+ }
+ } else if ( tCol.m_eAttrType==SPH_ATTR_STRING ) {
+ if ( uOffset ) {
+ iRespLen += sphUnpackStr ( pStrings + uOffset, NULL );
+ }
+ }
+ }
+
+ tOut.SendInt ( iRespLen );
+ tOut.SendInt ( tCount );
+
+ for ( int i= 0; i< tCount; i++ ){
+ const CSphColumnInfo & tCol = pResult->m_tSchema.GetAttr(i);
+ tOut.SendString ( tCol.m_sName.cstr() );
+ tOut.SendDword ( tCol.m_eAttrType );
+ }
+
+ for ( int j = 0; j < tCount; j++ )
+ {
+ const CSphColumnInfo & tCol = pResult->m_tSchema.GetAttr(j);
+ DWORD uOffset = (DWORD) tMatch.GetAttr(tCol.m_tLocator );
+ if ( tCol.m_eAttrType == SPH_ATTR_UINT32SET ) {
+ const DWORD * pValues = tMatch.GetAttrMVA ( tCol.m_tLocator, pResult->m_pMva );
+ if ( !pValues ){
+ tOut.SendString ( 0 );
+ } else {
+ int iValues = *pValues++;
+ tOut.SendDword ( iValues );
+ while ( iValues-- ){
+ tOut.SendDword ( *pValues++ );
+ }
+ }
+ } else if ( tCol.m_eAttrType==SPH_ATTR_STRING ) {
+ if ( !uOffset ) {
+ tOut.SendDword ( 0 ); // null string
+ } else{
+ const BYTE * pStr;
+ int iLen = sphUnpackStr (pResult->m_pStrings + uOffset, &pStr );
+ tOut.SendDword ( iLen );
+ tOut.SendBytes ( pStr, iLen );
+ }
+ } else {
+ if ( tCol.m_eAttrType==SPH_ATTR_FLOAT )
+ tOut.SendFloat (uOffset );
+ else if (tCol.m_eAttrType==SPH_ATTR_BIGINT )
+ tOut.SendUint64 ( uOffset );
+ else
+ tOut.SendDword ( uOffset );
+ }
+ }
+ pIndex->Unlock();
+ tOut.Flush ();
+}
+
void HandleCommandSearch ( int iSock, int iVer, InputBuffer_c & tReq )
{
MEMORY ( SPH_MEM_SEARCH_NONSQL );
@@ -9267,6 +9371,7 @@ void HandleClientSphinx ( int iSock, con
switch ( iCommand )
{
case SEARCHD_COMMAND_SEARCH: HandleCommandSearch ( iSock, iCommandVer, tBuf ); break;
+ case SEARCHD_COMMAND_DOCINFO: HandleCommandDocinfo ( iSock, iCommandVer, tBuf ); break;
case SEARCHD_COMMAND_EXCERPT: HandleCommandExcerpt ( iSock, iCommandVer, tBuf ); break;
case SEARCHD_COMMAND_KEYWORDS: HandleCommandKeywords ( iSock, iCommandVer, tBuf ); break;
case SEARCHD_COMMAND_UPDATE: HandleCommandUpdate ( iSock, iCommandVer, tBuf, iPipeFD ); break;
diff -rup sphinx-2.0.1-beta/src/sphinx.cpp 2.0.1/src/sphinx.cpp
--- sphinx-2.0.1-beta/src/sphinx.cpp 2011-04-19 16:39:24.000000000 +0800
+++ 2.0.1/src/sphinx.cpp 2011-09-01 13:07:53.000000000 +0800
@@ -1354,6 +1354,7 @@ public:
virtual SphAttr_t * GetKillList () const;
virtual int GetKillListSize () const { return m_iKillListSize; }
virtual bool HasDocid ( SphDocID_t uDocid ) const;
+ virtual bool GetDocinfo (CSphQueryResult *pResult ,SphDocID_t uDocid) const;
virtual const CSphSourceStats & GetStats () const { return m_tStats; }
@@ -11792,6 +11793,27 @@ bool CSphIndex_VLN::HasDocid ( SphDocID_
return FindDocinfo ( uDocid )!=NULL;
}
+bool CSphIndex_VLN::GetDocinfo (CSphQueryResult *pResult, SphDocID_t uDocid ) const
+{
+ const DWORD * pFound = FindDocinfo ( uDocid );
+
+ if ( !pFound )
+ return false;
+
+ CSphSwapVector<CSphMatch> m_dMatches;
+
+ m_dMatches.Add();
+ m_dMatches[0].m_iDocID = uDocid;
+ m_dMatches[0].m_pStatic = DOCINFO2ATTRS(pFound);
+
+ pResult->m_pStrings = m_pStrings.GetWritePtr();
+ pResult->m_pMva = m_pMva.GetWritePtr();
+ pResult->m_tSchema = m_tSchema;
+ pResult->m_dMatches = m_dMatches;
+ pResult->m_iCount = 1;
+
+ return true;
+}
const DWORD * CSphIndex_VLN::FindDocinfo ( SphDocID_t uDocID ) const
{
diff -rup sphinx-2.0.1-beta/src/sphinx.h 2.0.1/src/sphinx.h
--- sphinx-2.0.1-beta/src/sphinx.h 2011-04-22 10:40:59.000000000 +0800
+++ 2.0.1/src/sphinx.h 2011-09-01 11:44:10.000000000 +0800
@@ -2605,6 +2605,7 @@ public:
virtual int GetKillListSize () const = 0;
virtual bool HasDocid ( SphDocID_t uDocid ) const = 0;
virtual bool IsRT() const { return false; }
+ virtual bool GetDocinfo ( CSphQueryResult *pResult, SphDocID_t uDocid ) const = 0;
public:
/// build index by indexing given sources
diff -rup sphinx-2.0.1-beta/src/sphinxrt.cpp 2.0.1/src/sphinxrt.cpp
--- sphinx-2.0.1-beta/src/sphinxrt.cpp 2011-04-22 02:16:02.000000000 +0800
+++ 2.0.1/src/sphinxrt.cpp 2011-09-01 12:10:59.000000000 +0800
@@ -925,6 +925,7 @@ public:
virtual SphAttr_t * GetKillList () const { return NULL; }
virtual int GetKillListSize () const { return 0; }
virtual bool HasDocid ( SphDocID_t ) const { assert ( 0 ); return false; }
+ virtual bool GetDocinfo (CSphQueryResult *pResult ,SphDocID_t uDocid) const { assert ( 0 ); return false; }
virtual int Build ( const CSphVector<CSphSource*> & dSources, int iMemoryLimit, int iWriteBuffer ) { return 0; }
virtual bool Merge ( CSphIndex * pSource, CSphVector<CSphFilterSettings> & dFilters, bool bMergeKillLists ) { return false; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment