mpserviceplugins/mpxsqlitedbcommon/src/mpxdbmanager.cpp
changeset 25 3ec52facab4d
parent 22 ecf06a08d4d9
child 29 8192e5b5c935
equal deleted inserted replaced
22:ecf06a08d4d9 25:3ec52facab4d
   191 
   191 
   192     iTables.Close();
   192     iTables.Close();
   193     CloseAllDatabases();
   193     CloseAllDatabases();
   194 
   194 
   195     delete iDbFile;
   195     delete iDbFile;
   196 #ifdef __RAMDISK_PERF_ENABLE
       
   197     TInt count(iDatabaseHandles.Count());
       
   198     for (TInt i = 0; i < count; ++i)
       
   199         {
       
   200         RemoveDummyFile(i);
       
   201         }    
       
   202 #endif //__RAMDISK_PERF_ENABLE
       
   203     iDatabaseHandles.Close();
   196     iDatabaseHandles.Close();
   204     }
   197     }
   205 
   198 
   206 // ----------------------------------------------------------------------------
   199 // ----------------------------------------------------------------------------
   207 // Checks if all databases have been initialized.
   200 // Checks if all databases have been initialized.
   276             iTransactionCount = 0;
   269             iTransactionCount = 0;
   277             DoCommitL();
   270             DoCommitL();
   278             }
   271             }
   279 
   272 
   280         TInt count(iDatabaseHandles.Count());
   273         TInt count(iDatabaseHandles.Count());
   281         TBool ret = EFalse;
   274         //TBool ret = EFalse;
   282         for ( TInt i = 0; i < count ; ++i )
   275         for ( TInt i = 0; i < count ; ++i )
   283             {
   276             {
   284             if ( iDatabaseHandles[i].iUseRAMdb )
   277             if ( iDatabaseHandles[i].iUseRAMdb )
   285                 {
   278                 {
   286                 // already used
   279                 // already used
   287                 MPX_DEBUG1("CMPXDbManager::CopyDBsToRamL iUseRAMdb already ETrue");
   280                 MPX_DEBUG1("CMPXDbManager::CopyDBsToRamL iUseRAMdb already ETrue");
   288                 continue;
   281                 continue;
   289                 }
   282                 }
   290             iDatabaseHandles[i].iUseRAMdb = ETrue;
   283             CloseDatabaseAtIndexL( i ); // let leave: not much we can't do if we can't close the original DB
   291             TRAPD(err, ret = DoCopyDBToRamL( iDatabaseHandles[i].iDrive, aIsMTPInUse ))
   284             DoCopyDBToRam( i, aIsMTPInUse ); // copies if it can
       
   285             TRAPD( err, OpenDatabaseAtIndexL( i ) );
   292             if ( err != KErrNone )
   286             if ( err != KErrNone )
   293                 {
   287                 {
   294                 MPX_DEBUG2("CMPXDbManager::CopyDBsToRamL error=%d", err);
   288                 MPX_DEBUG2("CMPXDbManager::CopyDBsToRamL OpenDatabaseAtIndexL leave=%d", err);
   295                 // remove dymmy file
       
   296                 RemoveDummyFile(i);
   289                 RemoveDummyFile(i);
   297                 // try to close database that is opened from RAM disk
   290                 if ( iDatabaseHandles[i].iUseRAMdb ) 
   298                 TRAP_IGNORE(CloseDatabaseL( iDatabaseHandles[i].iDrive ));
   291                     {
   299                 iDatabaseHandles[i].iUseRAMdb = EFalse;
   292                     // go back to disk DB
   300                 // reopen database from drive not from RAM
   293                     TRAP_IGNORE(CloseDatabaseAtIndexL( i ));
   301                 OpenDatabaseL( iDatabaseHandles[i].iDrive );
   294                     iDatabaseHandles[i].iUseRAMdb = EFalse;
   302                 continue; // continue to copy for next drive
   295                     OpenDatabaseAtIndexL( i );
   303                 }
   296                     continue;
   304             if ( !ret )
   297                     }
   305                 {
   298                 else
   306                 iDatabaseHandles[i].iUseRAMdb = EFalse;
   299                     {
       
   300                     User::Leave( err );
       
   301                     }
   307                 }
   302                 }
   308             }
   303             }
   309             
   304             
   310         if (transactionCount > 0) 
   305         if (transactionCount > 0) 
   311             {
   306             {
   320 
   315 
   321     }
   316     }
   322 
   317 
   323 
   318 
   324 // ----------------------------------------------------------------------------
   319 // ----------------------------------------------------------------------------
   325 // CMPXDbManager::DoCopyDBsToRamL
   320 // CMPXDbManager::DoCopyDBToRam
   326 // ----------------------------------------------------------------------------
   321 // ----------------------------------------------------------------------------
   327 //
   322 //
   328 TBool CMPXDbManager::DoCopyDBToRamL( TDriveUnit aDrive, TBool aIsMTPInUse )
   323 TBool CMPXDbManager::DoCopyDBToRam( TInt aIndex, TBool aIsMTPInUse )
   329     {
   324     {
   330 #ifdef __RAMDISK_PERF_ENABLE
   325 #ifdef __RAMDISK_PERF_ENABLE
   331     MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsToRamL drive=%d", (TInt)aDrive);
   326     MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsToRam drive=%d", (TInt)iDatabaseHandles[aIndex].iDrive);
   332     TFileName dst;
   327     DatabaseHandle& database = iDatabaseHandles[aIndex];
   333     TFileName src;
   328     TInt err = KErrNone;
   334     dst.Append(iRAMFolder);
   329 
   335     src.Append(aDrive.Name());
   330     delete database.iOrigFullFilePath;
   336     src.Append(KDBFilePath);
   331     database.iOrigFullFilePath = 0;
   337     TRAPD( err, BaflUtils::EnsurePathExistsL( iFs, dst ));
   332     delete database.iTargetFullFilePath;
   338     if ( err != KErrNone )
   333     database.iTargetFullFilePath = 0;
   339         {
   334     TRAP (err, 
       
   335 	      database.iOrigFullFilePath = CreateFullFilenameL( database.iDrive );
       
   336           database.iUseRAMdb = ETrue; // must turn this on to create RAM filename
       
   337           database.iTargetFullFilePath = CreateFullFilenameL( database.iDrive );
       
   338           BaflUtils::EnsurePathExistsL( iFs, *database.iTargetFullFilePath ));
       
   339     database.iUseRAMdb = EFalse;
       
   340     if (err != KErrNone)
       
   341         {
       
   342         MPX_DEBUG1("CMPXDbManager::DoCopyDBsToRamL() CreateFilenameL or EnsurePathExistsL failed");
   340         return EFalse;
   343         return EFalse;
   341         }
   344         }
   342     TFileName filename;            
   345     MPX_DEBUG2("RAMDisk src path=%S", database.iOrigFullFilePath);
   343     filename.Format(KSecurePath, User::Identity().iUid, iDbFile); //x:\private\10281e17\[sldfdsf]mpxv2_5.db
   346     MPX_DEBUG2("RAMDisk dst path=%S", database.iTargetFullFilePath);
   344     src.Append(filename);
   347 
   345     MPX_DEBUG2("RAMDisk src path=%S", &src);
   348     if (!BlockDiskSpace( aIndex, aIsMTPInUse ) )
   346     TEntry entry;
       
   347     iFs.Entry( src, entry );
       
   348     if (!BlockDiskSpace( aDrive, entry.iSize, aIsMTPInUse ) )
       
   349         {
   349         {
   350         MPX_DEBUG1("CMPXDbManager::DoCopyDBsToRamL() BlockDiskSpace failed");
   350         MPX_DEBUG1("CMPXDbManager::DoCopyDBsToRamL() BlockDiskSpace failed");
   351         return EFalse; // continue for next drive
   351         return EFalse; // continue for next drive
   352         }
   352         }
   353     TBuf<2> d;
   353 
   354     d.Append(aDrive.Name());
   354     if ( BaflUtils::CopyFile(iFs, *database.iOrigFullFilePath, *database.iTargetFullFilePath ) != KErrNone )
   355     HBufC* temp = HBufC::NewLC(KMaxFileName);
   355         {
   356     temp->Des().Append(d.Left(1));
   356         RemoveDummyFile( aIndex );
   357     temp->Des().Append(iDbFile->Des());
       
   358     filename.Format(KSecurePath, User::Identity().iUid, temp);
       
   359     CleanupStack::PopAndDestroy(temp);
       
   360     dst.Append(filename);
       
   361     MPX_DEBUG2("RAMDisk dst path=%S", &dst);
       
   362     TInt index( GetDatabaseIndex((TInt)aDrive) );
       
   363     delete iDatabaseHandles[index].iOrigFullFilePath;
       
   364     iDatabaseHandles[index].iOrigFullFilePath = 0;
       
   365     delete iDatabaseHandles[index].iTargetFullFilePath;
       
   366     iDatabaseHandles[index].iTargetFullFilePath = 0;
       
   367     
       
   368     // Save these path so it is convenient to copy back
       
   369     iDatabaseHandles[index].iOrigFullFilePath = HBufC::NewL(src.Length());
       
   370     iDatabaseHandles[index].iTargetFullFilePath = HBufC::NewL(dst.Length());
       
   371         
       
   372     iDatabaseHandles[index].iOrigFullFilePath->Des().Append(src);
       
   373     iDatabaseHandles[index].iTargetFullFilePath->Des().Append(dst);
       
   374 
       
   375     TRAP(err, CloseDatabaseL(aDrive));
       
   376     if ( err != KErrNone )
       
   377         {
       
   378         MPX_DEBUG2("<--CMPXDbManager::DoCopyDBsToRamL error=%d", err);
       
   379         TInt index(GetDatabaseIndex((TInt)aDrive));
       
   380         if ( index >= 0 )
       
   381             {
       
   382             RemoveDummyFile( index );
       
   383             }
       
   384         return EFalse;
   357         return EFalse;
   385         }
   358         }
   386     User::LeaveIfError( BaflUtils::CopyFile(iFs, src, dst ));
   359     MPX_DEBUG2("RAMDisk Database copied=%d", (TInt)database.iDrive);
   387     OpenDatabaseL((TInt)aDrive);
   360     database.iUseRAMdb = ETrue; // succeeded moving DB to RAM
   388 
       
   389     MPX_DEBUG2("RAMDisk Database opened=%d", (TInt)aDrive);
       
   390     MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsToRamL");
   361     MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsToRamL");
   391     return ETrue;    
   362     return ETrue;
       
   363 #else
       
   364     return EFalse;
   392 #endif //__RAMDISK_PERF_ENABLE
   365 #endif //__RAMDISK_PERF_ENABLE
   393     }
   366     }
   394 
   367 
   395 // ----------------------------------------------------------------------------
   368 // ----------------------------------------------------------------------------
   396 // Copy all DBs from RAM disk back to normal drives
   369 // Copy all DBs from RAM disk back to normal drives
   397 // ----------------------------------------------------------------------------
   370 // ----------------------------------------------------------------------------
   398 //
   371 //
   399 EXPORT_C void CMPXDbManager::CopyDBsFromRamL()
   372 EXPORT_C void CMPXDbManager::CopyDBsFromRamL()
   400     {
   373     {
       
   374     MPX_FUNC("CMPXDbManager::CopyDBsFromRamL");
   401 #ifdef __RAMDISK_PERF_ENABLE
   375 #ifdef __RAMDISK_PERF_ENABLE
   402     MPX_DEBUG1("-->CMPXDbManager::CopyDBsFromRamL");
       
   403     if( iRAMDiskPerfEnabled )
   376     if( iRAMDiskPerfEnabled )
   404        {
   377        {
   405         TInt transactionCount = iTransactionCount;
   378         TInt transactionCount = iTransactionCount;
   406         if (iTransactionCount > 0) 
   379         if (iTransactionCount > 0) 
   407             {
   380             {
   408             iTransactionCount = 0;
   381             iTransactionCount = 0;
   409             DoCommitL();
   382             TRAP_IGNORE( DoCommitL() );
   410             }
   383             }
   411 
   384 
   412         TInt count(iDatabaseHandles.Count());
   385         TInt count(iDatabaseHandles.Count());
   413         for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb; ++i)
   386         TInt leaveError = KErrNone;
   414             {
   387         iRAMInUse = EFalse;
   415             TRAPD(err, DoCopyDBFromRamL(i));
   388         // Should not leave until all the databases have been copied from RAM drive. 
   416             if ( err != KErrNone )
   389         for (TInt i = 0; i < count; ++i)
       
   390             {
       
   391             if ( !iDatabaseHandles[i].iUseRAMdb )
   417                 {
   392                 {
   418                 MPX_DEBUG2("<--CMPXDbManager::CopyDBsFromRamL error=%d", err);                
   393                 continue;
   419                 //anyting wrong, delete the temp file and open database from drive
   394                 }
       
   395             TRAPD( error, CloseDatabaseAtIndexL( i ) );
       
   396             if ( error )
       
   397                 {
       
   398                 // Can't close db on RAM drive, so cleanup.
       
   399                 MPX_DEBUG2("CMPXDbManager::CopyDBsFromRamL CloseDatabaseAtIndexL fail: error = %d", error);
       
   400                 // Delete database on RAM drive.
       
   401                 BaflUtils::DeleteFile(iFs, *iDatabaseHandles[i].iTargetFullFilePath);
       
   402                 // Delete dummy file
   420                 RemoveDummyFile(i);
   403                 RemoveDummyFile(i);
   421                 // delete Db on RAM
   404                 }
   422                 User::LeaveIfError( BaflUtils::DeleteFile(iFs, 
   405             else
   423                     *iDatabaseHandles[i].iTargetFullFilePath));
   406                 {
       
   407                 DoCopyDBFromRam(i);
   424                 }
   408                 }
   425             iDatabaseHandles[i].iUseRAMdb = EFalse;
   409             iDatabaseHandles[i].iUseRAMdb = EFalse;
   426             // open db from drive
   410             // open db from drive
   427             OpenDatabaseL( iDatabaseHandles[i].iDrive );      
   411             TRAP( error, OpenDatabaseAtIndexL( i ) );      
   428             }
   412             if ( error && !leaveError )
       
   413                 {
       
   414                 leaveError = error;
       
   415                 }
       
   416             }
       
   417         
       
   418         // leave if error
       
   419         User::LeaveIfError(leaveError);
   429 
   420 
   430         if (transactionCount > 0) 
   421         if (transactionCount > 0) 
   431             {
   422             {
   432             DoBeginL();
   423             DoBeginL();
   433             iTransactionCount = transactionCount;
   424             iTransactionCount = transactionCount;
   434             }
   425             }
   435         }
   426         }
   436     iRAMInUse = EFalse;
   427 #endif //__RAMDISK_PERF_ENABLE
       
   428     }
       
   429 
       
   430 
       
   431 // ----------------------------------------------------------------------------
       
   432 // CMPXDbManager::DoCopyDBsToRam
       
   433 // ----------------------------------------------------------------------------
       
   434 //
       
   435 void CMPXDbManager::DoCopyDBFromRam( TInt aIndex )
       
   436     {
       
   437 #ifdef __RAMDISK_PERF_ENABLE
       
   438     MPX_DEBUG1("-->CMPXDbManager::DoCopyDBsFromRam");    
       
   439     DatabaseHandle& database = iDatabaseHandles[aIndex];
       
   440 
       
   441     //Copy Db from RAM to replace dummy file
       
   442     TRAPD(error, ReplaceFileL( *database.iTargetFullFilePath, database.iDummyFilePath));
       
   443     MPX_DEBUG2("CMPXDbManager::CopyDBsFromRam RAMDisk copied over dummy, error=%d", error);
       
   444 
       
   445     // done with RAM DB (whether copying succeeded or not) so can delete it
       
   446     // can ignore errors since we cannot do anything if this fails
       
   447     BaflUtils::DeleteFile(iFs, *database.iTargetFullFilePath);
       
   448     MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRam RAM DB deleted");
   437         
   449         
   438     MPX_DEBUG1("<--CMPXDbManager::CopyDBsFromRamL");
   450     if ( error == KErrNone )
   439 #endif //__RAMDISK_PERF_ENABLE
   451         {
   440     }
   452         // Delete old DB on drive
   441 
   453         // Can ignore error: either original does not exist or something is wrong and can't help it
   442 
   454         BaflUtils::DeleteFile(iFs, *database.iOrigFullFilePath);
   443 // ----------------------------------------------------------------------------
   455         MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRam old DB on drive deleted");
   444 // CMPXDbManager::DoCopyDBsToRamL
   456 
   445 // ----------------------------------------------------------------------------
   457         // Rename dummy file to be original file name
   446 //
   458         error = BaflUtils::RenameFile(iFs, database.iDummyFilePath, *database.iOrigFullFilePath);
   447 void CMPXDbManager::DoCopyDBFromRamL( TInt aIndex )
   459         MPX_DEBUG2("CMPXDbManager::CopyDBsFromRam dummy file renamed, error=%d", error);
   448     {
   460         if ( error )
   449 #ifdef __RAMDISK_PERF_ENABLE
   461             {
   450     MPX_DEBUG1("-->CMPXDbManager::DoCopyDBsFromRamL");    
   462             // Error renaming dummy file, delete dummy file.
   451     MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsFromRamL Drive %d will be closed before copying db from RAM.",
   463             RemoveDummyFile(aIndex);
   452          iDatabaseHandles[aIndex].iDrive);
   464             }
   453 
   465         }
   454     CloseDatabaseL(iDatabaseHandles[aIndex].iDrive);            
   466     else
   455 
   467         {
   456     // Delete existing DB on drive
   468         RemoveDummyFile(aIndex);
   457     User::LeaveIfError( BaflUtils::DeleteFile(iFs, 
   469         MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRam dummy file deleted");
   458         *iDatabaseHandles[aIndex].iOrigFullFilePath));
   470         }
   459     MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRamL old DB on drive deleted");
   471 
   460 
   472     MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsFromRam");
   461     // Rename dummy file to be orignal file name
       
   462     User::LeaveIfError( BaflUtils::RenameFile(iFs, 
       
   463         iDatabaseHandles[aIndex].iDummyFilePath, 
       
   464         *iDatabaseHandles[aIndex].iOrigFullFilePath) );
       
   465     MPX_DEBUG1("CMPXDbManager::CopyDBsFromRamL RAMDisk renamed.");
       
   466 
       
   467     //Copy Db from RAM to replace dummy file
       
   468     ReplaceFileL( *iDatabaseHandles[aIndex].iTargetFullFilePath, *iDatabaseHandles[aIndex].iOrigFullFilePath);
       
   469     
       
   470     MPX_DEBUG1("CMPXDbManager::CopyDBsFromRamL RAMDisk copied back.");
       
   471 
       
   472     // Delete existing DB on RAM
       
   473     User::LeaveIfError( BaflUtils::DeleteFile(iFs, *iDatabaseHandles[aIndex].iTargetFullFilePath));
       
   474     MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRamL RAMDisk deleted");
       
   475 
       
   476     MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsFromRamL");
       
   477 #endif //__RAMDISK_PERF_ENABLE
   473 #endif //__RAMDISK_PERF_ENABLE
   478     } 
   474     } 
   479 
   475 
   480 // ----------------------------------------------------------------------------
   476 // ----------------------------------------------------------------------------
   481 // CMPXDbManager::ReplaceFileL
   477 // CMPXDbManager::ReplaceFileL
   482 //
   478 //
   483 // Replaces a file with another writing over the destination file.
   479 // Replaces a file with another writing over the destination file.
   484 // Leaves on error.
   480 // Leaves on error.
   485 // Implementation follows CFileMan::Copy except that 
   481 // Implementation follows CFileMan::Copy except that 
   486 //  - we don't resize target file to zero
   482 //  - we don't resize target file to zero
   487 //  - we can assume that files already exist
   483 //  - we can assume that source file already exists
   488 //  - we don't copy file attributes & timestamp
   484 //  - we don't copy file attributes & timestamp
   489 // ----------------------------------------------------------------------------
   485 // ----------------------------------------------------------------------------
   490 //
   486 //
   491 void CMPXDbManager::ReplaceFileL( const TDesC& aSrcName, const TDesC& aDstName )
   487 void CMPXDbManager::ReplaceFileL( const TDesC& aSrcName, const TDesC& aDstName )
   492     {
   488     {
   494     RFile srcFile;
   490     RFile srcFile;
   495     User::LeaveIfError( srcFile.Open(iFs, aSrcName, EFileRead|EFileShareReadersOnly) );
   491     User::LeaveIfError( srcFile.Open(iFs, aSrcName, EFileRead|EFileShareReadersOnly) );
   496     CleanupClosePushL( srcFile );
   492     CleanupClosePushL( srcFile );
   497     
   493     
   498     RFile dstFile;
   494     RFile dstFile;
   499     User::LeaveIfError( dstFile.Open(iFs, aDstName, EFileWrite|EFileWriteDirectIO|EFileShareExclusive) );
   495 	TInt error = dstFile.Open(iFs, aDstName, EFileWrite|EFileWriteDirectIO|EFileShareExclusive);
       
   496 	if (error == KErrNotFound)
       
   497 	   {
       
   498 	   error = dstFile.Create(iFs, aDstName, EFileWrite|EFileWriteDirectIO|EFileShareExclusive);
       
   499 	   }
       
   500 	User::LeaveIfError ( error );
   500     CleanupClosePushL( dstFile );
   501     CleanupClosePushL( dstFile );
   501     
   502     
   502     // resize destination file
   503     // resize destination file
   503     TInt remainingBytes;
   504     TInt remainingBytes;
   504     User::LeaveIfError( srcFile.Size(remainingBytes) );
   505     User::LeaveIfError( srcFile.Size(remainingBytes) );
   549 #ifdef __RAMDISK_PERF_ENABLE
   550 #ifdef __RAMDISK_PERF_ENABLE
   550     MPX_DEBUG1("-->CMPXDbManager::RemoveDummyFile");
   551     MPX_DEBUG1("-->CMPXDbManager::RemoveDummyFile");
   551        
   552        
   552     if ( iDatabaseHandles[index].iDummyFilePath.Length() )
   553     if ( iDatabaseHandles[index].iDummyFilePath.Length() )
   553         {
   554         {
   554         TInt err = BaflUtils::DeleteFile(iFs, iDatabaseHandles[index].iDummyFilePath);
   555         BaflUtils::DeleteFile(iFs, iDatabaseHandles[index].iDummyFilePath);
   555         if ( !err )
   556         iDatabaseHandles[index].iDummyFilePath.Zero();
   556             {
       
   557             iDatabaseHandles[index].iDummyFilePath.Zero();
       
   558             }
       
   559         }
   557         }
   560     MPX_DEBUG1("<--CMPXDbManager::RemoveDummyFile");
   558     MPX_DEBUG1("<--CMPXDbManager::RemoveDummyFile");
   561 #endif //__RAMDISK_PERF_ENABLE
   559 #endif //__RAMDISK_PERF_ENABLE
   562 
   560 
   563     }
   561     }
   650     CloseAllDatabases();
   648     CloseAllDatabases();
   651 
   649 
   652     TDriveUnit cdrive(KRootDrive());
   650     TDriveUnit cdrive(KRootDrive());
   653 
   651 
   654     CreateDatabaseL(cdrive);
   652     CreateDatabaseL(cdrive);
   655     OpenDatabaseL(cdrive);
   653     OpenRootDatabaseL();
   656 
   654 
   657     TInt count(aDrives.Count());
   655     TInt count(aDrives.Count());
   658     for (TInt i = 0; i < count; ++i)
   656     for (TInt i = 0; i < count; ++i)
   659         {
   657         {
   660         TDriveUnit drive(aDrives[i]);
   658         TDriveUnit drive(aDrives[i]);
   673             handle.iTargetFullFilePath = HBufC::NewL(0);
   671             handle.iTargetFullFilePath = HBufC::NewL(0);
   674             handle.iDummyFilePath.Zero();
   672             handle.iDummyFilePath.Zero();
   675             handle.iUseRAMdb = EFalse;
   673             handle.iUseRAMdb = EFalse;
   676 #endif //__RAMDISK_PERF_ENABLE
   674 #endif //__RAMDISK_PERF_ENABLE
   677 
   675 
       
   676             TInt index = iDatabaseHandles.Count();
   678             iDatabaseHandles.AppendL(handle);
   677             iDatabaseHandles.AppendL(handle);
   679 
   678 
   680             TVolumeInfo vol;
   679             TVolumeInfo vol;
   681             if (iFs.Volume(vol, drive) == KErrNone)
   680             if (iFs.Volume(vol, drive) == KErrNone)
   682                 {
   681                 {
   683                 CreateDatabaseL(drive);
   682                 CreateDatabaseL(drive);
   684                 AttachDatabaseL(drive);
   683                 AttachDatabaseL( index );
   685                 }
   684                 }
   686             }
   685             }
   687         }
   686         }
   688 
   687 
   689     iInitialized = ETrue;
   688     iInitialized = ETrue;
   713         for (TInt i = 0; i < count; ++i)
   712         for (TInt i = 0; i < count; ++i)
   714             {
   713             {
   715             if (iDatabaseHandles[i].iDrive == aDrive)
   714             if (iDatabaseHandles[i].iDrive == aDrive)
   716                 {
   715                 {
   717                 MPX_DEBUG2("CMPXDbManager::OpenDatabaseL found %d", aDrive);
   716                 MPX_DEBUG2("CMPXDbManager::OpenDatabaseL found %d", aDrive);
   718                 if (!iDatabaseHandles[i].iOpen)
   717                 TInt transactionCount = iTransactionCount;
       
   718                 if (iTransactionCount > 0) 
   719                     {
   719                     {
   720                     MPX_DEBUG1("CMPXDbManager::OpenDatabaseL not open found");
   720                     iTransactionCount = 0;
   721                     // make sure the database is created
   721                     DoCommitL();
   722 					CreateDatabaseL(drive);
       
   723 
       
   724 					// attach
       
   725                     AttachDatabaseL(drive);
       
   726                     }
   722                     }
   727 
   723                 OpenDatabaseAtIndexL( i );
       
   724                 if (transactionCount > 0) 
       
   725                     {
       
   726                     DoBeginL();
       
   727                     iTransactionCount = transactionCount;
       
   728                     }
   728                 found = ETrue;
   729                 found = ETrue;
   729                 break;
   730                 break;
   730                 }
   731                 }
   731             }
   732             }
   732         }
   733         }
   738 
   739 
   739     // Close all prepared statements if a db is opened
   740     // Close all prepared statements if a db is opened
   740     //
   741     //
   741     ResetPreparedQueries();
   742     ResetPreparedQueries();
   742     }
   743     }
       
   744     
       
   745 void CMPXDbManager::OpenDatabaseAtIndexL( TInt aIndex )
       
   746     {
       
   747     	  DatabaseHandle & database = iDatabaseHandles[aIndex];
       
   748         if (!database.iOpen)
       
   749             {
       
   750             MPX_DEBUG1("CMPXDbManager::OpenDatabaseAtIndexL not open");
       
   751             // make sure the database is created
       
   752             CreateDatabaseL( TDriveUnit(database.iDrive) );
       
   753             AttachDatabaseL( aIndex );
       
   754             }
       
   755     }
   743 
   756 
   744 // ----------------------------------------------------------------------------
   757 // ----------------------------------------------------------------------------
   745 // Closes a specified database.
   758 // Closes a specified database.
   746 // ----------------------------------------------------------------------------
   759 // ----------------------------------------------------------------------------
   747 //
   760 //
   752 
   765 
   753     if (iInitialized == EFalse)
   766     if (iInitialized == EFalse)
   754         {
   767         {
   755         User::Leave(KErrNotReady);
   768         User::Leave(KErrNotReady);
   756         }
   769         }
   757 
   770     
       
   771     TDriveUnit drive(aDrive);
       
   772     TDriveUnit cdrive(KRootDrive());
       
   773     TBool found(EFalse);
       
   774 
       
   775     if ((drive != cdrive) && !IsRemoteDrive(static_cast<TDriveNumber>(aDrive)))
       
   776         {
       
   777         TInt count(iDatabaseHandles.Count());
       
   778         for (TInt i = 0; i < count; ++i)
       
   779             {
       
   780             if (iDatabaseHandles[i].iDrive == aDrive)
       
   781                 {
       
   782                 TBool inTransaction = InTransaction();
       
   783                 TInt transactionCount = iTransactionCount;
       
   784                 iTransactionCount = 0;								
       
   785 								
       
   786                 if (inTransaction) //if the transaction is ongoing, try committing
       
   787                     {
       
   788                     //if transaction committing fails, try roll-back	
       
   789                     TInt error = iDatabase.Exec( KCommitTransaction );
       
   790                     if ( error != KErrNone )
       
   791                         {
       
   792                         //The error is ignored since we can't do nothing about it
       
   793                         iDatabase.Exec( KRollbackTransaction ); 
       
   794                         }
       
   795                     }
       
   796                 	
       
   797 #ifdef __RAMDISK_PERF_ENABLE                	
       
   798                 if ( iRAMDiskPerfEnabled && iDatabaseHandles[i].iUseRAMdb )
       
   799             	      {
       
   800                     MPX_DEBUG2("CMPXDbManager::CloseDatabaseL found %d at RAM", aDrive);
       
   801                     TRAPD( err, CloseDatabaseAtIndexL( i ) );
       
   802                     if ( err != KErrNone )
       
   803                         {
       
   804                         // Can't close db on RAM drive, so cleanup.
       
   805                         MPX_DEBUG2("CMPXDbManager::CloseDatabaseL CloseDatabaseAtIndexL fail: error = %d", err);
       
   806                         // Delete dummy file
       
   807                         RemoveDummyFile(i);
       
   808                         iDatabaseHandles[i].iUseRAMdb = EFalse;
       
   809                         // Delete database on RAM drive.
       
   810                         User::LeaveIfError( BaflUtils::DeleteFile(iFs, *iDatabaseHandles[i].iTargetFullFilePath) );
       
   811                         }
       
   812                     else
       
   813                         {
       
   814                         DoCopyDBFromRam(i);
       
   815                         }
       
   816                     iDatabaseHandles[i].iUseRAMdb = EFalse;
       
   817                     }
       
   818                 else
       
   819 #endif
       
   820                     {
       
   821                     MPX_DEBUG2("CMPXDbManager::CloseDatabaseL found %d", aDrive);
       
   822                     CloseDatabaseAtIndexL( i );
       
   823                     }
       
   824                 
       
   825                 //Let MTP handle the transcation if there is any 
       
   826                 if ( inTransaction ) 
       
   827                     {
       
   828                     DoBeginL();
       
   829                     iTransactionCount = transactionCount;
       
   830                     }
       
   831 
       
   832                 found = ETrue;
       
   833                 break;
       
   834                 }
       
   835             }
       
   836         }
       
   837     if (!found)
       
   838         {
       
   839         MPX_DEBUG1("CMPXDbManager::CloseDatabaseL not found");
       
   840         User::Leave(KErrArgument);
       
   841         }
       
   842 
       
   843     }
       
   844 
       
   845 void CMPXDbManager::CloseDatabaseAtIndexL( TInt aIndex )
       
   846     {
   758     // Close all prepared statements if a db is closed
   847     // Close all prepared statements if a db is closed
   759     //
   848     //
   760     ResetPreparedQueries();
   849     ResetPreparedQueries();
   761 
   850 
   762     TDriveUnit drive(aDrive);
   851     if (iDatabaseHandles[aIndex].iOpen)
   763     TDriveUnit cdrive(KRootDrive());
   852         {
   764     TBool found(EFalse);
   853         MPX_DEBUG1("CMPXDbManager::CloseDatabaseAtIndexL found open");
   765 
   854         DetachDatabaseL( aIndex );
   766     if ((drive != cdrive) && !IsRemoteDrive(static_cast<TDriveNumber>(aDrive)))
   855         }
   767         {
   856 }
   768         TInt count(iDatabaseHandles.Count());
       
   769         for (TInt i = 0; i < count; ++i)
       
   770             {
       
   771             if (iDatabaseHandles[i].iDrive == aDrive)
       
   772                 {
       
   773                 MPX_DEBUG2("CMPXDbManager::CloseDatabaseL found %d", aDrive);
       
   774                 if (iDatabaseHandles[i].iOpen)
       
   775                     {
       
   776                     MPX_DEBUG1("CMPXDbManager::CloseDatabaseL found open");
       
   777                     DetachDatabaseL(drive);
       
   778                     }
       
   779 
       
   780                 found = ETrue;
       
   781                 break;
       
   782                 }
       
   783             }
       
   784         }
       
   785     if (!found)
       
   786         {
       
   787         MPX_DEBUG1("CMPXDbManager::CloseDatabaseL not found");
       
   788         User::Leave(KErrArgument);
       
   789         }
       
   790 
       
   791     }
       
   792 
   857 
   793 // ----------------------------------------------------------------------------
   858 // ----------------------------------------------------------------------------
   794 // Closes all databases.
   859 // Closes all databases.
   795 // ----------------------------------------------------------------------------
   860 // ----------------------------------------------------------------------------
   796 //
   861 //
   806 
   871 
   807         TInt count(iDatabaseHandles.Count());
   872         TInt count(iDatabaseHandles.Count());
   808         for (TInt i = 0; i < count; ++i)
   873         for (TInt i = 0; i < count; ++i)
   809             {
   874             {
   810             delete iDatabaseHandles[i].iAliasname;
   875             delete iDatabaseHandles[i].iAliasname;
       
   876             iDatabaseHandles[i].iAliasname = 0;
   811 #ifdef __RAMDISK_PERF_ENABLE 
   877 #ifdef __RAMDISK_PERF_ENABLE 
       
   878             RemoveDummyFile(i);            	
   812             delete iDatabaseHandles[i].iOrigFullFilePath;
   879             delete iDatabaseHandles[i].iOrigFullFilePath;
       
   880 			iDatabaseHandles[i].iOrigFullFilePath = 0;
   813             delete iDatabaseHandles[i].iTargetFullFilePath;
   881             delete iDatabaseHandles[i].iTargetFullFilePath;
   814             iDatabaseHandles[i].iDummyFilePath.Zero();
   882 			iDatabaseHandles[i].iTargetFullFilePath = 0;
   815 #endif //__RAMDISK_PERF_ENABLE 
   883 #endif //__RAMDISK_PERF_ENABLE 
   816             }
   884             }
   817 
   885 
   818         iDatabaseHandles.Reset();
   886         iDatabaseHandles.Reset();
   819         iDatabase.Close();
   887         iDatabase.Close();
   829     {
   897     {
   830     MPX_FUNC("CMPXDbManager::OpenAllDatabasesL");
   898     MPX_FUNC("CMPXDbManager::OpenAllDatabasesL");
   831 
   899 
   832     if (!iInitialized)
   900     if (!iInitialized)
   833         {
   901         {
   834         TDriveUnit cdrive(KRootDrive());
   902         OpenRootDatabaseL();
   835         OpenDatabaseL(cdrive);
       
   836         }
   903         }
   837 
   904 
   838     TInt count(iDatabaseHandles.Count());
   905     TInt count(iDatabaseHandles.Count());
   839     for (TInt i = 0; i < count; ++i)
   906     for (TInt i = 0; i < count; ++i)
   840         {
   907         {
   841         TVolumeInfo vol;
   908         TVolumeInfo vol;
   842         if (iFs.Volume(vol, iDatabaseHandles[i].iDrive) == KErrNone)
   909         if (iFs.Volume(vol, iDatabaseHandles[i].iDrive) == KErrNone)
   843             {
   910             {
   844             AttachDatabaseL(iDatabaseHandles[i].iDrive);
   911             AttachDatabaseL( i );
   845             }
   912             }
   846         }
   913         }
   847     iInitialized = ETrue;
   914     iInitialized = ETrue;
   848 
   915 
   849     // Close all prepared statements if a db is closed
   916     // Close all prepared statements if a db is closed
   937     if (iInitialized == EFalse)
  1004     if (iInitialized == EFalse)
   938         {
  1005         {
   939         User::Leave(KErrNotReady);
  1006         User::Leave(KErrNotReady);
   940         }
  1007         }
   941 
  1008 
   942     TBool found(EFalse);
  1009     TInt index = KErrNotFound;
   943 
  1010 
   944     if (aDrive == EDriveC)
  1011     if (aDrive == EDriveC)
   945         {
  1012         {
   946         found = ETrue;
  1013         index = iDatabaseHandles.Count();
   947         }
  1014         }
   948     else
  1015     else
   949         {
  1016         {
   950         TInt count(iDatabaseHandles.Count());
  1017         TInt count(iDatabaseHandles.Count());
   951         for (TInt i = 0; i < count; ++i)
  1018         for (TInt i = 0; i < count; ++i)
   952             {
  1019             {
   953             if ((iDatabaseHandles[i].iDrive == aDrive) && (iDatabaseHandles[i].iOpen))
  1020             if ((iDatabaseHandles[i].iDrive == aDrive) && (iDatabaseHandles[i].iOpen))
   954                 {
  1021                 {
   955                 found = ETrue;
  1022                 index = i;
   956                 break;
  1023                 break;
   957                 }
  1024                 }
   958             }
  1025             }
   959         }
  1026         }
   960     if (found)
  1027     if ( index >= 0 )
   961         {
  1028         {
   962         HBufC * filename = CreateFilenameL(aDrive);
  1029         HBufC * filename = CreateFilenameL(aDrive);
   963         CleanupStack::PushL(filename);
  1030         CleanupStack::PushL(filename);
   964 
  1031 
   965         TRAPD(err, DoRecreateDatabaseL(filename));
  1032         TRAPD(err, DoRecreateDatabaseL(filename));
   978                 User::LeaveIfError(iDatabase.Open(*filename));
  1045                 User::LeaveIfError(iDatabase.Open(*filename));
   979                 iInitialized = ETrue;
  1046                 iInitialized = ETrue;
   980                 }
  1047                 }
   981             else
  1048             else
   982                 {
  1049                 {
   983                 DetachDatabaseL(drive_unit);
  1050                 DetachDatabaseL( index );
   984 
  1051 
   985                 RSqlDatabase::Delete(*filename);
  1052                 RSqlDatabase::Delete(*filename);
   986                 CreateDatabaseL(drive_unit);
  1053                 CreateDatabaseL(drive_unit);
   987 
  1054 
   988                 AttachDatabaseL(drive_unit);
  1055                 AttachDatabaseL( index );
   989                 }
  1056                 }
   990             }
  1057             }
   991 
  1058 
   992         CleanupStack::PopAndDestroy(filename);
  1059         CleanupStack::PopAndDestroy(filename);
   993         }
  1060         }
  1635 // Resets all prepared queries
  1702 // Resets all prepared queries
  1636 // ----------------------------------------------------------------------------
  1703 // ----------------------------------------------------------------------------
  1637 //
  1704 //
  1638 void CMPXDbManager::ResetPreparedQueries()
  1705 void CMPXDbManager::ResetPreparedQueries()
  1639     {
  1706     {
       
  1707     MPX_FUNC("CMPXDbManager::ResetPreparedQueries");
  1640     iPreparedStatements.Reset();
  1708     iPreparedStatements.Reset();
  1641 
  1709 
  1642     TInt c( iStatements.Count() );
  1710     TInt c( iStatements.Count() );
  1643     for( TInt i=0; i<c; ++i )
  1711     for( TInt i=0; i<c; ++i )
  1644         {
  1712         {
  1673         iTables[i]->CreateTableL(aDatabase, aCorrupt);
  1741         iTables[i]->CreateTableL(aDatabase, aCorrupt);
  1674         }
  1742         }
  1675 	}
  1743 	}
  1676 
  1744 
  1677 // ----------------------------------------------------------------------------
  1745 // ----------------------------------------------------------------------------
  1678 // Opens a specified database.
  1746 // Opens root database on C-drive
  1679 // ----------------------------------------------------------------------------
  1747 // ----------------------------------------------------------------------------
  1680 //
  1748 //
  1681 void CMPXDbManager::OpenDatabaseL(
  1749 void CMPXDbManager::OpenRootDatabaseL()
  1682     TDriveUnit aDrive)
  1750     {
  1683     {
  1751     MPX_FUNC("CMPXDbManager::OpenRootDatabaseL");
  1684     MPX_FUNC("CMPXDbManager::OpenDatabaseL");
  1752         TDriveUnit cdrive(KRootDrive());
  1685 
  1753     HBufC * filename = CreateFilenameL(cdrive);
  1686     HBufC * filename = CreateFilenameL(aDrive);
       
  1687     CleanupStack::PushL(filename);
  1754     CleanupStack::PushL(filename);
  1688     User::LeaveIfError(iDatabase.Open(filename->Des()));
  1755     User::LeaveIfError(iDatabase.Open(*filename));
  1689 
  1756 
  1690     CleanupStack::PopAndDestroy(filename);
  1757     CleanupStack::PopAndDestroy(filename);
  1691     }
  1758     }
  1692 
  1759 
  1693 // ----------------------------------------------------------------------------
  1760 // ----------------------------------------------------------------------------
  1744 
  1811 
  1745 // ----------------------------------------------------------------------------
  1812 // ----------------------------------------------------------------------------
  1746 // Attaches a specified database.
  1813 // Attaches a specified database.
  1747 // ----------------------------------------------------------------------------
  1814 // ----------------------------------------------------------------------------
  1748 //
  1815 //
  1749 void CMPXDbManager::AttachDatabaseL(
  1816 void CMPXDbManager::AttachDatabaseL( TInt aIndex )
  1750     TDriveUnit aDrive)
       
  1751     {
  1817     {
  1752     MPX_FUNC("CMPXDbManager::AttachDatabaseL");
  1818     MPX_FUNC("CMPXDbManager::AttachDatabaseL");
  1753 
  1819     ASSERT( aIndex < iDatabaseHandles.Count() );
  1754     TBool found(EFalse);
  1820     DatabaseHandle & database = iDatabaseHandles[ aIndex ];
  1755 
  1821     if (!database.iOpen)
  1756     TInt count(iDatabaseHandles.Count());
  1822         {
  1757     for (TInt i = 0; i < count; ++i)
  1823         HBufC* filename = CreateFilenameL( database.iDrive );
  1758         {
  1824         CleanupStack::PushL(filename);
  1759         if (iDatabaseHandles[i].iDrive == aDrive)
       
  1760             {
       
  1761             if (!iDatabaseHandles[i].iOpen)
       
  1762                 {
       
  1763                 HBufC* filename = CreateFilenameL(aDrive);
       
  1764                 CleanupStack::PushL(filename);
       
  1765                 
  1825                 
  1766 #ifdef __RAMDISK_PERF_ENABLE
  1826 #ifdef __RAMDISK_PERF_ENABLE
  1767                 if( iDatabaseHandles[i].iUseRAMdb )
  1827         if( database.iUseRAMdb )
  1768                     {
  1828             {
  1769                     delete iDatabaseHandles[i].iAliasname;
  1829             delete database.iAliasname;
  1770                     iDatabaseHandles[i].iAliasname = HBufC::NewL(KAliasName().Length());
  1830             database.iAliasname = HBufC::NewL(KAliasName().Length());
  1771                     HBufC* temp = HBufC::NewLC(2); // form of DE, DF, DX,...
  1831             HBufC* temp = HBufC::NewLC(2); // form of DE, DF, DX,...
  1772                     temp->Des().Append(iRAMDrive); // length == 2
  1832             temp->Des().Append(iRAMDrive); // length == 2
  1773                     TDriveUnit pdrive(aDrive);
  1833             TDriveUnit pdrive( database.iDrive );
  1774                     temp->Des().Append(pdrive.Name().Left(1)); //length == 2+ 1
  1834             temp->Des().Append(pdrive.Name().Left(1)); //length == 2+ 1
  1775                     iDatabaseHandles[i].iAliasname->Des().Format(KRAMAliasName, temp);
  1835             database.iAliasname->Des().Format(KRAMAliasName, temp);
  1776                     MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - RAM change aliasname of %S", iDatabaseHandles[i].iAliasname );
  1836             MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - RAM change aliasname of %S", database.iAliasname );
  1777                     CleanupStack::PopAndDestroy(temp);
  1837             CleanupStack::PopAndDestroy(temp);
  1778                     }
  1838             }
  1779                 else
  1839         else
  1780 #endif //__RAMDISK_PERF_ENABLE
  1840 #endif //__RAMDISK_PERF_ENABLE
  1781                    {
  1841             {
  1782                    delete iDatabaseHandles[i].iAliasname;
  1842             delete database.iAliasname;
  1783                    TDriveUnit drive(aDrive);
  1843             TDriveUnit drive( database.iDrive );
  1784                    const TDesC& driveName = drive.Name();
  1844             const TDesC& driveName = drive.Name();
  1785                    iDatabaseHandles[i].iAliasname = HBufC::NewL(KAliasName().Length());
  1845             database.iAliasname = HBufC::NewL(KAliasName().Length());
  1786                    iDatabaseHandles[i].iAliasname->Des().Format(KAliasName, &driveName);
  1846             database.iAliasname->Des().Format(KAliasName, &driveName);
  1787                    MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - normal change aliasname of %S", iDatabaseHandles[i].iAliasname);
  1847             MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - normal change aliasname of %S", database.iAliasname);
  1788                    }
  1848             }
  1789 
  1849 
  1790                 TInt err = iDatabase.Attach(filename->Des(), *(iDatabaseHandles[i].iAliasname));
  1850         TInt err = iDatabase.Attach( *filename, *database.iAliasname );
  1791                 MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - Attach Error =%d", err);
  1851         MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - Attach Error =%d", err);
  1792                 User::LeaveIfError(err);
  1852         User::LeaveIfError(err);
  1793                 iDatabaseHandles[i].iOpen = ETrue;
  1853         database.iOpen = ETrue;
  1794 
  1854 
  1795                 CleanupStack::PopAndDestroy(filename);
  1855         CleanupStack::PopAndDestroy(filename);
  1796                 }
  1856         }
  1797             else
  1857     else
  1798                 {
  1858         {
  1799                 MPX_DEBUG1("CMPXDbManager::AttachDatabaseL - found already open");    
  1859         MPX_DEBUG1("CMPXDbManager::AttachDatabaseL - found already open");    
  1800                 }
       
  1801             
       
  1802             found = ETrue;
       
  1803             break;
       
  1804             }
       
  1805         }
       
  1806     if (!found)
       
  1807         {
       
  1808         MPX_DEBUG1("CMPXDbManager::AttachDatabaseL - not found");
       
  1809         User::Leave(KErrNotFound);
       
  1810         }
  1860         }
  1811     }
  1861     }
  1812 
  1862 
  1813 // ----------------------------------------------------------------------------
  1863 // ----------------------------------------------------------------------------
  1814 // Detaches a specified database.
  1864 // Detaches a specified database.
  1815 // ----------------------------------------------------------------------------
  1865 // ----------------------------------------------------------------------------
  1816 //
  1866 //
  1817 void CMPXDbManager::DetachDatabaseL(
  1867 void CMPXDbManager::DetachDatabaseL( TInt aIndex )
  1818     TDriveUnit aDrive)
       
  1819     {
  1868     {
  1820     MPX_FUNC("CMPXDbManager::DetachDatabaseL");
  1869     MPX_FUNC("CMPXDbManager::DetachDatabaseL");
  1821 
  1870 
  1822     ASSERT(iInitialized);
  1871     ASSERT( iInitialized && aIndex < iDatabaseHandles.Count() );
  1823     TBool found(EFalse);
  1872     DatabaseHandle & database = iDatabaseHandles[ aIndex ];
  1824 
  1873     if ( database.iOpen )
  1825     TInt count(iDatabaseHandles.Count());
  1874         {
  1826     for (TInt i = 0; i < count; ++i)
  1875         MPX_DEBUG2("CMPXDbManager::DetachDatabaseL iAliasname=%S is open",database.iAliasname );
  1827         {
  1876         TInt err = iDatabase.Detach(*(database.iAliasname));
  1828         if (iDatabaseHandles[i].iDrive == aDrive)
  1877         if ( err )
  1829             {
  1878             {
  1830 #ifdef __RAMDISK_PERF_ENABLE
  1879             MPX_DEBUG2("CMPXDbManager::DetachDatabaseL detach failed Error=%d", err);
  1831             if ( iDatabaseHandles[i].iOpen || iDatabaseHandles[i].iUseRAMdb )
  1880             }
  1832 #else //__RAMDISK_PERF_ENABLE
  1881         User::LeaveIfError(err);
  1833             if ( iDatabaseHandles[i].iOpen )
  1882         database.iOpen = EFalse;
  1834 #endif //__RAMDISK_PERF_ENABLE
       
  1835 
       
  1836                 {
       
  1837                 MPX_DEBUG1("CMPXDbManager::DetachDatabaseL found drive that is opening");
       
  1838                 TInt err = iDatabase.Detach(*(iDatabaseHandles[i].iAliasname));
       
  1839                 if ( err )
       
  1840                     {
       
  1841                     MPX_DEBUG2("CMPXDbManager::DetachDatabaseL detach failed Error=%d", err);
       
  1842                     }
       
  1843                 else
       
  1844                     {
       
  1845                     MPX_DEBUG2("CMPXDbManager::DetachDatabaseL iAliasname=%S", iDatabaseHandles[i].iAliasname);
       
  1846                     }
       
  1847                 User::LeaveIfError(err);
       
  1848                 iDatabaseHandles[i].iOpen = EFalse;
       
  1849                 }
       
  1850 
       
  1851             found = ETrue;
       
  1852             break;
       
  1853             }
       
  1854         }
       
  1855     if (!found)
       
  1856         {
       
  1857         MPX_DEBUG1("CMPXDbManager::DetachDatabaseL drive not found in iDatabaseHandlers");
       
  1858         User::Leave(KErrNotFound);
       
  1859         }
  1883         }
  1860     }
  1884     }
  1861 
  1885 
  1862 // ----------------------------------------------------------------------------
  1886 // ----------------------------------------------------------------------------
  1863 // Creates the absolute database filename on a specified drive.
  1887 // Creates the absolute database filename on a specified drive.
  1881         TFileName path;
  1905         TFileName path;
  1882         path.Append(iRAMDrive);
  1906         path.Append(iRAMDrive);
  1883         path.Append(_L(":"));
  1907         path.Append(_L(":"));
  1884         TBuf<2> d;
  1908         TBuf<2> d;
  1885         d.Append(aDrive.Name());
  1909         d.Append(aDrive.Name());
  1886         HBufC* temp = HBufC::NewLC(KMaxFileName);
  1910         TFileName temp;
  1887         temp->Des().Append(d.Left(1)); // attach original drive name
  1911         temp.Append(d.Left(1)); // attach original drive name
  1888         temp->Des().Append(iDbFile->Des()); 
  1912         temp.Append(iDbFile->Des()); 
  1889         filename->Des().Format(securefilePath, &path, User::Identity().iUid, temp);
  1913         filename->Des().Format(securefilePath, &path, User::Identity().iUid, &temp);
  1890         MPX_DEBUG3("CMPXDbManager::CreateFilenameL - path=%S filename=%S", &path, filename);
  1914         MPX_DEBUG3("CMPXDbManager::CreateFilenameL - path=%S filename=%S", &path, filename);
  1891         CleanupStack::PopAndDestroy(temp);
       
  1892         }
  1915         }
  1893     else
  1916     else
  1894 #endif //__RAMDISK_PERF_ENABLE
  1917 #endif //__RAMDISK_PERF_ENABLE
  1895         {
  1918         {
  1896         MPX_DEBUG1("CMPXDbManager::CreateFilenameL - use normal drive");
  1919         MPX_DEBUG1("CMPXDbManager::CreateFilenameL - use normal drive");
  1897         const TDesC& driveName = aDrive.Name();
  1920         const TDesC& driveName = aDrive.Name();
  1898         filename->Des().Format(securefilePath, &driveName, User::Identity().iUid, iDbFile);
  1921         filename->Des().Format(securefilePath, &driveName, User::Identity().iUid, iDbFile);
  1899 
       
  1900         }
  1922         }
  1901     
  1923     
  1902     MPX_DEBUG2("CMPXDbManager::CreateFilenameL filename = %S", filename); 
  1924     MPX_DEBUG2("CMPXDbManager::CreateFilenameL filename = %S", filename); 
  1903     return filename;
  1925     return filename;
  1904     }
  1926     }
  2052             err = RSqlDatabase::Delete(*filename);
  2074             err = RSqlDatabase::Delete(*filename);
  2053             MPX_DEBUG2("RegenerateAllDatabasesL: Deleted[err=%d]; Creating new DB",err);
  2075             MPX_DEBUG2("RegenerateAllDatabasesL: Deleted[err=%d]; Creating new DB",err);
  2054             TDriveUnit drive(iDatabaseHandles[i].iDrive);
  2076             TDriveUnit drive(iDatabaseHandles[i].iDrive);
  2055             CreateDatabaseL(drive);
  2077             CreateDatabaseL(drive);
  2056             MPX_DEBUG1("RegenerateAllDatabasesL: Attaching new DB");
  2078             MPX_DEBUG1("RegenerateAllDatabasesL: Attaching new DB");
  2057             AttachDatabaseL(drive);    
  2079             AttachDatabaseL( i );    
  2058             MPX_DEBUG1("RegenerateAllDatabasesL: DB regeneration complete");
  2080             MPX_DEBUG1("RegenerateAllDatabasesL: DB regeneration complete");
  2059             CleanupStack::PopAndDestroy(filename);
  2081             CleanupStack::PopAndDestroy(filename);
  2060             }
  2082             }
  2061         else
  2083         else
  2062             {
  2084             {
  2605 
  2627 
  2606 // ---------------------------------------------------------------------------
  2628 // ---------------------------------------------------------------------------
  2607 // CMPXDbManager::BlockDiskSpaceL
  2629 // CMPXDbManager::BlockDiskSpaceL
  2608 // ---------------------------------------------------------------------------
  2630 // ---------------------------------------------------------------------------
  2609 //
  2631 //
  2610 TBool CMPXDbManager::BlockDiskSpace( TDriveUnit aDrive, TInt aOrigDbSize, TBool aIsMTPInUse )
  2632 TBool CMPXDbManager::BlockDiskSpace( TInt aIndex, TBool aIsMTPInUse )
  2611     {
  2633     {
  2612 #ifdef __RAMDISK_PERF_ENABLE
  2634 #ifdef __RAMDISK_PERF_ENABLE
  2613 
  2635 
  2614     MPX_DEBUG2("-->CMPXDbManager::BlockDiskSpaceL %d", (TInt)aDrive );
  2636     MPX_DEBUG2("-->CMPXDbManager::BlockDiskSpaceL %d", aIndex );
  2615     
  2637     DatabaseHandle & database = iDatabaseHandles[aIndex];    
  2616     // if current DB size can not fit in RAM, abort now
  2638     // if current DB size can not fit in RAM, abort now
  2617     TInt ramDrive;
  2639     TInt ramDrive;
  2618     RFs::CharToDrive(iRAMDrive, ramDrive);
  2640     RFs::CharToDrive(iRAMDrive, ramDrive);
  2619     TVolumeInfo vol;
  2641     TVolumeInfo vol;
  2620     TInt err = iFs.Volume( vol, ramDrive );
  2642     TInt err = iFs.Volume( vol, ramDrive );
  2621     if ( vol.iFree <= aOrigDbSize + KMPMinimumRAMSizeToRun )
  2643     TEntry origDb;
       
  2644     iFs.Entry( *database.iOrigFullFilePath, origDb );
       
  2645     if ( vol.iFree <= origDb.iSize + KMPMinimumRAMSizeToRun )
  2622         {
  2646         {
  2623         MPX_DEBUG1("-->CMPXDbManager::BlockDiskSpaceL Not enough even for copy original DB file, leave" );
  2647         MPX_DEBUG1("-->CMPXDbManager::BlockDiskSpaceL Not enough even for copy original DB file, leave" );
  2624         return EFalse;
  2648         return EFalse;
  2625         }
  2649         }
  2626 
  2650 
  2627     // ensure you have the disk volume and database
  2651     // ensure you have the disk volume and database
  2628     err = iFs.Volume( vol, (TInt)aDrive );
  2652     err = iFs.Volume( vol, database.iDrive );
  2629     if (err != KErrNone) 
  2653     if (err != KErrNone) 
  2630         {
  2654         {
  2631         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Volume not available on drive %d", (TInt)aDrive);
  2655         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Volume not available on drive %d", database.iDrive);
  2632         return EFalse;
       
  2633         }
       
  2634 
       
  2635     TInt index( GetDatabaseIndex((TInt)aDrive) );
       
  2636     if (index < 0) 
       
  2637         {
       
  2638         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Database not available for drive %d", (TInt)aDrive);
       
  2639         return EFalse;
  2656         return EFalse;
  2640         }
  2657         }
  2641 
  2658 
  2642     // Check if the drive has enough space to block
  2659     // Check if the drive has enough space to block
  2643     MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk total free space in bytes =%Lu", vol.iFree);
  2660     MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk total free space in bytes =%Lu", vol.iFree);
  2644     TInt64 blockingSize( CalculateInitalDummyDBSize( vol, aOrigDbSize, aIsMTPInUse ));
  2661     TInt64 blockingSize( CalculateInitalDummyDBSize( vol, origDb.iSize, aIsMTPInUse ));
  2645     MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk blocking size =%Lu", blockingSize);
  2662     MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk blocking size =%Lu", blockingSize);
  2646     if ( vol.iFree <= blockingSize + 1*KMPMegaByte )
  2663     if ( vol.iFree <= blockingSize + 1*KMPMegaByte )
  2647         {
  2664         {
  2648         MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL NOk to block");
  2665         MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL NOk to block");
  2649         return EFalse;
  2666         return EFalse;
  2650         }
  2667         }
  2651 
  2668 
  2652     // Create and resize the dummy file
  2669     // Create and resize the dummy file
  2653     TFileName dummyDbFileName; 
       
  2654     TChar ch;
  2670     TChar ch;
  2655     RFs::DriveToChar((TInt)aDrive, ch );
  2671     RFs::DriveToChar(database.iDrive, ch );
  2656     dummyDbFileName.Format( KDummyDbFile, (TUint)ch);
  2672     database.iDummyFilePath.Format( KDummyDbFile, (TUint)ch);
  2657     RFile dummyDb;
  2673     RFile dummyDb;
  2658     err = dummyDb.Replace( iFs, dummyDbFileName, EFileWrite );
  2674     err = dummyDb.Replace( iFs, database.iDummyFilePath, EFileWrite );
  2659     if (err != KErrNone) 
  2675     if (err != KErrNone) 
  2660         {
  2676         {
  2661         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't open dummy file %d", err);
  2677         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't open dummy file %d", err);
       
  2678         database.iDummyFilePath.Zero();
  2662         return EFalse;
  2679         return EFalse;
  2663         }
  2680         }
  2664     err = dummyDb.SetSize( blockingSize );
  2681     err = dummyDb.SetSize( blockingSize );
  2665     if ( err )
  2682     if ( err )
  2666         {
  2683         {
  2667         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't resize dummy file %d", err);
  2684         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't resize dummy file %d", err);
  2668         dummyDb.Close();
  2685         dummyDb.Close();
       
  2686         RemoveDummyFile(aIndex);
  2669         return EFalse;
  2687         return EFalse;
  2670         }
  2688         }
  2671 
  2689 
  2672     dummyDb.Close();
  2690     dummyDb.Close();
  2673     MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL Ok to block");
  2691     MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL Ok to block");
  2674 
       
  2675     iDatabaseHandles[index].iDummyFilePath.Copy(dummyDbFileName);
       
  2676     MPX_DEBUG1("<--CMPXDbManager::BlockDiskSpace");
  2692     MPX_DEBUG1("<--CMPXDbManager::BlockDiskSpace");
  2677 
  2693 
  2678     return ETrue;
  2694     return ETrue;
  2679 #else
  2695 #else
  2680     return EFalse;
  2696     return EFalse;
  2799             CopyDBsFromRamL();
  2815             CopyDBsFromRamL();
  2800             }
  2816             }
  2801         else
  2817         else
  2802             {
  2818             {
  2803             TInt size=0;
  2819             TInt size=0;
  2804             TInt err = GetTotalRamDatabasesSize(size);
  2820             TInt err = GetTotalRamDatabasesSizeL(size);
  2805             if ( err || (size > iMaximumAllowedRAMDiskSpaceToCopy) )
  2821             if ( err || (size > iMaximumAllowedRAMDiskSpaceToCopy) )
  2806                 {
  2822                 {
  2807                 // Databases using too much RAM space, copy back to normal drive and continue to harvest.
  2823                 // Databases using too much RAM space, copy back to normal drive and continue to harvest.
  2808                 if ( err )
  2824                 if ( err )
  2809                     {
  2825                     {
  2887         }
  2903         }
  2888     
  2904     
  2889     TInt count(iDatabaseHandles.Count());
  2905     TInt count(iDatabaseHandles.Count());
  2890     for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb ; ++i)
  2906     for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb ; ++i)
  2891         {
  2907         {
  2892         CloseDatabaseL( iDatabaseHandles[i].iDrive );            
  2908         CloseDatabaseAtIndexL( i );            
  2893 
  2909 
  2894         TInt err= BaflUtils::CopyFile(iFs, 
  2910         TInt err= BaflUtils::CopyFile(iFs, 
  2895             iDatabaseHandles[i].iTargetFullFilePath->Des(), 
  2911             iDatabaseHandles[i].iTargetFullFilePath->Des(), 
  2896             iDatabaseHandles[i].iOrigFullFilePath->Des());
  2912             iDatabaseHandles[i].iOrigFullFilePath->Des());
  2897 
  2913 
  2898         MPX_DEBUG2("CMPXDbManager::BackupDBsL err = %d", err);     
  2914         MPX_DEBUG2("CMPXDbManager::BackupDBsL err = %d", err);     
  2899 
  2915 
  2900         OpenDatabaseL( iDatabaseHandles[i].iDrive );      
  2916         OpenDatabaseAtIndexL( i );      
  2901         }
  2917         }
  2902         
  2918         
  2903     if (transactionCount > 0) 
  2919     if (transactionCount > 0) 
  2904         {
  2920         {
  2905         DoBeginL();
  2921         DoBeginL();
  2958     
  2974     
  2959 // ---------------------------------------------------------------------------
  2975 // ---------------------------------------------------------------------------
  2960 // CMPXDbManager::GetTotalRamDatabasesSize
  2976 // CMPXDbManager::GetTotalRamDatabasesSize
  2961 // ---------------------------------------------------------------------------
  2977 // ---------------------------------------------------------------------------
  2962 //
  2978 //
  2963 TInt CMPXDbManager::GetTotalRamDatabasesSize(TInt& aSize)
  2979 TInt CMPXDbManager::GetTotalRamDatabasesSizeL(TInt& aSize)
  2964     {
  2980     {
  2965     MPX_FUNC("CMPXDbManager::GetTotalRamDatabasesSize");
  2981     MPX_FUNC("CMPXDbManager::GetTotalRamDatabasesSize");
  2966     TInt err = KErrNotSupported;
  2982     TInt err = KErrNotSupported;
  2967 #ifdef __RAMDISK_PERF_ENABLE
  2983 #ifdef __RAMDISK_PERF_ENABLE
  2968     TInt size=0;
  2984     TInt size=0;
  2986         temp->Des().Append(iDbFile->Des());
  3002         temp->Des().Append(iDbFile->Des());
  2987         TFileName filename;            
  3003         TFileName filename;            
  2988         filename.Format(KSecurePath, User::Identity().iUid, temp);
  3004         filename.Format(KSecurePath, User::Identity().iUid, temp);
  2989         CleanupStack::PopAndDestroy(temp);
  3005         CleanupStack::PopAndDestroy(temp);
  2990         dbFilename.Append(filename);
  3006         dbFilename.Append(filename);
  2991         MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Database name = %S", &dbFilename);
  3007         MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSizeL - Database name = %S", &dbFilename);
  2992         TEntry entry;
  3008         TEntry entry;
  2993         err = iFs.Entry( dbFilename, entry );
  3009         err = iFs.Entry( dbFilename, entry );
  2994         if ( (err != KErrNone) && (err != KErrNotFound) )
  3010         if ( (err != KErrNone) && (err != KErrNotFound) )
  2995             {
  3011             {
  2996             break;
  3012             break;
  2997             }
  3013             }
  2998         MPX_DEBUG3("CMPXDbManager::GetTotalRamDatabasesSize - Size of Db %S = %d", &dbFilename, entry.iSize);
  3014         MPX_DEBUG3("CMPXDbManager::GetTotalRamDatabasesSizeL - Size of Db %S = %d", &dbFilename, entry.iSize);
  2999         // sum up size
  3015         // sum up size
  3000         size += entry.iSize;
  3016         size += entry.iSize;
  3001         }
  3017         }
  3002     aSize = size;
  3018     aSize = size;
  3003     MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Total Size of Dbs = %d", size);
  3019     MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSizeL - Total Size of Dbs = %d", size);
  3004 #endif //__RAMDISK_PERF_ENABLE    
  3020 #endif //__RAMDISK_PERF_ENABLE    
  3005     MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Return err = %d", err);
  3021     MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSizeL - Return err = %d", err);
  3006     return err;
  3022     return err;
  3007     }
  3023     }
  3008     
  3024 
       
  3025 // ----------------------------------------------------------------------------
       
  3026 // Creates the absolute database filename on a specified drive.
       
  3027 // ----------------------------------------------------------------------------
       
  3028 //
       
  3029 HBufC* CMPXDbManager::CreateFullFilenameL(TDriveUnit aDrive)
       
  3030     {
       
  3031     MPX_FUNC("CMPXDbManager::CreateFullFilenameL");
       
  3032 
       
  3033     HBufC* filename = HBufC::NewL(KMaxFileName);
       
  3034     const TDesC& securefilePath = KSecureFilePath;
       
  3035     TDriveUnit cdrive(KRootDrive());
       
  3036 
       
  3037 #ifdef __RAMDISK_PERF_ENABLE
       
  3038     TInt index(GetDatabaseIndex((TInt)aDrive));    
       
  3039     if ( index >=0 && iDatabaseHandles[index].iUseRAMdb && aDrive != cdrive )
       
  3040         {
       
  3041         MPX_DEBUG1("CMPXDbManager::CreateFullFilenameL - use RAMDisk");
       
  3042         TFileName path;
       
  3043         path.Append(iRAMDrive);
       
  3044         path.Append(_L(":"));
       
  3045         path.Append(KDBFilePath);
       
  3046         TBuf<2> d;
       
  3047         d.Append(aDrive.Name());
       
  3048         TFileName temp;
       
  3049         temp.Append(d.Left(1)); // attach original drive name
       
  3050         temp.Append(iDbFile->Des()); 
       
  3051         filename->Des().Format(securefilePath, &path, User::Identity().iUid, &temp);
       
  3052         MPX_DEBUG3("CMPXDbManager::CreateFullFilenameL - path=%S filename=%S", &path, filename);
       
  3053         }
       
  3054     else
       
  3055 #endif //__RAMDISK_PERF_ENABLE
       
  3056         {
       
  3057         MPX_DEBUG1("CMPXDbManager::CreateFullFilenameL - use normal drive");
       
  3058         TFileName dbPath;
       
  3059         dbPath.Append(aDrive.Name());
       
  3060         dbPath.Append(KDBFilePath);
       
  3061         filename->Des().Format(securefilePath, &dbPath, User::Identity().iUid, iDbFile);
       
  3062         }
       
  3063     
       
  3064     MPX_DEBUG2("CMPXDbManager::CreateFullFilenameL filename = %S", filename); 
       
  3065     return filename;
       
  3066     }
       
  3067 
  3009 // End of File
  3068 // End of File