emailservices/emailstore/message_store/server/src/ContainerStoreUtils.cpp
changeset 0 8466d47a6819
child 16 4ce476e64c59
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Container store utils implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // ========
       
    21 // INCLUDES
       
    22 // ========
       
    23 
       
    24 #include "ContainerStoreUtils.h"
       
    25 #include "ContainerStoreDeleteHandler.h"
       
    26 
       
    27 #include <sysutil.h>
       
    28 #include <d32dbms.h>  // database
       
    29 #include <bautils.h>
       
    30 
       
    31 //#include <s32crypt.h>
       
    32 
       
    33 // =============
       
    34 // LOCAL CLASSES
       
    35 // =============
       
    36 
       
    37 // =============================================================================
       
    38 // CLASS: CCompactionHandler
       
    39 // 
       
    40 // This class performs database compaction operations incrementally using the
       
    41 // RDbIncremental class.  Each active object invocation performs one step of the
       
    42 // compaction.  
       
    43 //
       
    44 // The Symbian documentation explains that certain operations cannot be performed
       
    45 // on the database while an incremental operation is in progress.  The Suspend
       
    46 // and Resume functions are provided to suspend incremental operations until the
       
    47 // database operation is completed.
       
    48 //
       
    49 // The wonderful Symbian documentation doesn't give a list of the functions that
       
    50 // do not work during incremental operations.  In order to determine this, the
       
    51 // code was temporarily modified to always have an incremental operation in
       
    52 // progress, unless Suspend and Resume has been called.  The unit tester were
       
    53 // executed, and each operation that had a problem was then wrapped with
       
    54 // Suspend/Resume, and the code was then returned to its original state.
       
    55 // =============================================================================
       
    56 class CCompactionHandler : public CActive
       
    57     {
       
    58     public:
       
    59 
       
    60         // ==============
       
    61         // PUBLIC METHODS
       
    62         // ==============
       
    63             
       
    64         static CCompactionHandler* NewL( RDbDatabase& aDatabase,
       
    65                                          TInt         aPriority );  
       
    66                                                
       
    67         virtual ~CCompactionHandler();
       
    68     
       
    69         void Compact();
       
    70         
       
    71         void Suspend();
       
    72         
       
    73         void SuspendLC();
       
    74         
       
    75         void Resume( TBool aPopCleanupItem = ETrue );
       
    76         
       
    77         void FinishCompactionL();
       
    78         
       
    79     private:
       
    80     
       
    81         // ===============
       
    82         // PRIVATE METHODS
       
    83         // ===============
       
    84             
       
    85         CCompactionHandler( RDbDatabase& aDatabase,
       
    86                             TInt         aPriority );
       
    87         
       
    88         void ConstructL();
       
    89         
       
    90         void Reschedule();
       
    91     
       
    92 		// inherited from CActive
       
    93 		virtual void RunL();
       
    94 		virtual void DoCancel();
       
    95 	
       
    96 	    RTimer         iDelayTimer;
       
    97 	    RDbDatabase&   iDatabase;
       
    98 	    RDbIncremental iIncremental;
       
    99 	    TInt           iStep;
       
   100 	    
       
   101 	    TBool          iCompactionNeeded;
       
   102 	    TInt           iSuspendCount;
       
   103 	    
       
   104 	    __LOG_DECLARATION
       
   105     
       
   106     }; // end class CCompactionHandler
       
   107 
       
   108 // ======================
       
   109 // METHOD IMPLEMENTATIONS
       
   110 // ======================
       
   111 
       
   112 // ==========================================================================
       
   113 // FUNCTION: NewL
       
   114 // ==========================================================================
       
   115 CContainerStoreUtils* CContainerStoreUtils::NewL(  TDriveNumber    aDriveNumber, 
       
   116                                                    TInt            aCompactionPriority, 
       
   117                                                    const TDesC&    aDbFilename,
       
   118                                                    CDeleteHandler& aDeleteHandler )
       
   119     {
       
   120     CContainerStoreUtils* self = new(ELeave) CContainerStoreUtils( aDriveNumber, 
       
   121                                                                    aCompactionPriority, 
       
   122                                                                    aDbFilename,
       
   123                                                                    aDeleteHandler );
       
   124     CleanupStack::PushL( self );
       
   125     self->ConstructL();
       
   126     CleanupStack::Pop( self );
       
   127     return self;
       
   128     } // end NewL
       
   129     
       
   130 // ==========================================================================
       
   131 // FUNCTION: Constructor
       
   132 // ==========================================================================
       
   133 CContainerStoreUtils::CContainerStoreUtils( TDriveNumber    aDriveNumber, 
       
   134                                             TInt            aCompactionPriority, 
       
   135                                             const TDesC&    aDbFilename,
       
   136                                             CDeleteHandler& aDeleteHandler ) :
       
   137     iDriveNumber( aDriveNumber ),
       
   138     iCompactionPriority( aCompactionPriority ),
       
   139     iDbFilename( aDbFilename ),
       
   140     iDeleteHandler( aDeleteHandler )
       
   141     {
       
   142 #ifdef _DEBUG		
       
   143     iLowDiskSpaceLatency = -1;
       
   144 #endif    
       
   145     } // end constructor
       
   146 
       
   147 // ==========================================================================
       
   148 // FUNCTION: ConstructL
       
   149 // ==========================================================================
       
   150 void CContainerStoreUtils::ConstructL()
       
   151     {
       
   152     __LOG_CONSTRUCT( "msg", "CContainerStoreUtils" )
       
   153     
       
   154     User::LeaveIfError( iFs.Connect() );
       
   155     
       
   156     iFs.CreatePrivatePath( iDriveNumber );
       
   157     iFs.SetSessionToPrivate( iDriveNumber ); 
       
   158     
       
   159     iFs.PrivatePath( iPrivatePath );
       
   160     const TUint bufSize = 3;
       
   161     TBuf<bufSize> driveLetter;
       
   162     _LIT( KFormatString, "%C:" ); 
       
   163     driveLetter.Format( KFormatString, (iDriveNumber - EDriveA + 'A' ) );
       
   164     iPrivatePath.Insert( 0, driveLetter );
       
   165     
       
   166 	} // end ConstructL
       
   167     
       
   168 // ==========================================================================
       
   169 // FUNCTION: Destructor
       
   170 // ==========================================================================
       
   171 CContainerStoreUtils::~CContainerStoreUtils()
       
   172     {
       
   173     delete iCompactionHandler;
       
   174     
       
   175     iTables.Close();
       
   176     
       
   177     iDatabase.Close();
       
   178     
       
   179     delete iFileStore;
       
   180     
       
   181     iFs.Close(); 
       
   182     
       
   183     __LOG_DESTRUCT
       
   184     } // end destructor
       
   185 
       
   186 // ==========================================================================
       
   187 // FUNCTION: LeaveIfFalseL
       
   188 // ==========================================================================
       
   189 void CContainerStoreUtils::LeaveIfFalse( TBool aCondition, TInt aLeaveCode )
       
   190 {
       
   191 	if ( !aCondition )
       
   192 	{
       
   193 		User::Leave( aLeaveCode );
       
   194 	}
       
   195 }
       
   196 
       
   197 
       
   198 // ==========================================================================
       
   199 // FUNCTION: FileSystem
       
   200 // ==========================================================================
       
   201 RFs& CContainerStoreUtils::FileSystem()
       
   202     {
       
   203     return iFs;
       
   204     } // end FileSystem
       
   205     
       
   206 // ==========================================================================
       
   207 // FUNCTION: CreateDatabaseL
       
   208 // ==========================================================================
       
   209 void CContainerStoreUtils::CreateDatabaseL()
       
   210 	{
       
   211 	__LOG_ENTER( "CreateDatabaseL" );
       
   212 
       
   213     // Create the file store.
       
   214 	iFileStore = CPermanentFileStore::ReplaceL( iFs, iDbFilename, EFileRead|EFileWrite );
       
   215 	
       
   216 	iFileStore->SetTypeL( iFileStore->Layout() );	
       
   217 	
       
   218 	// Create a database within the file store.
       
   219 	TStreamId id = iDatabase.CreateL( iFileStore );
       
   220 	
       
   221 	// Set the database as the file store root object.
       
   222 	iFileStore->SetRootL( id );
       
   223 	
       
   224 	// Commit the database.
       
   225 	iFileStore->CommitL();
       
   226 
       
   227     iCompactionHandler = CCompactionHandler::NewL( iDatabase, iCompactionPriority );
       
   228         
       
   229 	__LOG_EXIT
       
   230 	} // end CreateDatabaseL
       
   231 	
       
   232 // ==========================================================================
       
   233 // FUNCTION: OpenDatabaseL
       
   234 // ==========================================================================
       
   235 void CContainerStoreUtils::OpenDatabaseL()
       
   236 	{
       
   237 	__LOG_ENTER( "OpenDatabaseL" )
       
   238 
       
   239 	// construct a file store object
       
   240 	iFileStore = CPermanentFileStore::OpenL( iFs, iDbFilename, EFileRead|EFileWrite );
       
   241 	
       
   242 	// open database from the root of the store
       
   243 	iDatabase.OpenL( iFileStore, iFileStore->Root() );	
       
   244 	
       
   245 	if( iDatabase.IsDamaged() )
       
   246 		{
       
   247 		__LOG_WRITE_ERROR( "Recovering database" );
       
   248 		User::LeaveIfError( iDatabase.Recover() );
       
   249 		} // end if
       
   250 	
       
   251     iCompactionHandler = CCompactionHandler::NewL( iDatabase, iCompactionPriority );
       
   252 	iCompactionHandler->Compact();
       
   253 	
       
   254  	__LOG_EXIT
       
   255 	} // end OpenDatabaseL
       
   256 
       
   257 // ==========================================================================
       
   258 // FUNCTION: CloseDatabaseL
       
   259 // ==========================================================================
       
   260 void CContainerStoreUtils::CloseDatabaseL()
       
   261 	{
       
   262 	__LOG_ENTER( "CloseDatabaseL" )
       
   263 	
       
   264     // Finish compaction, if necessary.
       
   265     if( iCompactionHandler )
       
   266         {        
       
   267         iCompactionHandler->FinishCompactionL();
       
   268         } // end if
       
   269     
       
   270 	CloseDatabase();
       
   271 	
       
   272 	__LOG_EXIT
       
   273 	} // end CloseDatabaseL
       
   274 
       
   275 // ==========================================================================
       
   276 // FUNCTION: CloseDatabase
       
   277 // ==========================================================================
       
   278 void CContainerStoreUtils::CloseDatabase()
       
   279     {
       
   280     __LOG_ENTER( "CloseDatabase" )
       
   281     
       
   282     delete iCompactionHandler;
       
   283     iCompactionHandler = NULL;
       
   284     
       
   285     iDatabase.Close();
       
   286     
       
   287     delete iFileStore;
       
   288     iFileStore = NULL;
       
   289     
       
   290     __LOG_EXIT
       
   291     } // end CloseDatabase
       
   292 
       
   293 // ==========================================================================
       
   294 // FUNCTION: PopulateViewL
       
   295 // ==========================================================================
       
   296 void CContainerStoreUtils::PopulateViewL( RDbView& aView, const TDesC& aSqlQuery, RDbRowSet::TAccess aAccess )
       
   297     {
       
   298     __LOG_ENTER( "PopulateViewL" )
       
   299     
       
   300     User::LeaveIfError( aView.Prepare( iDatabase, TDbQuery(aSqlQuery), aAccess ) );
       
   301     
       
   302     // Evaluate the view (i.e. populate it based on the SQL query).
       
   303 	User::LeaveIfError( aView.EvaluateAll() );
       
   304 	
       
   305 	__LOG_EXIT
       
   306     } // end PopulateViewL
       
   307     
       
   308 // ==========================================================================
       
   309 // FUNCTION: Execute
       
   310 // ==========================================================================
       
   311 void CContainerStoreUtils::Execute( const TDesC &aSql, TDbTextComparison aComparison )
       
   312 	{
       
   313     __LOG_ENTER( "Execute" )
       
   314 	
       
   315     iDatabase.Execute( aSql, aComparison );
       
   316     
       
   317 	__LOG_EXIT
       
   318 	}
       
   319 
       
   320 
       
   321 // ==========================================================================
       
   322 // FUNCTION: CreateTableL
       
   323 // ==========================================================================
       
   324 void CContainerStoreUtils::CreateTableL( const TDesC& aTableName, CDbColSet& aColSet )
       
   325     {
       
   326     User::LeaveIfError( iDatabase.CreateTable( aTableName, aColSet ) );
       
   327     } // end CreateTableL
       
   328 
       
   329 // ==========================================================================
       
   330 // FUNCTION: CreateIndexL
       
   331 // ==========================================================================
       
   332 void CContainerStoreUtils::CreateIndexL( const TDesC& aName, const TDesC& aTable, const CDbKey& aKey )
       
   333     {
       
   334     User::LeaveIfError( iDatabase.CreateIndex( aName, aTable, aKey ) );    
       
   335     } // end CreateIndexL
       
   336 
       
   337 // ==========================================================================
       
   338 // FUNCTION: OpenTableL
       
   339 // ==========================================================================
       
   340 void CContainerStoreUtils::OpenTableL( RDbTable& aTable, const TDesC& aTableName )
       
   341     {
       
   342     User::LeaveIfError( aTable.Open( iDatabase, aTableName ) );
       
   343     
       
   344     iTables.AppendL( &aTable );
       
   345     
       
   346     } // end OpenTableL
       
   347 
       
   348 // ==========================================================================
       
   349 // FUNCTION: CloseTable
       
   350 // ==========================================================================
       
   351 void CContainerStoreUtils::CloseTable( RDbTable& aTable )
       
   352     {
       
   353     aTable.Close();
       
   354     
       
   355     TInt index = iTables.Find( &aTable );
       
   356     if( index != KErrNotFound )
       
   357         {        
       
   358         iTables.Remove( index );
       
   359         } // end if
       
   360     
       
   361     } // end CloseTable
       
   362     
       
   363 // ==========================================================================
       
   364 // FUNCTION: ReadLongColumnL
       
   365 // ==========================================================================
       
   366 void CContainerStoreUtils::ReadLongColumnL( RDbRowSet& aRowSet, 
       
   367                                             TUint      aColNum, 
       
   368                                             RBuf8&     aBuffer,
       
   369                                             TUint      aBufferPadSpace )
       
   370     {
       
   371     __LOG_ENTER_SUPPRESS( "ReadLongColumnL" )
       
   372     
       
   373     iCompactionHandler->SuspendLC();
       
   374     
       
   375 	RDbColReadStream readStream;
       
   376 	
       
   377 	TUint colLength = aRowSet.ColLength( aColNum );
       
   378 	
       
   379 	TUint desiredLength = colLength + aBufferPadSpace;
       
   380 	
       
   381 	if( aBuffer.MaxLength() < desiredLength )
       
   382 		{
       
   383 		__LOG_WRITE_INFO( "growing buffer" )
       
   384 		aBuffer.ReAllocL( desiredLength );		
       
   385 		} // end if
       
   386 	
       
   387 	aBuffer.SetLength( 0 );
       
   388 	readStream.OpenLC( aRowSet, aColNum );
       
   389 	readStream.ReadL( aBuffer, colLength );
       
   390 	
       
   391 	CleanupStack::PopAndDestroy( &readStream ); 
       
   392 	
       
   393 	iCompactionHandler->Resume();
       
   394 	
       
   395     } // end ReadLongColumnL
       
   396 
       
   397 // ==========================================================================
       
   398 // FUNCTION: WriteLongColumnL
       
   399 // ==========================================================================
       
   400 void CContainerStoreUtils::WriteLongColumnL( RDbRowSet&    aRowSet,
       
   401                                	             TUint         aColNum, 
       
   402                                       	     const TDesC8& aBuffer )
       
   403     { 
       
   404     __LOG_ENTER( "WriteLongColumnL" )
       
   405     
       
   406     if ( aBuffer.Length() > 0 )
       
   407         {
       
   408     	RDbColWriteStream writeStream;
       
   409     	
       
   410     	LeaveIfLowDiskSpaceL( aBuffer.Length() );
       
   411     	
       
   412     	writeStream.OpenLC( aRowSet, aColNum );
       
   413     	writeStream.WriteL( aBuffer );
       
   414     	writeStream.CommitL();
       
   415     
       
   416     	CleanupStack::PopAndDestroy( &writeStream ); 
       
   417         }
       
   418     else
       
   419         {
       
   420         aRowSet.SetColNullL( aColNum );
       
   421         }
       
   422 	__LOG_EXIT
       
   423     } // end WriteLongColumnL
       
   424 
       
   425 // ==========================================================================
       
   426 // FUNCTION: LeaveIfLowDiskSpaceL
       
   427 // ==========================================================================
       
   428 void CContainerStoreUtils::LeaveIfLowDiskSpaceL( TUint aBytesToWrite )
       
   429 	{
       
   430 	__LOG_ENTER_SUPPRESS( "LeaveIfLowDiskSpaceL" )
       
   431 	
       
   432 	// This safety margin is to account for some level of uncertainty in aBytesToWrite due to overhead
       
   433 	// in the database, file system, encryption, etc.
       
   434 	const TUint KSafetyMargin = 8*1024;
       
   435 
       
   436 	__LOG_WRITE8_FORMAT1_INFO( "LeaveIfLowDiskSpace(%i)", aBytesToWrite )
       
   437 
       
   438 	TBool belowCritical = SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, aBytesToWrite+KSafetyMargin, iDriveNumber );
       
   439 
       
   440 #ifdef _DEBUG
       
   441     TBool belowCriticalOverride = EFalse;    
       
   442     if( iLowDiskSpaceLatency > -1 )
       
   443         {
       
   444         belowCriticalOverride = (iLowDiskSpaceLatency == 0);        
       
   445         belowCritical = belowCritical || belowCriticalOverride;
       
   446         iLowDiskSpaceLatency--;
       
   447         } // end if
       
   448 #endif
       
   449 
       
   450     if( belowCritical )    
       
   451         {
       
   452         // Force the deletes and database compaction to complete in the foreground.  This may take
       
   453         // a while, but may free up enough space to fix this condition.
       
   454 
       
   455         // : It was found that the following call will fail if a database row update is in progress (calls
       
   456         //       such as FirstL assert in those cases).  The cleanup mechanism would need to be refined in order
       
   457         //       to avoid that situation.
       
   458         // iDeleteHandler.FinishDeletes();
       
   459         
       
   460         iCompactionHandler->FinishCompactionL();        
       
   461 
       
   462 	    belowCritical = SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, aBytesToWrite+KSafetyMargin, iDriveNumber );
       
   463 
       
   464 #ifdef _DEBUG
       
   465         belowCritical = belowCritical || belowCriticalOverride;
       
   466 #endif
       
   467 
       
   468         } // end if
       
   469 
       
   470 	if( belowCritical )
       
   471 		{
       
   472 		__LOG_WRITE_ERROR( "below critical disk space!" );	
       
   473 		User::Leave( KErrNoMemory );
       
   474 		} // end if
       
   475 	
       
   476 	} // end LeaveIfLowDiskSpaceL
       
   477 
       
   478 // ==========================================================================
       
   479 // FUNCTION: PrivatePath
       
   480 // ==========================================================================
       
   481 const TDesC& CContainerStoreUtils::PrivatePath() const
       
   482     {
       
   483     return iPrivatePath;
       
   484     } // end PrivatePath
       
   485     
       
   486 // ==========================================================================
       
   487 // FUNCTION: BeginDatabaseTransactionLC
       
   488 // ==========================================================================
       
   489 void CContainerStoreUtils::BeginDatabaseTransactionLC()
       
   490 	{
       
   491 	__LOG_ENTER( "BeginDatabaseTransactionL" )
       
   492 	
       
   493 	// A database transaction cannot be started while an incremental operation is in progress, so
       
   494 	// suspend it now.
       
   495 	iCompactionHandler->Suspend();
       
   496 	
       
   497 	TUint result = iDatabase.Begin();
       
   498 	
       
   499 	if( result != KErrNone )
       
   500 		{
       
   501 		__LOG_WRITE8_FORMAT1_ERROR( "failed, err=%i", result );
       
   502 		User::Leave( result );
       
   503 		} // end if
       
   504 		
       
   505     // Push an item on the cleanup stack taht will rollback the database transaction
       
   506     // if something fails during the transaction.		
       
   507 	CleanupStack::PushL(TCleanupItem(&RollbackDatabaseTransactionL, this) );
       
   508 
       
   509     __LOG_EXIT
       
   510 	} // end BeginDatabaseTransactionL
       
   511 
       
   512 // ==========================================================================
       
   513 // FUNCTION: CommitDatabaseTransactionL
       
   514 // ==========================================================================
       
   515 void CContainerStoreUtils::CommitDatabaseTransactionL()
       
   516 	{
       
   517 	__LOG_ENTER( "CommitDatabaseTransactionL" )
       
   518 	
       
   519 	User::LeaveIfError( iDatabase.Commit() );
       
   520 
       
   521     // Pop the rollback item.	
       
   522 	CleanupStack::Pop( this );
       
   523 
       
   524 	iCompactionHandler->Resume( EFalse );
       
   525 	
       
   526 	iCompactionHandler->Compact();
       
   527 	
       
   528     __LOG_EXIT
       
   529 	} // end CommitDatabaseTransactionL
       
   530 	
       
   531 // ==========================================================================
       
   532 // FUNCTION: RollbackDatabaseTransactionL
       
   533 // ==========================================================================
       
   534 void CContainerStoreUtils::RollbackDatabaseTransactionL()
       
   535 	{
       
   536 	__LOG_ENTER( "RollbackDatabaseTransaction" )
       
   537 
       
   538 	iDatabase.Rollback();
       
   539 	
       
   540 	// Rollbacks can damage the database indexes.  This will fix them.
       
   541 	User::LeaveIfError( iDatabase.Recover() );
       
   542 
       
   543     // All rowsets must be reset after a Rollback, otherwise row functions will leave with
       
   544     // KErrNotReady.
       
   545     for( TInt index = 0; index < iTables.Count(); index++ )
       
   546         {
       
   547         iTables[index]->Reset();
       
   548         } // end for
       
   549 			
       
   550 	iCompactionHandler->Resume( EFalse );
       
   551 	iCompactionHandler->Compact();
       
   552 	
       
   553 	__LOG_EXIT
       
   554 	} // end RollbackDatabaseTransactionL
       
   555 
       
   556 // ==========================================================================
       
   557 // FUNCTION: RollbackDatabaseTransactionL
       
   558 // ==========================================================================
       
   559 void CContainerStoreUtils::RollbackDatabaseTransactionL( TAny* aObject )
       
   560 	{
       
   561 	__LOG_STATIC_ENTER( "msg", "RollbackDatabaseTransactionL" )	
       
   562 	
       
   563 	CContainerStoreUtils* utils = reinterpret_cast<CContainerStoreUtils*>(aObject);	
       
   564 	utils->RollbackDatabaseTransactionL();
       
   565 	
       
   566 	__LOG_STATIC_EXIT
       
   567 	} // end RollbackDatabaseTransaction
       
   568 	
       
   569 // ==========================================================================
       
   570 // FUNCTION: SuspendCompactionLC
       
   571 // ==========================================================================
       
   572 void CContainerStoreUtils::SuspendCompactionLC()
       
   573 	{
       
   574 	iCompactionHandler->SuspendLC();
       
   575 	}
       
   576 
       
   577 // ==========================================================================
       
   578 // FUNCTION: ResumeCompaction
       
   579 // ==========================================================================
       
   580 void CContainerStoreUtils::ResumeCompaction()
       
   581 	{
       
   582 	iCompactionHandler->Resume();	
       
   583 	}
       
   584 
       
   585 // ==========================================================================
       
   586 // FUNCTION: FindFirstEncryptedOrUnencryptedL
       
   587 // ==========================================================================
       
   588 TBool CContainerStoreUtils::FindFirstEncryptedOrUnencryptedL( RDbTable&    aTable, 
       
   589                                                              const TDesC& aColName, 
       
   590                                                              TBool        aFindEncrypted, 
       
   591                                                              TDbBookmark& aBookmark )
       
   592     {
       
   593     TBool found = EFalse;
       
   594     
       
   595     if ( !aTable.IsEmptyL() )
       
   596         {
       
   597         const TUint querrySize=60;
       
   598         TBuf<querrySize> queryString;
       
   599         
       
   600         _LIT( KEquals, "=" );
       
   601         
       
   602         queryString.Copy( aColName );
       
   603         queryString.Append( KEquals );
       
   604         queryString.AppendNum( aFindEncrypted );
       
   605         
       
   606         if ( aTable.FirstL() )
       
   607             {
       
   608             found = aTable.FindL( RDbRowSet::EForwards, queryString ) != KErrNotFound ;
       
   609             
       
   610             if( found )
       
   611                 {
       
   612                 aBookmark = aTable.Bookmark( );
       
   613                 } // end if
       
   614             }
       
   615         }    
       
   616     
       
   617     return found;
       
   618     }
       
   619 
       
   620 // ------------------    
       
   621 // CCompactionHandler
       
   622 // ------------------    
       
   623     
       
   624 // Used to make sure that compaction occurs at most once every 10 seconds.  This protects lower priority clients
       
   625 // from being shut out during many sequential operations (initial sync, for example).
       
   626 const TUint KInitialCompactionDelay = 10000000;
       
   627     
       
   628 CCompactionHandler* CCompactionHandler::NewL( RDbDatabase& aDatabase,
       
   629                                               TInt         aPriority )
       
   630     {
       
   631     CCompactionHandler* self = new(ELeave) CCompactionHandler( aDatabase, aPriority );
       
   632     CleanupStack::PushL( self );
       
   633     self->ConstructL();
       
   634     CleanupStack::Pop( self );
       
   635     return self;    
       
   636     }
       
   637     
       
   638 CCompactionHandler::CCompactionHandler( RDbDatabase& aDatabase,
       
   639                                         TInt         aPriority ) :
       
   640     CActive( aPriority ),                                        
       
   641     iDatabase( aDatabase )                                        
       
   642     {
       
   643     __LOG_CONSTRUCT( "msg", "CCompactionHandler" )
       
   644 
       
   645    	CActiveScheduler::Add(this);
       
   646     }
       
   647 
       
   648 void CCompactionHandler::ConstructL()
       
   649     {
       
   650     User::LeaveIfError( iDelayTimer.CreateLocal() );
       
   651     }
       
   652 
       
   653 CCompactionHandler::~CCompactionHandler()
       
   654     {
       
   655     Cancel();
       
   656         
       
   657     iIncremental.Close();
       
   658     
       
   659     iDelayTimer.Close();
       
   660             
       
   661     __LOG_DESTRUCT
       
   662     }
       
   663 
       
   664 void CCompactionHandler::Compact()
       
   665     {
       
   666     __LOG_ENTER_SUPPRESS( "Compact" )
       
   667 
       
   668      iCompactionNeeded = ETrue;
       
   669     
       
   670     if( (iSuspendCount == 0) && !IsActive() )
       
   671         {
       
   672         __LOG_WRITE_INFO( "starting compaction" );
       
   673         iStep = 0;
       
   674         
       
   675         iDelayTimer.After( iStatus, KInitialCompactionDelay );
       
   676         SetActive();          
       
   677         } // end if
       
   678     }
       
   679 
       
   680 static void ResumeCompaction( TAny* aObject )
       
   681 	{
       
   682 	__LOG_STATIC_ENTER( "msg", "ResumeCompactionL" )	
       
   683 	
       
   684 	CCompactionHandler* handler = reinterpret_cast<CCompactionHandler*>(aObject);	
       
   685 	handler->Resume( EFalse );  // do not pop the cleanup item
       
   686 	
       
   687 	__LOG_STATIC_EXIT
       
   688 	} // end RollbackDatabaseTransaction
       
   689 	
       
   690 void CCompactionHandler::Suspend()
       
   691     {
       
   692     __LOG_ENTER_SUPPRESS( "Suspend" )
       
   693     
       
   694     if( iSuspendCount == 0 )
       
   695         {        
       
   696         __LOG_WRITE_DEBUG3( "suspending compaction" )
       
   697 
       
   698         Cancel();    
       
   699         iIncremental.Close();
       
   700     
       
   701         } // end if
       
   702     
       
   703     iSuspendCount++;    
       
   704     }         
       
   705 
       
   706 void CCompactionHandler::SuspendLC()
       
   707     {
       
   708     Suspend();
       
   709     
       
   710     // Push an item on the cleanup stack taht will resume compaction if something leaves.
       
   711 	CleanupStack::PushL(TCleanupItem(&ResumeCompaction, this) );
       
   712     }         
       
   713 
       
   714 void CCompactionHandler::Resume( TBool aPopCleanupItem )
       
   715     {
       
   716     __LOG_ENTER_SUPPRESS( "Resume" )
       
   717     
       
   718     iSuspendCount--;
       
   719     
       
   720     if( iCompactionNeeded && (iSuspendCount == 0) )
       
   721         {
       
   722         __LOG_WRITE_INFO( "resuming compaction" );
       
   723         Compact();
       
   724         } // end if
       
   725 
       
   726 	if( aPopCleanupItem )
       
   727 		{		
       
   728 		// Pop the resume item.	
       
   729 		CleanupStack::Pop( this );
       
   730 		} // end if
       
   731     }    
       
   732     
       
   733 void CCompactionHandler::FinishCompactionL()
       
   734     {
       
   735     __LOG_ENTER( "FinishCompactionL" )
       
   736     
       
   737     SuspendLC();
       
   738     
       
   739     // Call the synchronous compaction.
       
   740     iDatabase.Compact();
       
   741     iCompactionNeeded = EFalse;
       
   742     
       
   743     Resume();    
       
   744     
       
   745     __LOG_EXIT
       
   746     }
       
   747         
       
   748 void CCompactionHandler::Reschedule()
       
   749     {
       
   750 	SetActive();
       
   751 	TRequestStatus* status = &iStatus;
       
   752 	User::RequestComplete( status, KErrNone );      
       
   753     }
       
   754 
       
   755 void CCompactionHandler::RunL()
       
   756     {
       
   757     __LOG_ENTER_SUPPRESS( "RunL" )
       
   758     
       
   759     if( iStep == 0 )
       
   760         {        
       
   761         iIncremental.Compact( iDatabase, iStep );
       
   762         }
       
   763     else
       
   764         {
       
   765         iIncremental.Next( iStep );
       
   766         } // end if
       
   767 
       
   768     if( iStep == 0 )
       
   769         {
       
   770         __LOG_WRITE_INFO( "Compaction completed" )
       
   771 
       
   772         iCompactionNeeded = EFalse;
       
   773         iIncremental.Close();
       
   774         }
       
   775     else
       
   776         {            
       
   777         Reschedule();
       
   778         } // end if
       
   779     }
       
   780 
       
   781 void CCompactionHandler::DoCancel()
       
   782     {
       
   783     // The timer is only active on the first step.
       
   784     if( iStep == 0 )
       
   785         {
       
   786         iDelayTimer.Cancel();        
       
   787         } // end if
       
   788     } // end DoCancel
       
   789 	    
       
   790 // FUNCTIONS TO SUPPORT AUTOMATED UNIT TESTING
       
   791     
       
   792 #ifdef _DEBUG		
       
   793 void CContainerStoreUtils::SimulateLowDiskSpace( TInt aLatency )
       
   794     {
       
   795     iLowDiskSpaceLatency = aLatency;
       
   796     }    
       
   797 #endif
       
   798