predictivesearch/PcsAlgorithm/Algorithm1/src/CPcsCache.cpp
branchRCL_3
changeset 39 a6539d1e8e43
parent 32 2828b4d142c0
child 45 34879f5cfc63
equal deleted inserted replaced
35:4ae315f230bc 39:a6539d1e8e43
    21 */
    21 */
    22 
    22 
    23 
    23 
    24 // INCLUDE FILES
    24 // INCLUDE FILES
    25 #include <MVPbkContactLink.h>
    25 #include <MVPbkContactLink.h>
    26 
    26 #include <vpbkeng.rsg>
       
    27 #include "CPcsDefs.h"
    27 #include "CPsData.h"
    28 #include "CPsData.h"
    28 #include "CPcsCache.h"
    29 #include "CPcsCache.h"
    29 #include "CPcsDebug.h"
    30 #include "CPcsDebug.h"
    30 #include "CWords.h"
    31 #include "CWords.h"
    31 #include "CPcsAlgorithm1Utils.h"
    32 #include "CPcsAlgorithm1Utils.h"
    32 
    33 #include "CPcsAdaptiveGridItem.h"
    33 
    34 
    34 // ============================== MEMBER FUNCTIONS ============================
    35 // ============================== MEMBER FUNCTIONS ============================
    35 
    36 
    36 // ----------------------------------------------------------------------------
    37 // ----------------------------------------------------------------------------
    37 // CPcsCache::NewL
    38 // CPcsCache::NewL
    85     for(TInt i= 0; i <aKeyMap.PoolCount();i++ )
    86     for(TInt i= 0; i <aKeyMap.PoolCount();i++ )
    86         {
    87         {
    87         RPointerArray<CPcsPoolElement> *keyMap = new (ELeave) RPointerArray<CPcsPoolElement>(1);
    88         RPointerArray<CPcsPoolElement> *keyMap = new (ELeave) RPointerArray<CPcsPoolElement>(1);
    88         iKeyArr.InsertL(keyMap,i);
    89         iKeyArr.InsertL(keyMap,i);
    89         }
    90         }
    90    
    91 
       
    92     // Adaptive Grid map
       
    93     iAdaptiveGridMap = CPcsAdaptiveGrid::NewL( );
    91     
    94     
    92     PRINT ( _L("End CPcsCache::ConstructL") );
    95     PRINT ( _L("End CPcsCache::ConstructL") );
    93 }
    96 }
    94 
    97 
    95 // ----------------------------------------------------------------------------
    98 // ----------------------------------------------------------------------------
   107     iKeyArr.ResetAndDestroy();
   110     iKeyArr.ResetAndDestroy();
   108     iDataFields.Reset();
   111     iDataFields.Reset();
   109     iSortOrder.Reset();
   112     iSortOrder.Reset();
   110     iIndexOrder.Reset();
   113     iIndexOrder.Reset();
   111     iMasterPoolBackup.Close();
   114     iMasterPoolBackup.Close();
       
   115 
       
   116     delete iAdaptiveGridMap;
   112 
   117 
   113     PRINT ( _L("End CPcsCache::~CPcsCache") );
   118     PRINT ( _L("End CPcsCache::~CPcsCache") );
   114 }
   119 }
   115 
   120 
   116 // ----------------------------------------------------------------------------
   121 // ----------------------------------------------------------------------------
   153 // CPcsCache::AddToPool
   158 // CPcsCache::AddToPool
   154 // Adds a contact to cache
   159 // Adds a contact to cache
   155 // ----------------------------------------------------------------------------
   160 // ----------------------------------------------------------------------------
   156 void CPcsCache::AddToPoolL(TUint64& aPoolMap, CPsData& aData)
   161 void CPcsCache::AddToPoolL(TUint64& aPoolMap, CPsData& aData)
   157 {
   162 {
   158      // Temp hash to remember the location of pool elements
   163     // Temp hash to remember the location of pool elements
   159      // First TInt  = Pool 
   164     // First TInt  = Pool 
   160      // Second TInt = Location in the pool
   165     // Second TInt = Location in the pool
   161      // Required for memory optimization so that more than one pool
   166     // Required for memory optimization so that more than one pool
   162      // element doesn't get added for the same data
   167     // element doesn't get added for the same data
   163      RHashMap<TInt, TInt> elementHash;
   168     RHashMap<TInt, TInt> elementHash;
   164      CleanupClosePushL( elementHash );
   169     CleanupClosePushL( elementHash );
   165      TLinearOrder<CPcsPoolElement> rule( CPcsPoolElement::CompareByData );
   170     TLinearOrder<CPcsPoolElement> rule( CPcsPoolElement::CompareByData );
   166 
   171 
   167      // Parse thru each data element
   172     TBool unnamed = ETrue;
   168      for ( TInt dataIndex = 0; dataIndex < aData.DataElementCount(); dataIndex++ )
   173     // Parse thru each data element
   169      {
   174     for ( TInt dataIndex = 0; dataIndex < aData.DataElementCount(); dataIndex++ )
       
   175     {
       
   176         HBufC* dataStr = aData.Data(dataIndex);
       
   177 
   170         // Find store all the pool IDs where this contact should be
   178         // Find store all the pool IDs where this contact should be
   171 		RArray<TUint> poolIds;
   179 		RArray<TUint> poolIds;
   172 		CleanupClosePushL( poolIds );
   180 		CleanupClosePushL( poolIds );
   173 		
   181 		
   174 		// Recover the first character
   182 		// Recover the first character
   175 		if ( aData.Data(dataIndex) && aData.Data(dataIndex)->Length() != 0 )
   183 		if ( dataStr && dataStr->Length() != 0 )
   176 		{
   184 		{
   177 		    // Split the data into words
   185 		    // Split the data into words
   178 		    CWords* words = CWords::NewLC(*aData.Data(dataIndex));
   186 		    CWords* words = CWords::NewLC(*dataStr);
   179 
   187 
   180 		    // Store the first numeric key for each word
   188 		    // Store the first numeric key for each word
   181 		    for ( TInt i = 0; i < words->MdcaCount(); i++ )
   189 		    for ( TInt i = 0; i < words->MdcaCount(); i++ )
   182 		    {
   190 		    {
   183 		    	TChar firstChar = (words->MdcaPoint(i))[0];
   191 		    	TChar firstChar = (words->MdcaPoint(i))[0];
   199                     if ( qwertyPoolId != KErrNotFound )
   207                     if ( qwertyPoolId != KErrNotFound )
   200                         {
   208                         {
   201                         poolIds.Append(qwertyPoolId);
   209                         poolIds.Append(qwertyPoolId);
   202                         }
   210                         }
   203                     }
   211                     }
       
   212 
       
   213 #ifdef _DEBUG 
       
   214                 PRINT2 ( _L("CPcsCache::AddToPoolL: iURI=\"%S\", iUriId=%d"), &*iURI, iUriId );
       
   215                 TPtrC16 ptr = words->MdcaPoint(i); 
       
   216                 PRINT5 ( _L("CPcsCache::AddToPoolL: Data[%d]=\"%S\", word=\"%S\", firstChar='%c', Match=%d" ),
       
   217                          dataIndex, &*dataStr, &(ptr), (TUint) firstChar, aData.IsDataMatch(dataIndex) );
       
   218 #endif // _DEBUG
       
   219 
       
   220                 // Set the 1st char of the word for the Adaptive Grid or
       
   221                 // increment the reference counter
       
   222                 TUint selector = GridItemSelector( dataIndex, unnamed );
       
   223                 iAdaptiveGridMap->IncrementRefCounterL( firstChar, selector );
   204 		    }
   224 		    }
   205 		    
   225 		    
   206 		    CleanupStack::PopAndDestroy(words); 
   226 		    CleanupStack::PopAndDestroy(words); 
   207 		}
   227 		}
   208 		
   228 		
   249 			
   269 			
   250 		} // for 2 loop
   270 		} // for 2 loop
   251 		
   271 		
   252 		CleanupStack::PopAndDestroy( &poolIds );
   272 		CleanupStack::PopAndDestroy( &poolIds );
   253 		
   273 		
   254      } // for 1 loop
   274     } // for 1 loop
   255      
   275 
   256      CleanupStack::PopAndDestroy( &elementHash );
   276     CleanupStack::PopAndDestroy( &elementHash );
   257 }
   277 }
   258 
   278 
   259 // ---------------------------------------------------------------------
   279 // ---------------------------------------------------------------------
   260 // CPcsCache::AddToCacheL
   280 // CPcsCache::AddToCacheL
   261 // 
   281 // 
   301 
   321 
   302         if ( !present )
   322         if ( !present )
   303         {
   323         {
   304         	continue;
   324         	continue;
   305         }
   325         }
   306 
       
   307         const RPointerArray<CPcsPoolElement>& tmpKeyMap = *(iKeyArr[keyIndex]);
       
   308         for ( TInt arrayIndex = 0; 
       
   309               arrayIndex < tmpKeyMap.Count();
       
   310               arrayIndex++ )
       
   311         {
       
   312 		    CPcsPoolElement *element = tmpKeyMap[arrayIndex];
       
   313 		    TInt id = element->GetPsData()->Id();
       
   314 		    if ( id == aItemId )
       
   315 		    {
       
   316 		        data = element->GetPsData();
       
   317 		    	delete element;
       
   318 		    	iKeyArr[keyIndex]->Remove(arrayIndex);  
       
   319 		    }
       
   320         }
       
   321     }
       
   322         
   326         
       
   327         TInt arrayIndex = 0;
       
   328         TInt arrayCount = iKeyArr[keyIndex]->Count();
       
   329         while ( arrayIndex < arrayCount )
       
   330         {
       
   331             CPcsPoolElement* element = (*(iKeyArr[keyIndex]))[arrayIndex];
       
   332             if ( element->GetPsData()->Id() == aItemId )
       
   333             {
       
   334                 data = element->GetPsData();
       
   335                 delete element;
       
   336                 iKeyArr[keyIndex]->Remove(arrayIndex);
       
   337                 arrayCount--;
       
   338             }
       
   339             else
       
   340             {
       
   341                 arrayIndex++;
       
   342             }
       
   343         }
       
   344     }
       
   345 
   323     // Remove this element from master pool
   346     // Remove this element from master pool
   324     TInt arrayIndex = 0;
   347     TInt arrayIndex = 0;
   325     while ( arrayIndex < iMasterPool.Count() )
   348     TInt arrayCount = iMasterPool.Count();
       
   349     while ( arrayIndex < arrayCount )
   326     {
   350     {
   327         if ( iMasterPool[arrayIndex]->Id() == aItemId )
   351         if ( iMasterPool[arrayIndex]->Id() == aItemId )
   328         {
   352         {
   329             iMasterPool.Remove(arrayIndex);  
   353             iMasterPool.Remove(arrayIndex);
       
   354             arrayCount--;
   330         }
   355         }
   331         else
   356         else
   332         {
   357         {
   333             arrayIndex++;
   358             arrayIndex++;
   334         }
   359         }
   335     }
   360     }
   336     
   361     
       
   362     if ( data )
       
   363     {
       
   364         TBool unnamed = ETrue;
       
   365         // Parse thru each data element
       
   366         for ( TInt dataIndex = 0; dataIndex < data->DataElementCount(); dataIndex++ )
       
   367         {
       
   368             HBufC* dataStr = data->Data(dataIndex);
       
   369     
       
   370             // Recover the first character
       
   371             if ( dataStr && dataStr->Length() != 0 )
       
   372             {
       
   373                 // Split the data into words
       
   374                 CWords* words = CWords::NewLC(*dataStr);
       
   375     
       
   376                 // Store the first numeric key for each word
       
   377                 for ( TInt i = 0; i < words->MdcaCount(); i++ )
       
   378                 {
       
   379                     TChar firstChar = (words->MdcaPoint(i))[0];
       
   380     
       
   381 #ifdef _DEBUG
       
   382                     PRINT2 ( _L("CPcsCache::RemoveFromCacheL: iURI=\"%S\", iUriId=%d"), &*iURI, iUriId );
       
   383                     TPtrC16 ptr = words->MdcaPoint(i); 
       
   384                     PRINT5 ( _L("CPcsCache::RemoveFromCacheL: Data[%d]=\"%S\", word=\"%S\", firstChar='%c', Match=%d" ),
       
   385                              dataIndex, &*dataStr, &(ptr), (TUint) firstChar, data->IsDataMatch(dataIndex) );
       
   386 #endif // _DEBUG
       
   387     
       
   388                     // Decrement the reference counter of the 1st char of the word for the Adaptive Grid or
       
   389                     // delete the Adaptive Grid item if there are no references to it anymore
       
   390                     TUint selector = GridItemSelector( dataIndex, unnamed );
       
   391                     iAdaptiveGridMap->DecrementRefCounter( firstChar, selector );
       
   392                 }
       
   393     
       
   394                 CleanupStack::PopAndDestroy( words ); 
       
   395             }
       
   396         }
       
   397     }
       
   398 
   337     // Delete data 
   399     // Delete data 
   338     delete data;
   400     delete data;
   339     data = NULL;
   401     data = NULL;
   340 
   402 
   341     // Clear up cache information
   403     // Clear up cache information
   348 // ---------------------------------------------------------------------
   410 // ---------------------------------------------------------------------
   349 void CPcsCache::RemoveAllFromCache()
   411 void CPcsCache::RemoveAllFromCache()
   350 {
   412 {
   351     PRINT ( _L("Enter CPcsCache::RemoveAllFromCache") );
   413     PRINT ( _L("Enter CPcsCache::RemoveAllFromCache") );
   352     
   414     
   353     for ( TInt i = 0 ; i < iKeyArr.Count() ; i++ )
   415     for ( TInt i = 0; i < iKeyArr.Count(); i++ )
   354         {
   416         {
   355         iKeyArr[i]->ResetAndDestroy();
   417         iKeyArr[i]->ResetAndDestroy();
   356         }
   418         }
   357 	
   419 	
   358 	iMasterPool.ResetAndDestroy();
   420 	iMasterPool.ResetAndDestroy();
   359 	iCacheInfo.Close();
   421 	iCacheInfo.Close();
       
   422 
       
   423     delete iAdaptiveGridMap;
       
   424 	iAdaptiveGridMap = NULL;
   360 	
   425 	
   361 	PRINT ( _L("End CPcsCache::RemoveAllFromCache") );
   426 	PRINT ( _L("End CPcsCache::RemoveAllFromCache") );
       
   427 }
       
   428 
       
   429 // ---------------------------------------------------------------------
       
   430 // CPcsCache::GetAdaptiveGridL
       
   431 // 
       
   432 // ---------------------------------------------------------------------
       
   433 void CPcsCache::GetAdaptiveGridL( const TBool aCompanyName, TDes& aAdaptiveGrid )
       
   434 {
       
   435     PRINT ( _L("Enter CPcsCache::GetAdaptiveGridL") );
       
   436     
       
   437     iAdaptiveGridMap->GetAdaptiveGrid( aCompanyName, aAdaptiveGrid );
       
   438     
       
   439     PRINT ( _L("End CPcsCache::GetAdaptiveGridL") );
   362 }
   440 }
   363 
   441 
   364 // ---------------------------------------------------------------------
   442 // ---------------------------------------------------------------------
   365 // CPcsCache::SetPoolMap
   443 // CPcsCache::SetPoolMap
   366 // 
   444 // 
   557 // CPcsCache::ResortdataInPools
   635 // CPcsCache::ResortdataInPools
   558 // 
   636 // 
   559 // ---------------------------------------------------------------------
   637 // ---------------------------------------------------------------------
   560 void CPcsCache::ResortdataInPoolsL()
   638 void CPcsCache::ResortdataInPoolsL()
   561     {
   639     {
   562     // copy iMasterPool data into iMasterPoolBackup
   640     // Copy iMasterPool data into iMasterPoolBackup
   563     for (TInt i = 0; i < iMasterPool.Count(); i++ )
   641     for (TInt i = 0; i < iMasterPool.Count(); i++ )
   564         {
   642         {
   565         iMasterPoolBackup.Append( iMasterPool[i] );
   643         iMasterPoolBackup.Append( iMasterPool[i] );
   566         }
   644         }
   567     //Now reset the key array
   645 
       
   646     // Now reset the key array
   568     for (TInt i = 0; i < iKeyArr.Count(); i++ )
   647     for (TInt i = 0; i < iKeyArr.Count(); i++ )
   569         {
   648         {
   570         iKeyArr[i]->ResetAndDestroy();
   649         iKeyArr[i]->ResetAndDestroy();
   571         }
   650         }
   572     iMasterPool.Reset();
   651     iMasterPool.Reset();
   573     iCacheInfo.Close();
   652     iCacheInfo.Close();
   574     //now add data again from the iMasterPoolBackup
   653 
       
   654     // Now add data again from the iMasterPoolBackup
   575     for (TInt i = 0; i < iMasterPoolBackup.Count(); i++ )
   655     for (TInt i = 0; i < iMasterPoolBackup.Count(); i++ )
   576         {
   656         {
   577         CPsData* temp = iMasterPoolBackup[i];
   657         CPsData* temp = iMasterPoolBackup[i];
   578         AddToCacheL( *temp );
   658         AddToCacheL( *temp );
   579         }
   659         }
   580     iMasterPoolBackup.Reset();
   660     iMasterPoolBackup.Reset();
   581     }
   661     }
   582 
   662 
       
   663 // ---------------------------------------------------------------------
       
   664 // CPcsCache::GridItemSelectorL
       
   665 // 
       
   666 // ---------------------------------------------------------------------
       
   667 TUint CPcsCache::GridItemSelector( TInt aIndex, TBool& aUnnamed )
       
   668     {
       
   669     __ASSERT_ALWAYS( iDataFields.Count() > aIndex, User::Panic(_L("CPcsCache"), KErrArgument ) );
       
   670 
       
   671     if ( iDataFields[aIndex] == R_VPBK_FIELD_TYPE_LASTNAME || iDataFields[aIndex] == R_VPBK_FIELD_TYPE_FIRSTNAME )
       
   672         {
       
   673         aUnnamed = EFalse;
       
   674         return CPcsAdaptiveGridItem::EFirstNameLastName;
       
   675         }
       
   676     else if ( iDataFields[aIndex] == R_VPBK_FIELD_TYPE_COMPANYNAME && !aUnnamed )
       
   677         {
       
   678         return CPcsAdaptiveGridItem::ECompanyName;
       
   679         }
       
   680     else if ( iDataFields[aIndex] == R_VPBK_FIELD_TYPE_COMPANYNAME )
       
   681         {
       
   682         return CPcsAdaptiveGridItem::EUnnamedCompanyName;
       
   683         }
       
   684     else
       
   685         {
       
   686         return CPcsAdaptiveGridItem::ENumberCounters;
       
   687         }
       
   688     }
       
   689 
   583 // End of file
   690 // End of file