mmappcomponents/harvester/filehandler/src/mpxharvesterdbmanager.cpp
branchRCL_3
changeset 9 bee149131e4b
parent 0 a2952bb97e68
child 17 780c925249c1
equal deleted inserted replaced
4:d45095c2f4f3 9:bee149131e4b
    19 #include <e32base.h>
    19 #include <e32base.h>
    20 #ifdef RD_MULTIPLE_DRIVE
    20 #ifdef RD_MULTIPLE_DRIVE
    21 #include <pathinfo.h>
    21 #include <pathinfo.h>
    22 #include <driveinfo.h>
    22 #include <driveinfo.h>
    23 #endif //RD_MULTIPLE_DRIVE
    23 #endif //RD_MULTIPLE_DRIVE
       
    24 
       
    25 #ifdef __RAMDISK_PERF_ENABLE
       
    26 #include <centralrepository.h>
       
    27 #include <BAUTILS.H>  
       
    28 #include <mpxharvestercrkeys.h>
       
    29 #include "mpxdbcommon.h"
       
    30 #endif //__RAMDISK_PERF_ENABLE
       
    31 
    24 #include <mpxlog.h>
    32 #include <mpxlog.h>
    25 #include "mpxharvesterdbmanager.h"
    33 #include "mpxharvesterdbmanager.h"
    26 #include "mpxharvesterdb.h"
    34 #include "mpxharvesterdb.h"
    27 #include "mpxharvesterdbtable.h"
    35 #include "mpxharvesterdbtable.h"
    28 
    36 
       
    37 // CONSTANTS
       
    38 #ifdef __RAMDISK_PERF_ENABLE
       
    39 _LIT( KHarvesterDummyDBName, "harvesterDummy.dat" );
       
    40 const TInt64 KMPMegaByte = 1048576;
       
    41 const TInt64 KMPEstimatedSongInBytes = KMPMegaByte * 2; 
       
    42 const TInt KMPEstimatedSizePerDBEntry = KMaxFileName; // worst scenario, can be lower if needed
       
    43 //const TInt KUpdateDBCount = 200;
       
    44 const TInt KMPMinimumRAMSizeToRun = 6 * KMPMegaByte;
       
    45 #endif //__RAMDISK_PERF_ENABLE
       
    46 
       
    47 
    29 // ---------------------------------------------------------------------------
    48 // ---------------------------------------------------------------------------
    30 // C++ Constructor
    49 // C++ Constructor
    31 // ---------------------------------------------------------------------------
    50 // ---------------------------------------------------------------------------
    32 //
    51 //
    33 CMPXHarvesterDatabaseManager::CMPXHarvesterDatabaseManager( RFs& aFs )
    52 CMPXHarvesterDatabaseManager::CMPXHarvesterDatabaseManager( RFs& aFs ):
    34                                                                   : iFs( aFs )
    53         iFs( aFs )
       
    54 #ifdef __RAMDISK_PERF_ENABLE
       
    55         ,iRAMDiskPerfEnabled(EFalse),
       
    56         iMaximumAllowedRAMDiskSpaceToCopy(0),
       
    57         //iUpdateCount(0),
       
    58         iMtpMode(EFalse)
       
    59         //iMtpAddCount(0)
       
    60 #endif //__RAMDISK_PERF_ENABLE
    35     {
    61     {
    36 
    62 
    37     }
    63     }
    38 
    64 
    39 
    65 
    41 // 2nd Phase Constructor
    67 // 2nd Phase Constructor
    42 // ---------------------------------------------------------------------------
    68 // ---------------------------------------------------------------------------
    43 //
    69 //
    44 void CMPXHarvesterDatabaseManager::ConstructL()
    70 void CMPXHarvesterDatabaseManager::ConstructL()
    45     {
    71     {
       
    72     MPX_FUNC("CMPXHarvesterDatabaseManager::ConstructL");
    46     User::LeaveIfError( iDBSession.Connect() );
    73     User::LeaveIfError( iDBSession.Connect() );
       
    74 
       
    75 #ifdef __RAMDISK_PERF_ENABLE
       
    76     TInt temp;
       
    77     CRepository* repository = CRepository::NewLC( KCRUIDMpxHarvesterFeatures );
       
    78     MPX_DEBUG1("CMPXHarvesterDatabaseManager::ConstructL got repository");        
       
    79     User::LeaveIfError( repository->Get( KMpxHarvesterEnableRamDisk, temp ));
       
    80     iRAMDiskPerfEnabled = temp;
       
    81     MPX_DEBUG2("CMPXHarvesterDatabaseManager::ConstructL KMpxHarvesterEnableRamDisk %d", iRAMDiskPerfEnabled);        
       
    82     
       
    83     User::LeaveIfError( repository->Get( KMpxHarvesterMaxAllowedRamDiskSpace, temp) );
       
    84     iMaximumAllowedRAMDiskSpaceToCopy = temp * KMPMegaByte;
       
    85     MPX_DEBUG2("CMPXHarvesterDatabaseManager::ConstructL KMpxHarvesterMaxAllowedRamDiskSpace %Lu", iMaximumAllowedRAMDiskSpaceToCopy);        
       
    86     CleanupStack::PopAndDestroy(repository);
       
    87             
       
    88     if ( iRAMDiskPerfEnabled )
       
    89         {
       
    90         MPX_DEBUG1("CMPXHarvesterDatabaseManager::ConstructL RAMDisk performance is enabled.");
       
    91         MPX_DEBUG2("CMPXHarvesterDatabaseManager::ConstructL RAMDisk iMaximumAllowedRAMDiskSpaceToCopy=%Lu", iMaximumAllowedRAMDiskSpaceToCopy);
       
    92         if ( GetRAMDiskPath() != KErrNone )
       
    93             {
       
    94             // Error finding ram drive, disable ram disk
       
    95             iRAMDiskPerfEnabled = EFalse;
       
    96             }
       
    97         }
       
    98     else
       
    99         {
       
   100         MPX_DEBUG2("CMPXHarvesterDatabaseManager::ConstructL RAMDisk performance is NOT enabled iRAMDiskPerfEnabled=%d", iRAMDiskPerfEnabled);
       
   101         MPX_DEBUG2("CMPXHarvesterDatabaseManager::ConstructL RAMDisk iMaximumAllowedRAMDiskSpaceToCopy=%Lu", iMaximumAllowedRAMDiskSpaceToCopy);
       
   102         }
       
   103 #endif //__RAMDISK_PERF_ENABLE
    47     }
   104     }
    48 
   105 
    49 
   106 
    50 // ---------------------------------------------------------------------------
   107 // ---------------------------------------------------------------------------
    51 // Two-Phased Constructor
   108 // Two-Phased Constructor
    66 // Destructor
   123 // Destructor
    67 // ---------------------------------------------------------------------------
   124 // ---------------------------------------------------------------------------
    68 //
   125 //
    69 CMPXHarvesterDatabaseManager::~CMPXHarvesterDatabaseManager()
   126 CMPXHarvesterDatabaseManager::~CMPXHarvesterDatabaseManager()
    70     {
   127     {
       
   128 #ifdef __RAMDISK_PERF_ENABLE
       
   129     TInt count(iDatabases.Count());
       
   130     for (TInt i = 0; i < count; ++i)
       
   131         {
       
   132         RemoveDummyFile(i);
       
   133         }
       
   134 #endif // __RAMDISK_PERF_ENABLE
    71     iDatabases.ResetAndDestroy();
   135     iDatabases.ResetAndDestroy();
    72     iDatabases.Close();
   136     iDatabases.Close();
    73     iDBSession.Close();
   137     iDBSession.Close();
    74     }
   138     }
    75 
   139 
   187 
   251 
   188     // Re-open a specific database
   252     // Re-open a specific database
   189     //
   253     //
   190     if (!IsRemoteDrive(aDrive))
   254     if (!IsRemoteDrive(aDrive))
   191         {
   255         {
   192     TInt count( iDatabases.Count() );
   256         TInt count( iDatabases.Count() );
   193     for( TInt i=0; i<count; ++i )
   257         for( TInt i=0; i<count; ++i )
   194         {
   258             {
   195         CMPXHarvesterDB* db = (CMPXHarvesterDB*) iDatabases[i];
   259             CMPXHarvesterDB* db = (CMPXHarvesterDB*) iDatabases[i];
   196         if( db->GetDbDrive() == aDrive )
   260             if( db->GetDbDrive() == aDrive )
   197             {
   261                 {
   198             db->OpenL();
   262                 db->OpenL();
   199             break;
   263                 break;
   200             }
   264                 }
   201         }
   265             }
   202         }
   266         }
   203     MPX_DEBUG1("CMPXHarvesterDatabaseManager::OpenDatabaseL --->");
   267     MPX_DEBUG1("CMPXHarvesterDatabaseManager::OpenDatabaseL --->");
   204     }
   268     }
   205 
   269 
   206 // ---------------------------------------------------------------------------
   270 // ---------------------------------------------------------------------------
   413             }
   477             }
   414         }      
   478         }      
   415     MPX_DEBUG1("CMPXHarvesterDatabaseManager::Rollback -->");
   479     MPX_DEBUG1("CMPXHarvesterDatabaseManager::Rollback -->");
   416     }
   480     }
   417 
   481 
       
   482 #ifdef __RAMDISK_PERF_ENABLE
       
   483 // ---------------------------------------------------------------------------
       
   484 // CMPXHarvesterDatabaseManager::CopyDBsToRamL
       
   485 // ---------------------------------------------------------------------------
       
   486 //
       
   487 void CMPXHarvesterDatabaseManager::CopyDBsToRamL( TBool aMtpMode )
       
   488     {
       
   489     MPX_FUNC("CMPXHarvesterDatabaseManager::CopyDBsToRamL");
       
   490     iMtpMode = aMtpMode;
       
   491     //iMtpAddCount = 0;
       
   492     //iUpdateCount = 0;
       
   493     
       
   494     if( iRAMDiskPerfEnabled )
       
   495         {
       
   496         // Check for low ram disk space.
       
   497         if ( !IsRamDiskSpaceAvailable() || IsRamDiskLow() )
       
   498             {
       
   499             return;
       
   500             }
       
   501         
       
   502         // Check if we are over the allowed ram space.
       
   503         TInt dbSize=0;
       
   504         TInt err = GetTotalDatabasesSize(dbSize, EFalse);
       
   505         if ( err || (dbSize > iMaximumAllowedRAMDiskSpaceToCopy) )
       
   506             {
       
   507             MPX_DEBUG2("CMPXDbManager::CopyDBsToRamL Over the allowed Ram disk limit %Lu", iMaximumAllowedRAMDiskSpaceToCopy );
       
   508             return;
       
   509             }
       
   510     
       
   511         TInt count(iDatabases.Count());
       
   512         MPX_DEBUG2("CMPXHarvesterDatabaseManager::CopyDBsToRamL() iDatabaseHandles.Count()=%d", count);
       
   513         for (TInt i = 0; i < count; ++i)
       
   514             {
       
   515             TDriveUnit drive( iDatabases[i]->GetDbDrive() );
       
   516             // Don't copy db on C drive.
       
   517             if ( (TInt)drive == EDriveC )
       
   518                 {
       
   519                 MPX_DEBUG1("CMPXHarvesterDatabaseManager::CopyDBsToRamL() Drive C: skipped.");
       
   520                 continue;
       
   521                 }
       
   522             
       
   523             // Check if database is in RAM drive.
       
   524             if ( iDatabases[i]->IsUseRamDrive())
       
   525                 {
       
   526                 MPX_DEBUG2("CMPXHarvesterDatabaseManager::CopyDBsToRamL Drive %d is already in Ram Drive.", (TInt)drive);
       
   527                 continue;
       
   528                 }
       
   529             
       
   530             // Get the db state in order to restore it later.
       
   531             TDbState dbState = iDatabases[i]->GetDbState();
       
   532             iDatabases[i]->SetDbStateL( EDbClose );
       
   533             TRAPD(err, DoCopyDBToRamL( drive ));
       
   534             if ( err != KErrNone )
       
   535                 {
       
   536                 MPX_DEBUG2("CMPXHarvesterDatabaseManager::CopyDBsToRamL error=%d", err);
       
   537                 // delete dummy file
       
   538                 RemoveDummyFile(i);
       
   539                 
       
   540                 // delete db in ram drive
       
   541                 TFileName ramDb = GenerateHarvesterDbName( drive, ETrue );
       
   542                 TInt delErr = BaflUtils::DeleteFile(iFs, ramDb);
       
   543                 MPX_DEBUG3("CMPXHarvesterDatabaseManager::CopyDBsToRamL db on ram drive deleted file=%S, err=%d", &ramDb, delErr);
       
   544                 
       
   545                 iDatabases[i]->SetRamDriveInfo( iRAMDrive, EFalse);
       
   546                 }
       
   547             // Restore the db state.
       
   548             iDatabases[i]->SetDbStateL( dbState );
       
   549             }
       
   550         }
       
   551     }
       
   552 
       
   553 // ---------------------------------------------------------------------------
       
   554 // CMPXHarvesterDatabaseManager::DoCopyDBToRamL
       
   555 // ---------------------------------------------------------------------------
       
   556 //
       
   557 void CMPXHarvesterDatabaseManager::DoCopyDBToRamL(TDriveUnit aDriveUnit)
       
   558     {
       
   559     MPX_FUNC("CMPXHarvesterDatabaseManager::DoCopyDBToRamL");
       
   560     MPX_DEBUG2("CMPXHarvesterDatabaseManager::DoCopyDBToRamL Database from drive %d", (TInt)aDriveUnit );
       
   561     TFileName dst;
       
   562     TFileName src;
       
   563     dst.Zero();
       
   564     dst.Append(iRAMFolder);
       
   565     BaflUtils::EnsurePathExistsL( iFs, dst );
       
   566     src = GenerateHarvesterDbName( aDriveUnit );
       
   567     MPX_DEBUG2("RAMDisk src path=%S", &src);
       
   568     dst = GenerateHarvesterDbName( aDriveUnit, ETrue );
       
   569     MPX_DEBUG2("RAMDisk dst path=%S", &dst);
       
   570     TEntry entry;
       
   571     iFs.Entry( src, entry );
       
   572     User::LeaveIfError( BlockDiskSpaceL( aDriveUnit, entry.iSize ));
       
   573     User::LeaveIfError( BaflUtils::CopyFile(iFs, src, dst ) );
       
   574     MPX_DEBUG1("CMPXHarvesterDatabaseManager::DoCopyDBToRamL Copy to Ram Drive Successful.");
       
   575     GetDatabaseL( (TDriveNumber) (TInt)aDriveUnit ).SetRamDriveInfo(iRAMDrive, ETrue);
       
   576     }
       
   577 
       
   578 // ---------------------------------------------------------------------------
       
   579 // CMPXHarvesterDatabaseManager::CopyDBsFromRamL
       
   580 // ---------------------------------------------------------------------------
       
   581 //
       
   582 void CMPXHarvesterDatabaseManager::CopyDBsFromRamL()
       
   583     {
       
   584     MPX_FUNC("CMPXHarvesterDatabaseManager::CopyDBsFromRamL");
       
   585     // Stop monitoring ram drive for low disk space.
       
   586     iMtpMode = EFalse;
       
   587     //iMtpAddCount = 0;
       
   588     //iUpdateCount = 0;
       
   589 
       
   590     if( iRAMDiskPerfEnabled )
       
   591        {
       
   592         TInt count(iDatabases.Count());
       
   593         for (TInt i = 0; i < count; ++i)
       
   594             {
       
   595             TInt err = KErrNone;
       
   596             TDriveUnit drive( iDatabases[i]->GetDbDrive() );
       
   597             // Check if database is in RAM drive.
       
   598             if ( !iDatabases[i]->IsUseRamDrive())
       
   599                 {
       
   600                 MPX_DEBUG2("CMPXHarvesterDatabaseManager::CopyDBsFromRamL Drive %d is NOT in Ram Drive.", (TInt)drive);
       
   601                 continue;
       
   602                 }
       
   603 
       
   604             // Set the UseRamDrive bool to False.
       
   605             iDatabases[i]->SetRamDriveInfo( iRAMDrive, EFalse );
       
   606             // Get the db state in order to restore it later.
       
   607             TDbState dbState = iDatabases[i]->GetDbState();
       
   608             iDatabases[i]->SetDbStateL(EDbClose);
       
   609             TRAP( err, DoCopyDBFromRamL(drive) );
       
   610             if ( err )
       
   611                 {
       
   612                 MPX_DEBUG2("CMPXHarvesterDatabaseManager::CopyDBsFromRamL copy error=%d", err);                
       
   613                 //anyting wrong, delete the temp file.
       
   614                 RemoveDummyFile(i);
       
   615                 }
       
   616 
       
   617             // Restore the db state.
       
   618             iDatabases[i]->SetDbStateL( dbState );
       
   619             }
       
   620         }
       
   621     }
       
   622 
       
   623 // ---------------------------------------------------------------------------
       
   624 // CMPXHarvesterDatabaseManager::DoCopyDBFromRamL
       
   625 // ---------------------------------------------------------------------------
       
   626 //
       
   627 void CMPXHarvesterDatabaseManager::DoCopyDBFromRamL(TDriveUnit aDriveUnit)
       
   628     {
       
   629     MPX_FUNC("CMPXHarvesterDatabaseManager::DoCopyDBFromRamL");
       
   630     TFileName dst;
       
   631     TFileName src;
       
   632     TInt err = KErrNone;
       
   633     
       
   634     dst = GenerateHarvesterDbName( aDriveUnit );
       
   635     src = GenerateHarvesterDbName( aDriveUnit, ETrue );
       
   636     MPX_DEBUG3("CMPXHarvesterDatabaseManager::DoCopyDBFromRamL from %S to %S", &src, &dst );
       
   637 
       
   638     // Rename the temp file into real Db name
       
   639     TFileName dummyDbFileName = GenerateDummyDbName( aDriveUnit ); 
       
   640 
       
   641     //Copy Db from RAM to replace dummy file
       
   642     err = BaflUtils::CopyFile(iFs, src, dummyDbFileName);
       
   643     MPX_DEBUG2("CMPXHarvesterDatabaseManager::DoCopyDBFromRamL database copied from ram drive err=%d.", err);
       
   644     
       
   645     // delete db in ram drive.
       
   646     TInt delErr = BaflUtils::DeleteFile(iFs, src);
       
   647     MPX_DEBUG3("CMPXHarvesterDatabaseManager::DoCopyDBFromRamL db on ram drive deleted file=%S, err=%d", &src, delErr);
       
   648 
       
   649     // Make sure we del db from ram drive before leaving.
       
   650     User::LeaveIfError( err );
       
   651     
       
   652     // Delete existing DB on drive
       
   653     delErr = BaflUtils::DeleteFile(iFs, dst);
       
   654     MPX_DEBUG2("CMPXHarvesterDatabaseManager::DoCopyDBFromRamL destination file deleted err=%d", delErr);
       
   655 
       
   656     // rename dummy file to real db name
       
   657     User::LeaveIfError( BaflUtils::RenameFile(iFs, dummyDbFileName, dst) );
       
   658     MPX_DEBUG1("CMPXHarvesterDatabaseManager::DoCopyDBFromRamL dummy file renamed.");
       
   659     }
       
   660 
       
   661 // ---------------------------------------------------------------------------
       
   662 // CMPXHarvesterDatabaseManager::GetRAMDiskPath
       
   663 // ---------------------------------------------------------------------------
       
   664 //
       
   665 TInt CMPXHarvesterDatabaseManager::GetRAMDiskPath()
       
   666     {
       
   667     MPX_FUNC("CMPXHarvesterDatabaseManager::GetRAMDiskPath");
       
   668     TDriveList driveList;
       
   669     TBool ramDriveFound = EFalse;
       
   670     iRAMFolder.Zero();
       
   671     
       
   672     TInt error = iFs.DriveList( driveList );
       
   673     if ( error == KErrNone )
       
   674         {
       
   675         for ( TInt i = 0; i < driveList.Length(); i++ )
       
   676             {
       
   677             if ( driveList[i] != 0 )
       
   678                 {
       
   679                 TDriveInfo info;
       
   680                 TInt err = iFs.Drive( info, i ); 
       
   681                 if ( !err && info.iType == EMediaRam )
       
   682                     {
       
   683                     TDriveUnit driveUnit( i );
       
   684                     iRAMDrive = (TDriveNumber)i;
       
   685                     iRAMFolder.Append(driveUnit.Name());
       
   686                     iRAMFolder.Append(KHarvesterDBPath);
       
   687                     ramDriveFound = ETrue;
       
   688                     MPX_DEBUG2("RAMDisk path=%S", &iRAMFolder);
       
   689                     break;
       
   690                     }
       
   691                 }
       
   692             }
       
   693         
       
   694         // Check if ram drive is found.
       
   695         if ( !ramDriveFound )
       
   696             {
       
   697             error = KErrNotFound;
       
   698             }
       
   699         }
       
   700     MPX_DEBUG2("CMPXDbManager::GetRAMDiskPath Get DriveList error=%d", error);
       
   701     return error;
       
   702     }
       
   703 
       
   704 // ---------------------------------------------------------------------------
       
   705 // CMPXHarvesterDatabaseManager::IsRamDiskSpaceAvailable
       
   706 // ---------------------------------------------------------------------------
       
   707 //
       
   708 TBool CMPXHarvesterDatabaseManager::IsRamDiskSpaceAvailable()
       
   709     {
       
   710     MPX_FUNC("CMPXHarvesterDatabaseManager::IsRamDiskSpaceAvailable");
       
   711     TVolumeInfo vol;
       
   712     TInt err = iFs.Volume( vol, iRAMDrive );
       
   713     if ( err != KErrNone )
       
   714         {
       
   715         MPX_DEBUG2("CMPXHarvesterDatabaseManager::IsRamDiskSpaceAvailable Disk Not available to use. Error = %d", err);
       
   716         return EFalse;
       
   717         }
       
   718     MPX_DEBUG2("CMPXHarvesterDatabaseManager::IsRamDiskSpaceAvailable Free in bytes =%Lu", vol.iFree);
       
   719     if ( vol.iFree > KMPMinimumRAMSizeToRun )
       
   720         {
       
   721         MPX_DEBUG1("CMPXHarvesterDatabaseManager::IsRamDiskSpaceAvailable Ok to copy");
       
   722         return ETrue;
       
   723         }
       
   724         
       
   725     MPX_DEBUG1("CMPXHarvesterDatabaseManager::IsRamDiskSpaceAvailable NOT Ok to copy");
       
   726     return EFalse;
       
   727     }
       
   728 
       
   729 // ---------------------------------------------------------------------------
       
   730 // CMPXHarvesterDatabaseManager::BlockDiskSpaceL
       
   731 // ---------------------------------------------------------------------------
       
   732 //
       
   733 TInt CMPXHarvesterDatabaseManager::BlockDiskSpaceL( TDriveUnit aDrive, TInt aOrigDbSize )
       
   734     {
       
   735     MPX_FUNC("CMPXHarvesterDatabaseManager::BlockDiskSpaceL");
       
   736     
       
   737     // if current DB size can not fit in RAM, abort now
       
   738     TVolumeInfo vol;
       
   739     TInt err = KErrNone;
       
   740     err = iFs.Volume( vol, iRAMDrive );
       
   741     if ( err )
       
   742         {
       
   743         return err;
       
   744         }
       
   745     
       
   746     if ( vol.iFree < aOrigDbSize + KMPMinimumRAMSizeToRun )
       
   747         {
       
   748         MPX_DEBUG1("CMPXHarvesterDatabaseManager::BlockDiskSpaceL Not enough space on ram drive." );
       
   749         return KErrDiskFull;
       
   750         }
       
   751 
       
   752     MPX_DEBUG2("CMPXHarvesterDatabaseManager::BlockDiskSpaceL %d", (TInt)aDrive );
       
   753     err = iFs.Volume( vol, (TInt)aDrive );
       
   754     if ( err )
       
   755         {
       
   756         return err;
       
   757         }
       
   758     
       
   759     MPX_DEBUG2("CMPXHarvesterDatabaseManager::BlockDiskSpaceL Disk total free space in bytes =%Lu", vol.iFree);
       
   760     TInt64 blockingSize( CalculateInitalDummyDBSizeL( vol, aOrigDbSize ));
       
   761     MPX_DEBUG2("CMPXHarvesterDatabaseManager::BlockDiskSpaceL Disk blocking size =%Lu", blockingSize);
       
   762     // Pad blockingSize by 1Mb so we don't use all the free disk space.
       
   763     if ( vol.iFree > (blockingSize + 1*KMPMegaByte) )
       
   764         {
       
   765         TFileName dummyDbFileName = GenerateDummyDbName( aDrive ); 
       
   766         MPX_DEBUG2("CMPXHarvesterDatabaseManager::BlockDiskSpaceL  Dummy db file name=%S", &dummyDbFileName);
       
   767         
       
   768         RFile dummyDb;
       
   769         err = dummyDb.Replace( iFs, dummyDbFileName, EFileWrite );
       
   770         if (err != KErrNone) 
       
   771             {
       
   772             MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't open dummy file %d", err);
       
   773             return err;
       
   774             }
       
   775         
       
   776         err = dummyDb.SetSize( blockingSize );
       
   777         // Close the dummy RFile
       
   778         dummyDb.Close();
       
   779         if ( err )
       
   780             {
       
   781             MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't resize dummy file %d", err);
       
   782             return err;
       
   783             }
       
   784         }
       
   785     else
       
   786         {
       
   787         MPX_DEBUG1("CMPXHarvesterDatabaseManager::BlockDiskSpaceL - Not enough disk space for dummy file.");
       
   788         err = KErrDiskFull;
       
   789         }
       
   790     return err;
       
   791     }
       
   792 
       
   793 // ---------------------------------------------------------------------------
       
   794 // CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSizeL
       
   795 // ---------------------------------------------------------------------------
       
   796 //
       
   797 TInt64 CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSizeL( TVolumeInfo aVol, TInt aOrigDbSize )
       
   798     {
       
   799     MPX_FUNC("CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSizeL");
       
   800     TInt64 size;
       
   801     
       
   802     if ( iMtpMode )
       
   803         {
       
   804         MPX_DEBUG2("CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSize aVol.iFree=%Lu", aVol.iFree );
       
   805         TInt64 totalNumOfSongsCanFit = aVol.iFree / KMPEstimatedSongInBytes;
       
   806         MPX_DEBUG2("CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSize (MTP) totalNumOfSongsCanFit=%Lu", totalNumOfSongsCanFit );
       
   807         TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize;
       
   808         MPX_DEBUG2("CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSize (MTP) estimated DB size from calculation=%Lu", estimatedSize );
       
   809         if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy )
       
   810             {
       
   811             size = iMaximumAllowedRAMDiskSpaceToCopy;
       
   812             }
       
   813         else
       
   814             {
       
   815             size = estimatedSize;
       
   816             }
       
   817         }
       
   818     else
       
   819         {
       
   820         TInt64 totalNumOfSongsCanFit = aVol.iSize / KMPEstimatedSongInBytes;
       
   821         MPX_DEBUG2("CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSize totalNumOfSongsCanFit=%Lu", totalNumOfSongsCanFit );
       
   822         TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize;
       
   823         MPX_DEBUG2("CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSize estimated DB size from calculation=%Lu", estimatedSize );
       
   824         if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy )
       
   825             {
       
   826             size = iMaximumAllowedRAMDiskSpaceToCopy;
       
   827             }
       
   828         else
       
   829             {
       
   830             size = estimatedSize;
       
   831             }
       
   832         }
       
   833 
       
   834     MPX_DEBUG2("CMPXHarvesterDatabaseManager::CalculateInitalDummyDBSize Dummy DB size=%Lu", size );
       
   835     return size;
       
   836     }
       
   837 
       
   838 // ---------------------------------------------------------------------------
       
   839 // CMPXHarvesterDatabaseManager::RemoveDummyFile
       
   840 // ---------------------------------------------------------------------------
       
   841 //
       
   842 void CMPXHarvesterDatabaseManager::RemoveDummyFile( TInt aIndex )
       
   843     {
       
   844     MPX_FUNC("CMPXHarvesterDatabaseManager::RemoveDummyFile");
       
   845     
       
   846     TDriveUnit driveUnit(iDatabases[aIndex]->GetDbDrive());
       
   847     TFileName file = GenerateDummyDbName(driveUnit);
       
   848     
       
   849     if ( (file.Length() > 0) &&
       
   850          (BaflUtils::FileExists(iFs, file)) )
       
   851         {
       
   852         BaflUtils::DeleteFile(iFs, file);
       
   853         }
       
   854     }
       
   855 
       
   856 // ---------------------------------------------------------------------------
       
   857 // CMPXHarvesterDatabaseManager::UpdateDBsFromRamL
       
   858 // ---------------------------------------------------------------------------
       
   859 //
       
   860 /*void CMPXHarvesterDatabaseManager::UpdateDBsFromRamL( TInt aCount )
       
   861     {
       
   862     MPX_FUNC("CMPXHarvesterDatabaseManager::UpdateDBsFromRamL");
       
   863     TBool updateDb = EFalse;
       
   864     
       
   865     // In MTP mode, aCount is invalid, songs are added one at a time.
       
   866     if ( iMtpMode )
       
   867         {
       
   868         iMtpAddCount++;
       
   869         MPX_DEBUG2("CMPXHarvesterDatabaseManager::UpdateDBsFromRamL iMtpAddCount=%d", iMtpAddCount);
       
   870         if ( (iMtpAddCount - iUpdateCount) > KUpdateDBCount )
       
   871             {
       
   872             updateDb = ETrue;
       
   873             iUpdateCount = iMtpAddCount;
       
   874             }
       
   875         }
       
   876     else
       
   877         {
       
   878         MPX_DEBUG2("CMPXHarvesterDatabaseManager::UpdateDBsFromRamL aCount=%d", aCount);
       
   879         if ( (aCount - iUpdateCount) > KUpdateDBCount )
       
   880             {
       
   881             updateDb = ETrue;
       
   882             iUpdateCount = aCount;
       
   883             }
       
   884         }
       
   885     
       
   886     if ( updateDb )
       
   887         {
       
   888         CommitL();
       
   889         TInt count(iDatabases.Count());
       
   890         for (TInt i = 0; i < count; ++i)
       
   891             {
       
   892             TDriveUnit drive( iDatabases[i]->GetDbDrive() );
       
   893             // Check if database is not in RAM drive.
       
   894             if ( !iDatabases[i]->IsUseRamDrive() )
       
   895                 {
       
   896                 MPX_DEBUG2("CMPXHarvesterDatabaseManager::UpdateDBsFromRamL Drive %d is not in Ram Drive.", (TInt)drive);
       
   897                 continue;
       
   898                 }
       
   899 
       
   900             iDatabases[i]->Close();
       
   901             // update the database from ram drive.
       
   902             TRAP_IGNORE( DoUpdateDBFromRamL( drive ) );
       
   903             // 
       
   904             iDatabases[i]->OpenL();
       
   905             }
       
   906         BeginL();
       
   907         }
       
   908     }
       
   909 
       
   910 // ---------------------------------------------------------------------------
       
   911 // CMPXHarvesterDatabaseManager::DoUpdateDBFromRamL
       
   912 // ---------------------------------------------------------------------------
       
   913 //
       
   914 void CMPXHarvesterDatabaseManager::DoUpdateDBFromRamL(TDriveUnit aDriveUnit)
       
   915     {
       
   916     MPX_FUNC("CMPXHarvesterDatabaseManager::DoUpdateDBFromRamL");
       
   917     
       
   918     TFileName dst;
       
   919     TFileName src;
       
   920     
       
   921     dst = GenerateHarvesterDbName( aDriveUnit );
       
   922     src = GenerateHarvesterDbName( aDriveUnit, ETrue );
       
   923     MPX_DEBUG3("CMPXHarvesterDatabaseManager::DoUpdateDBFromRamL from %S to %S", &src, &dst );
       
   924 
       
   925     //Copy Db from RAM to real db file
       
   926     TInt err = BaflUtils::CopyFile(iFs, src, dst);
       
   927     MPX_DEBUG2("CMPXHarvesterDatabaseManager::DoUpdateDBFromRamL database copied from ram drive err=%d.", err);
       
   928     }*/
       
   929 
       
   930 // ---------------------------------------------------------------------------
       
   931 // CMPXHarvesterDatabaseManager::GenerateHarvesterDbName
       
   932 // ---------------------------------------------------------------------------
       
   933 //
       
   934 TFileName CMPXHarvesterDatabaseManager::GenerateHarvesterDbName( TDriveUnit aDriveUnit, TBool aForRamDrive )
       
   935     {
       
   936     MPX_FUNC("CMPXHarvesterDatabaseManager::GenerateHarvesterDbName");
       
   937     TFileName name;
       
   938     name.Zero();
       
   939     
       
   940     if ( aForRamDrive )
       
   941         {
       
   942         name.Append(iRAMFolder);
       
   943         name.Append(aDriveUnit.Name()[0]);
       
   944         name.Append(KHarvesterDBName);
       
   945         }
       
   946     else
       
   947         {
       
   948         name.Append(aDriveUnit.Name());
       
   949         name.Append(KHarvesterDBPath);
       
   950         name.Append(KHarvesterDBName);
       
   951         }
       
   952     
       
   953     MPX_DEBUG2("CMPXHarvesterDatabaseManager::GenerateHarvesterDbName name = %S", &name );
       
   954     return name;
       
   955     }
       
   956 
       
   957 // ---------------------------------------------------------------------------
       
   958 // CMPXHarvesterDatabaseManager::GenerateDummyDbName
       
   959 // ---------------------------------------------------------------------------
       
   960 //
       
   961 TFileName CMPXHarvesterDatabaseManager::GenerateDummyDbName( TDriveUnit aDriveUnit )
       
   962     {
       
   963     MPX_FUNC("CMPXHarvesterDatabaseManager::GenerateDummyDbName");
       
   964     TFileName name;
       
   965     name.Zero();
       
   966     name.Append(aDriveUnit.Name());
       
   967     name.Append(KHarvesterDBPath);
       
   968     name.Append(KHarvesterDummyDBName);
       
   969     MPX_DEBUG2("CMPXHarvesterDatabaseManager::GenerateDummyDbName name = %S", &name );
       
   970     return name;
       
   971     }
       
   972 
       
   973 // ---------------------------------------------------------------------------
       
   974 // CMPXHarvesterDatabaseManager::EnsureRamSpaceL
       
   975 // ---------------------------------------------------------------------------
       
   976 //
       
   977 void CMPXHarvesterDatabaseManager::EnsureRamSpaceL()
       
   978     {
       
   979     MPX_FUNC("CMPXHarvesterDatabaseManager::EnsureRamSpaceL()");
       
   980     
       
   981     // Ram disk disabled, no need to continue.
       
   982     if( !iRAMDiskPerfEnabled )
       
   983         {
       
   984         MPX_DEBUG1("CMPXHarvesterDatabaseManager::EnsureRamSpaceL RAM Disk Disabled.");
       
   985         return;
       
   986         }
       
   987 
       
   988     // Check if any DBs are in RAM drive.
       
   989     TInt count(iDatabases.Count());
       
   990     TBool ramDriveInUse = EFalse;
       
   991     for (TInt i = 0; i < count; ++i)
       
   992         {
       
   993         // Check if database is in RAM drive.
       
   994         if ( iDatabases[i]->IsUseRamDrive() )
       
   995             {
       
   996             ramDriveInUse = ETrue;
       
   997             break;
       
   998             }
       
   999         }
       
  1000     
       
  1001     // DBs not in ram drive, no need to continue.
       
  1002     if ( !ramDriveInUse )
       
  1003         {
       
  1004         MPX_DEBUG1("CMPXHarvesterDatabaseManager::EnsureRamSpaceL No DBs in RAM drive.");
       
  1005         return;
       
  1006         }
       
  1007         
       
  1008     // Check if low on Ram disk.
       
  1009     if ( IsRamDiskLow() )
       
  1010         {
       
  1011         // RAM not enough, copy back to normal drive and continue to harvest.
       
  1012         MPX_DEBUG1("CMPXHarvesterDatabaseManager::EnsureRamSpaceL RAM diskspace is full, copy dbs back.");
       
  1013         CopyDBsFromRamL();
       
  1014         }
       
  1015     else
       
  1016         {
       
  1017         TInt size=0;
       
  1018         TInt err = GetTotalDatabasesSize(size, ETrue);
       
  1019         if ( err || (size > iMaximumAllowedRAMDiskSpaceToCopy) )
       
  1020             {
       
  1021             // Databases using too much RAM space, copy back to normal drive and continue to harvest.
       
  1022             if ( err )
       
  1023                 {
       
  1024                 MPX_DEBUG2("CMPXHarvesterDatabaseManager::EnsureRamSpaceL Get DBs Size Err = %d, copy dbs back.", err);
       
  1025                 }
       
  1026             else
       
  1027                 {
       
  1028                 MPX_DEBUG2("CMPXHarvesterDatabaseManager::EnsureRamSpaceL DBs using too much RAM space size = %d, copy dbs back.", size);
       
  1029                 }
       
  1030             CopyDBsFromRamL();
       
  1031             }
       
  1032         }
       
  1033     }
       
  1034 
       
  1035 // ---------------------------------------------------------------------------
       
  1036 // CMPXHarvesterDatabaseManager::IsRamDiskLow
       
  1037 // ---------------------------------------------------------------------------
       
  1038 //
       
  1039 TBool CMPXHarvesterDatabaseManager::IsRamDiskLow()
       
  1040     {
       
  1041     MPX_FUNC("CMPXHarvesterDatabaseManager::IsRamDiskLow()");
       
  1042     TBool low = EFalse;
       
  1043     TVolumeInfo vol;
       
  1044     TInt errRAM = iFs.Volume( vol, iRAMDrive );
       
  1045     MPX_DEBUG2("CMPXHarvesterDatabaseManager::IsRamDiskLow Get vol err: %d", errRAM);
       
  1046     MPX_DEBUG2("CMPXHarvesterDatabaseManager::IsRamDiskLow Free RAM Disk Space: %Lu", vol.iFree);
       
  1047     if ( errRAM == KErrNone && vol.iFree < KMPMinimumRAMSizeToRun )
       
  1048         {
       
  1049         low = ETrue;
       
  1050         }
       
  1051     return low;
       
  1052     }
       
  1053 
       
  1054 // ---------------------------------------------------------------------------
       
  1055 // CMPXHarvesterDatabaseManager::GetTotalDatabasesSize
       
  1056 // ---------------------------------------------------------------------------
       
  1057 //
       
  1058 TInt CMPXHarvesterDatabaseManager::GetTotalDatabasesSize(TInt& aSize, TBool aRamDrive)
       
  1059     {
       
  1060     MPX_FUNC("CMPXHarvesterDatabaseManager::GetTotalDatabasesSize");
       
  1061     TInt err = KErrNone;
       
  1062     TInt size=0;
       
  1063     TInt count(iDatabases.Count());
       
  1064     for ( TInt i = 0; i < count ; ++i )
       
  1065         {
       
  1066         TDriveUnit drive( iDatabases[i]->GetDbDrive() );
       
  1067         // Ignore C drive
       
  1068         if ( (TInt)drive == EDriveC )
       
  1069             {
       
  1070             continue;
       
  1071             }
       
  1072         // Generate database name.
       
  1073         TFileName dbFilename = GenerateHarvesterDbName(drive, aRamDrive);
       
  1074         MPX_DEBUG2("CMPXHarvesterDatabaseManager::GetTotalDatabasesSize - Database name = %S", &dbFilename);
       
  1075         TEntry entry;
       
  1076         err = iFs.Entry( dbFilename, entry );
       
  1077         if (err == KErrNotFound || err == KErrNotReady)
       
  1078             {
       
  1079             MPX_DEBUG3("CMPXHarvesterDatabaseManager::GetTotalDatabasesSize - Ignored %S, err = %d", &dbFilename, err);
       
  1080             err = KErrNone;
       
  1081             continue;
       
  1082             }
       
  1083         if ( err != KErrNone )
       
  1084             {
       
  1085             break;
       
  1086             }
       
  1087         MPX_DEBUG3("CMPXHarvesterDatabaseManager::GetTotalDatabasesSize - Size of Db %S = %d", &dbFilename, entry.iSize);
       
  1088         // sum up size
       
  1089         size += entry.iSize;
       
  1090         }
       
  1091     aSize = size;
       
  1092     MPX_DEBUG3("CMPXHarvesterDatabaseManager::GetTotalDatabasesSize - Total Size of Dbs = %d, err = %d", size, err);
       
  1093     return err;
       
  1094     }
       
  1095 
       
  1096 #endif // __RAMDISK_PERF_ENABLE
       
  1097 
   418 // END OF FILE
  1098 // END OF FILE
   419 
  1099