Browse Source

Add kill hook

When set, it will collect killed documents ID.
Alexey N. Vinogradov 4 years ago
parent
commit
d94e737708
4 changed files with 28 additions and 10 deletions
  1. 2 2
      src/indextool.cpp
  2. 6 3
      src/killlist.h
  3. 10 2
      src/sphinx.cpp
  4. 10 3
      src/sphinx.h

+ 2 - 2
src/indextool.cpp

@@ -866,7 +866,7 @@ static void ApplyKilllist ( IndexInfo_t & tTarget, const IndexInfo_t & tKiller,
 		LookupReaderIterator_c tTargetReader ( tTarget.m_tLookup.GetWritePtr() );
 		LookupReaderIterator_c tKillerReader ( tKiller.m_tLookup.GetWritePtr() );
 
-		KillByLookup ( tTargetReader, tKillerReader, tTarget.m_tDeadRowMap );
+		KillByLookup ( tTargetReader, tKillerReader, tTarget.m_tDeadRowMap, [] ( DocID_t ) {} );
 	}
 
 	if ( tSettings.m_uFlags & KillListTarget_t::USE_KLIST )
@@ -874,7 +874,7 @@ static void ApplyKilllist ( IndexInfo_t & tTarget, const IndexInfo_t & tKiller,
 		LookupReaderIterator_c tTargetReader ( tTarget.m_tLookup.GetWritePtr() );
 		DocidListReader_c tKillerReader ( tKiller.m_dKilllist );
 
-		KillByLookup ( tTargetReader, tKillerReader, tTarget.m_tDeadRowMap );
+		KillByLookup ( tTargetReader, tKillerReader, tTarget.m_tDeadRowMap, [] ( DocID_t ) {} );
 	}
 }
 

+ 6 - 3
src/killlist.h

@@ -139,8 +139,8 @@ private:
 };
 
 
-template <typename TARGET, typename KILLER, typename MAP>
-int KillByLookup ( TARGET & tTargetReader, KILLER & tKillerReader, MAP & tDeadRowMap )
+template <typename TARGET, typename KILLER, typename MAP, typename FNHOOK>
+int KillByLookup ( TARGET & tTargetReader, KILLER & tKillerReader, MAP & tDeadRowMap, FNHOOK fnHook )
 {
 	RowID_t tTargetRowID = INVALID_ROWID;
 
@@ -165,7 +165,10 @@ int KillByLookup ( TARGET & tTargetReader, KILLER & tKillerReader, MAP & tDeadRo
 		else
 		{
 			if ( tDeadRowMap.Set ( tTargetRowID ) )
-				iKilled++;
+			{
+				fnHook ( tKillerDocID );
+				++iKilled;
+			}
 
 			bHaveKillerDocs = tKillerReader.ReadDocID ( tKillerDocID );
 			bHaveTargetDocs = tTargetReader.Read ( tTargetDocID, tTargetRowID );

+ 10 - 2
src/sphinx.cpp

@@ -7843,7 +7843,13 @@ int CSphIndex_VLN::KillMulti ( const VecTraits_T<DocID_t> & dKlist )
 	LookupReaderIterator_c tTargetReader ( m_tDocidLookup.GetWritePtr() );
 	DocidListReader_c tKillerReader ( dKlist );
 
-	int iTotalKilled = KillByLookup ( tTargetReader, tKillerReader, m_tDeadRowMap );
+	int iTotalKilled;
+	if ( !m_pKillHook )
+		iTotalKilled = KillByLookup ( tTargetReader, tKillerReader, m_tDeadRowMap, [] ( DocID_t ) {} );
+	else
+		iTotalKilled = KillByLookup ( tTargetReader, tKillerReader, m_tDeadRowMap,
+				[this] ( DocID_t tDoc ) { m_pKillHook->Kill ( tDoc ); } );
+
 	if ( iTotalKilled )
 		m_uAttrsStatus |= IndexUpdateHelper_c::ATTRS_ROWMAP_UPDATED;
 
@@ -11036,7 +11042,7 @@ std::pair<DWORD,DWORD> CSphIndex_VLN::CreateRowMapsAndCountTotalDocs ( const CSp
 		LookupReaderIterator_c tDstLookupReader ( pDstIndex->m_tDocidLookup.GetWritePtr() );
 		LookupReaderIterator_c tSrcLookupReader ( pSrcIndex->m_tDocidLookup.GetWritePtr() );
 
-		KillByLookup ( tDstLookupReader, tSrcLookupReader, tExtraDeadMap );
+		KillByLookup ( tDstLookupReader, tSrcLookupReader, tExtraDeadMap, [] (DocID_t) {} );
 	}
 
 	dSrcRowMap.Fill ( INVALID_ROWID );
@@ -11948,6 +11954,8 @@ int	CSphIndex_VLN::Kill ( DocID_t tDocID )
 	if ( m_tDeadRowMap.Set ( GetRowidByDocid ( tDocID ) ) )
 	{
 		m_uAttrsStatus |= IndexUpdateHelper_c::ATTRS_ROWMAP_UPDATED;
+		if ( m_pKillHook )
+			m_pKillHook->Kill ( tDocID );
 		return 1;
 	}
 

+ 10 - 3
src/sphinx.h

@@ -2696,10 +2696,18 @@ enum KeywordExpansion_e
 // an index or a part of an index that has its own row ids
 class IndexSegment_c
 {
+protected:
+	mutable IndexSegment_c * m_pKillHook = nullptr; // if set, killed docids will be emerged also here.
+
 public:
-	virtual			~IndexSegment_c() {}
 	virtual int		Kill ( DocID_t tDocID ) { return 0; }
-	virtual int		KillMulti ( const VecTraits_T<DocID_t> & dKlist ) { return 0; }
+	virtual int		KillMulti ( const VecTraits_T<DocID_t> & dKlist ) { return 0; };
+	virtual			~IndexSegment_c() {};
+
+	inline void SetKillHook ( IndexSegment_c * pKillHook ) const
+	{
+		m_pKillHook = pKillHook;
+	}
 };
 
 
@@ -2935,7 +2943,6 @@ public:
 	virtual bool				LoadKillList ( CSphFixedVector<DocID_t> * pKillList, KillListTargets_c & tTargets, CSphString & sError ) const { return true; }
 	virtual bool				AlterKillListTarget ( KillListTargets_c & tTargets, CSphString & sError ) { return false; }
 	virtual void				KillExistingDocids ( CSphIndex * pTarget ) {}
-	int							KillMulti ( const VecTraits_T<DocID_t> & dKlist ) override { return 0; }
 	virtual bool				IsAlive ( DocID_t tDocID ) const { return false; }
 
 	bool						GetDoc ( DocstoreDoc_t & tDoc, DocID_t tDocID, const VecTraits_T<int> * pFieldIds, int64_t iSessionId, bool bPack ) const override { return false; }