mmappcomponents/harvester/filehandler/src/mpxharvesterdb.cpp
changeset 25 d881023c13eb
parent 0 a2952bb97e68
child 23 4740b34b83ce
equal deleted inserted replaced
21:a05c44bc3c61 25:d881023c13eb
    29 // ---------------------------------------------------------------------------
    29 // ---------------------------------------------------------------------------
    30 // C++ Constructor
    30 // C++ Constructor
    31 // ---------------------------------------------------------------------------
    31 // ---------------------------------------------------------------------------
    32 //
    32 //
    33 CMPXHarvesterDB::CMPXHarvesterDB( TDriveNumber aDrive, RFs& aFs  ) :
    33 CMPXHarvesterDB::CMPXHarvesterDB( TDriveNumber aDrive, RFs& aFs  ) :
    34                                                              iDrive( aDrive),
    34                                   iDrive( aDrive),
    35                                                              iFs( aFs )
    35                                   iFs( aFs )
       
    36 #ifdef __RAMDISK_PERF_ENABLE
       
    37                                   ,iRamDrive(aDrive),
       
    38                                   iUseRamDrive(EFalse)
       
    39 #endif // __RAMDISK_PERF_ENABLE
    36     {
    40     {
    37     }
    41     }
    38 
    42 
    39 
    43 
    40 // ---------------------------------------------------------------------------
    44 // ---------------------------------------------------------------------------
    73 // Open a database
    77 // Open a database
    74 // ---------------------------------------------------------------------------
    78 // ---------------------------------------------------------------------------
    75 //
    79 //
    76 TInt CMPXHarvesterDB::OpenL()
    80 TInt CMPXHarvesterDB::OpenL()
    77     {
    81     {
    78     MPX_DEBUG1("CMPXHarvesterDB::OpenL <---");
    82     MPX_FUNC("CMPXHarvesterDB::OpenL");
    79 
    83 
    80     // There is no need to re-open if it was already open
    84     // There is no need to re-open if it was already open
    81     if( iDBOpen )
    85     if( iDBOpen )
    82         {
    86         {
    83         return KErrNone;
    87         return KErrNone;
    84         }
    88         }
    85 
    89 
    86     TInt rtn( KErrNone );
    90     TInt rtn( KErrNone );
    87     TDriveUnit drive( iDrive );
    91     TFileName fileName = GenerateDbName();
    88     TFileName fileName;
    92     TParsePtr fileParser( fileName );
    89     fileName.Append( drive.Name() );
    93     TFileName filePath = fileParser.DriveAndPath();
    90     fileName.Append( KHarvesterDBPath );
    94     // Make sure Path exists; allow to leave if can't create the path
    91 
    95     BaflUtils::EnsurePathExistsL(iFs, filePath);
    92     // Make sure Path exists
       
    93     if (!BaflUtils::PathExists(iFs, fileName))
       
    94         {
       
    95         iFs.MkDirAll(fileName);
       
    96         }
       
    97 
       
    98     fileName.Append( KHarvesterDBName );
       
    99 
    96 
   100     // Try to open the stream
    97     // Try to open the stream
   101     TRAPD( err,
    98     TRAPD( err,
   102         iStore = CPermanentFileStore::OpenL(iFs, fileName ,EFileRead|EFileWrite);
    99         iStore = CPermanentFileStore::OpenL(iFs, fileName ,EFileRead|EFileWrite);
   103         );
   100         );
   105     // DB doesn't exist or error, replace it and recreate DB
   102     // DB doesn't exist or error, replace it and recreate DB
   106     //
   103     //
   107     if( err )
   104     if( err )
   108         {
   105         {
   109         MPX_DEBUG2("CMPXHarvesterDB::OpenL -- New database %i", err);
   106         MPX_DEBUG2("CMPXHarvesterDB::OpenL -- New database %i", err);
   110 
   107         iStore = CPermanentFileStore::ReplaceL(iFs, fileName ,EFileRead|EFileWrite);
   111         TRAPD( openErr,
   108         iStore->SetTypeL(iStore->Layout());
   112             iStore = CPermanentFileStore::ReplaceL(iFs, fileName ,EFileRead|EFileWrite);
   109         CreateDBL();
   113         	iStore->SetTypeL(iStore->Layout());
       
   114             CreateDBL();
       
   115             iDBOpen = ETrue;
       
   116             );
       
   117 
       
   118         if( KErrNone != openErr )
       
   119             {
       
   120             iDBOpen = EFalse;
       
   121             User::Leave( openErr );
       
   122             }
       
   123 
       
   124         // If the open stream error was not found, that is fine
   110         // If the open stream error was not found, that is fine
   125         // because it is a new db, other errors means the stream
   111         // because it is a new db, other errors means the stream
   126         // is corrupted
   112         // is corrupted
   127         //
   113         //
   128         rtn = err!=KErrNotFound ? KErrCorrupt : KErrNone;
   114         rtn = err!=KErrNotFound ? KErrCorrupt : KErrNone;
   129         }
   115         }
   130     else
   116     else
   131         {
   117         {
   132         MPX_DEBUG1("CMPXHarvesterDB::OpenL -- Opening database" );
   118         MPX_DEBUG1("CMPXHarvesterDB::OpenL -- Opening database" );
   133         rtn = OpenDBL();
   119         rtn = OpenDBL();
   134         iDBOpen = ETrue;
   120         }
   135         }
   121     iDBOpen = ETrue;
   136 
       
   137 
   122 
   138     // Check volume Id
   123     // Check volume Id
   139     //
   124     //
   140     TVolumeInfo volInfo;
   125     TVolumeInfo volInfo;
   141     iFs.Volume(volInfo, iDrive);
   126     iFs.Volume(volInfo, iDrive);
   158         Close();
   143         Close();
   159         User::LeaveIfError(DeleteDatabase());
   144         User::LeaveIfError(DeleteDatabase());
   160         rtn = OpenL();
   145         rtn = OpenL();
   161         }
   146         }
   162 
   147 
   163     MPX_DEBUG1("CMPXHarvesterDB::OpenL --->");
       
   164     return rtn;
   148     return rtn;
   165     }
   149     }
   166 
   150 
   167 // ---------------------------------------------------------------------------
   151 // ---------------------------------------------------------------------------
   168 // Close a database
   152 // Close a database
   194 void CMPXHarvesterDB::CreateDBL()
   178 void CMPXHarvesterDB::CreateDBL()
   195     {
   179     {
   196     MPX_DEBUG1("CMPXHarvesterDB::CreateDBL <---");
   180     MPX_DEBUG1("CMPXHarvesterDB::CreateDBL <---");
   197 
   181 
   198     // remove old databases before creating/replacing new database
   182     // remove old databases before creating/replacing new database
   199 
   183     TFileName fileName = GenerateDbName();
   200     TFileName dbFileName;
   184     TParsePtr fileParser( fileName );
   201     TDriveUnit drive( iDrive );
   185     TFileName filePath = fileParser.DriveAndPath();
   202     dbFileName.Append( drive.Name() );
   186 #ifdef __RAMDISK_PERF_ENABLE
   203     dbFileName.Append( KHarvesterDBPath );
   187     if ( iUseRamDrive )
   204     dbFileName.Append( KHarvesterDBPattern );
   188         {
       
   189         TDriveUnit drive( iDrive );
       
   190         filePath.Append(drive.Name()[0]);
       
   191         filePath.Append(KHarvesterDBPattern);
       
   192         }
       
   193     else
       
   194         {
       
   195 #endif // __RAMDISK_PERF_ENABLE
       
   196         filePath.Append( KHarvesterDBPattern );
       
   197 #ifdef __RAMDISK_PERF_ENABLE
       
   198         }
       
   199 #endif // __RAMDISK_PERF_ENABLE
   205     
   200     
   206     CFileMan* fileManager = CFileMan::NewL(iFs);
   201     CFileMan* fileManager = CFileMan::NewL(iFs);
   207     TInt ret = fileManager->Delete(dbFileName);	
   202     TInt ret = fileManager->Delete(filePath); 
   208     delete fileManager;
   203     delete fileManager;
   209     fileManager = NULL;
   204     fileManager = NULL;
   210 
   205 
   211 
   206 
   212     //Create the database
   207     //Create the database
   472 // Deletes the database file, the user should call Close() before hand
   467 // Deletes the database file, the user should call Close() before hand
   473 // ---------------------------------------------------------------------------
   468 // ---------------------------------------------------------------------------
   474 //
   469 //
   475 TInt CMPXHarvesterDB::DeleteDatabase()
   470 TInt CMPXHarvesterDB::DeleteDatabase()
   476     {
   471     {
   477     TDriveUnit drive( iDrive );
   472     TFileName fileName = GenerateDbName();
   478     TFileName fileName;
       
   479     fileName.Append( drive.Name() );
       
   480     fileName.Append( KHarvesterDBPath );
       
   481     fileName.Append( KHarvesterDBName );
       
   482 
       
   483     return iFs.Delete( fileName );
   473     return iFs.Delete( fileName );
   484     }
   474     }
   485 
   475 
   486 // ---------------------------------------------------------------------------
   476 // ---------------------------------------------------------------------------
   487 // Read the unique id of the database file
   477 // Read the unique id of the database file
   605 void CMPXHarvesterDB::Rollback()
   595 void CMPXHarvesterDB::Rollback()
   606     {
   596     {
   607     iDatabase->Rollback();
   597     iDatabase->Rollback();
   608     }
   598     }
   609 
   599 
       
   600 #ifdef __RAMDISK_PERF_ENABLE
       
   601 // ---------------------------------------------------------------------------
       
   602 // Set RAM drive info
       
   603 // ---------------------------------------------------------------------------
       
   604 //
       
   605 void CMPXHarvesterDB::SetRamDriveInfo(TDriveNumber aDrive, TBool aUseRamDrive)
       
   606     {
       
   607     MPX_FUNC("CMPXHarvesterDB::SetRamDriveInfo");
       
   608     iRamDrive = aDrive;
       
   609     iUseRamDrive = aUseRamDrive;
       
   610     }
       
   611 
       
   612 // ---------------------------------------------------------------------------
       
   613 // Get UseRamDrive
       
   614 // ---------------------------------------------------------------------------
       
   615 //
       
   616 TBool CMPXHarvesterDB::IsUseRamDrive()
       
   617     {
       
   618     MPX_FUNC("CMPXHarvesterDB::IsUseRamDrive");
       
   619     return iUseRamDrive;
       
   620     }
       
   621 
       
   622 // ---------------------------------------------------------------------------
       
   623 // Get the state of the database.
       
   624 // ---------------------------------------------------------------------------
       
   625 //
       
   626 TDbState CMPXHarvesterDB::GetDbState()
       
   627     {
       
   628     MPX_FUNC("CMPXHarvesterDB::GetDbState");
       
   629     TDbState state = EDbClose;
       
   630     if ( InTransaction() )
       
   631         {
       
   632         state = EDbInTransaction;
       
   633         }
       
   634     else if ( iDBOpen )
       
   635         {
       
   636         state = EDbOpen;
       
   637         }
       
   638     
       
   639     MPX_DEBUG2("CMPXHarvesterDB::GetDbState state = %d", state );
       
   640     return state;
       
   641     }
       
   642 
       
   643 // ---------------------------------------------------------------------------
       
   644 // Set the state of the database.
       
   645 // ---------------------------------------------------------------------------
       
   646 //
       
   647 void CMPXHarvesterDB::SetDbStateL( TDbState aState )
       
   648     {
       
   649     MPX_FUNC("CMPXHarvesterDB::SetDbState");
       
   650     MPX_DEBUG2("CMPXHarvesterDB::SetDbState state = %d", aState );
       
   651     
       
   652     switch( aState )
       
   653         {
       
   654         case EDbClose:
       
   655             {
       
   656             if ( InTransaction() )
       
   657                 {
       
   658                 CommitL();
       
   659                 }
       
   660             Close();
       
   661             break;
       
   662             }
       
   663         case EDbOpen:
       
   664             {
       
   665             if ( InTransaction() )
       
   666                 {
       
   667                 CommitL();
       
   668                 }
       
   669             OpenL();
       
   670             break;
       
   671             }
       
   672         case EDbInTransaction:
       
   673             {
       
   674             if ( !InTransaction() )
       
   675                 {
       
   676                 OpenL();
       
   677                 BeginL();
       
   678                 }
       
   679             break;
       
   680             }
       
   681         default:
       
   682             {
       
   683             // should never get here
       
   684             User::Leave(KErrNotSupported);
       
   685             }
       
   686         }
       
   687     }
       
   688 
       
   689 #endif // __RAMDISK_PERF_ENABLE
       
   690 
       
   691 // ---------------------------------------------------------------------------
       
   692 // Generate the database name
       
   693 // ---------------------------------------------------------------------------
       
   694 //
       
   695 TFileName CMPXHarvesterDB::GenerateDbName()
       
   696     {
       
   697     MPX_FUNC("CMPXHarvesterDB::GenerateDbName");
       
   698     TFileName fileName;
       
   699 #ifdef __RAMDISK_PERF_ENABLE
       
   700     if ( iUseRamDrive )
       
   701         {
       
   702         TDriveUnit ramDrive( iRamDrive );
       
   703         fileName.Append( ramDrive.Name() );
       
   704         fileName.Append( KHarvesterDBPath );
       
   705         TDriveUnit drive( iDrive );
       
   706         fileName.Append(drive.Name()[0]);
       
   707         fileName.Append(KHarvesterDBName);
       
   708         }
       
   709     else
       
   710         {
       
   711 #endif // __RAMDISK_PERF_ENABLE
       
   712         TDriveUnit drive( iDrive );
       
   713         fileName.Append( drive.Name() );
       
   714         fileName.Append( KHarvesterDBPath );
       
   715         fileName.Append( KHarvesterDBName );
       
   716 #ifdef __RAMDISK_PERF_ENABLE
       
   717         }
       
   718 #endif // __RAMDISK_PERF_ENABLE
       
   719     MPX_DEBUG2("CMPXHarvesterDB::GenerateDbName file name = %S", &fileName );
       
   720     return fileName;
       
   721     }
       
   722 
   610 // End of file
   723 // End of file