messagingfw/msgsrvnstore/server/src/msvsearchsortcachemanager.cpp
changeset 22 bde600d88860
child 35 f8ad95794a08
child 40 320ec5cd0227
equal deleted inserted replaced
21:08008ce8a6df 22:bde600d88860
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // MSVSEARCHSORTCACHEMANGER.CPP
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "msvinifile.h"
       
    19 #include "msvsearchsortcacheentry.h"
       
    20 #include "msvsearchsortcachemanager.h"
       
    21 #include "msventryfreepool.h"
       
    22 #include "MSVSERV.H"
       
    23 #include "msvsearchsortdbwrapper.h"
       
    24 #include "msvsearchsortdeltacache.h"
       
    25 #include "msvdbadapter.h"
       
    26 #include "msvindexadapter.h"
       
    27 
       
    28 const TInt KPercentageValue = 20 ; //in percentage
       
    29 const TInt KDefaultMaxCacheSize = 1024 ; //In Bytes.
       
    30 const TInt KSearchSortDeltaCacheLimit = 350 ; //Number of maximum Delta Cache Entry .
       
    31 
       
    32 _LIT(KAttachmentYes, "0");
       
    33 _LIT(KAttachmentNo, "1");
       
    34 _LIT(KAttachmentTypeEMsvFile, "2");
       
    35 _LIT(KAttachmentTypeEMsvLinkedFile, "3"); 
       
    36 _LIT(KAttachmentTypeEMsvMessageEntry, "4"); 
       
    37 
       
    38 
       
    39 CMSvSearchSortCacheManager* CMSvSearchSortCacheManager::iMsvSearchSortCacheManager =NULL;
       
    40 
       
    41 
       
    42  /**
       
    43   * CMsvCacheIndexTableEntry()
       
    44   * @param CMsvServer& .
       
    45   * @return None.
       
    46   * Single Ton Class.
       
    47   * Constructor
       
    48   */
       
    49  CMSvSearchSortCacheManager::CMSvSearchSortCacheManager(CMsvServer& aServer):iServer(aServer)
       
    50  	{
       
    51    	}
       
    52 
       
    53  /**
       
    54   * ~CMsvCacheIndexTableEntry()
       
    55   * @param None.
       
    56   * @return None.
       
    57   * Destructor
       
    58   */
       
    59  CMSvSearchSortCacheManager::~CMSvSearchSortCacheManager()
       
    60  	{
       
    61 	iOutstandingSOSOperations = 0;
       
    62 	iMsvIdWithSortFieldArray.Close();
       
    63 	iToFindResultAsIdArray.Close();
       
    64 	iFinalResultAsIdArray.Close();
       
    65 	iUpdateIdsToCacheArray.Close();
       
    66 	iDeltaCacheIdArray.Close();
       
    67 
       
    68 	delete iSearchSortDeltaCache;
       
    69 
       
    70 	if(iSearchDbAdapter)
       
    71 		{
       
    72 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)   	
       
    73    		iSearchDbAdapter->CloseResources();
       
    74 #endif
       
    75    		delete iSearchDbAdapter;
       
    76    		iSearchDbAdapter = NULL;
       
    77 		}
       
    78    	
       
    79    	if(iManagerEntry)
       
    80 	   	{
       
    81 	   	if(iManagerEntry->Count()>0)
       
    82 	   		{
       
    83 	   		iManagerEntry->ResetAndDestroy();
       
    84 	   		}
       
    85 	   	iManagerEntry->Close();	
       
    86 	   	}
       
    87   	delete iManagerEntry;
       
    88   	iMsvSearchSortCacheManager = NULL; 
       
    89   	}
       
    90 
       
    91  /**
       
    92  * Instance()
       
    93  *
       
    94  * The function returns already created instance of
       
    95  * this class to the caller. To create a new instance
       
    96  * the caller should call CreateL(). If an instance of
       
    97  * this object does not already exists, the function
       
    98  * throws a panic EMsvSearchSortCacheMangerNotCreated in DEBUG mode.
       
    99  *
       
   100  * @param CMsvServer& .
       
   101  * @return CMSvSearchSortCacheManager*
       
   102  *
       
   103  */
       
   104 CMSvSearchSortCacheManager* CMSvSearchSortCacheManager::Instance()
       
   105 	{
       
   106 	__ASSERT_DEBUG(iMsvSearchSortCacheManager!=NULL , PanicServer(EMsvSearchSortCacheMangerNotCreated));
       
   107 	return iMsvSearchSortCacheManager;
       
   108 	}
       
   109 
       
   110 
       
   111 /**
       
   112  * CreateManagerL()
       
   113  * 
       
   114  * The only way to create an object of this class.
       
   115  * This is a static interface.
       
   116  * The function is made private to ensure that only
       
   117  * friend function can create instance of this class.
       
   118  *
       
   119  * @param CMsvServer& .
       
   120  * @return CMSvSearchSortCacheManager*
       
   121  */
       
   122 CMSvSearchSortCacheManager* CMSvSearchSortCacheManager::CreateManagerL(CMsvServer& aServer)
       
   123 	{
       
   124 	CMSvSearchSortCacheManager* self ;
       
   125 	self = new (ELeave) CMSvSearchSortCacheManager(aServer);
       
   126 	CleanupStack::PushL(self);
       
   127 	self->ConstructL();
       
   128 	CleanupStack::Pop(self);
       
   129 	iMsvSearchSortCacheManager = self;
       
   130 	return self;
       
   131 	}
       
   132 
       
   133 
       
   134 /**
       
   135  * ConstructL()
       
   136  *
       
   137  * The function is called from InstanceL() and used to
       
   138  * initialize the instance of this class. It firsts reads
       
   139  * cache configuration information from msgcache.ini file
       
   140  * and stores them into member variable. It then allocates
       
   141  * creates initial set of entries as described in conf file.
       
   142  */
       
   143 void CMSvSearchSortCacheManager::ConstructL()
       
   144 	{
       
   145 	iProgress = KMsvSearchSortOpNone;
       
   146 	iCancelFlag = EFalse;
       
   147 	iExplicitSortOnDateTime = EFalse;
       
   148 
       
   149 	//For Simultaneously query
       
   150 	iOutstandingSOSOperations = 0;
       
   151 	
       
   152 	if(CMsvEntryFreePool::Instance()->iMsvMaximumCacheSize != NULL && CMsvEntryFreePool::Instance()->iMsvSearchSortCache)
       
   153 		{
       
   154 		iMsvMaximumSearchSortCacheSize = ((CMsvEntryFreePool::Instance()->iMsvMaximumCacheSize * CMsvEntryFreePool::Instance()->iMsvSearchSortCache)/100)*1024 ;		
       
   155 		}
       
   156 	else
       
   157 		{
       
   158 		iMsvMaximumSearchSortCacheSize = ((KDefaultMaxCacheSize * KPercentageValue)/100)*1024 ;
       
   159 		}
       
   160 	//Default Granularity
       
   161 	iManagerEntry = new(ELeave) RPointerArray<CMsvSearchSortCacheEntry>();
       
   162 	iSearchSortDeltaCache = CMsvSearchSortDeltaCache::CreateDeltaCacheL();
       
   163 	iSearchDbAdapter = NULL;
       
   164 	}
       
   165 
       
   166 
       
   167 void CMSvSearchSortCacheManager::InstantiateDBwrapperL()
       
   168 	{
       
   169 	//Leave with the errorstate if the Db closed
       
   170 	if(iServer.Context().IndexAdapter()->GetDbAdapter() == NULL)
       
   171 		{
       
   172 		if(iSearchDbAdapter)
       
   173 			{
       
   174 	#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)   	
       
   175    			iSearchDbAdapter->CloseResources();
       
   176 	#endif
       
   177 			delete iSearchDbAdapter;
       
   178 			iSearchDbAdapter = NULL;
       
   179 			}
       
   180 		User::Leave(iServer.Context().IndexAdapter()->ErrorState());
       
   181 		}
       
   182 	//If the searchsortwrapper is not instantiated, instantiate it
       
   183 	else if(iSearchDbAdapter == NULL)
       
   184 		{
       
   185 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
   186 		iSearchDbAdapter = TSearchSortDbWrapper::NewL(iServer);
       
   187 #else		
       
   188 		iSearchDbAdapter = TSearchSortDbWrapper::NewL(*(iServer.Context().IndexAdapter()->GetDbAdapter()));
       
   189 #endif		
       
   190 		}
       
   191 	//If the Db pointer has been changed since the last operation, reinstantiate it
       
   192 	//so get new iDbAdapter
       
   193 	else
       
   194 		{
       
   195 		if(iSearchDbAdapter->GetDbAdapter() != iServer.Context().IndexAdapter()->GetDbAdapter())
       
   196 			{
       
   197 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)   	
       
   198    			iSearchDbAdapter->CloseResources();
       
   199 #endif			
       
   200 			delete iSearchDbAdapter;
       
   201 			iSearchDbAdapter = NULL;
       
   202 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
   203 		iSearchDbAdapter = TSearchSortDbWrapper::NewL(iServer);
       
   204 #else		
       
   205 		iSearchDbAdapter = TSearchSortDbWrapper::NewL(*(iServer.Context().IndexAdapter()->GetDbAdapter()));
       
   206 #endif			
       
   207 			}
       
   208 		}
       
   209 	}
       
   210 /**
       
   211  * AddEntryL()
       
   212  * @param CMsvSearchSortCacheEntry*& :  apointer toa reference of CMsvSearchSortCacheEntry.
       
   213  * @return TInt
       
   214  * 				Return  -1 : Not Added into Manager.
       
   215  * 				Return 0 : Sussefuly added into manager.
       
   216  */
       
   217  TInt CMSvSearchSortCacheManager::AddEntryL(const CMsvSearchSortCacheEntry& aEntry)
       
   218 	{
       
   219 	if(QueryExists(aEntry)<0) //Query is not Exist
       
   220 		{
       
   221 	 	TInt iRequiredSize = sizeof(aEntry);
       
   222 	 	if(iRequiredSize > iMsvMaximumSearchSortCacheSize)
       
   223 	 		{
       
   224 	 		return KErrNoMemory;
       
   225 	 		}
       
   226 	 		
       
   227 	 	else
       
   228 		 	{
       
   229 		 	if(iMsvSearchSortCacheManager->iManagerEntry->Count() == 0)
       
   230 		 		{
       
   231 		 		if(IsInRange(aEntry))
       
   232 		 			{
       
   233 		 			User::LeaveIfError(iMsvSearchSortCacheManager->iManagerEntry->Append(&aEntry));
       
   234 		 			}
       
   235 		 		}
       
   236 		 	else
       
   237 		 		{
       
   238 		 		if(IsInRange(aEntry))
       
   239 		 			{
       
   240 	 				User::LeaveIfError(iMsvSearchSortCacheManager->iManagerEntry->Append(&aEntry));
       
   241 		 			}
       
   242 		 		else
       
   243 		 			{
       
   244 		 			TInt iRequiredSize = sizeof(aEntry);
       
   245 		 			if(RemoveEntry(iRequiredSize))
       
   246 		 				{
       
   247 		 				User::LeaveIfError(iMsvSearchSortCacheManager->iManagerEntry->Append(&aEntry));
       
   248 	 					}
       
   249 	 				}
       
   250 	 			}
       
   251 	   		}/* End of  if(iRequiredSize > iMsvMaximumSearchSortCacheSize)*/
       
   252 		}/* END of if(QueryExists(&aEntry)<0) //Query is not Exist */
       
   253 	else
       
   254 		{
       
   255 		//Query Already Exist
       
   256 		delete &aEntry;
       
   257 		}
       
   258 	return 0;
       
   259 	}
       
   260 
       
   261 
       
   262  /**
       
   263   * MoveEntryAtEnd()
       
   264   * @param CMsvSearchSortCacheEntry* :Pointer to a Cache entry need to add.
       
   265   * @param TInt : aIndex 
       
   266   * @return None
       
   267   */
       
   268 
       
   269 void CMSvSearchSortCacheManager::MoveEntryAtEnd(CMsvSearchSortCacheEntry* aEntry,TInt aIndex)
       
   270 	{
       
   271 	iMsvSearchSortCacheManager->iManagerEntry->Remove(aIndex);
       
   272 	iMsvSearchSortCacheManager->iManagerEntry->Append(aEntry);
       
   273 	}
       
   274  
       
   275  
       
   276  /**
       
   277   * QueryExists()
       
   278   * @param CMsvSearchSortCacheEntry* :Pointer to a Cache entry need to add.
       
   279   * @return TInt : index of manager class  if Query Exist , else 0 if query does not exists.
       
   280   */
       
   281 TInt CMSvSearchSortCacheManager::QueryExists(const CMsvSearchSortCacheEntry& aEntry)
       
   282 	{
       
   283 	TBool queryNotFound = EFalse;
       
   284 	TInt return_position  = -1;
       
   285 	if(iMsvSearchSortCacheManager->iManagerEntry->Count() > 0)
       
   286 		{
       
   287 		//Total Number of available Node in SearchSortCacheManager Class.
       
   288 		for(TInt IndexEntryCount = 0; IndexEntryCount <iMsvSearchSortCacheManager->iManagerEntry->Count(); IndexEntryCount++)
       
   289 			{
       
   290 			//Checking ParentID and ResultType
       
   291 			if((aEntry.iParentId == (*iManagerEntry)[IndexEntryCount]->iParentId) && (aEntry.iQueryLevel == (*iManagerEntry)[IndexEntryCount]->iQueryLevel))/*(aEntry.iResultType == (*iManagerEntry)[IndexEntryCount]->iResultType))*/
       
   292 				{
       
   293 				//Checking QueryType(Serach/sort) and Sort Option (Ascending/Desending)
       
   294 				if(aEntry.IsSortQuery()  == (*iManagerEntry)[IndexEntryCount]->IsSortQuery() && (aEntry.IsAscendingSort() == (*iManagerEntry)[IndexEntryCount]->IsAscendingSort()))
       
   295 					{
       
   296 					//Checking WholeWord and WildCharacter
       
   297 					 if(aEntry.IsWholeWord() == (*iManagerEntry)[IndexEntryCount]->IsWholeWord() && (aEntry.IsWildCharacter() == (*iManagerEntry)[IndexEntryCount]->IsWildCharacter()))
       
   298 						{
       
   299 						//Checking ExplicitSort 
       
   300 						 if(aEntry.IsExplicitSortReq() == (*iManagerEntry)[IndexEntryCount]->IsExplicitSortReq() && (aEntry.iMsgExplicitSortPart == (*iManagerEntry)[IndexEntryCount]->iMsgExplicitSortPart))
       
   301 							{
       
   302 							//Checking Explicit Sort part
       
   303 							if((*iManagerEntry)[IndexEntryCount]->iMsgExplicitSortPart == aEntry.iMsgExplicitSortPart /*EMsgDate*/)
       
   304 								{
       
   305 								// For query Level Search
       
   306 								for (TInt qCount = 0; qCount < aEntry.iQueryLevel; qCount++)
       
   307 									{
       
   308 									//Checking mesagepart and relational Op
       
   309 									if ((*iManagerEntry)[IndexEntryCount]->iMsgQuery[qCount].iMessagePart == aEntry.iMsgQuery[qCount].iMessagePart) 
       
   310 										{
       
   311 										//Checking QueryString
       
   312 										 if(((*iManagerEntry)[IndexEntryCount]->iMsgQuery[qCount].iQueryString.Compare(aEntry.iMsgQuery[qCount].iQueryString) == 0) && ((*iManagerEntry)[IndexEntryCount]->iMsgQuery[qCount].iRelationOp == aEntry.iMsgQuery[qCount].iRelationOp ))
       
   313 											{
       
   314 											queryNotFound = EFalse;
       
   315 											}
       
   316 										else
       
   317 											{
       
   318 											queryNotFound = ETrue;
       
   319 											}
       
   320 												
       
   321 										}
       
   322 									else
       
   323 										{
       
   324 										queryNotFound = ETrue;
       
   325 										}
       
   326 									}/* End of  for (TInt qCount = 0; qCount < aEntry.iQueryLevel; qCount++)  */
       
   327 									return_position = IndexEntryCount;
       
   328 								}
       
   329 														
       
   330 							}
       
   331 					
       
   332 						}
       
   333 					
       
   334 					}
       
   335 				
       
   336 				}
       
   337 					
       
   338 			}/*	End of while (iIndexEntryCount--)*/
       
   339 		
       
   340 		//All Check match. Query Found. Returning the index.
       
   341 		if(queryNotFound)
       
   342 			{
       
   343 			iProgress = KMsvSearchSortOpInProgress;
       
   344 			return KErrNotFound;
       
   345 			}
       
   346 			
       
   347 		else
       
   348 			{
       
   349 			iProgress = KMsvSearchSortOpInProgress;
       
   350 			return return_position;
       
   351 			}
       
   352 			
       
   353 		}/*End of if(iMsvSearchSortCacheManager->iManagerEntry->Count() > 0) */
       
   354 	iProgress = KMsvSearchSortOpInProgress;
       
   355 	return KErrNotFound;
       
   356 	}
       
   357 
       
   358 /**
       
   359 * QueryExists()
       
   360 * @param : const TInt : a Query ID. 
       
   361 * @return :TInt : return index if query exist , else return 0
       
   362 */
       
   363 
       
   364 TInt CMSvSearchSortCacheManager::QueryExists(const TUint32 aQueryID)
       
   365 	{
       
   366 	for(TInt IndexEntryCount = 0; IndexEntryCount <iMsvSearchSortCacheManager->iManagerEntry->Count(); IndexEntryCount++)
       
   367 		{
       
   368 		if(aQueryID == (*iManagerEntry)[IndexEntryCount]->iQueryID)
       
   369 			{
       
   370 			if((*iManagerEntry)[IndexEntryCount]->iMsgExplicitSortPart == EMsvDate)
       
   371 				{
       
   372 				iExplicitSortOnDateTime = ETrue;
       
   373 				}
       
   374 			else
       
   375 				{
       
   376 				iExplicitSortOnDateTime = EFalse;	
       
   377 				}
       
   378 			return IndexEntryCount;
       
   379 			}
       
   380 		}
       
   381 	iReturnResultType = KMsvSearchSortQueryIdNotFound;
       
   382 	return KMsvSearchSortQueryIdNotFound;
       
   383 	}
       
   384 
       
   385 /**
       
   386 * ResetSearchSortCache() : To reste the Search Sort Cache .
       
   387 * @param :None 
       
   388 * @return :None.
       
   389 */
       
   390 
       
   391 void CMSvSearchSortCacheManager::ResetSearchSortCache()
       
   392 	{
       
   393 	if(iMsvSearchSortCacheManager)
       
   394 		{
       
   395 		CMsvSearchSortDeltaCache::Instance()->iNewEntry.Reset();
       
   396 		CMsvSearchSortDeltaCache::Instance()->iUpdateEntry.Reset();
       
   397 		CMsvSearchSortDeltaCache::Instance()->iDeleteEntry.Reset();
       
   398 		iMsvSearchSortCacheManager->iManagerEntry->ResetAndDestroy();
       
   399 		}
       
   400 		
       
   401 	if(iSearchDbAdapter)
       
   402 		{
       
   403 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)   	
       
   404 		iSearchDbAdapter->CloseResources();
       
   405 #endif			
       
   406 		delete iSearchDbAdapter;
       
   407 		iSearchDbAdapter = NULL;
       
   408 		}
       
   409 	}
       
   410 
       
   411 /**
       
   412 * IsInRange()
       
   413 * @param :const CMsvSearchSortCacheEntry* 
       
   414 * @return :TBool : if cache size is available to grow return TRUE, else return False.
       
   415 */
       
   416 TBool CMSvSearchSortCacheManager::IsInRange(const CMsvSearchSortCacheEntry& aEntry)// Will Chek the Size of the Cache.
       
   417 	{
       
   418 	iIndexEntryCount = iMsvSearchSortCacheManager->iManagerEntry->Count();
       
   419 	
       
   420 	iSearchSortDeltaCache = CMsvSearchSortDeltaCache::Instance();
       
   421 	iSearchSortDeltaCacheSize = sizeof(*iSearchSortDeltaCache);
       
   422 	TInt usedSize =0;
       
   423 	for(TInt ii = 0 ; ii < iIndexEntryCount; ii++)
       
   424 		{
       
   425 		usedSize += 4 + sizeof(*((*iManagerEntry)[ii])) ;
       
   426 		}
       
   427 	iRequiredSize = 4 + sizeof(aEntry);
       
   428 	iTotalUsedCacheSize = usedSize + iSearchSortDeltaCacheSize;
       
   429 	iMsvAvailableSearchSortCacheSize = (iMsvMaximumSearchSortCacheSize - iTotalUsedCacheSize);
       
   430 	
       
   431 	if(iMsvAvailableSearchSortCacheSize>iRequiredSize)
       
   432 		{
       
   433 		// if delta cache grow too much invalidate the search sort cache and delta cahce.
       
   434 		if(iSearchSortDeltaCache->iNewEntry.Count() + iSearchSortDeltaCache->iDeleteEntry.Count() + iSearchSortDeltaCache->iUpdateEntry.Count()> KSearchSortDeltaCacheLimit)
       
   435 			{
       
   436 			iMsvSearchSortCacheManager->ResetSearchSortCache();
       
   437 			return ETrue;
       
   438 			}
       
   439 		else
       
   440 			{
       
   441 			return ETrue;
       
   442 			}
       
   443 		}
       
   444 		
       
   445 	else
       
   446 		return EFalse;
       
   447 	}
       
   448 
       
   449 /**
       
   450 * RemoveEntryL()
       
   451 * @param const TInt : aRequiredSize
       
   452 * @return TBool :TRUE : if suscessuly remove ,FALSE: if fails to remove from Cache manager.
       
   453 */
       
   454 
       
   455 TBool CMSvSearchSortCacheManager::RemoveEntry(const TInt aRequiredSize) // Will Remove an entry from manager array based on the requirsed Size.
       
   456 	{
       
   457 	TBool AllMarkedEntry = EFalse ;
       
   458 	if(iManagerEntry->Count() != 0)
       
   459 		{
       
   460 			TInt count = iManagerEntry->Count();
       
   461 
       
   462 			for(TInt iStart = 0;  iStart < count;  iStart++)
       
   463 				{
       
   464 					TBool  markQuery = (*iManagerEntry)[iStart]->iMarkQuery;
       
   465 					if(!markQuery) //Entry is Not Marked
       
   466 						{
       
   467 						delete (*iManagerEntry)[iStart];
       
   468 						iMsvSearchSortCacheManager->iManagerEntry->Remove(iStart);
       
   469 
       
   470 						iIndexEntryCount = iManagerEntry->Count();
       
   471 						for(TInt ii =0; ii<iIndexEntryCount; ii++)
       
   472 							{
       
   473 							iTotalUsedCacheSize +=  4 + sizeof(*((*iManagerEntry)[ii])) ;
       
   474 							}
       
   475 						if(iIndexEntryCount == 0)
       
   476 							{
       
   477 							iMsvAvailableSearchSortCacheSize = iMsvMaximumSearchSortCacheSize;
       
   478 							}
       
   479 						else
       
   480 							{
       
   481 							iMsvAvailableSearchSortCacheSize = (iMsvMaximumSearchSortCacheSize - iTotalUsedCacheSize);
       
   482 							}
       
   483 						if(aRequiredSize<iMsvAvailableSearchSortCacheSize)
       
   484 							{
       
   485 							return ETrue;
       
   486 							}
       
   487 						AllMarkedEntry = EFalse;
       
   488 
       
   489 						}
       
   490 					else //Marked
       
   491 						{
       
   492 						AllMarkedEntry = ETrue;
       
   493 						}
       
   494 
       
   495 				}/* end of for loop*/
       
   496 
       
   497 			if(AllMarkedEntry)	 //All entry are marked.
       
   498 				{
       
   499 				for(TInt start=0;  start< count;  start++)
       
   500 					{
       
   501 					//TInt entrySize = 4 + sizeof(*((*iManagerEntry)[start]));
       
   502 
       
   503 					delete (*iManagerEntry)[start];
       
   504 					iMsvSearchSortCacheManager->iManagerEntry->Remove(start);
       
   505 
       
   506 					iIndexEntryCount = iManagerEntry->Count();
       
   507 					for(TInt i =0; i<iIndexEntryCount; i++)
       
   508 						{
       
   509 						iTotalUsedCacheSize +=  4 + sizeof(*((*iManagerEntry)[i])) ;
       
   510 						}
       
   511 					if(iIndexEntryCount == 0)
       
   512 						{
       
   513 						iMsvAvailableSearchSortCacheSize = iMsvMaximumSearchSortCacheSize;
       
   514 						}
       
   515 					else
       
   516 						{
       
   517 						iMsvAvailableSearchSortCacheSize = (iMsvMaximumSearchSortCacheSize - iTotalUsedCacheSize);
       
   518 						}
       
   519 				
       
   520 					if(aRequiredSize<iMsvAvailableSearchSortCacheSize)
       
   521 						{
       
   522 						return ETrue;
       
   523 						}
       
   524 			  		 }/* END OF for */
       
   525 
       
   526 				}/* end of if(iAllMarkedEntry)	 //All entry are marked.*/
       
   527 
       
   528 		}/*End of if(iManagerEntry->Count()!= 0) */
       
   529 	return EFalse;
       
   530 	}
       
   531 
       
   532 
       
   533 /***********
       
   534  * RemoveEndEntry()
       
   535  * @param :const TInt : aIndex .
       
   536  * @return :TBool 
       
   537  *
       
   538  ***********/
       
   539 
       
   540 TBool CMSvSearchSortCacheManager::RemoveEndEntry(const TInt aIndex) 
       
   541 	{
       
   542 	delete (*iManagerEntry)[aIndex];
       
   543 	iMsvSearchSortCacheManager->iManagerEntry->Remove(aIndex);
       
   544 	return ETrue;
       
   545 	}
       
   546 
       
   547 
       
   548 /***********
       
   549  * GetTypeOfSearchQuery()
       
   550  * @param :CMsvSearchSortCacheEntry& :A Cache Entry .
       
   551  * @return :void
       
   552  *
       
   553  ***********/
       
   554 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)	
       
   555 
       
   556 void CMSvSearchSortCacheManager::TypeOfSearchQuery(CMsvSearchSortCacheEntry& aEntry)
       
   557 	{
       
   558 	
       
   559 	aEntry.iTypeOfQuery = EQueryInvalid;
       
   560 	
       
   561 	TInt iTotalCount=aEntry.iMsgQuery.Count();
       
   562 	
       
   563 	//Search
       
   564 	for(TInt count = 0; count<iTotalCount; count++)
       
   565 		{
       
   566 		switch (aEntry.iMsgQuery[count].iMessagePart)
       
   567 			{
       
   568 			case EMsvTo:
       
   569 			case EMsvFrom:
       
   570 			case EMsvCc:
       
   571 			case EMsvBcc:
       
   572 			case EMsvSubject:
       
   573 				{
       
   574 				aEntry.iTypeOfQuery |= EQueryOnHeader;
       
   575 				break;
       
   576 				}
       
   577 			
       
   578 			case EMsvBody:
       
   579 				{
       
   580 				aEntry.iTypeOfQuery |= EQueryOnBody;
       
   581 				break;
       
   582 				}
       
   583 				
       
   584 			case EMsvDescription:
       
   585 			case EMsvDetails:
       
   586 			case EMsvDate:  
       
   587 			case EMsvSize:
       
   588 			case EMsvAttachment:
       
   589 				{
       
   590 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentYes) ==0)
       
   591 					{
       
   592 					aEntry.iTypeOfQuery |= EQueryOnIndexEntry;
       
   593 					break;	
       
   594 					}
       
   595 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentNo) ==0)
       
   596 					{
       
   597 					aEntry.iTypeOfQuery |= EQueryOnIndexEntry;
       
   598 					break;	
       
   599 					}
       
   600 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentTypeEMsvFile) ==0)
       
   601 					{
       
   602 					aEntry.iTypeOfQuery |= EQueryOnBody;
       
   603 					break;	
       
   604 					}
       
   605 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentTypeEMsvLinkedFile) ==0)
       
   606 					{
       
   607 					aEntry.iTypeOfQuery |= EQueryOnBody;
       
   608 					break;	
       
   609 					}
       
   610 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentTypeEMsvMessageEntry) ==0)
       
   611 					{
       
   612 					aEntry.iTypeOfQuery |= EQueryOnBody;
       
   613 					break;	
       
   614 					}
       
   615 				}
       
   616 			case EMsvMtmTypeUID:
       
   617 			case EMsvPriority:
       
   618 			case EMsvUnreadMessages:
       
   619 			case EMsvNew:
       
   620 				{
       
   621 				aEntry.iTypeOfQuery |= EQueryOnIndexEntry;
       
   622 				break;
       
   623 				}
       
   624 			}
       
   625 		}
       
   626 	
       
   627 	
       
   628 	if(aEntry.IsExplicitSortReq())
       
   629 		{
       
   630 		//Search With explicit sort Sort	
       
   631 		switch (aEntry.iMsgExplicitSortPart)
       
   632 			{
       
   633 			case EMsvDescription:
       
   634 			case EMsvDetails:
       
   635 			case EMsvDate:  
       
   636 			case EMsvSize:
       
   637 			case EMsvAttachment:
       
   638 			case EMsvMtmTypeUID:
       
   639 			case EMsvPriority:
       
   640 			case EMsvUnreadMessages:
       
   641 			case EMsvNew:
       
   642 				{
       
   643 				aEntry.iSortOnHeader = EFalse;
       
   644 				}
       
   645 				break;
       
   646 				
       
   647 			case EMsvTo:
       
   648 			case EMsvFrom:
       
   649 			case EMsvCc:
       
   650 			case EMsvBcc:
       
   651 			case EMsvSubject:
       
   652 				{
       
   653 				aEntry.iSortOnHeader = ETrue;
       
   654 				}
       
   655 				break;
       
   656 			
       
   657 			case EMsvBody:
       
   658 				{
       
   659 				aEntry.iSortOnHeader = EFalse;
       
   660 				}
       
   661 				break;
       
   662 			}
       
   663 		}
       
   664 	}
       
   665 
       
   666 #else
       
   667 
       
   668 void CMSvSearchSortCacheManager::TypeOfSearchQuery(CMsvSearchSortCacheEntry& aEntry)
       
   669 	{
       
   670 	TBool aFileAccess = EFalse;
       
   671 	TBool aIndexAccess =EFalse;
       
   672 	TInt iTotalCount=aEntry.iMsgQuery.Count();
       
   673 	//Search
       
   674 	for(TInt count = 0; count<iTotalCount; count++)
       
   675 		{
       
   676 		switch (aEntry.iMsgQuery[count].iMessagePart)
       
   677 			{
       
   678 			case EMsvTo:
       
   679 			case EMsvFrom:
       
   680 			case EMsvCc:
       
   681 			case EMsvBcc:
       
   682 			case EMsvSubject:
       
   683 			case EMsvBody:
       
   684 				{
       
   685 				aFileAccess=ETrue;
       
   686 				break;
       
   687 				}
       
   688 			case EMsvDescription:
       
   689 			case EMsvDetails:
       
   690 			case EMsvDate:  
       
   691 			case EMsvSize:
       
   692 			case EMsvAttachment:
       
   693 				{
       
   694 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentYes) ==0)
       
   695 					{
       
   696 					aIndexAccess=ETrue;
       
   697 					break;	
       
   698 					}
       
   699 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentNo) ==0)
       
   700 					{
       
   701 					aIndexAccess=ETrue;
       
   702 					break;	
       
   703 					}
       
   704 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentTypeEMsvFile) ==0)
       
   705 					{
       
   706 					aFileAccess=ETrue;
       
   707 					break;	
       
   708 					}
       
   709 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentTypeEMsvLinkedFile) ==0)
       
   710 					{
       
   711 					aFileAccess=ETrue;
       
   712 					break;	
       
   713 					}
       
   714 				if(aEntry.iMsgQuery[count].iQueryString.Compare(KAttachmentTypeEMsvMessageEntry) ==0)
       
   715 					{
       
   716 					aFileAccess=ETrue;
       
   717 					break;	
       
   718 					}
       
   719 				}
       
   720 			case EMsvMtmTypeUID:
       
   721 			case EMsvPriority:
       
   722 			case EMsvUnreadMessages:
       
   723 			case EMsvNew:
       
   724 				{
       
   725 				aIndexAccess=ETrue;
       
   726 				break;
       
   727 				}
       
   728 			}
       
   729 		}
       
   730 	
       
   731 	if(!aEntry.IsExplicitSortReq())	
       
   732 	{
       
   733 		if(aFileAccess && !aIndexAccess)
       
   734 			{
       
   735 			aEntry.iFileAccess = ETrue;
       
   736 			aEntry.iQueryType = EHeaderBodyQuery;
       
   737 			}
       
   738 		else if(aIndexAccess && !aFileAccess)
       
   739 			{	
       
   740 			aEntry.iFileAccess = EFalse;
       
   741 			aEntry.iQueryType = EIndexQuery;
       
   742 			}
       
   743 		else if(aFileAccess && aIndexAccess)
       
   744 			{
       
   745 			aEntry.iFileAccess = ETrue;
       
   746 			aEntry.iQueryType = ECombinedQuery;
       
   747 			}
       
   748 		else
       
   749 			aEntry.iQueryType = ENotValid;
       
   750 		
       
   751 	}
       
   752 	
       
   753 	else
       
   754 	{
       
   755 		
       
   756 		if(aEntry.IsExplicitSortReq())
       
   757 		{
       
   758 			//Search With explicit sort Sort	
       
   759 			switch (aEntry.iMsgExplicitSortPart)
       
   760 				{
       
   761 			case EMsvTo:
       
   762 			case EMsvFrom:
       
   763 			case EMsvCc:
       
   764 			case EMsvBcc:
       
   765 			case EMsvSubject:
       
   766 			case EMsvBody:
       
   767 					{
       
   768 					aEntry.iSortOnHeader = ETrue;
       
   769 					break;
       
   770 					}
       
   771 			case EMsvDescription:
       
   772 			case EMsvDetails:
       
   773 			case EMsvDate:  
       
   774 			case EMsvSize:
       
   775 			case EMsvAttachment:
       
   776 			case EMsvMtmTypeUID:
       
   777 			case EMsvPriority:
       
   778 			case EMsvUnreadMessages:
       
   779 			case EMsvNew:
       
   780 					{
       
   781 					aEntry.iSortOnHeader=EFalse;
       
   782 					break;
       
   783 					}
       
   784 				}
       
   785 			}
       
   786 			
       
   787 	
       
   788 
       
   789 		if(aIndexAccess && !aEntry.iSortOnHeader )
       
   790 			aEntry.iQueryType = EIndexQuery;
       
   791 		if(aIndexAccess  && aEntry.iSortOnHeader)
       
   792 			aEntry.iQueryType = ECombinedQuery;
       
   793 		if(aFileAccess && aEntry.iSortOnHeader)
       
   794 			aEntry.iQueryType = EHeaderBodyQuery;
       
   795 		if(aFileAccess && !aEntry.iSortOnHeader)
       
   796 		    aEntry.iQueryType = ECombinedQuery;
       
   797 		
       
   798 		}
       
   799 	}
       
   800 
       
   801 #endif	
       
   802 
       
   803 	
       
   804 	
       
   805 
       
   806 /***********
       
   807  * GetTypeOfSortQuery()
       
   808  * @param :CMsvSearchSortCacheEntry& :A Cache Entry .
       
   809  * @return :void 
       
   810  *
       
   811  ***********/
       
   812 
       
   813 void CMSvSearchSortCacheManager::TypeOfSortQuery(CMsvSearchSortCacheEntry& aEntry)
       
   814 	{
       
   815 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   816 	aEntry.iTypeOfQuery = EQueryInvalid;
       
   817 #endif
       
   818 	
       
   819 	//Single level of sort
       
   820 		switch (aEntry.iMsgExplicitSortPart)
       
   821 			{
       
   822 			case EMsvTo:
       
   823 			case EMsvFrom:
       
   824 			case EMsvCc:
       
   825 			case EMsvBcc:
       
   826 			case EMsvSubject:
       
   827 			case EMsvBody:
       
   828 				{
       
   829 			#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   830 				aEntry.iTypeOfQuery |= EQueryOnHeader;
       
   831 			#else
       
   832 				aEntry.iFileAccess = ETrue;
       
   833 				aEntry.iQueryType = EHeaderBodyQuery;
       
   834 			#endif	
       
   835 				aEntry.iSortOnHeader = ETrue;
       
   836 				break;
       
   837 				}
       
   838 			case EMsvDescription:
       
   839 			case EMsvDetails:
       
   840 			case EMsvDate:  
       
   841 			case EMsvSize:
       
   842 			case EMsvAttachment:
       
   843 			case EMsvMtmTypeUID:
       
   844 			case EMsvPriority:
       
   845 			case EMsvUnreadMessages:
       
   846 			case EMsvNew:
       
   847 				{
       
   848 			#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)				
       
   849 				aEntry.iTypeOfQuery |= EQueryOnIndexEntry;
       
   850 			#else	
       
   851 				aEntry.iFileAccess = EFalse;
       
   852 				aEntry.iQueryType = EIndexQuery;
       
   853 			#endif	
       
   854 				aEntry.iSortOnHeader = EFalse;
       
   855 				
       
   856 				break;
       
   857 				}
       
   858 			}
       
   859 	}
       
   860 
       
   861 
       
   862 /***********
       
   863  * GetTypeOfQuery()
       
   864  * @param :const CMsvSearchSortCacheEntry& :A Cache Entry .
       
   865  * @return :Type of Query 
       
   866  *
       
   867  ***********/
       
   868 
       
   869 
       
   870 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   871 TInt CMSvSearchSortCacheManager::IsTypeOfQuery(const CMsvSearchSortCacheEntry& aEntry)
       
   872 #else
       
   873 TTypeOfQuery CMSvSearchSortCacheManager::IsTypeOfQuery(const CMsvSearchSortCacheEntry& aEntry)
       
   874 #endif
       
   875 	{
       
   876 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   877 	return aEntry.iTypeOfQuery;
       
   878 #else	
       
   879 	return (aEntry.iQueryType);
       
   880 #endif	
       
   881 	}
       
   882 
       
   883 
       
   884 /***********
       
   885  * DoProcessQueryL()
       
   886  * @param :const CMsvSearchSortCacheEntry& :A Cache Entry .
       
   887  * @return :TInt
       
   888  *
       
   889  ***********/
       
   890 //Query is NEW
       
   891 TInt CMSvSearchSortCacheManager::DoProcessQueryL(const CMsvSearchSortCacheEntry& aEntry, TInt iteratorCount)
       
   892 	{
       
   893 	iProgress = KMsvSearchSortOpInProgress;
       
   894 	if(iteratorCount == 0)
       
   895 		{//With out iterator.
       
   896 		iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
   897 		iMsvSearchSortCacheManager->iToFindResultAsIdArray.Reset();
       
   898 
       
   899 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   900 		
       
   901 		FinalResultAsIdL(aEntry);
       
   902 		
       
   903 		if(iReturnResultType == KFinalResult)
       
   904 			{
       
   905 			iProgress = KMsvSearchSortOpCompleted; 
       
   906 			}
       
   907 		else
       
   908 			{
       
   909 			iProgress = KMsvSearchSortOpInProgress; 
       
   910 			//New Query it is on Header and Body
       
   911 			iSearchSortDeltaCache->iIsHeaderSearchEnabled = ETrue;	
       
   912 			}
       
   913 				
       
   914 #else	
       
   915 		if(EIndexQuery == IsTypeOfQuery(aEntry))
       
   916 			{
       
   917 			FinalResultAsIdL(aEntry);
       
   918 			iReturnResultType = KFinalResult;
       
   919 			iProgress = KMsvSearchSortOpCompleted; 
       
   920 			if(iOutstandingSOSOperations>0)
       
   921 				{
       
   922 				iOutstandingSOSOperations--;
       
   923 				}
       
   924 			}
       
   925 		else
       
   926 			{
       
   927 			FindResultAsIdL(aEntry);
       
   928 			iReturnResultType = KNewQuery;
       
   929 			iProgress = KMsvSearchSortOpInProgress; 
       
   930 			//New Query it is on Header and Body
       
   931 			iSearchSortDeltaCache->iIsHeaderSearchEnabled = ETrue; 
       
   932 			}
       
   933 #endif			
       
   934 		}
       
   935 	else
       
   936 		{//with itarator
       
   937 		iMsvSearchSortCacheManager->iIteratorId = 0;
       
   938 
       
   939 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   940 		if(IsTypeOfQuery(aEntry) == EQueryOnIndexEntry)
       
   941 			{
       
   942 			FirstResultForInteraratorNewQueryL(aEntry);
       
   943 			}
       
   944 #else
       
   945 		if(EIndexQuery == IsTypeOfQuery(aEntry))
       
   946 			{
       
   947 			FirstResultForInteraratorNewQueryL(aEntry);
       
   948 			iReturnResultType = KFinalResult;
       
   949 			if(iOutstandingSOSOperations>0)
       
   950 				{
       
   951 				iOutstandingSOSOperations--;
       
   952 				}
       
   953 			}
       
   954 #endif
       
   955 		else 
       
   956 			{
       
   957 			return KErrNotSupported;
       
   958 			// Not Supported.
       
   959 			}
       
   960 		}
       
   961 	//Add the Entry to Manager Object.
       
   962 	if(!iCancelFlag)
       
   963 		{
       
   964 		AddEntryL(aEntry);	
       
   965 		}
       
   966 	else
       
   967 		{
       
   968 		iProgress = KMsvSearchSortOpCanceled; 
       
   969 		InstantiateDBwrapperL();
       
   970 		iSearchDbAdapter->DeleteQueryFromTableL(aEntry.iQueryID);
       
   971 		iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
   972 		iMsvSearchSortCacheManager->iToFindResultAsIdArray.Reset();
       
   973 		delete &aEntry; 
       
   974 		}		
       
   975 	return KErrNone;
       
   976 	}
       
   977 
       
   978 
       
   979 /***********
       
   980  * DoProcessQueryL()
       
   981  * @param :const TInt aIndex .
       
   982  * @return :TInt
       
   983  *
       
   984  ***********/
       
   985 
       
   986 //Query is Exist in manager.
       
   987 TInt CMSvSearchSortCacheManager::DoProcessQueryL(const TInt aIndex)
       
   988 	{
       
   989 	iProgress = KMsvSearchSortOpInProgress;
       
   990 	if((*iManagerEntry)[aIndex]->iteratorCount == 0)
       
   991 		{
       
   992 		
       
   993 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   994 		if(IsTypeOfQuery(*(*iManagerEntry)[aIndex]) == EQueryOnIndexEntry)
       
   995 #else					
       
   996 		if(EIndexQuery == IsTypeOfQuery(*(*iManagerEntry)[aIndex]))
       
   997 #endif		
       
   998 			{
       
   999 			OnDemandUpdateCacheEntryL(aIndex);
       
  1000 			iReturnResultType = KFinalResult;
       
  1001 			iProgress = KMsvSearchSortOpCompleted; 
       
  1002 			if(iOutstandingSOSOperations>0)
       
  1003 				{
       
  1004 				iOutstandingSOSOperations--;
       
  1005 				}
       
  1006 			}
       
  1007 		else
       
  1008 			{
       
  1009 			OnDemandUpdateCacheEntryL(aIndex);
       
  1010 			if(!iSearchSortDeltaCache->iDeltaCacheDirtyFlag)
       
  1011 				{
       
  1012 				iReturnResultType = KFinalResult;
       
  1013 				iProgress = KMsvSearchSortOpCompleted;
       
  1014 				if(iOutstandingSOSOperations>0)
       
  1015 					{
       
  1016 					iOutstandingSOSOperations--;
       
  1017 					}
       
  1018 				}
       
  1019 			else
       
  1020 				{
       
  1021 				for(TInt ii =0; ii<iSearchSortDeltaCache->iNewEntry.Count(); ii++ )
       
  1022 					{
       
  1023 					iDeltaCacheIdArray.Append(iSearchSortDeltaCache->iNewEntry[ii]);
       
  1024 					}
       
  1025 			
       
  1026 				for(TInt ii =0; ii<iSearchSortDeltaCache->iUpdateEntry.Count(); ii++ )
       
  1027 					{
       
  1028 					iDeltaCacheIdArray.Append(iSearchSortDeltaCache->iUpdateEntry[ii]);
       
  1029 					}
       
  1030 				iReturnResultType = KPartialResult;
       
  1031 				iProgress = KMsvSearchSortOpInProgress;  
       
  1032 				// Partial Query and it is Repetetive  an header and body.
       
  1033 				iSearchSortDeltaCache->iIsHeaderSearchEnabled = ETrue;  
       
  1034 				}	
       
  1035 			}
       
  1036 		}
       
  1037 	else
       
  1038 		{
       
  1039 		iMsvSearchSortCacheManager->iIteratorId = 0;
       
  1040 		
       
  1041 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
  1042 		if( IsTypeOfQuery(*(*iManagerEntry)[aIndex]) == EQueryOnIndexEntry )
       
  1043 #else		
       
  1044 		if(EIndexQuery == IsTypeOfQuery(*(*iManagerEntry)[aIndex]))
       
  1045 #endif		
       
  1046 			{
       
  1047 			FirstResultForInteraratorQueryIdL(aIndex);
       
  1048 			iReturnResultType = KFinalResult;
       
  1049 			iProgress = KMsvSearchSortOpIterationEnabled;
       
  1050 			if(iOutstandingSOSOperations>0)
       
  1051 				{
       
  1052 				iOutstandingSOSOperations--;
       
  1053 				}
       
  1054  			}
       
  1055 		else 
       
  1056 			{
       
  1057 			// Not Supported.
       
  1058 			return KErrNotSupported;
       
  1059 			}
       
  1060 		}
       
  1061 	if(!iCancelFlag)
       
  1062 		{
       
  1063 		if(iMsvSearchSortCacheManager->iManagerEntry->Count()>1)
       
  1064 			{
       
  1065 			MoveEntryAtEnd((*iManagerEntry)[aIndex],aIndex);
       
  1066 			}
       
  1067 		iProgress = KMsvSearchSortOpInProgress;
       
  1068 		}
       
  1069 	else
       
  1070 		{
       
  1071 		iProgress = KMsvSearchSortOpCanceled; 
       
  1072 		}
       
  1073 	return KErrNone;
       
  1074 	}
       
  1075 
       
  1076 
       
  1077 /***********
       
  1078  * GetFinalResultAsId()
       
  1079  * @param :const CMsvSearchSortCacheEntry& :A Cache Entry .
       
  1080  * @return :TInt : KErrNone: if successful else  return error value.
       
  1081  *
       
  1082  ***********/
       
  1083 
       
  1084 //New Query on Index entry.
       
  1085  TInt CMSvSearchSortCacheManager::FinalResultAsIdL(const CMsvSearchSortCacheEntry& aEntry)
       
  1086 	{
       
  1087 	iFinalResultCount = 0;
       
  1088 	iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1089 	InstantiateDBwrapperL();
       
  1090  	
       
  1091  #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)	
       
  1092  	TRAPD(err, iSearchDbAdapter->FindL(aEntry, iMsvSearchSortCacheManager->iFinalResultAsIdArray, aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType));
       
  1093  #else	
       
  1094  	TRAPD(err,iSearchDbAdapter->FindL(aEntry,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader));
       
  1095  #endif	
       
  1096  	if(err!= KErrNone)
       
  1097  		{
       
  1098  		iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1099  		return err;
       
  1100  		}
       
  1101  	iFinalResultCount = iMsvSearchSortCacheManager->iFinalResultAsIdArray.Count();
       
  1102   	return KErrNone;
       
  1103 	}
       
  1104 
       
  1105 
       
  1106 /***********
       
  1107  * GetFindResultAsId()
       
  1108  * @param :const CMsvSearchSortCacheEntry& :A Cache Entry .
       
  1109  * @return :TInt : KErrNone: if successful else  return error value.
       
  1110  *
       
  1111  ***********/
       
  1112 
       
  1113 //New Query on Header and Body.
       
  1114 TInt CMSvSearchSortCacheManager::FindResultAsIdL(const CMsvSearchSortCacheEntry& aEntry)
       
  1115 	{
       
  1116 	iMsvSearchSortCacheManager->iToFindResultAsIdArray.Reset();
       
  1117 	iMsvSearchSortCacheManager->iFindResultCount = 0;
       
  1118 	InstantiateDBwrapperL();
       
  1119 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)	
       
  1120  	TRAPD(err, iSearchDbAdapter->FindL(aEntry, iMsvSearchSortCacheManager->iFinalResultAsIdArray, aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType));
       
  1121  	if(err!= KErrNone)
       
  1122  		{
       
  1123  		iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1124  		return err;
       
  1125  		}
       
  1126 	iFinalResultCount = iMsvSearchSortCacheManager->iFinalResultAsIdArray.Count();
       
  1127 #else	
       
  1128  	TRAPD(err,iSearchDbAdapter->FindL(aEntry,iMsvSearchSortCacheManager->iToFindResultAsIdArray,aEntry.iSortOnHeader));
       
  1129 	if(err!= KErrNone)
       
  1130  		{
       
  1131  		iMsvSearchSortCacheManager->iToFindResultAsIdArray.Reset();
       
  1132  		return err;
       
  1133  		}
       
  1134 	iMsvSearchSortCacheManager->iFindResultCount = iMsvSearchSortCacheManager->iToFindResultAsIdArray.Count();
       
  1135 #endif	
       
  1136 	return KErrNone;
       
  1137 	}
       
  1138 
       
  1139 
       
  1140 /**
       
  1141  * OnDemandUpdateCacheEntryL()
       
  1142  * @param const TInt aIndex
       
  1143  * @return TInt
       
  1144  *
       
  1145  */
       
  1146 
       
  1147 TInt CMSvSearchSortCacheManager::OnDemandUpdateCacheEntryL(const TInt aIndex) //Background task to update the Search and Sort cache.
       
  1148 	{
       
  1149  	iSearchSortDeltaCache = CMsvSearchSortDeltaCache::Instance();
       
  1150  	InstantiateDBwrapperL();
       
  1151 	if(iSearchSortDeltaCache->iDeltaCacheDirtyFlag)
       
  1152 		{
       
  1153 		//Delta cache has something
       
  1154 		return UpdateQueryResultOnDemandInDBL(*(*iManagerEntry)[aIndex],iSearchSortDeltaCache->iNewEntry,iSearchSortDeltaCache->iUpdateEntry,iSearchSortDeltaCache->iDeleteEntry);
       
  1155 		}
       
  1156 	else
       
  1157 		{
       
  1158 		iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1159 		//if nothing is their in delta cache.
       
  1160 		TRAPD(err,iSearchDbAdapter->GetSortedTMsvIdsfromTableL((*iManagerEntry)[aIndex]->iQueryID,iMsvSearchSortCacheManager->iFinalResultAsIdArray, (*iManagerEntry)[aIndex]->IsAscendingSort()/*Default = EFalse*/,(*iManagerEntry)[aIndex]->iMsgExplicitSortPart));
       
  1161 		if(err!= KErrNone)
       
  1162  			{
       
  1163  			iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1164  			return err;
       
  1165  			}
       
  1166 	 	}
       
  1167 	return KErrNone;
       
  1168 	}
       
  1169 
       
  1170 
       
  1171 /**
       
  1172 * UpdateQueryResultOnDemandInDBL(): 
       
  1173 * @param const CMsvSearchSortCacheEntry& aEntry,RArray<TMsvId>& aNewEntry,RArray<TMsvId>& aUpdateEntry, RArray<TMsvId>& aDeleteEntry.
       
  1174 * @return TInt
       
  1175 */
       
  1176 
       
  1177 TInt CMSvSearchSortCacheManager::UpdateQueryResultOnDemandInDBL(const CMsvSearchSortCacheEntry& aEntry,RArray<TMsvId>& aNewEntry,RArray<TMsvId>& aUpdateEntry, RArray<TMsvId>& aDeleteEntry)
       
  1178 	{
       
  1179 	iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1180 	iMsvSearchSortCacheManager->iToFindResultAsIdArray.Reset();
       
  1181 	InstantiateDBwrapperL();
       
  1182 	
       
  1183 	//TMsvId's that are Deleted can be removed from all Id's as they are invalid
       
  1184 	if(aDeleteEntry.Count()!= 0)
       
  1185 		{
       
  1186 		iSearchDbAdapter->DeleteTMsvIdFromAllQueriesL(aDeleteEntry);
       
  1187 		}
       
  1188 	//TMsvId's that are Updated are removed from the present QueryID,
       
  1189 	//as they are updated they may or may not match the search criteria
       
  1190 	if(aUpdateEntry.Count() !=0)	
       
  1191 		{
       
  1192 		iSearchDbAdapter->DeleteTMsvIdFromQueryL(aEntry.iQueryID,aUpdateEntry);	
       
  1193 		}
       
  1194 	
       
  1195 	//If its EIndexQuery only Query then We need to get the complete result not the updated Id's
       
  1196 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
  1197 	if(IsTypeOfQuery(aEntry) == EQueryOnIndexEntry)
       
  1198 #else			
       
  1199 	if(EIndexQuery == IsTypeOfQuery(aEntry))
       
  1200 #endif	
       
  1201 		{
       
  1202 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
  1203 		iSearchDbAdapter->UpdateNewIdsL(aEntry,aUpdateEntry,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType);
       
  1204 #else
       
  1205 		iSearchDbAdapter->UpdateNewIdsL(aEntry,aUpdateEntry,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader);
       
  1206 #endif
       
  1207 		if(aNewEntry.Count() >= 0)
       
  1208 			{
       
  1209 			//Resetting IdArray, as we will get the correct result in the next
       
  1210 			//UpdateNewIdsL() call
       
  1211 			iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1212 
       
  1213 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
  1214 			iSearchDbAdapter->UpdateNewIdsL(aEntry,aNewEntry,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType);
       
  1215 #else
       
  1216 			iSearchDbAdapter->UpdateNewIdsL(aEntry,aNewEntry,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader);			
       
  1217 #endif			
       
  1218 			}
       
  1219 		}
       
  1220 	
       
  1221 	//If the sort is on Index and ECombinedQuery query we have send the updated and
       
  1222 	// new entries as a single array so that we can get a superset of sorted ids
       
  1223 	// which contains both new and updated id's
       
  1224 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
  1225 	else if( IsTypeOfQuery(aEntry) != EQueryOnIndexEntry )
       
  1226 #else
       
  1227 	else if (ECombinedQuery == IsTypeOfQuery(aEntry) && !aEntry.iSortOnHeader)
       
  1228 #endif	
       
  1229 		{
       
  1230 		RArray<TMsvId> updateAndNewIdlist(aUpdateEntry);
       
  1231 		
       
  1232 		if(aNewEntry.Count() >= 0)
       
  1233 			{
       
  1234 			for(TInt index=0; index<aNewEntry.Count(); ++index)
       
  1235 				{
       
  1236 				updateAndNewIdlist.AppendL(aNewEntry[index]);
       
  1237 				}
       
  1238 			}
       
  1239 
       
  1240 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
  1241 		iSearchDbAdapter->UpdateNewIdsL(aEntry,updateAndNewIdlist,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType);
       
  1242 #else
       
  1243 		iSearchDbAdapter->UpdateNewIdsL(aEntry,updateAndNewIdlist,iMsvSearchSortCacheManager->iToFindResultAsIdArray,aEntry.iSortOnHeader);
       
  1244 #endif
       
  1245 		
       
  1246 		updateAndNewIdlist.Close();
       
  1247 		}
       
  1248 	
       
  1249 	//When sort is on Header, the expected array is not sorted
       
  1250 	else
       
  1251 		{
       
  1252 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
  1253 		iSearchDbAdapter->UpdateNewIdsL(aEntry,aUpdateEntry,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType);
       
  1254 #else
       
  1255 		iSearchDbAdapter->UpdateNewIdsL(aEntry,aUpdateEntry,iMsvSearchSortCacheManager->iToFindResultAsIdArray,aEntry.iSortOnHeader);
       
  1256 #endif		
       
  1257 		if(aNewEntry.Count() >= 0)
       
  1258 			{
       
  1259 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)		
       
  1260 			iSearchDbAdapter->UpdateNewIdsL(aEntry,aNewEntry,iMsvSearchSortCacheManager->iFinalResultAsIdArray,aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType);
       
  1261 #else
       
  1262 			iSearchDbAdapter->UpdateNewIdsL(aEntry,aNewEntry,iMsvSearchSortCacheManager->iToFindResultAsIdArray,aEntry.iSortOnHeader);
       
  1263 #endif			
       
  1264 			}
       
  1265 		}
       
  1266 	return KErrNone;
       
  1267 	}
       
  1268 
       
  1269 
       
  1270 
       
  1271 /**
       
  1272 * GetResultCount(): 
       
  1273 * @param const CMsvSearchSortCacheEntry& aEntry.
       
  1274 * @return TInt
       
  1275 */
       
  1276 
       
  1277 TInt CMSvSearchSortCacheManager::ResultCountL(const CMsvSearchSortCacheEntry& aEntry)
       
  1278 	{
       
  1279 	iMsvSearchSortCacheManager->iFinalResultCount = 0;
       
  1280 	InstantiateDBwrapperL();
       
  1281 	if(!iCancelFlag)
       
  1282 		{
       
  1283 		TRAPD(err,iMsvSearchSortCacheManager->iEntryIdCount = iSearchDbAdapter->ReturnResultCountInTableL(aEntry.iQueryID));
       
  1284 		if(err!= KErrNone)
       
  1285 			{
       
  1286 			iMsvSearchSortCacheManager->iEntryIdCount = 0;
       
  1287 			return err;
       
  1288 			}
       
  1289 		iProgress = KMsvSearchSortOpCompleted;
       
  1290 		return iMsvSearchSortCacheManager->iEntryIdCount;
       
  1291 		}
       
  1292 	else
       
  1293 		{
       
  1294 		return KMsvSearchSortOpCanceled;	
       
  1295 		}
       
  1296 	}
       
  1297 	
       
  1298 
       
  1299 /* GetResultCount(): 
       
  1300  * @param const TInt aIndex.
       
  1301  * @return TInt
       
  1302  */
       
  1303 
       
  1304 TInt CMSvSearchSortCacheManager::ResultCountL(const TInt aIndex)
       
  1305 	{
       
  1306 	InstantiateDBwrapperL();
       
  1307 	if(!iCancelFlag)
       
  1308 		{
       
  1309 		iMsvSearchSortCacheManager->iFinalResultCount = 0;
       
  1310 		TRAPD(err,iMsvSearchSortCacheManager->iFinalResultCount = iSearchDbAdapter->ReturnResultCountInTableL((*iManagerEntry)[aIndex]->iQueryID));
       
  1311 		if(err!= KErrNone)
       
  1312 			{
       
  1313 			iMsvSearchSortCacheManager->iFinalResultCount = 0;
       
  1314 			return err;
       
  1315 			}
       
  1316 		iProgress = KMsvSearchSortOpCompleted;
       
  1317 		return iMsvSearchSortCacheManager->iFinalResultCount;
       
  1318 		}
       
  1319 	else
       
  1320 		{
       
  1321 		return KMsvSearchSortOpCanceled;	
       
  1322 		}
       
  1323 	}
       
  1324 
       
  1325 
       
  1326 /**
       
  1327 * GetQueryIDL(): 
       
  1328 * @param None.
       
  1329 * @return TInt
       
  1330 */
       
  1331 
       
  1332 TInt CMSvSearchSortCacheManager::QueryID() 
       
  1333 	{
       
  1334 	if(!iCancelFlag)
       
  1335 		{
       
  1336 		iIndexEntryCount = iMsvSearchSortCacheManager->iManagerEntry->Count();
       
  1337 		if(iIndexEntryCount != 0)
       
  1338 			{
       
  1339 			return (*iManagerEntry)[iIndexEntryCount-1]->iQueryID;	
       
  1340 			}
       
  1341 		else
       
  1342 			{
       
  1343 			return KErrNotFound;
       
  1344 			}	
       
  1345 		}
       
  1346 	else
       
  1347 		{
       
  1348 		return KMsvSearchSortOpCanceled;	
       
  1349 		}
       
  1350 	}
       
  1351 
       
  1352 
       
  1353 /**
       
  1354 * StoreResult() : 
       
  1355 * @param TInt aIndex, RArray<TMsvId>& aToUpdateDb.
       
  1356 * @return None
       
  1357 */
       
  1358 
       
  1359 void CMSvSearchSortCacheManager::StoreResultL(TInt aIndex, RArray<TMsvId>& aToUpdateDb)
       
  1360 	{
       
  1361 	InstantiateDBwrapperL();
       
  1362 	if(!iCancelFlag)
       
  1363 		{
       
  1364 	 	iSearchDbAdapter->DeleteTMsvIdFromQueryL((*iManagerEntry)[aIndex-1]->iQueryID,aToUpdateDb);
       
  1365 	  	iProgress = KMsvSearchSortOpCompleted;
       
  1366 		}
       
  1367 	
       
  1368 	iMsvSearchSortCacheManager->iFinalResultAsIdArray.Reset();
       
  1369 	iSearchDbAdapter->GetSortedTMsvIdsfromTableL((*iManagerEntry)[aIndex-1]->iQueryID,iMsvSearchSortCacheManager->iFinalResultAsIdArray, (*iManagerEntry)[aIndex-1]->IsAscendingSort()/*Default = EFalse*/,(*iManagerEntry)[aIndex-1]->iMsgExplicitSortPart);
       
  1370 	iReturnResultType = KFinalResult;
       
  1371 	iProgress = KMsvSearchSortOpCompleted; 
       
  1372 	if(iOutstandingSOSOperations>0)
       
  1373 		{
       
  1374 		iOutstandingSOSOperations--;
       
  1375 		}
       
  1376 	aToUpdateDb.Reset();
       
  1377 	}
       
  1378 
       
  1379 
       
  1380 /**
       
  1381 * StoreResult() : 
       
  1382 * @param TInt aIndex,RArray<TMsvIdWithSortField>& aResultIdData.
       
  1383 * @return None
       
  1384 */
       
  1385 
       
  1386 TInt CMSvSearchSortCacheManager::StoreSortResultL(TInt aIndex,RArray<TMsvIdWithSortField>& aResultIdData)
       
  1387 	{
       
  1388 	InstantiateDBwrapperL();
       
  1389 	if(!iCancelFlag)
       
  1390 		{
       
  1391 		iSearchDbAdapter->AddIdtoDBL((*iManagerEntry)[aIndex-1]->iQueryID, aResultIdData);
       
  1392 		iSearchDbAdapter->GetSortedTMsvIdsfromTableL((*iManagerEntry)[aIndex-1]->iQueryID,iMsvSearchSortCacheManager->iFinalResultAsIdArray, (*iManagerEntry)[aIndex-1]->IsAscendingSort(),(*iManagerEntry)[aIndex-1]->iMsgExplicitSortPart);
       
  1393 		iReturnResultType = KFinalResult;
       
  1394 		iProgress = KMsvSearchSortOpCompleted; 
       
  1395 		}
       
  1396 	iMsvSearchSortCacheManager->iMsvIdWithSortFieldArray.Reset();
       
  1397     if(iOutstandingSOSOperations>0)
       
  1398         {
       
  1399         iOutstandingSOSOperations--;
       
  1400         }
       
  1401 	return iReturnResultType;
       
  1402 	}
       
  1403 
       
  1404 
       
  1405 /**
       
  1406 * ReturnIdCount() : Return Total Number of TMsvId .
       
  1407 * @param TInt aQID.
       
  1408 * @return TInt : Total number of TMsvId
       
  1409 */
       
  1410 	
       
  1411 TInt CMSvSearchSortCacheManager::ReturnIdCountL(TInt aQID)
       
  1412 	{
       
  1413 	InstantiateDBwrapperL();
       
  1414 	iMsvSearchSortCacheManager->iEntryIdCount = iSearchDbAdapter->ReturnResultCountInTableL(aQID);
       
  1415 	return iMsvSearchSortCacheManager->iEntryIdCount;
       
  1416 	} 
       
  1417 
       
  1418 
       
  1419 /**
       
  1420 * GetFirstForInteraratorL() : 
       
  1421 * @param const CMsvSearchSortCacheEntry& aEntry.
       
  1422 * @return None
       
  1423 */
       
  1424 
       
  1425 void  CMSvSearchSortCacheManager::FirstResultForInteraratorNewQueryL(const CMsvSearchSortCacheEntry& aEntry)
       
  1426 	{
       
  1427 	InstantiateDBwrapperL();
       
  1428 	iProgress = KMsvSearchSortOpIterationInProgress;
       
  1429 	iMsvSearchSortCacheManager->iIteratorId = 0;
       
  1430 	iMsvSearchSortCacheManager->iIteratorId = 0;
       
  1431  	if(aEntry.iResultType == EMsvResultAsTMsvId)
       
  1432  		{
       
  1433 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB) 		
       
  1434  		iSearchDbAdapter->GetIdsInIteratorNewQueryL(aEntry, aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType);
       
  1435 #else 		
       
  1436  		iSearchDbAdapter->GetIdsInIteratorNewQueryL(aEntry);
       
  1437 #endif 		
       
  1438   		iProgress = KMsvSearchSortOpCompleted;
       
  1439 		if(iOutstandingSOSOperations>0)
       
  1440 			{
       
  1441 			iOutstandingSOSOperations--;
       
  1442 			}
       
  1443  		}
       
  1444 	else
       
  1445 		{
       
  1446 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB) 		
       
  1447  		iSearchDbAdapter->GetIdsInIteratorNewQueryL(aEntry, aEntry.iSortOnHeader, IsTypeOfQuery(aEntry), iReturnResultType);
       
  1448 #else 		
       
  1449 		iSearchDbAdapter->GetIdsInIteratorNewQueryL(aEntry);
       
  1450 #endif
       
  1451 		iProgress = KMsvSearchSortOpCompleted;
       
  1452 		if(iOutstandingSOSOperations>0)
       
  1453 			{
       
  1454 			iOutstandingSOSOperations--;
       
  1455 			}
       
  1456 		}
       
  1457 	}
       
  1458 
       
  1459 
       
  1460 /**
       
  1461 * FirstResultForInteraratorQueryIdL() : 
       
  1462 * @param const TInt index.
       
  1463 * @return None
       
  1464 */
       
  1465 
       
  1466 void  CMSvSearchSortCacheManager::FirstResultForInteraratorQueryIdL(const TInt index)
       
  1467 	{
       
  1468 	InstantiateDBwrapperL();
       
  1469 	iProgress = KMsvSearchSortOpIterationInProgress;
       
  1470 	iMsvSearchSortCacheManager->iIteratorId = 0;
       
  1471 	//iMsvSearchSortCacheManager->iIteratorId = 0;
       
  1472  	if((*iManagerEntry)[index]->iResultType == EMsvResultAsTMsvId)
       
  1473  		{
       
  1474  		//iSearchDbAdapter->GetIdsInIteratorQueryIdL((*iManagerEntry)[index]->iQueryID);
       
  1475  		iSearchDbAdapter->GetIdsInIteratorQueryIdL((*iManagerEntry)[index]->iQueryID, (*iManagerEntry)[index]->IsAscendingSort(), (*iManagerEntry)[index]->iMsgExplicitSortPart);
       
  1476   		iProgress = KMsvSearchSortOpCompleted;
       
  1477 		if(iOutstandingSOSOperations>0)
       
  1478 			{
       
  1479 			iOutstandingSOSOperations--;
       
  1480 			}
       
  1481    		}
       
  1482 	else
       
  1483 		{
       
  1484 		//iSearchDbAdapter->GetIdsInIteratorQueryIdL((*iManagerEntry)[index]->iQueryID);
       
  1485 		iSearchDbAdapter->GetIdsInIteratorQueryIdL((*iManagerEntry)[index]->iQueryID, (*iManagerEntry)[index]->IsAscendingSort(), (*iManagerEntry)[index]->iMsgExplicitSortPart);
       
  1486 		iProgress = KMsvSearchSortOpCompleted;
       
  1487 		if(iOutstandingSOSOperations>0)
       
  1488 			{
       
  1489 			iOutstandingSOSOperations--;
       
  1490 			}
       
  1491 		}
       
  1492 	}
       
  1493 
       
  1494 /**
       
  1495 * GetNextForInteraratorL() : 
       
  1496 * @param TInt aIndex.
       
  1497 * @return None
       
  1498 */
       
  1499 
       
  1500 void  CMSvSearchSortCacheManager::NextResultForInteraratorL(TInt aIndex)
       
  1501 	{
       
  1502 	InstantiateDBwrapperL();
       
  1503 	iMsvSearchSortCacheManager->iIteratorId = 0;
       
  1504 	iProgress = KMsvSearchSortOpIterationInProgress;
       
  1505  
       
  1506  	if((*iManagerEntry)[aIndex]->iResultType == EMsvResultAsTMsvId)
       
  1507  		{
       
  1508  		//iSearchDbAdapter->GetNextIdL( (*iManagerEntry)[aIndex]->iQueryID , iIteratorId,iIteratorRemainingResultCount);
       
  1509  		iSearchDbAdapter->GetNextIdL(iIteratorId,iIteratorRemainingResultCount);
       
  1510  		iProgress = KMsvSearchSortOpCompleted;
       
  1511 		if(iIteratorRemainingResultCount == 0)
       
  1512 			{
       
  1513 			if(iOutstandingSOSOperations>0)
       
  1514 				{
       
  1515 				iOutstandingSOSOperations--;
       
  1516 				}
       
  1517 			}
       
  1518  		}
       
  1519 	else
       
  1520 		{
       
  1521 		//iIteratorEntry
       
  1522 		//iSearchDbAdapter->GetNextIdL((*iManagerEntry)[aIndex]->iQueryID , iIteratorId,iIteratorRemainingResultCount);
       
  1523 		iSearchDbAdapter->GetNextIdL(iIteratorId,iIteratorRemainingResultCount);
       
  1524 		TMsvEntry *entry;
       
  1525 		iServer.IndexAdapter().GetEntry(iIteratorId,entry);
       
  1526 		iIteratorEntry  = *entry;
       
  1527 		iProgress = KMsvSearchSortOpCompleted;
       
  1528 		if(iIteratorRemainingResultCount == 0)
       
  1529 			{
       
  1530 			if(iOutstandingSOSOperations>0)
       
  1531 				{
       
  1532 				iOutstandingSOSOperations--;
       
  1533 				}
       
  1534        		}
       
  1535 		}
       
  1536 	}
       
  1537 
       
  1538 
       
  1539 /**
       
  1540 * UnMarkedQuery() : 
       
  1541 * @param TInt aQid, TInt aindex .
       
  1542 * @return TInt
       
  1543 */
       
  1544 
       
  1545 TInt  CMSvSearchSortCacheManager::UnMarkedQuery(TInt aindex)
       
  1546 	{
       
  1547 	(*iManagerEntry)[aindex]->iMarkQuery = EFalse;
       
  1548 	return KErrNone;
       
  1549 	}
       
  1550 
       
  1551 
       
  1552 /**
       
  1553 * GenerateQueryId() : 
       
  1554 * @param const CMsvSearchSortCacheEntry.
       
  1555 * @return TInt : Query ID.
       
  1556 */
       
  1557 
       
  1558 TInt CMSvSearchSortCacheManager::GenerateQueryId(CMsvSearchSortCacheEntry& aEntry) 
       
  1559 	{
       
  1560 	TInt queryId = 1001;
       
  1561 	if(iMsvSearchSortCacheManager !=  NULL)
       
  1562 		{
       
  1563 		TInt totalentrycount = iMsvSearchSortCacheManager->iManagerEntry->Count();
       
  1564 		if(iMsvSearchSortCacheManager->iManagerEntry->Count() == 0)
       
  1565 			{
       
  1566 			aEntry.iQueryID = queryId;
       
  1567 			iMaxQueryId = queryId;
       
  1568 			return queryId;
       
  1569 			}
       
  1570 		else
       
  1571 			{
       
  1572 			TInt index = QueryExists(aEntry);
       
  1573 			if(index<0) //Query is not Exist.
       
  1574 				{
       
  1575 				aEntry.iQueryID = iMaxQueryId + 1;
       
  1576 				iMaxQueryId = aEntry.iQueryID ;
       
  1577 				return aEntry.iQueryID;
       
  1578 				}
       
  1579 			else //Query Exist
       
  1580 				{
       
  1581 				queryId = (*iManagerEntry)[index]->iQueryID;
       
  1582 				aEntry.iQueryID = queryId;
       
  1583 				return queryId;
       
  1584 				}
       
  1585 			}
       
  1586 		
       
  1587 		}
       
  1588 	else
       
  1589 		{
       
  1590 		return -1;// EMsvSearchSortCacheMangerNotCreated
       
  1591 		}
       
  1592 	}
       
  1593 
       
  1594 
       
  1595 /**
       
  1596 * ReturnProgressInfo() : 
       
  1597 * @param None
       
  1598 * @return TInt.
       
  1599 */
       
  1600 
       
  1601 TInt  CMSvSearchSortCacheManager::ReturnProgressInfo() const
       
  1602 	{
       
  1603 	return iProgress;
       
  1604 	}
       
  1605 
       
  1606 
       
  1607 /**
       
  1608 * CancelOperation()  
       
  1609 * @param None
       
  1610 * @return Tint.
       
  1611 */
       
  1612 
       
  1613 TInt CMSvSearchSortCacheManager::CancelSearchSortOperation() const
       
  1614 	{
       
  1615 	iMsvSearchSortCacheManager->iCancelFlag = ETrue;	
       
  1616 	if(iOutstandingSOSOperations>0)
       
  1617 		{
       
  1618 		iOutstandingSOSOperations--;
       
  1619 		}
       
  1620 	return 0;
       
  1621 	}
       
  1622 
       
  1623 
       
  1624 /**
       
  1625 * RetrunQuery()  
       
  1626 * @param TMsvSearchSortQuery&
       
  1627 * @return None.
       
  1628 */
       
  1629 
       
  1630 void CMSvSearchSortCacheManager::RetrunQuery(TInt aIndex, CMsvSearchSortQuery* aCLientQuery)
       
  1631 	{
       
  1632 
       
  1633 	// iParentId;
       
  1634 	aCLientQuery->iParentId = (*iManagerEntry)[aIndex]->iParentId ;
       
  1635 	//for case sensitive search
       
  1636 	if((*iManagerEntry)[aIndex]->IsCaseSensetive())
       
  1637 		aCLientQuery->iCaseSensitive = ETrue;
       
  1638 	else
       
  1639 		aCLientQuery->iCaseSensitive = EFalse;
       
  1640 	
       
  1641 	//for whole world search
       
  1642 	if((*iManagerEntry)[aIndex]->IsWholeWord())
       
  1643 		aCLientQuery->iWholeWord = ETrue;
       
  1644 	else
       
  1645 		aCLientQuery->iWholeWord = EFalse;
       
  1646 	
       
  1647 	//for Wild Character 
       
  1648 	if((*iManagerEntry)[aIndex]->IsWildCharacter())
       
  1649 		aCLientQuery->iWildCardCharacter = ETrue;
       
  1650 	else
       
  1651 		aCLientQuery->iWildCardCharacter = EFalse;
       
  1652 	
       
  1653 	//for counting level of search
       
  1654 	aCLientQuery->iQueryCount = (*iManagerEntry)[aIndex]->iMsgQuery.Count();
       
  1655 	
       
  1656 
       
  1657 	//explicit sort on searched results
       
  1658 	if((*iManagerEntry)[aIndex]->IsExplicitSortReq())
       
  1659 		{
       
  1660 		aCLientQuery->iExplicitSort = ETrue;
       
  1661 		aCLientQuery->iSortOnMessagePart = (*iManagerEntry)[aIndex]->iMsgExplicitSortPart;	
       
  1662 		}
       
  1663 	else
       
  1664 		{
       
  1665 		aCLientQuery->iExplicitSort = EFalse;
       
  1666 		}
       
  1667 	
       
  1668 	//to check whether is this search query 
       
  1669 	if((*iManagerEntry)[aIndex]->IsSortQuery())
       
  1670 		{
       
  1671 		aCLientQuery->iIsSearchQuery = ETrue;
       
  1672 		aCLientQuery->iSortOnMessagePart = (*iManagerEntry)[aIndex]->iMsgExplicitSortPart;	
       
  1673 		}
       
  1674 	else
       
  1675 		aCLientQuery->iIsSearchQuery = EFalse;
       
  1676 		
       
  1677 	// to set result type
       
  1678 	aCLientQuery->iSearchSortResultType = (*iManagerEntry)[aIndex]->iResultType;
       
  1679 	
       
  1680 	// flag for sub folder search
       
  1681 	aCLientQuery->iSubFolderSearch = (*iManagerEntry)[aIndex]->iSubFolderOption;
       
  1682 	
       
  1683 	// Sort option
       
  1684 	if((*iManagerEntry)[aIndex]->IsAscendingSort())
       
  1685 		{
       
  1686 		aCLientQuery->iSortOrder = EMsvSortAscending;
       
  1687 		}
       
  1688 	else
       
  1689 		aCLientQuery->iSortOrder = EMsvSortDescending;
       
  1690 	
       
  1691 	//queryId
       
  1692 	aCLientQuery->iQueryId = (*iManagerEntry)[aIndex]->iQueryID;
       
  1693 	
       
  1694 	TInt ii = (*iManagerEntry)[aIndex]->iMsgQuery.Count();
       
  1695 	for (aCLientQuery->iQueryCount = 0; aCLientQuery->iQueryCount < ii ; aCLientQuery->iQueryCount++)
       
  1696 		{
       
  1697 		aCLientQuery->iQueryTable[aCLientQuery->iQueryCount].iQueryString.Copy((*iManagerEntry)[aIndex]->iQueryData.iQueryString);
       
  1698 		aCLientQuery->iQueryTable[aCLientQuery->iQueryCount].iMessagePart = (*iManagerEntry)[aIndex]->iQueryData.iMessagePart ;
       
  1699 		aCLientQuery->iQueryTable[aCLientQuery->iQueryCount].iRelationOp = (*iManagerEntry)[aIndex]->iQueryData.iRelationOp;
       
  1700 		}
       
  1701 	}
       
  1702 
       
  1703 TSearchSortDbWrapper* CMSvSearchSortCacheManager::GetDbWrapper()
       
  1704 	{
       
  1705 	return iSearchDbAdapter;
       
  1706 	}
       
  1707 
       
  1708 //For Simultaneously query
       
  1709 void CMSvSearchSortCacheManager::AddOutstandingSOSOperation()
       
  1710     {
       
  1711     iOutstandingSOSOperations++;
       
  1712     }
       
  1713 
       
  1714 TInt CMSvSearchSortCacheManager::OutstandingSOSOperations()
       
  1715     {
       
  1716     return iOutstandingSOSOperations;
       
  1717     }
       
  1718  
       
  1719 
       
  1720