predictivesearch/PcsAlgorithm/Algorithm1/src/CPcsCache.cpp
branchRCL_3
changeset 21 b3431bff8c19
parent 15 e8e3147d53eb
child 32 2828b4d142c0
equal deleted inserted replaced
15:e8e3147d53eb 21:b3431bff8c19
    69 // 2nd Phase Constructor
    69 // 2nd Phase Constructor
    70 // ----------------------------------------------------------------------------
    70 // ----------------------------------------------------------------------------
    71 void CPcsCache::ConstructL(const TDesC& aURI, CPcsKeyMap& aKeyMap, TUint8 aUriId)
    71 void CPcsCache::ConstructL(const TDesC& aURI, CPcsKeyMap& aKeyMap, TUint8 aUriId)
    72 {
    72 {
    73     PRINT ( _L("Enter CPcsCache::ConstructL") );
    73     PRINT ( _L("Enter CPcsCache::ConstructL") );
       
    74 
       
    75     PRINT2 ( _L("CPcsCache::ConstructL: aURI=%S, aUriId=%d"), &aURI, aUriId);
    74     
    76     
    75     iURI = aURI.AllocL();
    77     iURI = aURI.AllocL();
    76     iUriId = aUriId;
    78     iUriId = aUriId;
    77     //Update the caching status for this cache
    79     //Update the caching status for this cache
    78     iCacheStatus = ECachingNotStarted;
    80     iCacheStatus = ECachingNotStarted;
   108     iIndexOrder.Reset();
   110     iIndexOrder.Reset();
   109     iMasterPoolBackup.Close();
   111     iMasterPoolBackup.Close();
   110 
   112 
   111     PRINT ( _L("End CPcsCache::~CPcsCache") );
   113     PRINT ( _L("End CPcsCache::~CPcsCache") );
   112 }
   114 }
   113  
   115 
   114 // ----------------------------------------------------------------------------
   116 // ----------------------------------------------------------------------------
   115 // CPcsCache::GetContactsForKeyL
   117 // CPcsCache::GetContactsForKeyL
   116 // Get list of pool elements specific to a pool
   118 // Get list of pool elements specific to a pool
   117 // ----------------------------------------------------------------------------     
   119 // ----------------------------------------------------------------------------     
   118 void CPcsCache::GetContactsForKeyL(TInt aKeyId, RPointerArray<CPcsPoolElement>& aData)
   120 void CPcsCache::GetContactsForKeyL(TInt aKeyId, RPointerArray<CPcsPoolElement>& aData)
   119 {
   121     {
   120     PRINT ( _L("Enter CPcsCache::GetContactsForKeyL") );
   122     PRINT ( _L("Enter CPcsCache::GetContactsForKeyL") );
   121         	
   123     
   122 	const RPointerArray<CPcsPoolElement>& arr = *iKeyArr[aKeyId];
   124     if ( aKeyId >= 0 && aKeyId < iKeyArr.Count() )
   123 	for ( TInt i = 0; i < arr.Count(); i++ )
   125         {
   124 	{
   126         const RPointerArray<CPcsPoolElement>& arr = *iKeyArr[aKeyId];
   125 		CPcsPoolElement* value = arr[i];
   127         for ( TInt i = 0; i < arr.Count(); i++ )
   126         aData.AppendL(value);
   128             {
   127 	}
   129             aData.AppendL(arr[i]);
   128     
   130             }
   129 	PRINT ( _L("End CPcsCache::GetContactsForKeyL") );
   131         }
   130 }  
   132     
       
   133     PRINT ( _L("End CPcsCache::GetContactsForKeyL") );
       
   134     }
   131 
   135 
   132 // ----------------------------------------------------------------------------
   136 // ----------------------------------------------------------------------------
   133 // CPcsCache::GetAllContentsL
   137 // CPcsCache::GetAllContentsL
   134 // Get all data elements in this cache
   138 // Get all data elements in this cache
   135 // ----------------------------------------------------------------------------     
   139 // ----------------------------------------------------------------------------     
   137 {
   141 {
   138     PRINT ( _L("Enter CPcsCache::GetAllContentsL") );
   142     PRINT ( _L("Enter CPcsCache::GetAllContentsL") );
   139         	
   143         	
   140 	for ( TInt i = 0; i < iMasterPool.Count(); i++ )
   144 	for ( TInt i = 0; i < iMasterPool.Count(); i++ )
   141 	{
   145 	{
   142 		CPsData* value = iMasterPool[i];
   146         aData.AppendL(iMasterPool[i]);
   143         aData.AppendL(value);
       
   144 	}
   147 	}
   145     
   148     
   146 	PRINT ( _L("End CPcsCache::GetAllContentsL") );
   149 	PRINT ( _L("End CPcsCache::GetAllContentsL") );
   147 }  
   150 }
   148    
       
   149 
   151 
   150 // ----------------------------------------------------------------------------
   152 // ----------------------------------------------------------------------------
   151 // CPcsCache::AddToPool
   153 // CPcsCache::AddToPool
   152 // Adds a contact to cache
   154 // Adds a contact to cache
   153 // ----------------------------------------------------------------------------
   155 // ----------------------------------------------------------------------------
   154 void CPcsCache::AddToPoolL(TUint64& aPoolMap, CPsData& aData)
   156 void CPcsCache::AddToPoolL(TUint64& aPoolMap, CPsData& aData)
   155 {	
   157 {
   156      // Temp hash to remember the location of pool elements
   158      // Temp hash to remember the location of pool elements
   157      // First TInt  = Pool 
   159      // First TInt  = Pool 
   158      // Second TInt = Location in the pool
   160      // Second TInt = Location in the pool
   159      // Required for memory optimization so that more than one pool
   161      // Required for memory optimization so that more than one pool
   160      // element doesn't get added for the same data
   162      // element doesn't get added for the same data
   161      RHashMap<TInt, TInt> elementHash;
   163      RHashMap<TInt, TInt> elementHash;
   162      CleanupClosePushL( elementHash );
   164      CleanupClosePushL( elementHash );
   163      TLinearOrder<CPcsPoolElement> rule( CPcsPoolElement::CompareByData );
   165      TLinearOrder<CPcsPoolElement> rule( CPcsPoolElement::CompareByData );
   164               
   166 
   165      // Parse thru each data element
   167      // Parse thru each data element
   166      for ( TInt dataIndex = 0; dataIndex < aData.DataElementCount(); dataIndex++ )
   168      for ( TInt dataIndex = 0; dataIndex < aData.DataElementCount(); dataIndex++ )
   167      {     	
   169      {
   168      	// Find store all the pool IDs where this contact should be
   170         // Find store all the pool IDs where this contact should be
   169 		RArray<TUint> poolIds;
   171 		RArray<TUint> poolIds;
   170 		CleanupClosePushL( poolIds );
   172 		CleanupClosePushL( poolIds );
   171 		
   173 		
   172 		// Recover the first character
   174 		// Recover the first character
   173 		if ( aData.Data(dataIndex) && aData.Data(dataIndex)->Length() != 0 )
   175 		if ( aData.Data(dataIndex) && aData.Data(dataIndex)->Length() != 0 )
   174 		{
   176 		{
   175 		    // Split the data into words
   177 		    // Split the data into words
   176 		    CWords* words = CWords::NewLC(*aData.Data(dataIndex));
   178 		    CWords* words = CWords::NewLC(*aData.Data(dataIndex));
   177   
   179 
   178 		    // Store the first numeric key for each word
   180 		    // Store the first numeric key for each word
   179 		    for ( TInt i = 0; i < words->MdcaCount(); i++ )
   181 		    for ( TInt i = 0; i < words->MdcaCount(); i++ )
   180 		    {
   182 		    {
   181 		    	TChar firstChar = (words->MdcaPoint(i))[0];
   183 		    	TChar firstChar = (words->MdcaPoint(i))[0];
   182 		    	
   184 		    	
   183 		    	// Pool ID according to ITU-T mappings
   185 		    	// Pool ID according to ITU-T mappings
   184 		    	TInt itutPoolId = iKeyMap->PoolIdForCharacter(firstChar, EPredictiveItuT);
   186 		    	if ( iKeyMap->IsItutPredictiveAvailable() )
   185 		    	if ( itutPoolId != KErrNotFound )
       
   186 		    	    {
   187 		    	    {
   187                     poolIds.Append(itutPoolId);
   188                     TInt itutPoolId = iKeyMap->PoolIdForCharacter(firstChar, EPredictiveItuT);
       
   189                     if ( itutPoolId != KErrNotFound )
       
   190                         {
       
   191                         poolIds.Append(itutPoolId);
       
   192                         }
   188 		    	    }
   193 		    	    }
   189 		    	
   194 		    	
   190 		    	// Pool ID according to QWERTY mappings
   195 		    	// Pool ID according to QWERTY mappings
   191                 TInt qwertyPoolId = iKeyMap->PoolIdForCharacter(firstChar, EPredictiveQwerty);
   196                 if ( iKeyMap->IsQwertyPredictiveAvailable() )
   192                 if ( qwertyPoolId != KErrNotFound )
       
   193                     {
   197                     {
   194                     poolIds.Append(qwertyPoolId);
   198                     TInt qwertyPoolId = iKeyMap->PoolIdForCharacter(firstChar, EPredictiveQwerty);
       
   199                     if ( qwertyPoolId != KErrNotFound )
       
   200                         {
       
   201                         poolIds.Append(qwertyPoolId);
       
   202                         }
   195                     }
   203                     }
   196 		    }
   204 		    }
   197 		    
   205 		    
   198 		    CleanupStack::PopAndDestroy(words); 
   206 		    CleanupStack::PopAndDestroy(words); 
   199 		}
   207 		}
   200 		
   208 		
   201 		for ( TInt poolIdIndex = 0; poolIdIndex < poolIds.Count(); poolIdIndex++ )
   209 		for ( TInt poolIdIndex = 0; poolIdIndex < poolIds.Count(); poolIdIndex++ )
   202 		{
   210 		{
   203             TUint poolId = poolIds[ poolIdIndex ];
   211             TUint poolId = poolIds[poolIdIndex];
   204 		    CPcsPoolElement* element = NULL;
   212 		    CPcsPoolElement* element = NULL;
   205 		    
   213 		    
   206 		    // Check if an element already exists in the pool for this data
   214 		    // Check if an element already exists in the pool for this data
   207 		    TInt* loc = NULL;
   215 		    TInt* loc = NULL;
   208 		    loc = elementHash.Find(poolId);
   216 		    loc = elementHash.Find(poolId);
   265     AddToPoolL(poolMap, aData);
   273     AddToPoolL(poolMap, aData);
   266     iCacheInfo.InsertL(aData.Id(), poolMap);
   274     iCacheInfo.InsertL(aData.Id(), poolMap);
   267 
   275 
   268     // Include this element in master pool
   276     // Include this element in master pool
   269     TLinearOrder<CPsData> rule( CPcsAlgorithm1Utils::CompareDataBySortOrderL );
   277     TLinearOrder<CPsData> rule( CPcsAlgorithm1Utils::CompareDataBySortOrderL );
       
   278     
   270     iMasterPool.InsertInOrderAllowRepeatsL(&aData, rule);
   279     iMasterPool.InsertInOrderAllowRepeatsL(&aData, rule);
   271 }
   280 }
   272 
   281 
   273 // ---------------------------------------------------------------------
   282 // ---------------------------------------------------------------------
   274 // CPcsCache::RemoveContactL
   283 // CPcsCache::RemoveContactL
   275 // 
   284 // 
   276 // ---------------------------------------------------------------------
   285 // ---------------------------------------------------------------------
   277 void CPcsCache::RemoveFromCacheL( TInt aItemId )
   286 void CPcsCache::RemoveFromCacheL( TInt aItemId )
   278 {
   287 {
   279     CPsData *data = NULL;
   288     CPsData* data = NULL;
   280             
   289     
   281     TUint64* poolMap = iCacheInfo.Find(aItemId);
   290     TUint64* poolMap = iCacheInfo.Find(aItemId);
   282     
   291     
   283     if ( poolMap == NULL )
   292     if ( poolMap == NULL )
   284     {
   293     {
   285     	return;
   294     	return;
   287     
   296     
   288     // Remove this element from pools
   297     // Remove this element from pools
   289     for ( TInt keyIndex = 0; keyIndex < iKeyArr.Count(); keyIndex++ )
   298     for ( TInt keyIndex = 0; keyIndex < iKeyArr.Count(); keyIndex++ )
   290     {
   299     {
   291         TBool present = GetPoolMap(*poolMap, keyIndex); 
   300         TBool present = GetPoolMap(*poolMap, keyIndex); 
   292         
   301 
   293         if ( ! present )
   302         if ( !present )
   294         {
   303         {
   295         	continue;
   304         	continue;
   296         }
   305         }
   297 
   306 
   298         const RPointerArray<CPcsPoolElement>& tmpKeyMap = *(iKeyArr[keyIndex]);
   307         const RPointerArray<CPcsPoolElement>& tmpKeyMap = *(iKeyArr[keyIndex]);
   305 		    if ( id == aItemId )
   314 		    if ( id == aItemId )
   306 		    {
   315 		    {
   307 		        data = element->GetPsData();
   316 		        data = element->GetPsData();
   308 		    	delete element;
   317 		    	delete element;
   309 		    	iKeyArr[keyIndex]->Remove(arrayIndex);  
   318 		    	iKeyArr[keyIndex]->Remove(arrayIndex);  
   310 		    }  
   319 		    }
   311         }      	
   320         }
   312     };   
   321     }
   313     
   322         
   314     // Remove this element from master pool
   323     // Remove this element from master pool
   315     for ( TInt arrayIndex = 0; 
   324     TInt arrayIndex = 0;
   316               arrayIndex < iMasterPool.Count();
   325     while ( arrayIndex < iMasterPool.Count() )
   317               arrayIndex++ )
       
   318     {
   326     {
   319 	    CPsData *dataElement = iMasterPool[arrayIndex];
   327         if ( iMasterPool[arrayIndex]->Id() == aItemId )
   320 	    TInt id = dataElement->Id();
   328         {
   321 	    if ( id == aItemId )
   329             iMasterPool.Remove(arrayIndex);  
   322 	    {
   330         }
   323 	    	iMasterPool.Remove(arrayIndex);  
   331         else
   324 	    }  
   332         {
   325     }      	
   333             arrayIndex++;
   326      
   334         }
       
   335     }
       
   336     
   327     // Delete data 
   337     // Delete data 
   328     if ( data )
   338     delete data;
   329     {
   339     data = NULL;
   330     	delete data;
       
   331     	data = NULL;
       
   332     }
       
   333 
   340 
   334     // Clear up cache information
   341     // Clear up cache information
   335     iCacheInfo.Remove(aItemId);    
   342     iCacheInfo.Remove(aItemId);
   336 }
   343 }
   337 
   344 
   338 // ---------------------------------------------------------------------
   345 // ---------------------------------------------------------------------
   339 // CPcsCache::RemoveAllFromCache
   346 // CPcsCache::RemoveAllFromCache
   340 // 
   347 // 
   359 // 
   366 // 
   360 // ---------------------------------------------------------------------
   367 // ---------------------------------------------------------------------
   361 void CPcsCache::SetPoolMap(TUint64& aPoolMap, TInt aArrayIndex)
   368 void CPcsCache::SetPoolMap(TUint64& aPoolMap, TInt aArrayIndex)
   362 {
   369 {
   363     __ASSERT_DEBUG( aArrayIndex < 64, User::Panic(_L("CPcsCache"), KErrOverflow ) );
   370     __ASSERT_DEBUG( aArrayIndex < 64, User::Panic(_L("CPcsCache"), KErrOverflow ) );
   364     TUint64 val = 1 << aArrayIndex;
   371 
   365     aPoolMap |= val;
   372     /* Some platforms do not support 64 bits shift operations.
       
   373      * Split to two 32 bits operations.
       
   374      */
       
   375     
       
   376     TUint32 poolMapH = I64HIGH(aPoolMap);
       
   377     TUint32 poolMapL = I64LOW(aPoolMap);
       
   378     
       
   379     TUint32 valH = 0;
       
   380     TUint32 valL = 0;
       
   381     if (aArrayIndex < 32)
       
   382         {
       
   383         valL = 1 << aArrayIndex;
       
   384         }
       
   385     else
       
   386         {
       
   387         valH = 1 << (aArrayIndex-32);
       
   388         }
       
   389 
       
   390     poolMapH |= valH;
       
   391     poolMapL |= valL;
       
   392     
       
   393     aPoolMap = MAKE_TUINT64(poolMapH, poolMapL);
   366 }
   394 }
   367 
   395 
   368 // ---------------------------------------------------------------------
   396 // ---------------------------------------------------------------------
   369 // CPcsCache::GetPoolMap
   397 // CPcsCache::GetPoolMap
   370 // 
   398 // 
   371 // ---------------------------------------------------------------------
   399 // ---------------------------------------------------------------------
   372 TBool CPcsCache::GetPoolMap(TUint64& aPoolMap, TInt aArrayIndex)
   400 TBool CPcsCache::GetPoolMap(TUint64& aPoolMap, TInt aArrayIndex)
   373 {
   401 {
   374     __ASSERT_DEBUG( aArrayIndex < 64, User::Panic(_L("CPcsCache"), KErrOverflow ) );
   402     __ASSERT_DEBUG( aArrayIndex < 64, User::Panic(_L("CPcsCache"), KErrOverflow ) );
   375     TUint64 val = 1 << aArrayIndex;
   403 
   376     return (aPoolMap & val);
   404     /* Some platforms do not support 64 bits shift operations.
       
   405      * Split to two 32 bits operations.
       
   406      */
       
   407 
       
   408     TUint32 poolMapH = I64HIGH(aPoolMap);
       
   409     TUint32 poolMapL = I64LOW(aPoolMap);
       
   410     
       
   411     TUint32 valH = 0;
       
   412     TUint32 valL = 0;
       
   413     if (aArrayIndex < 32)
       
   414         {
       
   415         valL = 1 << aArrayIndex;
       
   416         }
       
   417     else
       
   418         {
       
   419         valH = 1 << (aArrayIndex-32);
       
   420         }
       
   421 
       
   422     TBool ret = (poolMapH & valH) || (poolMapL & valL);
       
   423 
       
   424     return (ret);
   377 }
   425 }
   378 
   426 
   379 // ---------------------------------------------------------------------
   427 // ---------------------------------------------------------------------
   380 // CPcsCache::GetURI
   428 // CPcsCache::GetURI
   381 // 
   429 // 
   432 // 
   480 // 
   433 // ---------------------------------------------------------------------
   481 // ---------------------------------------------------------------------
   434 TUint8 CPcsCache::GetUriId()
   482 TUint8 CPcsCache::GetUriId()
   435 {
   483 {
   436 	return iUriId;
   484 	return iUriId;
   437 }
       
   438 
       
   439 // ---------------------------------------------------------------------
       
   440 // CPcsCache::GetUri
       
   441 // 
       
   442 // ---------------------------------------------------------------------
       
   443 HBufC* CPcsCache::GetUri()
       
   444 {
       
   445 	return iURI;
       
   446 }
   485 }
   447 
   486 
   448 // ---------------------------------------------------------------------
   487 // ---------------------------------------------------------------------
   449 // CPcsCache::SetSortOrder
   488 // CPcsCache::SetSortOrder
   450 // 
   489 // 
   476 	for (TInt i(0); i < iSortOrder.Count(); i++)
   515 	for (TInt i(0); i < iSortOrder.Count(); i++)
   477 	{
   516 	{
   478 		aSortOrder.Append(iSortOrder[i]);
   517 		aSortOrder.Append(iSortOrder[i]);
   479 	}	
   518 	}	
   480 }
   519 }
   481 
       
   482 
   520 
   483 // ---------------------------------------------------------------------
   521 // ---------------------------------------------------------------------
   484 // CPcsCache::GetIndexOrder
   522 // CPcsCache::GetIndexOrder
   485 // 
   523 // 
   486 // ---------------------------------------------------------------------
   524 // ---------------------------------------------------------------------
   538         {
   576         {
   539         CPsData* temp = iMasterPoolBackup[i];
   577         CPsData* temp = iMasterPoolBackup[i];
   540         AddToCacheL( *temp );
   578         AddToCacheL( *temp );
   541         }
   579         }
   542     iMasterPoolBackup.Reset();
   580     iMasterPoolBackup.Reset();
   543     } 
   581     }
       
   582 
   544 // End of file
   583 // End of file