Пример #1
0
CFileList* CLibraryMaps::WhatsNew(const CQuerySearch* pSearch, int nMaximum) const
{
	ASSUME_LOCK( Library.m_pSection );

	const DWORD tNow = static_cast< DWORD >( time( NULL ) );
	CFileList* pHits = NULL;

	for ( POSITION pos = GetFileIterator() ; pos ; )
	{
		CLibraryFile* pFile = GetNextFile( pos );

		if ( pFile->IsAvailable() && pFile->IsShared() && pFile->m_oSHA1 &&
			( ! pSearch->m_pSchema || pSearch->m_pSchema->Equals( pFile->m_pSchema ) ) )
		{
			const DWORD nTime = pFile->GetCreationTime();
			if ( nTime && nTime + 12 * 60 * 60 > tNow )		// 12 hours
			{
				pFile->m_nHitsToday++;
				pFile->m_nHitsTotal++;

				if ( ! pHits )
					pHits = new CFileList;
				pHits->AddTail( pFile );
				if ( nMaximum && pHits->GetCount() >= nMaximum )
					break;
			}
		}
	}

	return pHits;
}
Пример #2
0
bool CLocalSearch::ExecuteSharedFiles(INT_PTR nMaximum, INT_PTR& nHits)
{
	CSingleLock oLock( &Library.m_pSection );
	if ( ! oLock.Lock( 250 ) )
		return false;

	auto_ptr< CFileList > pFiles( Library.Search(
		m_pSearch, nMaximum, FALSE,
		// Ghost files only for G2
		m_nProtocol != PROTOCOL_G2 ) );

	if ( pFiles.get() )
	{
		CFileList oFilesInPacket;

		for ( POSITION pos = pFiles->GetHeadPosition() ;
			pos && ( ! nMaximum || ( nHits + oFilesInPacket.GetCount() < nMaximum ) ); )
		{
			CLibraryFile* pFile = pFiles->GetNext( pos );

			if ( IsValidForHit( pFile ) )
			{
				oFilesInPacket.AddTail( pFile );
			}
		}

		SendHits( oFilesInPacket );

		nHits += oFilesInPacket.GetCount();
	}

	// Is it a browser request?
	if ( ! m_pSearch && m_nProtocol == PROTOCOL_G2 )
	{
		// Send virtual tree		
		DispatchPacket( AlbumToPacket( Library.GetAlbumRoot() ) );

		// Send physical tree
		DispatchPacket( FoldersToPacket() );
	}

	return true;
}
Пример #3
0
CFileList* CLibraryMaps::Browse(int nMaximum) const
{
	ASSUME_LOCK( Library.m_pSection );

	CFileList* pHits = NULL;

	for ( POSITION pos = GetFileIterator() ; pos ; )
	{
		CLibraryFile* pFile = GetNextFile( pos );

		if ( pFile->IsAvailable() && pFile->IsShared() && pFile->m_oSHA1 )
		{
			if ( ! pHits )
				pHits = new CFileList;
			pHits->AddTail( pFile );
			if ( nMaximum && pHits->GetCount() >= nMaximum )
				break;
		}
	}

	return pHits;
}
Пример #4
0
CFileList* CLibraryMaps::LookupFilesByHash(const CPeerProjectFile* pFilter, BOOL bSharedOnly, BOOL bAvailableOnly, int nMaximum) const
{
	CQuickLock oLock( Library.m_pSection );

	CFileList* pFiles = NULL;

	if ( pFilter->m_oSHA1 )
	{
		for ( CLibraryFile* pFile = m_pSHA1Map[ pFilter->m_oSHA1[ 0 ] & HASH_MASK ] ;
			pFile ; pFile = pFile->m_pNextSHA1 )
		{
			if ( validAndEqual( pFile->m_oSHA1, pFilter->m_oSHA1 ) &&
				 *pFile == *pFilter &&
				 pFile->CheckFileAttributes( pFilter->m_nSize, bSharedOnly, bAvailableOnly ) )
			{
				if ( ! pFiles )
					pFiles = new CFileList;
				if ( pFiles->Find( pFile ) == NULL )
				{
					if ( bSharedOnly )
					{
						pFile->m_nHitsToday++;
						pFile->m_nHitsTotal++;
					}
					pFiles->AddTail( pFile );
					if ( nMaximum && pFiles->GetCount() >= nMaximum )
						break;
				}
			}
		}

		return pFiles;
	}

	if ( pFilter->m_oED2K )
	{
		for ( CLibraryFile* pFile = m_pED2KMap[ pFilter->m_oED2K[ 0 ] & HASH_MASK ] ;
			pFile ; pFile = pFile->m_pNextED2K )
		{
			if ( validAndEqual( pFile->m_oED2K, pFilter->m_oED2K ) &&
				 *pFile == *pFilter &&
				 pFile->CheckFileAttributes( pFilter->m_nSize, bSharedOnly, bAvailableOnly ) )
			{
				if ( ! pFiles )
					pFiles = new CFileList;
				if ( pFiles->Find( pFile ) == NULL )
				{
					if ( bSharedOnly )
					{
						pFile->m_nHitsToday++;
						pFile->m_nHitsTotal++;
					}
					pFiles->AddTail( pFile );
					if ( nMaximum && pFiles->GetCount() >= nMaximum )
						return pFiles;
				}
			}
		}

		return pFiles;
	}

	if ( pFilter->m_oTiger )
	{
		for ( CLibraryFile* pFile = m_pTigerMap[ pFilter->m_oTiger[ 0 ] & HASH_MASK ] ;
			pFile ; pFile = pFile->m_pNextTiger )
		{
			if ( validAndEqual( pFile->m_oTiger, pFilter->m_oTiger ) &&
				 *pFile == *pFilter &&
				 pFile->CheckFileAttributes( pFilter->m_nSize, bSharedOnly, bAvailableOnly ) )
			{
				if ( ! pFiles )
					pFiles = new CFileList;
				if ( pFiles->Find( pFile ) == NULL )
				{
					if ( bSharedOnly )
					{
						pFile->m_nHitsToday++;
						pFile->m_nHitsTotal++;
					}
					pFiles->AddTail( pFile );
					if ( nMaximum && pFiles->GetCount() >= nMaximum )
						break;
				}
			}
		}

		return pFiles;
	}

	if ( pFilter->m_oBTH )
	{
		for ( CLibraryFile* pFile = m_pBTHMap[ pFilter->m_oBTH[ 0 ] & HASH_MASK ] ;
			pFile ; pFile = pFile->m_pNextBTH )
		{
			if ( validAndEqual( pFile->m_oBTH, pFilter->m_oBTH ) &&
				 *pFile == *pFilter &&
				 pFile->CheckFileAttributes( pFilter->m_nSize, bSharedOnly, bAvailableOnly ) )
			{
				if ( ! pFiles )
					pFiles = new CFileList;
				if ( pFiles->Find( pFile ) == NULL )
				{
					if ( bSharedOnly )
					{
						pFile->m_nHitsToday++;
						pFile->m_nHitsTotal++;
					}
					pFiles->AddTail( pFile );
					if ( nMaximum && pFiles->GetCount() >= nMaximum )
						return pFiles;
				}
			}
		}
		return pFiles;
	}

	if ( pFilter->m_oMD5 )
	{
		// Since MD5 is not commonly used for searches we use it for the duplicate file search
		// which requires getting a list of files not to return only 1 file. See CLibrary::CheckDuplicates

		for ( CLibraryFile* pFile = m_pMD5Map[ pFilter->m_oMD5[ 0 ] & HASH_MASK ] ;
			pFile ; pFile = pFile->m_pNextMD5 )
		{
			if ( validAndEqual( pFile->m_oMD5, pFilter->m_oMD5 ) &&
				 *pFile == *pFilter &&
				 pFile->CheckFileAttributes( pFilter->m_nSize, bSharedOnly, bAvailableOnly ) )
			{
				if ( ! pFiles )
					pFiles = new CFileList;
				if ( pFiles->Find( pFile ) == NULL )
				{
					if ( bSharedOnly )
					{
						pFile->m_nHitsToday++;
						pFile->m_nHitsTotal++;
					}
					pFiles->AddTail( pFile );
					if ( nMaximum && pFiles->GetCount() >= nMaximum )
						break;
				}
			}
		}

		return pFiles;
	}

	if ( ! pFilter->m_sName.IsEmpty() &&
		pFilter->m_nSize != SIZE_UNKNOWN && pFilter->m_nSize != 0 )
	{
		if ( CLibraryFile* pFile = LibraryMaps.LookupFileByName( pFilter->m_sName, pFilter->m_nSize, bSharedOnly, bAvailableOnly ) )
		{
			if ( ! pFiles )
				pFiles = new CFileList;
			if ( pFiles->Find( pFile ) == NULL )
			{
				if ( bSharedOnly )
				{
					pFile->m_nHitsToday++;
					pFile->m_nHitsTotal++;
				}
				pFiles->AddTail( pFile );
			}
		}

		return pFiles;
	}

	return pFiles;	// Null
}
Пример #5
0
CFileList* CLibraryDictionary::Search(const CQuerySearch* pSearch, const int nMaximum, const bool bLocal,	const bool bAvailableOnly)
{
	ASSUME_LOCK( Library.m_pSection );

	if ( ! m_bValid )
	{
		BuildHashTable();
		if ( ! m_bValid )
			return NULL;
	}

	// Only check the hash when a search comes from other client.
	if ( ! bLocal && ! m_pTable->Check( pSearch ) )
		return NULL;

	++m_nSearchCookie;
	CLibraryFile* pHit = NULL;

	CQuerySearch::const_iterator pWordEntry = pSearch->begin();
	const CQuerySearch::const_iterator pLastWordEntry = pSearch->end();
	for ( ; pWordEntry != pLastWordEntry; ++pWordEntry )
	{
		if ( pWordEntry->first[ 0 ] == L'-' )
			continue;

		CString strWord( pWordEntry->first, static_cast< int >( pWordEntry->second ) );
		CFileList* pList = NULL;
		if ( m_oWordMap.Lookup( strWord, pList ) )
		{
			for ( POSITION pos = pList->GetHeadPosition(); pos; )
			{
				CLibraryFile* pFile = pList->GetNext( pos );

				if ( bAvailableOnly && ! pFile->IsAvailable() )
					continue;

				if ( ! bLocal && ! pFile->IsShared() )
					continue;

				if ( pFile->m_nSearchCookie == m_nSearchCookie )
				{
					++pFile->m_nSearchWords;
				}
				else
				{
					pFile->m_nSearchCookie	= m_nSearchCookie;
					pFile->m_nSearchWords	= 1;
					pFile->m_pNextHit		= pHit;
					pHit = pFile;
				}
			}
		}
	}

	size_t nLowerBound = ( pSearch->tableSize() >= 3 ) ?
		( pSearch->tableSize() * 2 / 3 ) : pSearch->tableSize();

	CFileList* pHits = NULL;
	for ( ; pHit; pHit = pHit->m_pNextHit )
	{
		ASSERT( pHit->m_nSearchCookie == m_nSearchCookie );

		if ( pHit->m_nSearchWords < nLowerBound )
			continue;

		if ( pSearch->Match( pHit->GetSearchName(),
			pHit->m_pSchema ? (LPCTSTR)pHit->m_pSchema->GetURI() : NULL,
			pHit->m_pMetadata, pHit ) )
		{
			if ( ! pHits )
				pHits = new CFileList;

			pHits->AddTail( pHit );

			if ( ! bLocal )
			{
				pHit->m_nHitsToday ++;
				pHit->m_nHitsTotal ++;
			}

			if ( pHit->m_nCollIndex )
			{
				CLibraryFile* pCollection = LibraryMaps.LookupFile( pHit->m_nCollIndex, ! bLocal, bAvailableOnly );

				if ( pCollection )
				{
					if ( pCollection->m_nSearchCookie != m_nSearchCookie )
					{
						pCollection->m_nSearchCookie = m_nSearchCookie;
						pHits->AddHead( pCollection );
					}
				}
				else
				{
					// Collection removed without deleting indexes
					pHit->m_nCollIndex = 0ul;
				}
			}

			if ( nMaximum > 0 && pHits->GetCount() >= nMaximum )
				break;
		}
	}

	return pHits;
}