landmarks/locationlandmarks/server/src/EPos_CPosLmInitializer.cpp
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Provides initializing of landmark database
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <d32dbms.h>
       
    21 
       
    22 #include <epos_poslmcategoryhandler.h>
       
    23 #include <epos_cposlmglobalcategoryreader.h>
       
    24 #include <epos_landmarkdatabasestructure.h>
       
    25 #include <epos_poslmdatabaseutility.h>
       
    26 
       
    27 #include "epos_lmdebug.h"
       
    28 #include "EPos_PosLmServerCommon.h"
       
    29 #include "EPos_CPosLmServer.h"
       
    30 #include "EPos_CPosLmNameIndex.h"
       
    31 #include "epos_cposlmindexmanager.h"
       
    32 #include "epos_poslmserverutility.h"
       
    33 #include "EPos_CPosLmInitializer.h"
       
    34 
       
    35 const TInt KEstimatedNoOfGlobalCategories = 15;
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CPosLmInitializer::CPosLmInitializer( 
       
    43     CPosLmServer& aServer,
       
    44     MPosLmServerOperationObserver* aObserver)
       
    45 :   CPosLmServerOperation( aServer, aObserver )
       
    46     {
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 void CPosLmInitializer::ConstructL( const TDesC& aUri )
       
    53     {
       
    54     BaseConstructL(aUri);
       
    55 
       
    56     iGlCatReader = CPosLmGlobalCategoryReader::NewL();
       
    57     iGlobalCategoryCount = iGlCatReader->Count();
       
    58     
       
    59     iPhase = EPhaseRecovering;
       
    60     CompleteSelf();
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CPosLmInitializer* CPosLmInitializer::NewL(
       
    67     CPosLmServer& aServer,
       
    68     MPosLmServerOperationObserver* aObserver,
       
    69     const TDesC& aUri )
       
    70     {
       
    71     CPosLmInitializer* self = new( ELeave ) CPosLmInitializer( aServer, aObserver );
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL( aUri );
       
    74     CleanupStack::Pop( self );
       
    75     return self;
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CPosLmInitializer::~CPosLmInitializer()
       
    82     {
       
    83     delete iGlCatReader;
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void CPosLmInitializer::RunL()
       
    90     {
       
    91     if ( iStatusFlag == KPosLmOperationNotComplete )
       
    92         {
       
    93         NextStepL(); // perform one more step
       
    94         }
       
    95 
       
    96     if ( iStatusFlag == KPosLmOperationNotComplete )
       
    97         {
       
    98         CompleteSelf(); // continue executing
       
    99         }
       
   100     // else - stop executing, operation complete
       
   101     
       
   102     NotifyClients();
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 TInt CPosLmInitializer::RunError( TInt aError )
       
   109     {
       
   110     if ( aError == KErrDiskFull &&
       
   111          ( iPhase == EPhaseCreatingLmFieldIndex || 
       
   112            iPhase == EPhaseCreatingLmCatIndex ) )
       
   113         {
       
   114         iPhase++;       // ignore these phases
       
   115         CompleteSelf(); // and continue
       
   116         return KErrNone;
       
   117         }
       
   118     else
       
   119         {
       
   120         return CPosLmServerOperation::RunError( aError );
       
   121         }
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 void CPosLmInitializer::NextStepL()
       
   128     {
       
   129     TReal32 phaseProgress = 0.0;
       
   130     switch ( iPhase )
       
   131         {
       
   132         case EPhaseRecovering:
       
   133             phaseProgress = RecoveryNextStepL();
       
   134             break;
       
   135         case EPhaseCreateCatsStart:
       
   136         case EPhaseCreatingCats:
       
   137             phaseProgress = CreateCategoriesNextStepL();
       
   138             break;
       
   139         case EPhaseUpdateCatsStart:
       
   140         case EPhaseUpdatingCats:
       
   141             phaseProgress = UpdateCategoriesNextStepL();
       
   142             break;
       
   143         case EPhaseCreatingLmFieldIndex:
       
   144             phaseProgress = CreateLmFieldIndexNextStepL();
       
   145             break;
       
   146         case EPhaseCreatingLmCatIndex:
       
   147             phaseProgress = CreateLmCatIndexNextStepL();
       
   148             break;
       
   149         case EPhaseCompacting:
       
   150             phaseProgress = CompactNextStepL();
       
   151             break;
       
   152         case EPhaseBuildingLmNameIndex:
       
   153             phaseProgress = BuildLmNameIndexNextStepL();
       
   154             break;
       
   155         default:
       
   156             break;
       
   157         }
       
   158     
       
   159     UpdateTotalProgress( phaseProgress );
       
   160 
       
   161     if ( phaseProgress >= KOperationDone )
       
   162         {
       
   163         LOG3("Initializer: phase %d, progress %5.2f ( db '%S')", 
       
   164             iPhase, iProgress, iDbUri );
       
   165         iPhase++;
       
   166         }
       
   167     
       
   168     if ( iPhase == EPhaseDone )
       
   169         {
       
   170         iProgress = KOperationDone;
       
   171         iStatusFlag = KErrNone;
       
   172         }
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 TReal32 CPosLmInitializer::CreateCategoriesNextStepL()
       
   179     {
       
   180     TReal32 progress = 0.0;
       
   181     
       
   182 
       
   183     switch ( iPhase )
       
   184         {
       
   185         case EPhaseCreateCatsStart:
       
   186             {
       
   187             TBool initNeeded( EFalse );
       
   188             User::LeaveIfError( iDb->IsInitializeNeededL( initNeeded ) );
       
   189 
       
   190             if ( initNeeded )
       
   191                 {
       
   192                 iCategoryCounter = 0;
       
   193                 RemoveAllGlobalCategoriesL();
       
   194                 }
       
   195             else
       
   196                 {
       
   197                 progress = KOperationDone;
       
   198                 }
       
   199 
       
   200             iPhase = EPhaseCreatingCats;
       
   201             break;
       
   202             }
       
   203         case EPhaseCreatingCats:
       
   204             {
       
   205             if ( iCategoryCounter < iGlobalCategoryCount )
       
   206                 {
       
   207                 // Add global category to database
       
   208                 CPosLandmarkCategory* category =
       
   209                     iGlCatReader->GlobalCategoryLC( iCategoryCounter );
       
   210 
       
   211                 iDiskUtilities->DiskSpaceBelowCriticalLevelL(
       
   212                     iDiskUtilities->EstimatedDiskSizeOfLmOperation(
       
   213                     CPosLmDiskUtilities::EAddLmCategoryOp, *category ), iDbDrive );
       
   214 
       
   215                 PosLmCategoryHandler::AddCategoryL( *iDb, *category );
       
   216                 CleanupStack::PopAndDestroy( category );
       
   217                 
       
   218                 // Increase counter and calculate progress
       
   219                 ++iCategoryCounter;
       
   220                 progress = TReal32( iCategoryCounter ) / TReal32( iGlobalCategoryCount );
       
   221                 }
       
   222             break;
       
   223             }
       
   224         default:
       
   225             break;
       
   226         }
       
   227 
       
   228     // Check whether the operation is done or not
       
   229     if ( iCategoryCounter >= iGlobalCategoryCount || iGlobalCategoryCount == 0 )
       
   230     {
       
   231     	progress = KOperationDone;
       
   232     	iDb->SetLanguageL( User::Language() );
       
   233     }
       
   234 
       
   235 	if(progress == KOperationDone)
       
   236 	{
       
   237 		iDb->SetInitializeDoneL();
       
   238 	}
       
   239     
       
   240     
       
   241     return progress;
       
   242     }
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 TReal32 CPosLmInitializer::UpdateCategoriesNextStepL()
       
   248     {
       
   249     TReal32 progress = 0.0;
       
   250     switch ( iPhase )
       
   251         {
       
   252         case EPhaseUpdateCatsStart:
       
   253             {
       
   254             TBool langChangeNeeded( EFalse );
       
   255             iDb->IsLanguageChangeNeededL( langChangeNeeded );
       
   256 
       
   257             if ( langChangeNeeded )
       
   258                 {
       
   259                 iCategoryCounter = 0;
       
   260                 if ( iGlobalCategoryCount == 0 )
       
   261                     {
       
   262                     // There are no global categories defined
       
   263                     // so remove them all from database
       
   264                     RemoveAllGlobalCategoriesL();
       
   265                     progress = KOperationDone;
       
   266                     }
       
   267                 }
       
   268             else
       
   269                 {
       
   270                 progress = KOperationDone;
       
   271                 }
       
   272             iPhase = EPhaseUpdatingCats;
       
   273             break;
       
   274             }
       
   275         case EPhaseUpdatingCats:
       
   276             {
       
   277             if ( iCategoryCounter < iGlobalCategoryCount )
       
   278                 {
       
   279                 // Update global category in database
       
   280                 CPosLandmarkCategory* category = iGlCatReader->GlobalCategoryLC( iCategoryCounter );
       
   281             
       
   282                 iDiskUtilities->DiskSpaceBelowCriticalLevelL(
       
   283                     iDiskUtilities->EstimatedDiskSizeOfLmOperation(
       
   284                     CPosLmDiskUtilities::EUpdateLmCategoryOp, *category ), iDbDrive );
       
   285             
       
   286                 PosLmCategoryHandler::UpdateGlobalCategoryNameL( *iDb, *category );
       
   287                 CleanupStack::PopAndDestroy( category );
       
   288             
       
   289                 // Increase counter and calculate progress
       
   290                 ++iCategoryCounter;
       
   291                 progress = TReal32( iCategoryCounter ) / TReal32( iGlobalCategoryCount );
       
   292                 }
       
   293             break;
       
   294             }
       
   295         default:    
       
   296             break;
       
   297         }
       
   298     
       
   299     // Check whether the operation is done or not
       
   300     if ( iCategoryCounter >= iGlobalCategoryCount || iGlobalCategoryCount == 0 )
       
   301         {
       
   302         progress = KOperationDone;
       
   303         }
       
   304     
       
   305     if ( progress == KOperationDone )
       
   306         {
       
   307         iDb->SetLanguageL( User::Language() );
       
   308         }
       
   309 
       
   310     return progress;
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 void CPosLmInitializer::RemoveAllGlobalCategoriesL()
       
   317     {
       
   318     iDiskUtilities->DiskSpaceBelowCriticalLevelL(
       
   319         iDiskUtilities->EstimatedDiskSizeOfLmOperation(
       
   320         CPosLmDiskUtilities::ERemoveLmCategoryOp,
       
   321         TPosLmItemId()) * KEstimatedNoOfGlobalCategories, iDbDrive);
       
   322     
       
   323     PosLmCategoryHandler::RemoveAllGlobalCategoriesL( *iDb );
       
   324     }
       
   325 
       
   326 // -----------------------------------------------------------------------------
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 TReal32 CPosLmInitializer::CreateLmFieldIndexNextStepL()
       
   330     {
       
   331     if ( !iIncrementalStarted )
       
   332         {
       
   333         iDiskUtilities->DiskSpaceBelowCriticalLevelL(
       
   334             PosLmServerUtility::EstimatedDiskSizeOfIndex(
       
   335                 PosLmServerUtility::ECreateFieldsLmIdIndex,
       
   336                 PosLmServerUtility::GetTableRowCountL( *iDb, KPosLmLandmarkFieldTable ) ), 
       
   337             iDbDrive );
       
   338 
       
   339         RDbNamedDatabase database;
       
   340         iDb->GetDatabase( database );
       
   341 
       
   342         if ( !PosLmServerUtility::IndexExistsL( database, KPosLmLandmarkFieldTable, KPosLmFieldsLmIdIndex ) )
       
   343             {
       
   344             CDbKey* key = CDbKey::NewLC();
       
   345             key->AddL( TDbKeyCol( KPosLmLandmarkIdCol ) );
       
   346     
       
   347             iDbIncremental.CreateIndex( 
       
   348                 database,
       
   349                 KPosLmFieldsLmIdIndex,
       
   350                 KPosLmLandmarkFieldTable,
       
   351                 *key,
       
   352                 iIncrementalStep );
       
   353             CleanupStack::PopAndDestroy( key );
       
   354 
       
   355             iIncrementalStartValue = iIncrementalStep;
       
   356             iIncrementalStarted = ETrue;
       
   357             iCompactRequired = ETrue;
       
   358             }
       
   359         else
       
   360             {
       
   361             return KOperationDone;
       
   362             }
       
   363         }
       
   364 
       
   365     return IncrementalStepL();
       
   366     }
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 TReal32 CPosLmInitializer::CreateLmCatIndexNextStepL()
       
   372     {
       
   373     if ( !iIncrementalStarted )
       
   374         {
       
   375         iDiskUtilities->DiskSpaceBelowCriticalLevelL(
       
   376             PosLmServerUtility::EstimatedDiskSizeOfIndex(
       
   377                 PosLmServerUtility::ECreateCategoriesLmIdIndex,
       
   378                 PosLmServerUtility::GetTableRowCountL( *iDb, KPosLmLandmarkCategoryTable ) ), 
       
   379             iDbDrive );
       
   380 
       
   381         RDbNamedDatabase database;
       
   382         iDb->GetDatabase( database );
       
   383 
       
   384         if ( !PosLmServerUtility::IndexExistsL( database, KPosLmLandmarkCategoryTable, KPosLmCategoryLmIdIndex ) )
       
   385             {
       
   386             CDbKey* key = CDbKey::NewLC();
       
   387             key->AddL( TDbKeyCol( KPosLmLandmarkIdCol ) );
       
   388     
       
   389             iDbIncremental.CreateIndex( 
       
   390                 database,
       
   391                 KPosLmCategoryLmIdIndex,
       
   392                 KPosLmLandmarkCategoryTable,
       
   393                 *key,
       
   394                 iIncrementalStep );
       
   395             CleanupStack::PopAndDestroy( key );
       
   396 
       
   397             iIncrementalStartValue = iIncrementalStep;
       
   398             iIncrementalStarted = ETrue;
       
   399             iCompactRequired = ETrue;
       
   400             }
       
   401         else
       
   402             {
       
   403             return KOperationDone;
       
   404             }
       
   405         }
       
   406 
       
   407     return IncrementalStepL();
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 TReal32 CPosLmInitializer::RecoveryNextStepL()
       
   414     {
       
   415     if ( !iIncrementalStarted )
       
   416         {
       
   417         RDbNamedDatabase database;
       
   418         iDb->GetDatabase( database );
       
   419 
       
   420         if ( database.IsDamaged() )
       
   421             {
       
   422             User::LeaveIfError( iDbIncremental.Recover( database, iIncrementalStep ) );
       
   423             iIncrementalStartValue = iIncrementalStep;
       
   424             iIncrementalStarted = ETrue;
       
   425             }
       
   426         else
       
   427             {
       
   428             return KOperationDone;
       
   429             }
       
   430         }
       
   431 
       
   432     return IncrementalStepL();
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // -----------------------------------------------------------------------------
       
   437 //
       
   438 TReal32 CPosLmInitializer::IncrementalStepL()
       
   439     {
       
   440     if ( iIncrementalStep > 0 )
       
   441         {
       
   442         User::LeaveIfError( iDbIncremental.Next( iIncrementalStep ) );
       
   443         }
       
   444     
       
   445     if ( iIncrementalStep == 0 )
       
   446         {
       
   447         iDbIncremental.Close();
       
   448         iIncrementalStarted = EFalse;
       
   449         iIncrementalStep = 0;
       
   450         return KOperationDone;
       
   451         }
       
   452     else
       
   453         {
       
   454         return  TReal32( iIncrementalStartValue - iIncrementalStep ) / 
       
   455                 TReal32( iIncrementalStartValue );
       
   456         }
       
   457     }
       
   458 
       
   459 // -----------------------------------------------------------------------------
       
   460 // -----------------------------------------------------------------------------
       
   461 //
       
   462 TReal32 CPosLmInitializer::BuildLmNameIndexNextStepL()
       
   463     {
       
   464     TReal32 progress = 0;
       
   465     if ( iServer.IndexManager().Find( *iDbUri ) >= 0 )
       
   466         {
       
   467         CPosLmNameIndex& index = iServer.IndexManager().Index( *iDbUri );
       
   468         TInt status = index.Status(); 
       
   469         switch ( status )
       
   470             {
       
   471             case KErrNotReady:
       
   472                 progress = index.Evaluate();
       
   473                 break;
       
   474             case KErrNone:
       
   475                 progress = KOperationDone;
       
   476                 break;
       
   477             default:
       
   478                 User::Leave( status );
       
   479             }
       
   480         return progress;
       
   481         }
       
   482     else
       
   483         {
       
   484         return KOperationDone;
       
   485         }
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // -----------------------------------------------------------------------------
       
   490 //
       
   491 TReal32 CPosLmInitializer::CompactNextStepL()
       
   492     {
       
   493     if ( !iIncrementalStarted )
       
   494         {
       
   495         if ( iCompactRequired || iDb->IsCompactNeededL() )
       
   496             {
       
   497             RDbNamedDatabase database;
       
   498             iDb->GetDatabase( database );
       
   499     
       
   500             User::LeaveIfError( iDbIncremental.Compact( database, iIncrementalStep ) );
       
   501             iIncrementalStartValue = iIncrementalStep;
       
   502             iIncrementalStarted = ETrue;
       
   503             }
       
   504         else
       
   505             {
       
   506             return KOperationDone;
       
   507             }
       
   508         }
       
   509 
       
   510     return IncrementalStepL();
       
   511     }
       
   512 
       
   513 // -----------------------------------------------------------------------------
       
   514 // -----------------------------------------------------------------------------
       
   515 //
       
   516 void CPosLmInitializer::UpdateTotalProgress( TReal32 aCurrentPhaseProgress )
       
   517     {
       
   518     iProgress = 
       
   519         ( TReal32( iPhase - 1 ) + aCurrentPhaseProgress ) / 
       
   520         TReal32 ( EPhaseDone - 1 ); // all phases "-" final phase
       
   521     }