imagehandlingutilities/thumbnailmanager/thumbnailserver/src/thumbnailstore.cpp
branchRCL_3
changeset 10 ee674526fac5
parent 9 dea39715fc05
child 11 85a88bc05e45
--- a/imagehandlingutilities/thumbnailmanager/thumbnailserver/src/thumbnailstore.cpp	Wed Mar 31 22:04:49 2010 +0300
+++ b/imagehandlingutilities/thumbnailmanager/thumbnailserver/src/thumbnailstore.cpp	Wed Apr 14 16:16:51 2010 +0300
@@ -35,7 +35,7 @@
 #include "thumbnailserver.h"
 
 
-_LIT8( KThumbnailSqlConfig, "page_size=16384; cache_size=32;" );
+_LIT8( KThumbnailSqlConfig, "page_size=1024; cache_size=32;" );
 
 const TInt KStreamBufferSize = 1024 * 8;
 const TInt KMajor = 3;
@@ -44,7 +44,7 @@
 const TInt KStoreUnrecoverableErr = KErrCorrupt;
 
 // Database path without drive letter
-_LIT( KThumbnailDatabaseName, ":[102830AB]thumbnail_v3.db" );
+_LIT( KThumbnailDatabaseName, ":[102830AB]thumbnail_v4.db" );
 
 _LIT( KDrv, ":");
 
@@ -167,10 +167,6 @@
         iDiskFullNotifier = NULL;
         }
 
-    if(!iServer->IsFormatting())
-        {
- 	    FlushCacheTable( ETrue );
-        }
     if( iAutoFlushTimer )
         {
         iAutoFlushTimer->Cancel();
@@ -178,6 +174,18 @@
         iAutoFlushTimer = NULL;
         }
     
+    if( iMaintenanceTimer )
+        {
+        iMaintenanceTimer->Cancel();
+        delete iMaintenanceTimer;
+        iMaintenanceTimer = NULL;
+        }
+    
+    if(!iServer->IsFormatting())
+        {
+ 	    FlushCacheTable( ETrue );
+        }
+    
     CloseStatements();   
     iDatabase.Close();
     
@@ -191,7 +199,7 @@
 //
 CThumbnailStore::CThumbnailStore( RFs& aFs, TInt aDrive, TDesC& aImei, CThumbnailServer* aServer ): 
     iFs( aFs ), iDrive( aDrive ), iDriveChar( 0 ), iBatchItemCount(0), iImei(aImei), 
-    iServer(aServer), iDiskFull(EFalse), iUnrecoverable(ETrue)
+    iServer(aServer), iDiskFull(EFalse), iUnrecoverable(ETrue), iBatchFlushItemCount(KMInBatchItems)
     {
     // no implementation required
     }
@@ -293,18 +301,16 @@
                 checkError = CheckRowIDs();
                 }
             }
-        else
+
+        // if db file not found, wrong version, corrupted database or other error opening db
+        if ( err != KErrNone || checkError == KErrNotSupported )
             {
-            // if db file not found, wrong version, corrupted database or other error opening db
-            if ( checkError == KErrNotSupported || err != KErrNone )
-                {
-                CleanupClosePushL(iDatabase);
-                RecreateDatabaseL(ETrue);
-                CleanupStack::Pop(&iDatabase);
-                
-                aNewDatabase = ETrue;
-                }
-            }
+            CleanupClosePushL(iDatabase);
+            RecreateDatabaseL(ETrue);
+            CleanupStack::Pop(&iDatabase);
+            
+            aNewDatabase = ETrue;
+            }          
         }    
    
     // opened existing database file
@@ -319,7 +325,7 @@
         if(imeiError != KErrNone)
             {
             //take ownership
-            imeiError = UpdateImeiL();
+            TRAP(imeiError, UpdateImeiL() );
             
             //Touch blacklisted items
             TRAP(blistError, PrepareBlacklistedItemsForRetryL() );
@@ -332,9 +338,7 @@
             TRAP(blistError2, PrepareBlacklistedItemsForRetryL() );
             }
         
-        if(imeiError == KSqlErrCorrupt || imeiError == KErrCorrupt ||
-           blistError == KSqlErrCorrupt || blistError == KErrCorrupt || 
-           blistError2 == KSqlErrCorrupt || blistError2 == KErrCorrupt )
+        if(imeiError != KErrNone || blistError != KErrNone || blistError2 != KErrNone )
             {
             CleanupClosePushL(iDatabase);
             RecreateDatabaseL(ETrue);
@@ -745,31 +749,30 @@
 // UpdateImeiL()
 // -----------------------------------------------------------------------------
 //
-TInt CThumbnailStore::UpdateImeiL()
+void CThumbnailStore::UpdateImeiL()
     {
     TN_DEBUG1( "CThumbnailStore::UpdateImeiL()" );
     RSqlStatement stmt;
     CleanupClosePushL( stmt );
             
-    TInt ret = stmt.Prepare( iDatabase, KThumbnailUpdateIMEI );
+    User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailUpdateIMEI ) );
     
     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamImei );
     User::LeaveIfError( paramIndex );
     User::LeaveIfError( stmt.BindText( paramIndex, iImei ));
     
-    TInt err =  stmt.Exec();
+    TInt err = stmt.Exec();
     
     if(err < 0)
         {
 #ifdef _DEBUG
-        TPtrC errorMsg2 = iDatabase.LastErrorMessage();
-        TN_DEBUG2( "RThumbnailTransaction::ResetThumbnailIDs() lastError %S, ret = %d" , &errorMsg2);
+        TPtrC errorMsg = iDatabase.LastErrorMessage();
+        TN_DEBUG2( "RThumbnailTransaction::UpdateImeiL() lastError %S" , &errorMsg);
 #endif
-        return ret;
+        User::Leave(err);
         }
     
     CleanupStack::PopAndDestroy( &stmt );
-    return KErrNone;
     }
 
 // ---------------------------------------------------------------------------
@@ -961,6 +964,22 @@
 #endif
     User::LeaveIfError( err );
     
+    err = iStmt_KThumbnailRename.Prepare( iDatabase, KThumbnailRename );
+#ifdef _DEBUG
+    msg.Append( iDatabase.LastErrorMessage() );
+    TN_DEBUG2( "CThumbnailStore::PrepareStatementsL() KThumbnailRename %S" , &msg );
+    msg.Zero();
+#endif
+    User::LeaveIfError( err );
+    
+    err = iStmt_KThumbnailTempRename.Prepare( iDatabase, KThumbnailTempRename );
+#ifdef _DEBUG
+    msg.Append( iDatabase.LastErrorMessage() );
+    TN_DEBUG2( "CThumbnailStore::PrepareStatementsL() KThumbnailTempRename %S" , &msg );
+    msg.Zero();
+#endif
+    User::LeaveIfError( err );
+    
     TN_DEBUG1("CThumbnailStore::PrepareStatementsL() end");
     }
 
@@ -1005,6 +1024,8 @@
     iStmt_KThumbnailSqlDeleteInfoByRowID.Close();
     iStmt_KThumbnailSqlDeleteInfoDataByRowID.Close();
     iStmt_KThumbnailSelectAllPaths.Close();
+    iStmt_KThumbnailRename.Close();
+    iStmt_KThumbnailTempRename.Close();
     
     TN_DEBUG1("CThumbnailStore::CloseStatements() end");
     }
@@ -1347,10 +1368,11 @@
         found = ETrue;
         }
     
+    CleanupStack::PopAndDestroy( stmt );
+    
     // check if duplicate in Deleted
     if (found)
         {
-        CleanupStack::PopAndDestroy( stmt );
         stmt = &iStmt_KThumbnailSqlFindDeleted;
         CleanupStack::PushL(TCleanupItem(ResetStatement, stmt));   
             
@@ -1360,6 +1382,8 @@
         
         rowStatus = stmt->Next();
         
+        CleanupStack::PopAndDestroy( stmt );
+        
         if(rowStatus == KSqlAtRow)
             {
             TN_DEBUG1( "CThumbnailStore::FindDuplicateL() - duplicate marked deleted" );
@@ -1372,8 +1396,6 @@
             }
         }
     
-    CleanupStack::PopAndDestroy( stmt );
-    
     return found;
     }
 
@@ -1695,10 +1717,10 @@
         rowStatus = stmt->Next();   
         }
     
-    CleanupStack::PopAndDestroy( stmt );
+    CleanupStack::PopAndDestroy( stmt_infodata );
     CleanupStack::PopAndDestroy( stmt_info );
-    CleanupStack::PopAndDestroy( stmt_infodata );
-    
+    CleanupStack::PopAndDestroy( stmt );
+        
     // if forcing instant delete
     if (aForce)
         {
@@ -1744,6 +1766,9 @@
         
         CleanupStack::PopAndDestroy( stmt_infodata );
         CleanupStack::PopAndDestroy( stmt_info );
+        
+		//remove delete mark
+        User::LeaveIfError( iDatabase.Exec( KThumbnailSqlDeleteFromDeleted ) );
         } 
     else
         {
@@ -1783,6 +1808,84 @@
     CleanupStack::PopAndDestroy( path );
     }
 
+// -----------------------------------------------------------------------------
+// Rename thumbnails
+// -----------------------------------------------------------------------------
+//
+void CThumbnailStore::RenameThumbnailsL( const TDesC& aCurrentPath, const TDesC& aNewPath )
+    {
+    TN_DEBUG2( "CThumbnailStore::RenameThumbnailsL(%S)", &aCurrentPath );
+    
+#ifdef _DEBUG
+    TTime aStart, aStop;
+    aStart.UniversalTime();
+#endif
+    
+    User::LeaveIfError( CheckDbState() );
+    
+    TInt paramIndex = 0;
+
+    HBufC* path = aCurrentPath.AllocLC();
+    TPtr ptr(path->Des());
+    StripDriveLetterL( ptr );
+    
+    HBufC* newPath = aNewPath.AllocLC();
+    TPtr ptr2(newPath->Des());
+    StripDriveLetterL( ptr2 );
+    
+    RThumbnailTransaction transaction( iDatabase );
+    CleanupClosePushL( transaction );    
+    transaction.BeginL();
+        
+    TN_DEBUG1( "CThumbnailStore::RenameThumbnailsL() -- TEMP TABLE" );
+    
+    RSqlStatement* stmt = NULL;
+    stmt = &iStmt_KThumbnailTempRename;
+    CleanupStack::PushL(TCleanupItem(ResetStatement, stmt));
+    
+    paramIndex = stmt->ParameterIndex( KThumbnailSqlParamPath );
+    User::LeaveIfError( paramIndex );
+    User::LeaveIfError( stmt->BindText( paramIndex, *path ));
+    
+    paramIndex = stmt->ParameterIndex( KThumbnailSqlParamNewPath );
+    User::LeaveIfError( paramIndex );
+    User::LeaveIfError( stmt->BindText( paramIndex, *newPath ));
+    
+    User::LeaveIfError( stmt->Exec() );
+    
+    TN_DEBUG1( "CThumbnailStore::RenameThumbnailsL() -- MAIN TABLE" );
+    
+    CleanupStack::PopAndDestroy( stmt );
+    stmt = &iStmt_KThumbnailRename;
+    CleanupStack::PushL(TCleanupItem(ResetStatement, stmt));
+    
+    paramIndex = stmt->ParameterIndex( KThumbnailSqlParamPath );
+    User::LeaveIfError( paramIndex );
+    User::LeaveIfError( stmt->BindText( paramIndex, *path ));
+    
+    paramIndex = stmt->ParameterIndex( KThumbnailSqlParamNewPath );
+    User::LeaveIfError( paramIndex );
+    User::LeaveIfError( stmt->BindText( paramIndex, *newPath ));
+    
+    User::LeaveIfError( stmt->Exec() );
+    
+    CleanupStack::PopAndDestroy( stmt );
+    
+    // if thumb was for some reason already marked deleted, clean from deleted
+    User::LeaveIfError( iDatabase.Exec( KThumbnailSqlDeleteFromDeleted ) );
+    
+    transaction.CommitL();
+    CleanupStack::PopAndDestroy( &transaction );
+    
+    CleanupStack::PopAndDestroy( newPath );
+    CleanupStack::PopAndDestroy( path );
+    
+#ifdef _DEBUG
+    aStop.UniversalTime();
+    TN_DEBUG2( "CThumbnailStore::RenameThumbnailsL() took %d ms", (TInt)aStop.MicroSecondsFrom(aStart).Int64()/1000);
+#endif      
+    }
+
 // ---------------------------------------------------------------------------
 // CThumbnailStore::PersistentSizes()
 // ---------------------------------------------------------------------------
@@ -1808,17 +1911,15 @@
         return;
         }
     
-    if(iBatchItemCount < KMaxBatchItems && !aForce)
+    if(iBatchItemCount < iBatchFlushItemCount && !aForce)
        {
        //some items in cache
        StartAutoFlush();
        return;
        }
     
-#ifdef _DEBUG
-    TTime aStart, aStop;
-    aStart.UniversalTime();
-#endif
+    
+    iStartFlush.UniversalTime();
     
     // Move data from temp table to main....
     TInt err_begin = iDatabase.Exec( KThumbnailBeginTransaction );
@@ -1872,15 +1973,26 @@
         TRAP_IGNORE(OpenDatabaseL(ETrue));
         }
     
+	//adjust batch size dynamically between min and max based on read flush speed. 
+    iStopFlush.UniversalTime();
+    TInt aFlushDelay = (TInt)iStopFlush.MicroSecondsFrom(iStartFlush).Int64()/1000;
+    TN_DEBUG2( "CThumbnailStore::FlushCacheTable() took %d ms", aFlushDelay);
+    
     //cache flushed
     iBatchItemCount = 0;
     
-#ifdef _DEBUG
-    aStop.UniversalTime();
-    TN_DEBUG2( "CThumbnailStore::FlushCacheTable() took %d ms", (TInt)aStop.MicroSecondsFrom(aStart).Int64()/1000);
-#endif
+    //increase batch count if there room for one more item (based on average time per item)
+    if( aFlushDelay < KMaxFlushDelay && iBatchFlushItemCount < KMaxBatchItems )
+        {
+        iBatchFlushItemCount++;
+        }
+    //decrease batch count if we exeeced max time allowed in flushing the TEMP table
+    else if(aFlushDelay > KMaxFlushDelay && iBatchFlushItemCount > KMInBatchItems )
+        {
+        iBatchFlushItemCount--;
+        }
 
-    TN_DEBUG1("CThumbnailStore::FlushCacheTable() out");
+    TN_DEBUG2("CThumbnailStore::FlushCacheTable() out iBatchFlushItemCount = %d", iBatchFlushItemCount);
     }
 
 // -----------------------------------------------------------------------------