searcher/tsrc/robustnesstest/src/csearchingworker.cpp
changeset 0 671dee74050a
child 14 8bd192d47aaa
equal deleted inserted replaced
-1:000000000000 0:671dee74050a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CSearchingWorker.h"
       
    19 
       
    20 #include "CSearchDocument.h"
       
    21 #include "CCPixSearcher.h"
       
    22 
       
    23 #include <e32math.h>
       
    24 
       
    25 CSearchingWorker::CSearchingWorker( TInt aSleep, TBool aCancelSearch, TBool aCancelDocumentFetching ) 
       
    26 :   iSearches( 0 ),
       
    27     iTermSearches( 0 ),
       
    28     iSearchesMicroSeconds( 0 ),
       
    29     iTermSearchesMicroSeconds(),
       
    30     iSearchesPeakMicroSeconds( 0 ),
       
    31     iTermSearchesPeakMicroSeconds(),
       
    32     
       
    33     iDocs( 0 ), 
       
    34     iDocsMicroSeconds( 0 ), 
       
    35     iDocsPeakMicroSeconds( 0 ),
       
    36     
       
    37     iTerms( 0 ),
       
    38     iTermsMicroSeconds( 0 ), 
       
    39     iTermsPeakMicroSeconds( 0 ),
       
    40     
       
    41     iStop( EFalse ),
       
    42     iSleep( aSleep ),
       
    43     iCancelSearch( aCancelSearch ),
       
    44     iCancelDocumentFetching( aCancelDocumentFetching ),
       
    45     iAppClass( 0 ), 
       
    46 	iName( 0 )
       
    47 	{
       
    48 	}
       
    49 
       
    50 CSearchingWorker* CSearchingWorker::NewL( 
       
    51 		const TDesC& aAppClass,
       
    52 		TInt aSleep,
       
    53 		TBool aCancelSearch,
       
    54 		TBool aCancelDocumentFetching ) 
       
    55 	{
       
    56 	CSearchingWorker* self = new ( ELeave ) CSearchingWorker( aSleep, aCancelSearch, aCancelDocumentFetching ); 
       
    57 	CleanupStack::PushL( self ); 
       
    58 	self->ConstructL( aAppClass ); 
       
    59 	CleanupStack::Pop( self ); 
       
    60 	return self; 
       
    61 	}
       
    62 
       
    63 
       
    64 void CSearchingWorker::ConstructL( const TDesC& aAppClass ) 
       
    65 	{	
       
    66 	CWorker::ConstructL(); 
       
    67 	
       
    68 	iAppClass = aAppClass.AllocL(); 
       
    69 
       
    70 	iName = HBufC::NewL( 512 );
       
    71 	iName->Des().Append( _L( "searcher") ); 
       
    72 	iName->Des().AppendNum( WorkerId() ); 
       
    73 	iName->Des().Append( _L( " ") ); 
       
    74 	iName->Des().Append( aAppClass );
       
    75 	// Replace problematic characters
       
    76 	for ( TInt i = 0; i < iName->Length(); i++ ) 			
       
    77 		{
       
    78 		if ( (*iName)[i] == '@' || (*iName)[i] == ':' )
       
    79 			{
       
    80 			iName->Des()[i] = ' ';
       
    81 			}
       
    82 		}
       
    83 	}
       
    84 
       
    85 CSearchingWorker::~CSearchingWorker() 
       
    86 	{
       
    87 	delete iName;
       
    88 	delete iAppClass; 
       
    89 	}
       
    90 
       
    91 void CSearchingWorker::DoPrepareL() 
       
    92 	{
       
    93 	iStop = EFalse; 
       
    94 	}
       
    95 
       
    96 void CSearchingWorker::DoCancel() 
       
    97 	{
       
    98 	iStop = ETrue; 
       
    99 	}
       
   100 
       
   101 void CSearchingWorker::DoRunL()
       
   102 	{
       
   103 	RSearchServerSession session;
       
   104 	User::LeaveIfError(session.Connect());
       
   105 	CleanupClosePushL(session);
       
   106 	CCPixSearcher* searcher = CCPixSearcher::NewLC(session,
       
   107 			KContentsField);
       
   108 
       
   109 	searcher->OpenDatabaseL(*iAppClass);
       
   110 
       
   111 	while (!iStop)
       
   112 		{
       
   113 		TBuf<128> query;
       
   114 		// Pick randomly either normal search or term search
       
   115 		TInt op = Math::Random() % 2;
       
   116 		switch (op)
       
   117 			{
       
   118 			case 0:
       
   119 				// Normal query
       
   120 				query.AppendNum(LogRand(KTermDeviation));
       
   121 				iSearches++;
       
   122 				break;
       
   123 			case 1:
       
   124 				// Terms query
       
   125 				query.Append(_L("$terms<10>("));
       
   126 				query.AppendNum(LogRand(KPrefixDeviation));
       
   127 				query.Append(_L("*)"));
       
   128 				iTermSearches++;
       
   129 				break;
       
   130 			}
       
   131 
       
   132 		TTime before;
       
   133 		before.UniversalTime();
       
   134 
       
   135 		TInt n = 0;
       
   136 		if ( !iCancelSearch )
       
   137 			{
       
   138 			n = searcher->SearchL( query );
       
   139 			}
       
   140 		else
       
   141 			{
       
   142 			searcher->Cancel();
       
   143 			searcher->SearchL( *this, query);
       
   144 			}
       
   145 			
       
   146 		TTime after;
       
   147 		after.UniversalTime();
       
   148 		TInt64 time = after.MicroSecondsFrom(before).Int64();
       
   149 
       
   150 		switch (op)
       
   151 			{
       
   152 			case 0:
       
   153 				iSearchesMicroSeconds
       
   154 						+= after.MicroSecondsFrom(before).Int64();
       
   155 				iSearchesPeakMicroSeconds 
       
   156 						= Max( iSearchesPeakMicroSeconds, 
       
   157 							   time );  
       
   158 				break;
       
   159 			case 1:
       
   160 				iTermSearchesMicroSeconds += after.MicroSecondsFrom(
       
   161 						before).Int64();
       
   162 				iTermSearchesPeakMicroSeconds 
       
   163 						= Max( iTermSearchesPeakMicroSeconds, 
       
   164 								time );  
       
   165 				break;
       
   166 			}
       
   167 
       
   168 		if ( !iCancelSearch )
       
   169 			{
       
   170 			for ( TInt i = 0; i < n; i++ )
       
   171 				{
       
   172 				// Randomly reset GetDocumentL
       
   173 				if ( iCancelDocumentFetching && Math::Random() % 2 )
       
   174 					{
       
   175 					TRAP_IGNORE( searcher->GetDocumentL( i++, *this ) ); 
       
   176 					searcher->Cancel();
       
   177 					}
       
   178 				else
       
   179 					{
       
   180 					TTime before; 
       
   181 					before.UniversalTime();
       
   182 					
       
   183 					TRAP_IGNORE( delete searcher->GetDocumentL( i++ ); );
       
   184 					
       
   185 					TTime after;
       
   186 					after.UniversalTime();
       
   187 					
       
   188 					TInt64 time = after.MicroSecondsFrom(before).Int64();
       
   189 					switch (op) 
       
   190 						{
       
   191 						case 0:
       
   192 							iDocs ++; 
       
   193 							iDocsMicroSeconds += time; 
       
   194 							iDocsPeakMicroSeconds = Max(iDocsPeakMicroSeconds, time);
       
   195 							break;
       
   196 						case 1:
       
   197 							iTerms ++; 
       
   198 							iTermsMicroSeconds += time; 
       
   199 							iTermsPeakMicroSeconds = Max(iDocsPeakMicroSeconds, time);
       
   200 							break;
       
   201 						}
       
   202 					
       
   203 					}
       
   204 				}
       
   205 			}
       
   206 
       
   207 		Unlock();
       
   208 		SleepRandom(iSleep);
       
   209 		Lock();
       
   210 
       
   211 		if ( iCancelSearch && Math::Random()%2 )
       
   212 			{
       
   213 			searcher->Cancel();
       
   214 			}
       
   215 		}
       
   216 	CleanupStack::PopAndDestroy(searcher); // session
       
   217 	CleanupStack::PopAndDestroy(); // session
       
   218 	}
       
   219 	
       
   220 const TDesC& CSearchingWorker::Name() 
       
   221 	{
       
   222 	return *iName; 
       
   223 	}
       
   224 
       
   225 void CSearchingWorker::HandleSearchResultsL(TInt /*aError*/, TInt /*aEstimatedResultCount*/)
       
   226 	{
       
   227 	// TODO: Fetch documents and randomly cancel
       
   228 	}
       
   229 
       
   230 void CSearchingWorker::HandleDocumentL(TInt /*aError*/, CSearchDocument* /*aDocument*/)
       
   231 	{
       
   232 	}
       
   233 
       
   234 TInt CSearchingWorker::ConsumeSearches() 
       
   235 	{
       
   236 	TInt ret = iSearches; 
       
   237 	iSearches = 0; 
       
   238 	return ret; 
       
   239 	}
       
   240 
       
   241 TInt CSearchingWorker::ConsumeSearchesPeakMicroSeconds() 
       
   242 	{
       
   243 	TInt ret = iSearchesPeakMicroSeconds; 
       
   244 	iSearchesPeakMicroSeconds = 0; 
       
   245 	return ret; 
       
   246 	}
       
   247 
       
   248 TInt CSearchingWorker::ConsumeSearchesMicroSeconds() 
       
   249 	{
       
   250 	TInt ret = iSearchesMicroSeconds; 
       
   251 	iSearchesMicroSeconds = 0; 
       
   252 	return ret; 
       
   253 	}
       
   254 
       
   255 TInt CSearchingWorker::ConsumeTermSearches() 
       
   256 	{
       
   257 	TInt ret = iTermSearches; 
       
   258 	iTermSearches = 0; 
       
   259 	return ret; 
       
   260 	}
       
   261 
       
   262 TInt CSearchingWorker::ConsumeTermSearchesMicroSeconds() 
       
   263 	{
       
   264 	TInt ret = iTermSearchesMicroSeconds; 
       
   265 	iTermSearchesMicroSeconds = 0; 
       
   266 	return ret; 
       
   267 	}
       
   268 
       
   269 TInt CSearchingWorker::ConsumeTermSearchesPeakMicroSeconds() 
       
   270 	{
       
   271 	TInt ret = iTermSearchesPeakMicroSeconds; 
       
   272 	iTermSearchesPeakMicroSeconds = 0; 
       
   273 	return ret; 
       
   274 	}
       
   275 
       
   276 TInt CSearchingWorker::ConsumeDocs() 
       
   277 	{
       
   278 	TInt ret = iDocs; 
       
   279 	iDocs = 0; 
       
   280 	return ret; 
       
   281 	}
       
   282 
       
   283 TInt CSearchingWorker::ConsumeDocsMicroSeconds() 
       
   284 	{
       
   285 	TInt ret = iDocsMicroSeconds; 
       
   286 	iDocsMicroSeconds = 0; 
       
   287 	return ret; 
       
   288 	}
       
   289 
       
   290 TInt CSearchingWorker::ConsumeDocsPeakMicroSeconds() 
       
   291 	{
       
   292 	TInt ret = iDocsPeakMicroSeconds; 
       
   293 	iDocsPeakMicroSeconds = 0; 
       
   294 	return ret; 
       
   295 	}
       
   296 
       
   297 TInt CSearchingWorker::ConsumeTerms() 
       
   298 	{
       
   299 	TInt ret = iTerms; 
       
   300 	iTerms = 0; 
       
   301 	return ret; 
       
   302 	}
       
   303 
       
   304 TInt CSearchingWorker::ConsumeTermsMicroSeconds() 
       
   305 	{
       
   306 	TInt ret = iTermsMicroSeconds; 
       
   307 	iTermsMicroSeconds = 0; 
       
   308 	return ret; 
       
   309 	}
       
   310 
       
   311 TInt CSearchingWorker::ConsumeTermsPeakMicroSeconds() 
       
   312 	{
       
   313 	TInt ret = iTermsPeakMicroSeconds; 
       
   314 	iTermsPeakMicroSeconds = 0; 
       
   315 	return ret; 
       
   316 	}