mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbcommon/src/mpxdbmanager.cpp
branchRCL_3
changeset 9 13afc0e517bd
parent 0 ff3acec5bc43
child 14 c54d95799c80
equal deleted inserted replaced
5:2a40e88564c8 9:13afc0e517bd
    25 // PREQ2536 the files sqlrowsetutil.h and sqlrowsetutil.cpp has been removed
    25 // PREQ2536 the files sqlrowsetutil.h and sqlrowsetutil.cpp has been removed
    26 //#ifdef __WINSCW__    
    26 //#ifdef __WINSCW__    
    27 //#include <sqlrowsetutil.h>
    27 //#include <sqlrowsetutil.h>
    28 //#endif
    28 //#endif
    29 #include <sysutil.h>
    29 #include <sysutil.h>
       
    30 #ifdef __RAMDISK_PERF_ENABLE
       
    31 #include <centralrepository.h>
       
    32 #include <BAUTILS.H>  
       
    33 #include <mpxinternalcrkeys.h>
       
    34 #endif //__RAMDISK_PERF_ENABLE
    30 
    35 
    31 #include <mpxlog.h>
    36 #include <mpxlog.h>
    32 
    37 
    33 #include "mpxdbcommondef.h"
    38 #include "mpxdbcommondef.h"
    34 #include "mpxtable.h"
    39 #include "mpxtable.h"
    66 #endif
    71 #endif
    67 
    72 
    68 const TInt KMaxLogQuery = 248;
    73 const TInt KMaxLogQuery = 248;
    69 const TInt KBufIncrement = 10;
    74 const TInt KBufIncrement = 10;
    70 
    75 
       
    76 #ifdef __RAMDISK_PERF_ENABLE
       
    77 _LIT(KSecurePath,   "[%x]%S");
       
    78 _LIT(KRAMAliasName, "%S");
       
    79 _LIT( KDummyDbFile, "%c:\\private\\10281e17\\dummydb.dat" );
       
    80 const TInt64 KMPMegaByte = 1048576;
       
    81 const TInt64 KMPEstimatedSongInBytes = KMPMegaByte * 2; 
       
    82 const TInt KMPEstimatedSizePerDBEntry = 3000; // worst scenario, can be lower if needed
       
    83 const TInt KMPMinimumRAMSizeToRun = 6 * KMPMegaByte; 
       
    84 // if RAM is lower than 5MB, doesn't seem enough for SQL as well.
       
    85 // so we set this number to move back DBs before being kicked out
       
    86 
       
    87 #endif //__RAMDISK_PERF_ENABLE
       
    88 
    71 // Used to suppress overflow when appending formatted text to a buffer.
    89 // Used to suppress overflow when appending formatted text to a buffer.
    72 class TOverflowHandle :
    90 class TOverflowHandle :
    73     public TDesOverflow
    91     public TDesOverflow
    74     {
    92     {
    75     public:
    93     public:
   100 // Constructor
   118 // Constructor
   101 // ----------------------------------------------------------------------------
   119 // ----------------------------------------------------------------------------
   102 //
   120 //
   103 EXPORT_C CMPXDbManager::CMPXDbManager(
   121 EXPORT_C CMPXDbManager::CMPXDbManager(
   104     RFs& aFs) :
   122     RFs& aFs) :
   105     iFs(aFs)
   123     iFs(aFs), 
       
   124     iRAMDiskPerfEnabled(EFalse),
       
   125     iMaximumAllowedRAMDiskSpaceToCopy(0),
       
   126     iRAMInUse(EFalse)
   106     {
   127     {
   107     MPX_FUNC("CMPXDbManager::CMPXDbManager");
   128     MPX_FUNC("CMPXDbManager::CMPXDbManager");
   108     }
   129     }
   109 
   130 
   110 // ----------------------------------------------------------------------------
   131 // ----------------------------------------------------------------------------
   114 EXPORT_C void CMPXDbManager::ConstructL(
   135 EXPORT_C void CMPXDbManager::ConstructL(
   115     const TFileName& aDatabaseFile)
   136     const TFileName& aDatabaseFile)
   116     {
   137     {
   117     MPX_FUNC("CMPXDbManager::ConstructL");
   138     MPX_FUNC("CMPXDbManager::ConstructL");
   118     iDbFile = aDatabaseFile.AllocL();
   139     iDbFile = aDatabaseFile.AllocL();
       
   140     
       
   141 #ifdef __RAMDISK_PERF_ENABLE
       
   142     TInt flags( 0 );
       
   143     CRepository* repository = CRepository::NewLC( KCRUidMPXMPFeatures );
       
   144     User::LeaveIfError( repository->Get( KMPXMPLocalVariation, flags ));
       
   145     MPX_DEBUG2("CMPXDbManager::ConstructL KMPXMPLocalVariation %d", flags);        
       
   146     iRAMDiskPerfEnabled = static_cast<TBool>( flags & KMPXEnableRAMDisk );
       
   147     
       
   148     TInt temp;
       
   149     User::LeaveIfError( repository->Get( KMAXAllowedDiskSpaceToCopy, temp) );
       
   150     iMaximumAllowedRAMDiskSpaceToCopy = temp * KMPMegaByte;
       
   151     CleanupStack::PopAndDestroy(repository);
       
   152             
       
   153     if ( iRAMDiskPerfEnabled )
       
   154         {
       
   155         MPX_DEBUG1("CMPXDbManager::ConstructL RAMDisk performance is enabled.");
       
   156         MPX_DEBUG2("CMPXDbManager::ConstructL RAMDisk iMaximumAllowedRAMDiskSpaceToCopy=%Lu", iMaximumAllowedRAMDiskSpaceToCopy);
       
   157         if ( GetRAMDiskPath() != KErrNone )
       
   158             {
       
   159             // Error finding ram drive, disable ram disk
       
   160             iRAMDiskPerfEnabled = EFalse;
       
   161             }
       
   162         }
       
   163     else
       
   164         {
       
   165         MPX_DEBUG2("CMPXDbManager::ConstructL RAMDisk performance is NOT enabled flag=%d", flags);
       
   166         MPX_DEBUG2("CMPXDbManager::ConstructL RAMDisk iMaximumAllowedRAMDiskSpaceToCopy=%Lu", iMaximumAllowedRAMDiskSpaceToCopy);
       
   167         }
       
   168 #endif //__RAMDISK_PERF_ENABLE
   119     }
   169     }
   120 
   170 
   121 // ----------------------------------------------------------------------------
   171 // ----------------------------------------------------------------------------
   122 // Destructor
   172 // Destructor
   123 // ----------------------------------------------------------------------------
   173 // ----------------------------------------------------------------------------
   139 
   189 
   140     iTables.Close();
   190     iTables.Close();
   141     CloseAllDatabases();
   191     CloseAllDatabases();
   142 
   192 
   143     delete iDbFile;
   193     delete iDbFile;
       
   194 #ifdef __RAMDISK_PERF_ENABLE
       
   195     TInt count(iDatabaseHandles.Count());
       
   196     for (TInt i = 0; i < count; ++i)
       
   197         {
       
   198         RemoveDummyFile(i);
       
   199         }    
       
   200 #endif //__RAMDISK_PERF_ENABLE
       
   201     iDatabaseHandles.Close();
   144     }
   202     }
   145 
   203 
   146 // ----------------------------------------------------------------------------
   204 // ----------------------------------------------------------------------------
   147 // Checks if all databases have been initialized.
   205 // Checks if all databases have been initialized.
   148 // ----------------------------------------------------------------------------
   206 // ----------------------------------------------------------------------------
   163 
   221 
   164     ASSERT(iTransactionCount >= 0);
   222     ASSERT(iTransactionCount >= 0);
   165 
   223 
   166     if (++iTransactionCount == 1)
   224     if (++iTransactionCount == 1)
   167         {
   225         {
   168         TInt err = iDatabase.Exec(KBeginTransaction);
   226         DoBeginL();
       
   227         }
       
   228     }
       
   229 
       
   230 void CMPXDbManager::DoBeginL()
       
   231     {
       
   232     MPX_FUNC("CMPXDbManager::DoBeginL");
       
   233 
       
   234     TInt err = iDatabase.Exec(KBeginTransaction);
   169         
   235         
   170         // transforms SQL error to KErrNotReady
   236     // transforms SQL error to KErrNotReady
   171         if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired )
   237     if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired )
   172             {
   238         {
   173             User::Leave(KErrNotReady);
   239         User::Leave(KErrNotReady);
   174             }
   240         }
   175         else
   241     else
   176             {
   242         {
   177             User::LeaveIfError(err);
   243         User::LeaveIfError(err);
   178             }
   244         }
   179         }
   245     }
   180     }
   246 
       
   247 // ----------------------------------------------------------------------------
       
   248 // Copy all DBs to RAM disk
       
   249 // ----------------------------------------------------------------------------
       
   250 //
       
   251 EXPORT_C void CMPXDbManager::CopyDBsToRamL( TBool aIsMTPInUse )
       
   252     {
       
   253 #ifdef __RAMDISK_PERF_ENABLE
       
   254     MPX_DEBUG1("-->CMPXDbManager::CopyDBsToRamL");
       
   255     if( iRAMDiskPerfEnabled )
       
   256         {
       
   257         if ( !IsRamDiskSpaceAvailable() )
       
   258             {
       
   259             return;
       
   260             }
       
   261         
       
   262         // Check if we are over the allowed ram space.
       
   263         TInt dbSize=0;
       
   264         TInt err = GetTotalDatabasesSize(dbSize);
       
   265         if ( err || (dbSize > iMaximumAllowedRAMDiskSpaceToCopy) )
       
   266             {
       
   267             MPX_DEBUG2("<--CMPXDbManager::CopyDBsToRamL Over the allowed Ram disk limit %Lu", iMaximumAllowedRAMDiskSpaceToCopy );
       
   268             return;
       
   269             }
       
   270         
       
   271         TInt transactionCount = iTransactionCount;
       
   272         if (iTransactionCount > 0) 
       
   273             {
       
   274             iTransactionCount = 0;
       
   275             DoCommitL();
       
   276             }
       
   277 
       
   278         TInt count(iDatabaseHandles.Count());
       
   279         TBool ret = EFalse;
       
   280         for ( TInt i = 0; i < count ; ++i )
       
   281             {
       
   282             if ( iDatabaseHandles[i].iUseRAMdb )
       
   283                 {
       
   284                 // already used
       
   285                 MPX_DEBUG1("CMPXDbManager::CopyDBsToRamL iUseRAMdb already ETrue");
       
   286                 continue;
       
   287                 }
       
   288             iDatabaseHandles[i].iUseRAMdb = ETrue;
       
   289             TRAPD(err, ret = DoCopyDBToRamL( iDatabaseHandles[i].iDrive, aIsMTPInUse ))
       
   290             if ( err != KErrNone )
       
   291                 {
       
   292                 MPX_DEBUG2("CMPXDbManager::CopyDBsToRamL error=%d", err);
       
   293                 // remove dymmy file
       
   294                 RemoveDummyFile(i);
       
   295                 // try to close database that is opened from RAM disk
       
   296                 TRAP_IGNORE(CloseDatabaseL( iDatabaseHandles[i].iDrive ));
       
   297                 iDatabaseHandles[i].iUseRAMdb = EFalse;
       
   298                 // reopen database from drive not from RAM
       
   299                 OpenDatabaseL( iDatabaseHandles[i].iDrive );
       
   300                 continue; // continue to copy for next drive
       
   301                 }
       
   302             if ( !ret )
       
   303                 {
       
   304                 iDatabaseHandles[i].iUseRAMdb = EFalse;
       
   305                 }
       
   306             }
       
   307             
       
   308         if (transactionCount > 0) 
       
   309             {
       
   310             DoBeginL();
       
   311             iTransactionCount = transactionCount;
       
   312             }
       
   313         }
       
   314     iRAMInUse = ETrue;
       
   315 	
       
   316     MPX_DEBUG1("<--CMPXDbManager::CopyDBsToRamL");
       
   317 #endif //__RAMDISK_PERF_ENABLE
       
   318 
       
   319     }
       
   320 
       
   321 
       
   322 // ----------------------------------------------------------------------------
       
   323 // CMPXDbManager::DoCopyDBsToRamL
       
   324 // ----------------------------------------------------------------------------
       
   325 //
       
   326 TBool CMPXDbManager::DoCopyDBToRamL( TDriveUnit aDrive, TBool aIsMTPInUse )
       
   327     {
       
   328 #ifdef __RAMDISK_PERF_ENABLE
       
   329     MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsToRamL drive=%d", (TInt)aDrive);
       
   330     TFileName dst;
       
   331     TFileName src;
       
   332     dst.Append(iRAMFolder);
       
   333     src.Append(aDrive.Name());
       
   334     src.Append(KDBFilePath);
       
   335     TRAPD( err, BaflUtils::EnsurePathExistsL( iFs, dst ));
       
   336     if ( err != KErrNone )
       
   337         {
       
   338         return EFalse;
       
   339         }
       
   340     TFileName filename;            
       
   341     filename.Format(KSecurePath, User::Identity().iUid, iDbFile); //x:\private\10281e17\[sldfdsf]mpxv2_5.db
       
   342     src.Append(filename);
       
   343     MPX_DEBUG2("RAMDisk src path=%S", &src);
       
   344     TEntry entry;
       
   345     iFs.Entry( src, entry );
       
   346     if (!BlockDiskSpace( aDrive, entry.iSize, aIsMTPInUse ) )
       
   347         {
       
   348         MPX_DEBUG1("CMPXDbManager::DoCopyDBsToRamL() BlockDiskSpace failed");
       
   349         return EFalse; // continue for next drive
       
   350         }
       
   351     TBuf<2> d;
       
   352     d.Append(aDrive.Name());
       
   353     HBufC* temp = HBufC::NewLC(KMaxFileName);
       
   354     temp->Des().Append(d.Left(1));
       
   355     temp->Des().Append(iDbFile->Des());
       
   356     filename.Format(KSecurePath, User::Identity().iUid, temp);
       
   357     CleanupStack::PopAndDestroy(temp);
       
   358     dst.Append(filename);
       
   359     MPX_DEBUG2("RAMDisk dst path=%S", &dst);
       
   360     TInt index( GetDatabaseIndex((TInt)aDrive) );
       
   361     delete iDatabaseHandles[index].iOrigFullFilePath;
       
   362     iDatabaseHandles[index].iOrigFullFilePath = 0;
       
   363     delete iDatabaseHandles[index].iTargetFullFilePath;
       
   364     iDatabaseHandles[index].iTargetFullFilePath = 0;
       
   365     
       
   366     // Save these path so it is convenient to copy back
       
   367     iDatabaseHandles[index].iOrigFullFilePath = HBufC::NewL(src.Length());
       
   368     iDatabaseHandles[index].iTargetFullFilePath = HBufC::NewL(dst.Length());
       
   369         
       
   370     iDatabaseHandles[index].iOrigFullFilePath->Des().Append(src);
       
   371     iDatabaseHandles[index].iTargetFullFilePath->Des().Append(dst);
       
   372 
       
   373     TRAP(err, CloseDatabaseL(aDrive));
       
   374     if ( err != KErrNone )
       
   375         {
       
   376         MPX_DEBUG2("<--CMPXDbManager::DoCopyDBsToRamL error=%d", err);
       
   377         TInt index(GetDatabaseIndex((TInt)aDrive));
       
   378         if ( index >= 0 )
       
   379             {
       
   380             RemoveDummyFile( index );
       
   381             }
       
   382         return EFalse;
       
   383         }
       
   384     User::LeaveIfError( BaflUtils::CopyFile(iFs, src, dst ));
       
   385     OpenDatabaseL((TInt)aDrive);
       
   386 
       
   387     MPX_DEBUG2("RAMDisk Database opened=%d", (TInt)aDrive);
       
   388     MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsToRamL");
       
   389     return ETrue;    
       
   390 #endif //__RAMDISK_PERF_ENABLE
       
   391     }
       
   392 
       
   393 // ----------------------------------------------------------------------------
       
   394 // Copy all DBs from RAM disk back to normal drives
       
   395 // ----------------------------------------------------------------------------
       
   396 //
       
   397 EXPORT_C void CMPXDbManager::CopyDBsFromRamL()
       
   398     {
       
   399 #ifdef __RAMDISK_PERF_ENABLE
       
   400     MPX_DEBUG1("-->CMPXDbManager::CopyDBsFromRamL");
       
   401     if( iRAMDiskPerfEnabled )
       
   402        {
       
   403         TInt transactionCount = iTransactionCount;
       
   404         if (iTransactionCount > 0) 
       
   405             {
       
   406             iTransactionCount = 0;
       
   407             DoCommitL();
       
   408             }
       
   409 
       
   410         TInt count(iDatabaseHandles.Count());
       
   411         for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb; ++i)
       
   412             {
       
   413             TRAPD(err, DoCopyDBFromRamL(i));
       
   414             if ( err != KErrNone )
       
   415                 {
       
   416                 MPX_DEBUG2("<--CMPXDbManager::CopyDBsFromRamL error=%d", err);                
       
   417                 //anyting wrong, delete the temp file and open database from drive
       
   418                 RemoveDummyFile(i);
       
   419                 // delete Db on RAM
       
   420                 User::LeaveIfError( BaflUtils::DeleteFile(iFs, 
       
   421                     *iDatabaseHandles[i].iTargetFullFilePath));
       
   422                 }
       
   423             iDatabaseHandles[i].iUseRAMdb = EFalse;
       
   424             // open db from drive
       
   425             OpenDatabaseL( iDatabaseHandles[i].iDrive );      
       
   426             }
       
   427 
       
   428         if (transactionCount > 0) 
       
   429             {
       
   430             DoBeginL();
       
   431             iTransactionCount = transactionCount;
       
   432             }
       
   433         }
       
   434     iRAMInUse = EFalse;
       
   435         
       
   436     MPX_DEBUG1("<--CMPXDbManager::CopyDBsFromRamL");
       
   437 #endif //__RAMDISK_PERF_ENABLE
       
   438     }
       
   439 
       
   440 
       
   441 // ----------------------------------------------------------------------------
       
   442 // CMPXDbManager::DoCopyDBsToRamL
       
   443 // ----------------------------------------------------------------------------
       
   444 //
       
   445 void CMPXDbManager::DoCopyDBFromRamL( TInt aIndex )
       
   446     {
       
   447 #ifdef __RAMDISK_PERF_ENABLE
       
   448     MPX_DEBUG1("-->CMPXDbManager::DoCopyDBsFromRamL");    
       
   449     MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsFromRamL Drive %d will be closed before copying db from RAM.",
       
   450          iDatabaseHandles[aIndex].iDrive);
       
   451 
       
   452     CloseDatabaseL(iDatabaseHandles[aIndex].iDrive);            
       
   453 
       
   454     // Delete existing DB on drive
       
   455     User::LeaveIfError( BaflUtils::DeleteFile(iFs, 
       
   456         *iDatabaseHandles[aIndex].iOrigFullFilePath));
       
   457     MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRamL old DB on drive deleted");
       
   458 
       
   459     // Rename dummy file to be orignal file name
       
   460     User::LeaveIfError( BaflUtils::RenameFile(iFs, 
       
   461         iDatabaseHandles[aIndex].iDummyFilePath, 
       
   462         *iDatabaseHandles[aIndex].iOrigFullFilePath) );
       
   463     MPX_DEBUG1("CMPXDbManager::CopyDBsFromRamL RAMDisk renamed.");
       
   464 
       
   465     //Copy Db from RAM to replace dummy file
       
   466     ReplaceFileL( *iDatabaseHandles[aIndex].iTargetFullFilePath, *iDatabaseHandles[aIndex].iOrigFullFilePath);
       
   467     
       
   468     MPX_DEBUG1("CMPXDbManager::CopyDBsFromRamL RAMDisk copied back.");
       
   469 
       
   470     // Delete existing DB on RAM
       
   471     User::LeaveIfError( BaflUtils::DeleteFile(iFs, *iDatabaseHandles[aIndex].iTargetFullFilePath));
       
   472     MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRamL RAMDisk deleted");
       
   473 
       
   474     MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsFromRamL");
       
   475 #endif //__RAMDISK_PERF_ENABLE
       
   476     } 
       
   477 
       
   478 // ----------------------------------------------------------------------------
       
   479 // CMPXDbManager::ReplaceFileL
       
   480 //
       
   481 // Replaces a file with another writing over the destination file.
       
   482 // Leaves on error.
       
   483 // Implementation follows CFileMan::Copy except that 
       
   484 //  - we don't resize target file to zero
       
   485 //  - we can assume that files already exist
       
   486 //  - we don't copy file attributes & timestamp
       
   487 // ----------------------------------------------------------------------------
       
   488 //
       
   489 void CMPXDbManager::ReplaceFileL( const TDesC& aSrcName, const TDesC& aDstName )
       
   490     {
       
   491     // open files
       
   492     RFile srcFile;
       
   493     User::LeaveIfError( srcFile.Open(iFs, aSrcName, EFileRead|EFileShareReadersOnly) );
       
   494     CleanupClosePushL( srcFile );
       
   495     
       
   496     RFile dstFile;
       
   497     User::LeaveIfError( dstFile.Open(iFs, aDstName, EFileWrite|EFileWriteDirectIO|EFileShareExclusive) );
       
   498     CleanupClosePushL( dstFile );
       
   499     
       
   500     // resize destination file
       
   501     TInt remainingBytes;
       
   502     User::LeaveIfError( srcFile.Size(remainingBytes) );
       
   503     User::LeaveIfError( dstFile.SetSize(remainingBytes) );
       
   504 
       
   505     // allocate buffer
       
   506    	const TInt KBigBufSize = 512 * 1024;
       
   507     const TInt KMediumBufSize = 32 * 1024;
       
   508     const TInt KSmallBufSize = 4 * 1024;
       
   509     HBufC8* bufPtr=HBufC8::New( Min(KBigBufSize, remainingBytes) );
       
   510     if (bufPtr==NULL)
       
   511         bufPtr=HBufC8::New(KMediumBufSize);
       
   512     if (bufPtr==NULL)
       
   513         bufPtr=HBufC8::New(KSmallBufSize);
       
   514     if (bufPtr == NULL)
       
   515         User::Leave(KErrNoMemory);
       
   516     CleanupStack::PushL(bufPtr);
       
   517 
       
   518     // copy
       
   519     TPtr8 copyBuf=bufPtr->Des();
       
   520     TInt pos=0;
       
   521     while( remainingBytes > 0 )
       
   522         {
       
   523         TInt s = Min( remainingBytes, copyBuf.MaxSize() );
       
   524         TInt ret = srcFile.Read(pos, copyBuf, s);
       
   525         if (ret == KErrNone && copyBuf.Length()!= s )
       
   526             {
       
   527             ret = KErrCorrupt;
       
   528             }
       
   529         if (ret == KErrNone)
       
   530             {
       
   531             ret = dstFile.Write(pos, copyBuf, s);
       
   532             }
       
   533         User::LeaveIfError (ret);
       
   534         pos += s;
       
   535         remainingBytes -= s;
       
   536         }
       
   537     User::LeaveIfError( dstFile.Flush() );
       
   538     CleanupStack::PopAndDestroy(3); // bufPtr, dstFile, srcFile
       
   539     }
       
   540 
       
   541 // ----------------------------------------------------------------------------
       
   542 // CMPXDbManager::RemoveDummyFile
       
   543 // ----------------------------------------------------------------------------
       
   544 //
       
   545 void CMPXDbManager::RemoveDummyFile( TInt index )
       
   546     {
       
   547 #ifdef __RAMDISK_PERF_ENABLE
       
   548     MPX_DEBUG1("-->CMPXDbManager::RemoveDummyFile");
       
   549        
       
   550     if ( iDatabaseHandles[index].iDummyFilePath.Length() )
       
   551         {
       
   552         TInt err = BaflUtils::DeleteFile(iFs, iDatabaseHandles[index].iDummyFilePath);
       
   553         if ( !err )
       
   554             {
       
   555             iDatabaseHandles[index].iDummyFilePath.Zero();
       
   556             }
       
   557         }
       
   558     MPX_DEBUG1("<--CMPXDbManager::RemoveDummyFile");
       
   559 #endif //__RAMDISK_PERF_ENABLE
       
   560 
       
   561     }
       
   562 
   181 
   563 
   182 // ----------------------------------------------------------------------------
   564 // ----------------------------------------------------------------------------
   183 // Commits a transaction on all databases.
   565 // Commits a transaction on all databases.
   184 // ----------------------------------------------------------------------------
   566 // ----------------------------------------------------------------------------
   185 //
   567 //
   189 
   571 
   190     if(iTransactionCount > 0)
   572     if(iTransactionCount > 0)
   191         {
   573         {
   192         if (--iTransactionCount == 0)
   574         if (--iTransactionCount == 0)
   193             {
   575             {
   194             TInt err = iDatabase.Exec(KCommitTransaction);
   576             DoCommitL();
   195             
   577             }
   196             // transforms SQL error to KErrNotReady
   578         }
   197             if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired )
   579     }
   198                 {
   580     
   199                 User::Leave(KErrNotReady);
   581 // ----------------------------------------------------------------------------
   200                 }
   582 // Commits a transaction on all databases.
   201             else
   583 // ----------------------------------------------------------------------------
   202                 {
   584 //
   203                 User::LeaveIfError(err);
   585 void CMPXDbManager::DoCommitL()
   204                 }
   586     {
   205             }
   587     MPX_FUNC("CMPXDbManager::DoCommitL");
       
   588     TInt err = iDatabase.Exec(KCommitTransaction);
       
   589     
       
   590     // transforms SQL error to KErrNotReady
       
   591     if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired )
       
   592         {
       
   593         MPX_DEBUG2("CMPXDbManager::CommitL failed err=%d", err);
       
   594         User::Leave(KErrNotReady);
       
   595         }
       
   596     else
       
   597         {
       
   598         User::LeaveIfError(err);
   206         }
   599         }
   207     }
   600     }
   208 
   601 
   209 // ----------------------------------------------------------------------------
   602 // ----------------------------------------------------------------------------
   210 // Rolls back a transaction on all databases.
   603 // Rolls back a transaction on all databases.
   271 
   664 
   272             handle.iDrive = aDrives[i];
   665             handle.iDrive = aDrives[i];
   273             handle.iAliasname = HBufC::NewL(KAliasName().Length());
   666             handle.iAliasname = HBufC::NewL(KAliasName().Length());
   274             handle.iAliasname->Des().Format(KAliasName, &driveName);
   667             handle.iAliasname->Des().Format(KAliasName, &driveName);
   275             handle.iOpen = EFalse;
   668             handle.iOpen = EFalse;
       
   669 #ifdef __RAMDISK_PERF_ENABLE
       
   670             handle.iOrigFullFilePath = HBufC::NewL(0);
       
   671             handle.iTargetFullFilePath = HBufC::NewL(0);
       
   672             handle.iDummyFilePath.Zero();
       
   673             handle.iUseRAMdb = EFalse;
       
   674 #endif //__RAMDISK_PERF_ENABLE
   276 
   675 
   277             iDatabaseHandles.AppendL(handle);
   676             iDatabaseHandles.AppendL(handle);
   278 
   677 
   279             TVolumeInfo vol;
   678             TVolumeInfo vol;
   280             if (iFs.Volume(vol, drive) == KErrNone)
   679             if (iFs.Volume(vol, drive) == KErrNone)
   311         TInt count(iDatabaseHandles.Count());
   710         TInt count(iDatabaseHandles.Count());
   312         for (TInt i = 0; i < count; ++i)
   711         for (TInt i = 0; i < count; ++i)
   313             {
   712             {
   314             if (iDatabaseHandles[i].iDrive == aDrive)
   713             if (iDatabaseHandles[i].iDrive == aDrive)
   315                 {
   714                 {
       
   715                 MPX_DEBUG2("CMPXDbManager::OpenDatabaseL found %d", aDrive);
   316                 if (!iDatabaseHandles[i].iOpen)
   716                 if (!iDatabaseHandles[i].iOpen)
   317                     {
   717                     {
       
   718                     MPX_DEBUG1("CMPXDbManager::OpenDatabaseL not open found");
   318                     // make sure the database is created
   719                     // make sure the database is created
   319 					CreateDatabaseL(drive);
   720 					CreateDatabaseL(drive);
   320 
   721 
   321 					// attach
   722 					// attach
   322                     AttachDatabaseL(drive);
   723                     AttachDatabaseL(drive);
   327                 }
   728                 }
   328             }
   729             }
   329         }
   730         }
   330     if (!found)
   731     if (!found)
   331         {
   732         {
       
   733         MPX_DEBUG1("CMPXDbManager::OpenDatabaseL not found");
   332         User::Leave(KErrArgument);
   734         User::Leave(KErrArgument);
   333         }
   735         }
   334 
   736 
   335     // Close all prepared statements if a db is opened
   737     // Close all prepared statements if a db is opened
   336     //
   738     //
   364         TInt count(iDatabaseHandles.Count());
   766         TInt count(iDatabaseHandles.Count());
   365         for (TInt i = 0; i < count; ++i)
   767         for (TInt i = 0; i < count; ++i)
   366             {
   768             {
   367             if (iDatabaseHandles[i].iDrive == aDrive)
   769             if (iDatabaseHandles[i].iDrive == aDrive)
   368                 {
   770                 {
       
   771                 MPX_DEBUG2("CMPXDbManager::CloseDatabaseL found %d", aDrive);
   369                 if (iDatabaseHandles[i].iOpen)
   772                 if (iDatabaseHandles[i].iOpen)
   370                     {
   773                     {
       
   774                     MPX_DEBUG1("CMPXDbManager::CloseDatabaseL found open");
   371                     DetachDatabaseL(drive);
   775                     DetachDatabaseL(drive);
   372                     }
   776                     }
   373 
   777 
   374                 found = ETrue;
   778                 found = ETrue;
   375                 break;
   779                 break;
   376                 }
   780                 }
   377             }
   781             }
   378         }
   782         }
   379     if (!found)
   783     if (!found)
   380         {
   784         {
       
   785         MPX_DEBUG1("CMPXDbManager::CloseDatabaseL not found");
   381         User::Leave(KErrArgument);
   786         User::Leave(KErrArgument);
   382         }
   787         }
   383 
   788 
   384     }
   789     }
   385 
   790 
   399 
   804 
   400         TInt count(iDatabaseHandles.Count());
   805         TInt count(iDatabaseHandles.Count());
   401         for (TInt i = 0; i < count; ++i)
   806         for (TInt i = 0; i < count; ++i)
   402             {
   807             {
   403             delete iDatabaseHandles[i].iAliasname;
   808             delete iDatabaseHandles[i].iAliasname;
       
   809 #ifdef __RAMDISK_PERF_ENABLE 
       
   810             delete iDatabaseHandles[i].iOrigFullFilePath;
       
   811             delete iDatabaseHandles[i].iTargetFullFilePath;
       
   812             iDatabaseHandles[i].iDummyFilePath.Zero();
       
   813 #endif //__RAMDISK_PERF_ENABLE 
   404             }
   814             }
   405 
   815 
   406         iDatabaseHandles.Reset();
   816         iDatabaseHandles.Reset();
   407         iDatabase.Close();
   817         iDatabase.Close();
   408         iInitialized = EFalse;
   818         iInitialized = EFalse;
  1293     HBufC* filename = CreateFilenameL(aDrive);
  1703     HBufC* filename = CreateFilenameL(aDrive);
  1294     CleanupStack::PushL(filename);
  1704     CleanupStack::PushL(filename);
  1295 
  1705 
  1296     if (database.Open(filename->Des()) != KErrNone)
  1706     if (database.Open(filename->Des()) != KErrNone)
  1297         {
  1707         {
  1298         MPX_DEBUG2("CMPXDbManager::CreateDatabaseL - cannot open the database on drive %d", TInt(aDrive));
  1708         MPX_DEBUG3("CMPXDbManager::CreateDatabaseL - cannot open db on drive %d %S", TInt(aDrive), filename);
  1299 
  1709 
  1300         // close the database first
  1710         // close the database first
  1301         database.Close();
  1711         database.Close();
  1302         DoCreateDatabaseL( aDrive );
  1712         DoCreateDatabaseL( aDrive );
  1303         }
  1713         }
  1348             {
  1758             {
  1349             if (!iDatabaseHandles[i].iOpen)
  1759             if (!iDatabaseHandles[i].iOpen)
  1350                 {
  1760                 {
  1351                 HBufC* filename = CreateFilenameL(aDrive);
  1761                 HBufC* filename = CreateFilenameL(aDrive);
  1352                 CleanupStack::PushL(filename);
  1762                 CleanupStack::PushL(filename);
  1353 
  1763                 
  1354                 User::LeaveIfError(iDatabase.Attach(filename->Des(),
  1764 #ifdef __RAMDISK_PERF_ENABLE
  1355                     *(iDatabaseHandles[i].iAliasname)));
  1765                 if( iDatabaseHandles[i].iUseRAMdb )
       
  1766                     {
       
  1767                     delete iDatabaseHandles[i].iAliasname;
       
  1768                     iDatabaseHandles[i].iAliasname = HBufC::NewL(KAliasName().Length());
       
  1769                     HBufC* temp = HBufC::NewLC(2); // form of DE, DF, DX,...
       
  1770                     temp->Des().Append(iRAMDrive); // length == 2
       
  1771                     TDriveUnit pdrive(aDrive);
       
  1772                     temp->Des().Append(pdrive.Name().Left(1)); //length == 2+ 1
       
  1773                     iDatabaseHandles[i].iAliasname->Des().Format(KRAMAliasName, temp);
       
  1774                     MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - RAM change aliasname of %S", iDatabaseHandles[i].iAliasname );
       
  1775                     CleanupStack::PopAndDestroy(temp);
       
  1776                     }
       
  1777                 else
       
  1778 #endif //__RAMDISK_PERF_ENABLE
       
  1779                    {
       
  1780                    delete iDatabaseHandles[i].iAliasname;
       
  1781                    TDriveUnit drive(aDrive);
       
  1782                    const TDesC& driveName = drive.Name();
       
  1783                    iDatabaseHandles[i].iAliasname = HBufC::NewL(KAliasName().Length());
       
  1784                    iDatabaseHandles[i].iAliasname->Des().Format(KAliasName, &driveName);
       
  1785                    MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - normal change aliasname of %S", iDatabaseHandles[i].iAliasname);
       
  1786                    }
       
  1787 
       
  1788                 TInt err = iDatabase.Attach(filename->Des(), *(iDatabaseHandles[i].iAliasname));
       
  1789                 MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - Attach Error =%d", err);
       
  1790                 User::LeaveIfError(err);
  1356                 iDatabaseHandles[i].iOpen = ETrue;
  1791                 iDatabaseHandles[i].iOpen = ETrue;
  1357 
  1792 
  1358                 CleanupStack::PopAndDestroy(filename);
  1793                 CleanupStack::PopAndDestroy(filename);
  1359                 }
  1794                 }
       
  1795             else
       
  1796                 {
       
  1797                 MPX_DEBUG1("CMPXDbManager::AttachDatabaseL - found already open");    
       
  1798                 }
       
  1799             
  1360             found = ETrue;
  1800             found = ETrue;
  1361             break;
  1801             break;
  1362             }
  1802             }
  1363         }
  1803         }
  1364     if (!found)
  1804     if (!found)
  1365         {
  1805         {
       
  1806         MPX_DEBUG1("CMPXDbManager::AttachDatabaseL - not found");
  1366         User::Leave(KErrNotFound);
  1807         User::Leave(KErrNotFound);
  1367         }
  1808         }
  1368     }
  1809     }
  1369 
  1810 
  1370 // ----------------------------------------------------------------------------
  1811 // ----------------------------------------------------------------------------
  1382     TInt count(iDatabaseHandles.Count());
  1823     TInt count(iDatabaseHandles.Count());
  1383     for (TInt i = 0; i < count; ++i)
  1824     for (TInt i = 0; i < count; ++i)
  1384         {
  1825         {
  1385         if (iDatabaseHandles[i].iDrive == aDrive)
  1826         if (iDatabaseHandles[i].iDrive == aDrive)
  1386             {
  1827             {
  1387             if (iDatabaseHandles[i].iOpen)
  1828 #ifdef __RAMDISK_PERF_ENABLE
  1388                 {
  1829             if ( iDatabaseHandles[i].iOpen || iDatabaseHandles[i].iUseRAMdb )
  1389                 User::LeaveIfError(iDatabase.Detach(*(iDatabaseHandles[i].iAliasname)));
  1830 #else //__RAMDISK_PERF_ENABLE
       
  1831             if ( iDatabaseHandles[i].iOpen )
       
  1832 #endif //__RAMDISK_PERF_ENABLE
       
  1833 
       
  1834                 {
       
  1835                 MPX_DEBUG1("CMPXDbManager::DetachDatabaseL found drive that is opening");
       
  1836                 TInt err = iDatabase.Detach(*(iDatabaseHandles[i].iAliasname));
       
  1837                 if ( err )
       
  1838                     {
       
  1839                     MPX_DEBUG2("CMPXDbManager::DetachDatabaseL detach failed Error=%d", err);
       
  1840                     }
       
  1841                 else
       
  1842                     {
       
  1843                     MPX_DEBUG2("CMPXDbManager::DetachDatabaseL iAliasname=%S", iDatabaseHandles[i].iAliasname);
       
  1844                     }
       
  1845                 User::LeaveIfError(err);
  1390                 iDatabaseHandles[i].iOpen = EFalse;
  1846                 iDatabaseHandles[i].iOpen = EFalse;
  1391                 }
  1847                 }
  1392 
  1848 
  1393             found = ETrue;
  1849             found = ETrue;
  1394             break;
  1850             break;
  1395             }
  1851             }
  1396         }
  1852         }
  1397     if (!found)
  1853     if (!found)
  1398         {
  1854         {
       
  1855         MPX_DEBUG1("CMPXDbManager::DetachDatabaseL drive not found in iDatabaseHandlers");
  1399         User::Leave(KErrNotFound);
  1856         User::Leave(KErrNotFound);
  1400         }
  1857         }
  1401     }
  1858     }
  1402 
  1859 
  1403 // ----------------------------------------------------------------------------
  1860 // ----------------------------------------------------------------------------
  1412     HBufC* filename = HBufC::NewL(KMaxFileName);
  1869     HBufC* filename = HBufC::NewL(KMaxFileName);
  1413 
  1870 
  1414     const TDesC& securefilePath = KSecureFilePath;
  1871     const TDesC& securefilePath = KSecureFilePath;
  1415     TDriveUnit cdrive(KRootDrive());
  1872     TDriveUnit cdrive(KRootDrive());
  1416 
  1873 
  1417     const TDesC& driveName = aDrive.Name();
  1874 #ifdef __RAMDISK_PERF_ENABLE
  1418     filename->Des().Format(securefilePath, &driveName, User::Identity().iUid, iDbFile);
  1875     TInt index(GetDatabaseIndex((TInt)aDrive));    
       
  1876     if ( index >=0 && iDatabaseHandles[index].iUseRAMdb && aDrive != cdrive )
       
  1877         {
       
  1878         MPX_DEBUG1("CMPXDbManager::CreateFilenameL - use RAMDisk");
       
  1879         TFileName path;
       
  1880         path.Append(iRAMDrive);
       
  1881         path.Append(_L(":"));
       
  1882         TBuf<2> d;
       
  1883         d.Append(aDrive.Name());
       
  1884         HBufC* temp = HBufC::NewLC(KMaxFileName);
       
  1885         temp->Des().Append(d.Left(1)); // attach original drive name
       
  1886         temp->Des().Append(iDbFile->Des()); 
       
  1887         filename->Des().Format(securefilePath, &path, User::Identity().iUid, temp);
       
  1888         MPX_DEBUG3("CMPXDbManager::CreateFilenameL - path=%S filename=%S", &path, filename);
       
  1889         CleanupStack::PopAndDestroy(temp);
       
  1890         }
       
  1891     else
       
  1892 #endif //__RAMDISK_PERF_ENABLE
       
  1893         {
       
  1894         MPX_DEBUG1("CMPXDbManager::CreateFilenameL - use normal drive");
       
  1895         const TDesC& driveName = aDrive.Name();
       
  1896         filename->Des().Format(securefilePath, &driveName, User::Identity().iUid, iDbFile);
       
  1897 
       
  1898         }
       
  1899     
       
  1900     MPX_DEBUG2("CMPXDbManager::CreateFilenameL filename = %S", filename); 
  1419     return filename;
  1901     return filename;
  1420     }
  1902     }
  1421 
  1903 
  1422 // ----------------------------------------------------------------------------
  1904 // ----------------------------------------------------------------------------
  1423 // Replaces :dbname with a drive alias
  1905 // Replaces :dbname with a drive alias
  1510     if (aDrive < 0)
  1992     if (aDrive < 0)
  1511         {
  1993         {
  1512         MPX_DEBUG1("invalid driveId, leave with KErrNotReady");
  1994         MPX_DEBUG1("invalid driveId, leave with KErrNotReady");
  1513         User::Leave(KErrNotReady);
  1995         User::Leave(KErrNotReady);
  1514         }
  1996         }
  1515 
  1997     
  1516     // handle the case of C drive
  1998 #ifndef __RAMDISK_PERF_ENABLE 
  1517     TDriveUnit drive(aDrive);
  1999     
  1518     TDriveUnit cdrive(KRootDrive());
  2000     EnsureDiskSpaceL(aDrive);
  1519 
  2001     
  1520     if(drive == cdrive)
  2002 #endif //__RAMDISK_PERF_ENABLE
  1521         {
  2003 
  1522         if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0, aDrive))
       
  1523             {
       
  1524             User::Leave(KErrDiskFull);
       
  1525             }
       
  1526 
       
  1527         return;
       
  1528         }
       
  1529 
       
  1530     // handle other drives (eg. removable EDriveE)
       
  1531     TInt count(iDatabaseHandles.Count());
       
  1532     for (TInt i = 0; i < count; ++i)
       
  1533         {
       
  1534 
       
  1535         if (((KDbManagerAllDrives == aDrive) ||
       
  1536             (aDrive == iDatabaseHandles[i].iDrive)) &&
       
  1537             iDatabaseHandles[i].iOpen)
       
  1538             {
       
  1539             if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0,
       
  1540                 iDatabaseHandles[i].iDrive))
       
  1541                 {
       
  1542                 User::Leave(KErrDiskFull);
       
  1543                 }
       
  1544             }
       
  1545 
       
  1546         if (aDrive == iDatabaseHandles[i].iDrive)
       
  1547             {
       
  1548             // exit if just one drive to check
       
  1549             break;
       
  1550             }
       
  1551         }
       
  1552     }
  2004     }
  1553     
  2005     
  1554 // ----------------------------------------------------------------------------
  2006 // ----------------------------------------------------------------------------
  1555 // Regenerate all databases.
  2007 // Regenerate all databases.
  1556 // ----------------------------------------------------------------------------
  2008 // ----------------------------------------------------------------------------
  1982     CleanupStack::PushL(filename);
  2434     CleanupStack::PushL(filename);
  1983     
  2435     
  1984     // remove old databases before creating/replacing new database
  2436     // remove old databases before creating/replacing new database
  1985     TInt driveNameLen = aDrive.Name().Length();
  2437     TInt driveNameLen = aDrive.Name().Length();
  1986     
  2438     
  1987     TFileName dbFileName(aDrive.Name()); //initialise with drive name        
  2439     TFileName dbFileName;
       
  2440 
       
  2441 #ifdef __RAMDISK_PERF_ENABLE
       
  2442     TInt index(GetDatabaseIndex((TInt)aDrive));
       
  2443     if( index >= 0 && iDatabaseHandles[index].iUseRAMdb )
       
  2444         {
       
  2445         dbFileName.Append(iRAMDrive); // RAM
       
  2446         dbFileName.Append(_L(":")); // RAM
       
  2447         }
       
  2448     else 
       
  2449 #endif //__RAMDISK_PERF_ENABLE
       
  2450         {
       
  2451         MPX_DEBUG1("CMPXDbManager::CreateDatabaseL - E:");
       
  2452         dbFileName.Append(aDrive.Name()); //initialise with drive name
       
  2453         }
       
  2454         
       
  2455 
       
  2456     MPX_DEBUG2("CMPXDbManager::CreateDatabaseL - dbFileName=%S", &dbFileName);
  1988     dbFileName.Append(KDBFilePath);  // append private path
  2457     dbFileName.Append(KDBFilePath);  // append private path
  1989     
  2458     
  1990     //append file name
  2459     //append file name
  1991     dbFileName.Append(filename->Right((filename->Length())- driveNameLen));     
  2460     dbFileName.Append(filename->Right((filename->Length())- driveNameLen));     
  1992     
  2461     
  2046     CreateTablesL(database, corrupt);
  2515     CreateTablesL(database, corrupt);
  2047 
  2516 
  2048     CleanupStack::PopAndDestroy(filename);
  2517     CleanupStack::PopAndDestroy(filename);
  2049     CleanupStack::PopAndDestroy(&database);
  2518     CleanupStack::PopAndDestroy(&database);
  2050     }
  2519     }
  2051 
  2520 	
       
  2521 // ---------------------------------------------------------------------------
       
  2522 // CMPXDbManager::GetRAMDiskPath
       
  2523 // ---------------------------------------------------------------------------
       
  2524 //
       
  2525 TInt CMPXDbManager::GetRAMDiskPath()
       
  2526     {
       
  2527     TInt error = KErrNotSupported;
       
  2528 #ifdef __RAMDISK_PERF_ENABLE
       
  2529     MPX_DEBUG1("-->CMPXDbManager::GetRAMDiskPath");
       
  2530     TDriveList driveList;
       
  2531     TBool ramDriveFound = EFalse;
       
  2532     TInt driveOffset = 'A';
       
  2533     iRAMFolder.Zero();
       
  2534     
       
  2535     error = iFs.DriveList( driveList );
       
  2536     if ( error == KErrNone )
       
  2537         {
       
  2538         for ( TInt i = 0; i < driveList.Length(); i++ )
       
  2539             {
       
  2540             if ( driveList[i] != 0 )
       
  2541                 {
       
  2542                 TDriveInfo info;
       
  2543                 TInt err = iFs.Drive( info, i );
       
  2544                 if ( !err && info.iType == EMediaRam )
       
  2545                     {
       
  2546                     iRAMDrive = driveOffset + i;
       
  2547                     iRAMFolder.Append(iRAMDrive);
       
  2548                     iRAMFolder.Append(_L(":"));
       
  2549                     iRAMFolder.Append(KDBFilePath);
       
  2550                     ramDriveFound = ETrue;
       
  2551                     MPX_DEBUG2("RAMDisk path=%S", &iRAMFolder);
       
  2552                     break;
       
  2553                     }
       
  2554                 }
       
  2555             }
       
  2556         
       
  2557         // Check if ram drive is found.
       
  2558         if ( !ramDriveFound )
       
  2559             {
       
  2560             error = KErrNotFound;
       
  2561             }
       
  2562         }
       
  2563     MPX_DEBUG2("CMPXDbManager::GetRAMDiskPath Get DriveList error=%d", error);
       
  2564     MPX_DEBUG1("<--CMPXDbManager::GetRAMDiskPath");
       
  2565 #endif //__RAMDISK_PERF_ENABLE
       
  2566     return error;
       
  2567     }
       
  2568 
       
  2569 // ---------------------------------------------------------------------------
       
  2570 // CMPXDbManager::IsRamDiskSpaceAvailable
       
  2571 // ---------------------------------------------------------------------------
       
  2572 //
       
  2573 TBool CMPXDbManager::IsRamDiskSpaceAvailable()
       
  2574     {
       
  2575 
       
  2576 #ifdef __RAMDISK_PERF_ENABLE
       
  2577 
       
  2578     MPX_DEBUG1("-->CMPXDbManager::IsDiskSpaceAvailable" );
       
  2579     TInt driveIndex;
       
  2580     RFs::CharToDrive(iRAMDrive, driveIndex);
       
  2581     TVolumeInfo vol;
       
  2582     TInt err = iFs.Volume( vol, driveIndex );
       
  2583     if ( err == KErrNone )
       
  2584         {
       
  2585         MPX_DEBUG2("CMPXDbManager::IsRamDiskSpaceAvailable Free in bytes =%Lu", vol.iFree);
       
  2586         if ( vol.iFree > KMPMinimumRAMSizeToRun )
       
  2587             {
       
  2588             MPX_DEBUG1("CMPXDbManager::IsRamDiskSpaceAvailable Ok to copy");
       
  2589             return ETrue;
       
  2590             }
       
  2591         MPX_DEBUG1("CMPXDbManager::IsRamDiskSpaceAvailable NOT Ok to copy");
       
  2592         return EFalse;
       
  2593         }
       
  2594     
       
  2595     MPX_DEBUG2("CMPXDbManager::IsRamDiskSpaceAvailable Disk Not available to use. %d", err);
       
  2596     MPX_DEBUG1("<--CMPXDbManager::IsDiskSpaceAvailable");
       
  2597 
       
  2598 #endif //__RAMDISK_PERF_ENABLE
       
  2599 
       
  2600     return EFalse;
       
  2601     }
       
  2602 
       
  2603 
       
  2604 // ---------------------------------------------------------------------------
       
  2605 // CMPXDbManager::BlockDiskSpaceL
       
  2606 // ---------------------------------------------------------------------------
       
  2607 //
       
  2608 TBool CMPXDbManager::BlockDiskSpace( TDriveUnit aDrive, TInt aOrigDbSize, TBool aIsMTPInUse )
       
  2609     {
       
  2610 #ifdef __RAMDISK_PERF_ENABLE
       
  2611 
       
  2612     MPX_DEBUG2("-->CMPXDbManager::BlockDiskSpaceL %d", (TInt)aDrive );
       
  2613     
       
  2614     // if current DB size can not fit in RAM, abort now
       
  2615     TInt ramDrive;
       
  2616     RFs::CharToDrive(iRAMDrive, ramDrive);
       
  2617     TVolumeInfo vol;
       
  2618     TInt err = iFs.Volume( vol, ramDrive );
       
  2619     if ( vol.iFree <= aOrigDbSize + KMPMinimumRAMSizeToRun )
       
  2620         {
       
  2621         MPX_DEBUG1("-->CMPXDbManager::BlockDiskSpaceL Not enough even for copy original DB file, leave" );
       
  2622         return EFalse;
       
  2623         }
       
  2624 
       
  2625     // ensure you have the disk volume and database
       
  2626     err = iFs.Volume( vol, (TInt)aDrive );
       
  2627     if (err != KErrNone) 
       
  2628         {
       
  2629         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Volume not available on drive %d", (TInt)aDrive);
       
  2630         return EFalse;
       
  2631         }
       
  2632 
       
  2633     TInt index( GetDatabaseIndex((TInt)aDrive) );
       
  2634     if (index < 0) 
       
  2635         {
       
  2636         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Database not available for drive %d", (TInt)aDrive);
       
  2637         return EFalse;
       
  2638         }
       
  2639 
       
  2640     // Check if the drive has enough space to block
       
  2641     MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk total free space in bytes =%Lu", vol.iFree);
       
  2642     TInt64 blockingSize( CalculateInitalDummyDBSize( vol, aOrigDbSize, aIsMTPInUse ));
       
  2643     MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk blocking size =%Lu", blockingSize);
       
  2644     if ( vol.iFree <= blockingSize + 1*KMPMegaByte )
       
  2645         {
       
  2646         MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL NOk to block");
       
  2647         return EFalse;
       
  2648         }
       
  2649 
       
  2650     // Create and resize the dummy file
       
  2651     TFileName dummyDbFileName; 
       
  2652     TChar ch;
       
  2653     RFs::DriveToChar((TInt)aDrive, ch );
       
  2654     dummyDbFileName.Format( KDummyDbFile, (TUint)ch);
       
  2655     RFile dummyDb;
       
  2656     err = dummyDb.Replace( iFs, dummyDbFileName, EFileWrite );
       
  2657     if (err != KErrNone) 
       
  2658         {
       
  2659         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't open dummy file %d", err);
       
  2660         return EFalse;
       
  2661         }
       
  2662     err = dummyDb.SetSize( blockingSize );
       
  2663     if ( err )
       
  2664         {
       
  2665         MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't resize dummy file %d", err);
       
  2666         dummyDb.Close();
       
  2667         return EFalse;
       
  2668         }
       
  2669 
       
  2670     dummyDb.Close();
       
  2671     MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL Ok to block");
       
  2672 
       
  2673     iDatabaseHandles[index].iDummyFilePath.Copy(dummyDbFileName);
       
  2674     MPX_DEBUG1("<--CMPXDbManager::BlockDiskSpace");
       
  2675 
       
  2676     return ETrue;
       
  2677 #else
       
  2678     return EFalse;
       
  2679 #endif //__RAMDISK_PERF_ENABLE
       
  2680     }
       
  2681 
       
  2682 
       
  2683 // ---------------------------------------------------------------------------
       
  2684 // CMPXDbManager::CalculateInitalDummyDBSizeL
       
  2685 //
       
  2686 //a) MTP case
       
  2687 //-------------
       
  2688 //        totalNumOfSongsCanFit = <disk free space> / 2 MB;
       
  2689 //        metadataSize = totalNumOfSongsCanFit * 3000B 
       
  2690 //        estimatedDBSize = metadataSize + <orig DB size>;
       
  2691 //        dummyDBSize = MIN (iMaximumAllowedRAMDiskSpaceToCopy , estimatedDBSize )
       
  2692 
       
  2693 //b) Harvesting case
       
  2694 //-------------------
       
  2695 //        totalNumOfSongsCanFit = <disk total size>/ 2 MB
       
  2696 //        metadataSize = totalNumOfSongsCanFit * 3000B 
       
  2697 //        estimatedSize = metadataSize+ <orig DB size>
       
  2698 //        freeDiskSpace = <disk free space> - 1 MB
       
  2699 //        dummyDBSize = MIN (freeDiskSpace, iMaximumAllowedRAMDiskSpaceToCopy , estimatedSize);
       
  2700 //
       
  2701 // ---------------------------------------------------------------------------
       
  2702 //
       
  2703 TInt64 CMPXDbManager::CalculateInitalDummyDBSize( const TVolumeInfo& aVol, TInt aOrigDbSize, TBool aIsMTPInUse )
       
  2704     {
       
  2705 #ifdef __RAMDISK_PERF_ENABLE
       
  2706     MPX_DEBUG1("-->CMPXDbManager::CalculateInitalDummyDBSize");
       
  2707 
       
  2708     if ( aIsMTPInUse )
       
  2709         {
       
  2710         TInt64 totalNumOfSongsCanFit = aVol.iFree / KMPEstimatedSongInBytes;
       
  2711         MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize aVol.iFree=%Lu", aVol.iFree );
       
  2712         MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize totalNumOfSongsCanFit=%Lu", totalNumOfSongsCanFit );
       
  2713         TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize;
       
  2714         MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize (MTP case) estimated DB size from calculation=%Lu", estimatedSize );
       
  2715         if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy )
       
  2716             {
       
  2717             MPX_DEBUG2("<--CMPXDbManager::CalculateInitalDummyDBSize returned iMaximumAllowedRAMDiskSpaceToCopy %d", iMaximumAllowedRAMDiskSpaceToCopy);
       
  2718             return iMaximumAllowedRAMDiskSpaceToCopy;
       
  2719             }
       
  2720         else
       
  2721             {
       
  2722             MPX_DEBUG2("<--CMPXDbManager::CalculateInitalDummyDBSize returned %Lu", estimatedSize );
       
  2723             return estimatedSize;
       
  2724             }
       
  2725         }
       
  2726     else
       
  2727         {
       
  2728         TInt64 totalNumOfSongsCanFit = aVol.iSize / KMPEstimatedSongInBytes;
       
  2729         TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize;
       
  2730         MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize estimated DB size from calculation=%Lu", estimatedSize );
       
  2731         if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy )
       
  2732             {
       
  2733             MPX_DEBUG1("<--CMPXDbManager::CalculateInitalDummyDBSize");
       
  2734             // If estimated size is larger than expected free RAM size, 
       
  2735             // and if the RAM size is larger than free disk space,
       
  2736             // then use free disk space. 1*KMPMegaByte prevent MP to use up all diskspace
       
  2737             //return iMaximumAllowedRAMDiskSpaceToCopy > aVol.iFree - 1*KMPMegaByte  
       
  2738             //    ? aVol.iFree - 1*KMPMegaByte : iMaximumAllowedRAMDiskSpaceToCopy;
       
  2739             return iMaximumAllowedRAMDiskSpaceToCopy;
       
  2740             }
       
  2741         else
       
  2742             {
       
  2743             MPX_DEBUG1("<--CMPXDbManager::CalculateInitalDummyDBSize");
       
  2744             // If estimated size is larger than disk free size, use free diskspace size,            
       
  2745             //return estimatedSize > aVol.iFree - 1*KMPMegaByte
       
  2746             //    ? aVol.iFree - 1*KMPMegaByte : estimatedSize;
       
  2747             return estimatedSize;
       
  2748             }
       
  2749         }
       
  2750     
       
  2751 #endif //__RAMDISK_PERF_ENABLE    
       
  2752     }
       
  2753     
       
  2754 
       
  2755 // ---------------------------------------------------------------------------
       
  2756 // CMPXDbManager::GetDatabaseIndex
       
  2757 // ---------------------------------------------------------------------------
       
  2758 //
       
  2759 TInt CMPXDbManager::GetDatabaseIndex(TInt aDrive) 
       
  2760     {
       
  2761 #ifdef __RAMDISK_PERF_ENABLE
       
  2762     MPX_DEBUG2("-->CMPXDbManager::GetDatabaseIndex %d", aDrive );
       
  2763     TInt count(iDatabaseHandles.Count());
       
  2764     for (TInt i = 0; i < count; ++i)
       
  2765         {
       
  2766         if ( iDatabaseHandles[i].iDrive == aDrive )
       
  2767             {
       
  2768             return i;
       
  2769             }
       
  2770         }    
       
  2771 #endif //__RAMDISK_PERF_ENABLE    
       
  2772     MPX_DEBUG1("<--CMPXDbManager::GetDatabaseIndex returned -1");
       
  2773     return -1;
       
  2774     }
       
  2775 
       
  2776 
       
  2777 // ---------------------------------------------------------------------------
       
  2778 // CMPXDbManager::EnsureRamSpaceL
       
  2779 // ---------------------------------------------------------------------------
       
  2780 //
       
  2781 EXPORT_C void CMPXDbManager::EnsureRamSpaceL() 
       
  2782     {
       
  2783 #ifdef __RAMDISK_PERF_ENABLE
       
  2784     MPX_DEBUG1("-->CMPXDbManager::EnsureRamSpaceL");
       
  2785 
       
  2786     if ( iRAMInUse )
       
  2787         {
       
  2788         TVolumeInfo vol;
       
  2789         TInt driveIndex;
       
  2790         RFs::CharToDrive( iRAMDrive, driveIndex );
       
  2791 
       
  2792         TInt errRAM = iFs.Volume( vol, driveIndex );
       
  2793         if ( errRAM == KErrNone && vol.iFree < KMPMinimumRAMSizeToRun )
       
  2794             {
       
  2795             // RAM not enough, copy back to normal drive and continue to harvest.
       
  2796             MPX_DEBUG1("CMPXDbManager::EnsureRamSpaceL RAM diskspace is full, copy dbs back");
       
  2797             CopyDBsFromRamL();
       
  2798             }
       
  2799         else
       
  2800             {
       
  2801             TInt size=0;
       
  2802             TInt err = GetTotalRamDatabasesSize(size);
       
  2803             if ( err || (size > iMaximumAllowedRAMDiskSpaceToCopy) )
       
  2804                 {
       
  2805                 // Databases using too much RAM space, copy back to normal drive and continue to harvest.
       
  2806                 if ( err )
       
  2807                     {
       
  2808                     MPX_DEBUG2("CMPXDbManager::EnsureRamSpaceL Get DBs Size Err = %d, copy dbs back", err);
       
  2809                     }
       
  2810                 else
       
  2811                     {
       
  2812                     MPX_DEBUG2("CMPXDbManager::EnsureRamSpaceL DBs using too much RAM space size = %d, copy dbs back", size);
       
  2813                     }
       
  2814                 CopyDBsFromRamL();
       
  2815                 }
       
  2816             }
       
  2817         }
       
  2818     MPX_DEBUG1("<--CMPXDbManager::EnsureRamSpaceL");
       
  2819 #endif //__RAMDISK_PERF_ENABLE    
       
  2820     }
       
  2821 
       
  2822 
       
  2823 // ---------------------------------------------------------------------------
       
  2824 // CMPXDbManager::EnsureDiskSpaceL
       
  2825 // ---------------------------------------------------------------------------
       
  2826 //
       
  2827 void CMPXDbManager::EnsureDiskSpaceL(TInt aDrive) 
       
  2828     {
       
  2829     MPX_DEBUG2("-->CMPXDbManager::EnsureDiskSpaceL for drive %d", aDrive);
       
  2830     // handle the case of C drive
       
  2831     TDriveUnit drive(aDrive);
       
  2832     TDriveUnit cdrive(KRootDrive());
       
  2833 
       
  2834     if(drive == cdrive)
       
  2835         {
       
  2836         if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0, aDrive))
       
  2837             {
       
  2838             MPX_DEBUG1("CMPXDbManager::EnsureDiskSpaceL Error diskspace full");
       
  2839             User::Leave(KErrDiskFull);
       
  2840             }
       
  2841 
       
  2842         return;
       
  2843         }
       
  2844 
       
  2845     // handle other drives (eg. removable EDriveE)
       
  2846     TInt count(iDatabaseHandles.Count());
       
  2847     for (TInt i = 0; i < count; ++i)
       
  2848         {
       
  2849         if (((KDbManagerAllDrives == aDrive) ||
       
  2850             (aDrive == iDatabaseHandles[i].iDrive)) &&
       
  2851             iDatabaseHandles[i].iOpen)
       
  2852             {
       
  2853             if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0,
       
  2854                 iDatabaseHandles[i].iDrive))
       
  2855                 {
       
  2856                 MPX_DEBUG1("CMPXDbManager::EnsureDiskSpaceL Error diskspace full");
       
  2857                 User::Leave(KErrDiskFull);
       
  2858                 }
       
  2859             }
       
  2860 
       
  2861         if (aDrive == iDatabaseHandles[i].iDrive)
       
  2862             {
       
  2863             // exit if just one drive to check
       
  2864             break;
       
  2865             }
       
  2866         }
       
  2867     MPX_DEBUG1("<--CMPXDbManager::EnsureDiskSpaceL");
       
  2868     }
       
  2869 
       
  2870 
       
  2871 // ---------------------------------------------------------------------------
       
  2872 // CMPXDbManager::DoBackupDBs
       
  2873 // ---------------------------------------------------------------------------
       
  2874 //
       
  2875 /*EXPORT_C void CMPXDbManager::BackupDBsL()
       
  2876     {
       
  2877 #ifdef __RAMDISK_PERF_ENABLE
       
  2878     MPX_DEBUG1("-->CMPXDbManager::BackupDBsL");
       
  2879 
       
  2880     TInt transactionCount = iTransactionCount;
       
  2881     if (iTransactionCount > 0) 
       
  2882         {
       
  2883         iTransactionCount = 0;
       
  2884         DoCommitL();
       
  2885         }
       
  2886     
       
  2887     TInt count(iDatabaseHandles.Count());
       
  2888     for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb ; ++i)
       
  2889         {
       
  2890         CloseDatabaseL( iDatabaseHandles[i].iDrive );            
       
  2891 
       
  2892         TInt err= BaflUtils::CopyFile(iFs, 
       
  2893             iDatabaseHandles[i].iTargetFullFilePath->Des(), 
       
  2894             iDatabaseHandles[i].iOrigFullFilePath->Des());
       
  2895 
       
  2896         MPX_DEBUG2("CMPXDbManager::BackupDBsL err = %d", err);     
       
  2897 
       
  2898         OpenDatabaseL( iDatabaseHandles[i].iDrive );      
       
  2899         }
       
  2900         
       
  2901     if (transactionCount > 0) 
       
  2902         {
       
  2903         DoBeginL();
       
  2904         iTransactionCount = transactionCount;
       
  2905         }
       
  2906     MPX_DEBUG1("<--CMPXDbManager::BackupDBsL");
       
  2907 #endif //__RAMDISK_PERF_ENABLE    
       
  2908     }*/
       
  2909     
       
  2910 // ---------------------------------------------------------------------------
       
  2911 // CMPXDbManager::GetTotalDatabasesSize
       
  2912 // ---------------------------------------------------------------------------
       
  2913 //
       
  2914 TInt CMPXDbManager::GetTotalDatabasesSize(TInt& aSize)
       
  2915     {
       
  2916     MPX_FUNC("CMPXDbManager::GetTotalDatabasesSize");
       
  2917     TInt err = KErrNotSupported;
       
  2918 #ifdef __RAMDISK_PERF_ENABLE
       
  2919     TInt size=0;
       
  2920     TInt count(iDatabaseHandles.Count());
       
  2921     err = KErrNone;
       
  2922     for ( TInt i = 0; i < count ; ++i )
       
  2923         {
       
  2924         // Generate database name.
       
  2925         TFileName dbFilename;
       
  2926         TDriveUnit drive(iDatabaseHandles[i].iDrive);
       
  2927         dbFilename.Append(drive.Name());
       
  2928         dbFilename.Append(KDBFilePath);
       
  2929         TFileName filename;            
       
  2930         filename.Format(KSecurePath, User::Identity().iUid, iDbFile); //x:\private\10281e17\[sldfdsf]mpxv2_5.db
       
  2931         dbFilename.Append(filename);
       
  2932         MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Database name = %S", &dbFilename);
       
  2933         TEntry entry;
       
  2934         err = iFs.Entry( dbFilename, entry );
       
  2935         if (err == KErrNotFound || err == KErrNotReady )
       
  2936             {
       
  2937             MPX_DEBUG3("CMPXDbManager::GetTotalDatabasesSize - Ignored %S, error = %d", &dbFilename, err);
       
  2938             err = KErrNone;
       
  2939             continue;
       
  2940             }
       
  2941         if ( err != KErrNone )
       
  2942             {
       
  2943             break;
       
  2944             }
       
  2945         MPX_DEBUG3("CMPXDbManager::GetTotalDatabasesSize - Size of Db %S = %d", &dbFilename, entry.iSize);
       
  2946         // sum up size
       
  2947         size += entry.iSize;
       
  2948         }
       
  2949     aSize = size;
       
  2950     MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Total Size of Dbs = %d", size);
       
  2951     
       
  2952 #endif //__RAMDISK_PERF_ENABLE    
       
  2953     MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Return err = %d", err);
       
  2954     return err;
       
  2955     }
       
  2956     
       
  2957 // ---------------------------------------------------------------------------
       
  2958 // CMPXDbManager::GetTotalRamDatabasesSize
       
  2959 // ---------------------------------------------------------------------------
       
  2960 //
       
  2961 TInt CMPXDbManager::GetTotalRamDatabasesSize(TInt& aSize)
       
  2962     {
       
  2963     MPX_FUNC("CMPXDbManager::GetTotalRamDatabasesSize");
       
  2964     TInt err = KErrNotSupported;
       
  2965 #ifdef __RAMDISK_PERF_ENABLE
       
  2966     TInt size=0;
       
  2967     TInt count(iDatabaseHandles.Count());
       
  2968     err = KErrNone;
       
  2969     for ( TInt i = 0; i < count ; ++i )
       
  2970         {
       
  2971         // make sure this db is in ram drive.
       
  2972         if ( !iDatabaseHandles[i].iUseRAMdb )
       
  2973             {
       
  2974             continue;
       
  2975             }
       
  2976         // Generate database name.
       
  2977         TFileName dbFilename;
       
  2978         dbFilename.Append(iRAMFolder);
       
  2979         TBuf<2> d;
       
  2980         TDriveUnit drive(iDatabaseHandles[i].iDrive);
       
  2981         d.Append(drive.Name());
       
  2982         HBufC* temp = HBufC::NewLC(KMaxFileName);
       
  2983         temp->Des().Append(d.Left(1));
       
  2984         temp->Des().Append(iDbFile->Des());
       
  2985         TFileName filename;            
       
  2986         filename.Format(KSecurePath, User::Identity().iUid, temp);
       
  2987         CleanupStack::PopAndDestroy(temp);
       
  2988         dbFilename.Append(filename);
       
  2989         MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Database name = %S", &dbFilename);
       
  2990         TEntry entry;
       
  2991         err = iFs.Entry( dbFilename, entry );
       
  2992         if ( (err != KErrNone) && (err != KErrNotFound) )
       
  2993             {
       
  2994             break;
       
  2995             }
       
  2996         MPX_DEBUG3("CMPXDbManager::GetTotalRamDatabasesSize - Size of Db %S = %d", &dbFilename, entry.iSize);
       
  2997         // sum up size
       
  2998         size += entry.iSize;
       
  2999         }
       
  3000     aSize = size;
       
  3001     MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Total Size of Dbs = %d", size);
       
  3002 #endif //__RAMDISK_PERF_ENABLE    
       
  3003     MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Return err = %d", err);
       
  3004     return err;
       
  3005     }
       
  3006     
  2052 // End of File
  3007 // End of File