mtpdataproviders/mtpimagedp/mediasyncserver/src/cmediasyncdatabase.cpp
changeset 51 64200268cac2
parent 50 965bb42340b2
child 52 866b4af7ffbe
equal deleted inserted replaced
50:965bb42340b2 51:64200268cac2
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <bautils.h>
       
    22 #include <mdesession.h>
       
    23 #include <mdequery.h>
       
    24 #include <mdeconstants.h>
       
    25 
       
    26 #include "cmediasyncserverdef.h"
       
    27 #include "cmediasyncdatabase.h"
       
    28 #include "cmediasyncdatawriter.h"
       
    29 
       
    30 __FLOG_STMT(_LIT8(KComponent,"MediaSyncDatabase");)
       
    31 
       
    32 const TInt KCompactThreshold = 50;
       
    33 const TInt KMaxRetryTimes = 3;
       
    34 const TInt KDelayPeriod = 3 * 1000000;
       
    35 
       
    36 CMediaSyncDatabase* CMediaSyncDatabase::NewL(RFs& aFs)
       
    37     {
       
    38     CMediaSyncDatabase* self = new (ELeave) CMediaSyncDatabase(aFs);
       
    39     CleanupStack::PushL(self);
       
    40     self->ConstructL();
       
    41     CleanupStack::Pop(self);
       
    42     return self;
       
    43     }
       
    44 
       
    45 void CMediaSyncDatabase::ConstructL()
       
    46     {
       
    47     __FLOG_OPEN(KMSSSubsystem, KComponent);
       
    48     __FLOG(_L8("CMediaSyncDatabase::ConstructL - Entry"));
       
    49 
       
    50     //Connect to the file server
       
    51     User::LeaveIfError(iFs.Connect());
       
    52 
       
    53     TFileName databasePath;
       
    54     iFs.PrivatePath(databasePath);
       
    55     TDriveUnit driveNum = RFs::GetSystemDrive();
       
    56     databasePath.Insert(0, driveNum.Name());
       
    57     databasePath.Append(KMssDbName);
       
    58 
       
    59     CreateTableL(databasePath);    
       
    60     
       
    61     User::LeaveIfError(iBatched.Open(iDatabase, KImageTableName, RDbRowSet::EUpdatable));
       
    62     
       
    63     __FLOG(_L8("CMediaSyncDatabase::ConstructL - Exit"));
       
    64     }
       
    65 
       
    66 CMediaSyncDatabase::CMediaSyncDatabase(RFs& aFs) :
       
    67     iFs(aFs),
       
    68     iDbCorrupt(EFalse),
       
    69     iSavePosition(EFalse)
       
    70     {
       
    71     }
       
    72 
       
    73 CMediaSyncDatabase::~CMediaSyncDatabase()
       
    74     {
       
    75     __FLOG(_L8("CMediaSyncDatabase::~CMediaSyncDatabase - Entry"));
       
    76     
       
    77     iBatched.Close();
       
    78     iDatabase.Close();
       
    79     
       
    80     __FLOG(_L8("CMediaSyncDatabase::~CMediaSyncDatabase - Exit"));
       
    81     __FLOG_CLOSE;
       
    82     }
       
    83 
       
    84 void CMediaSyncDatabase::CreateTableL(const TDesC& aDbFile)
       
    85     {
       
    86     __FLOG(_L8("CMediaSyncDatabase::CreateTableL - Entry"));
       
    87     
       
    88     _LIT(KSQLCreateTable, "CREATE TABLE ImageStore(ObjectId UNSIGNED INTEGER, NotificationType UNSIGNED INTEGER, URI VARCHAR(255))");           
       
    89     
       
    90     TInt err = KErrNone;
       
    91     if (!BaflUtils::FileExists(iFs, aDbFile))
       
    92         {
       
    93         __FLOG(_L8("CreateTableL - Table ImageStore does not exist"));
       
    94         
       
    95         BaflUtils::EnsurePathExistsL(iFs, aDbFile);
       
    96         
       
    97         User::LeaveIfError(iDatabase.Create(iFs, aDbFile));
       
    98         User::LeaveIfError(iDatabase.Execute(KSQLCreateTable));
       
    99         TRAP_IGNORE(CreateTabIndexL());
       
   100         }    
       
   101     else
       
   102         {
       
   103         //Open the database
       
   104         TBool recreateDbFile = EFalse;
       
   105         err = iDatabase.Open(iFs, aDbFile);
       
   106         if (err == KErrNone)
       
   107             {
       
   108             if (iDatabase.IsDamaged())
       
   109                 {
       
   110                 recreateDbFile = (iDatabase.Recover() == KErrNone) ? EFalse : ETrue;
       
   111                 }
       
   112             }
       
   113         else
       
   114             {
       
   115             recreateDbFile = ETrue;
       
   116             }
       
   117         
       
   118         if (recreateDbFile)
       
   119             {
       
   120             __FLOG_VA((_L8("CreateTableL - Open Table ImageStore failed: %d"), err));
       
   121             iDatabase.Close();
       
   122 
       
   123             TInt retryCount = KMaxRetryTimes;
       
   124             TInt result = KErrNone;            
       
   125             for (; retryCount > 0; retryCount--)
       
   126                 {
       
   127                 result = BaflUtils::DeleteFile(iFs, aDbFile);
       
   128                 if (result == KErrNone)
       
   129                     {
       
   130                     // We have succesfully delete corrupt database file
       
   131                     break;
       
   132                     }       
       
   133                 else
       
   134                     {
       
   135                     User::After(KDelayPeriod);
       
   136                     }
       
   137                 }
       
   138             
       
   139             User::LeaveIfError(result);
       
   140             User::LeaveIfError(iDatabase.Create(iFs, aDbFile));
       
   141             User::LeaveIfError(iDatabase.Execute(KSQLCreateTable));
       
   142             TRAP_IGNORE(CreateTabIndexL());
       
   143             iDbCorrupt = ETrue;          
       
   144             }
       
   145         }    
       
   146     
       
   147     __FLOG(_L8("CMediaSyncDatabase::CreateTableL - Exit"));
       
   148     }
       
   149 
       
   150 void CMediaSyncDatabase::CreateTabIndexL()
       
   151     {    
       
   152     __FLOG(_L8("CMediaSyncDatabase::CreateTabIndexL - Entry"));
       
   153     
       
   154     _LIT(KSQLCreateCombinedIndexText,"CREATE UNIQUE INDEX CombinedIndex on ImageStore (ObjectId, NotificationType)");      
       
   155     User::LeaveIfError(iDatabase.Execute(KSQLCreateCombinedIndexText));
       
   156     
       
   157     __FLOG(_L8("CMediaSyncDatabase::CreateTabIndexL - Exit"));
       
   158     }
       
   159 
       
   160 void CMediaSyncDatabase::SaveNotificationsL(const RArray<TItemId>& aObjectIdArray, TObserverNotificationType aType, CMdESession& aSession)
       
   161     {    
       
   162     iDatabase.Begin();  
       
   163 
       
   164     switch (aType)
       
   165         {
       
   166         case ENotifyAdd:
       
   167             __FLOG(_L8("CMediaSyncDatabase::SaveNotificationsL Addition - Entry"));
       
   168             SaveAddNotificationsL(aObjectIdArray, aSession);
       
   169             break;
       
   170             
       
   171         case ENotifyRemove:
       
   172             __FLOG(_L8("CMediaSyncDatabase::SaveNotificationsL Remove - Entry"));
       
   173             SaveWithoutUriL(aObjectIdArray, KMssRemoval);
       
   174             break;
       
   175             
       
   176         case ENotifyModify:
       
   177             __FLOG(_L8("CMediaSyncDatabase::SaveNotificationsL Modify - Entry"));
       
   178             SaveAndCheckWithUriL(aObjectIdArray, KMssChange, aSession);
       
   179             break;
       
   180             
       
   181         default:
       
   182             __FLOG_VA((_L8("SaveNotificationsL - Unknown argument: %d"), aType));
       
   183             User::Leave(KErrArgument);
       
   184             break;
       
   185         }
       
   186    
       
   187     iDatabase.Commit();
       
   188     
       
   189     __FLOG(_L8("CMediaSyncDatabase::SaveNotificationsL - Exit"));
       
   190     }
       
   191 
       
   192 inline TBool CMediaSyncDatabase::OptimizeL(TItemId aObjectId, TUint aType)
       
   193     {    
       
   194     return OptimizeL(aObjectId, aType, KNullDesC);
       
   195     }
       
   196 
       
   197 void CMediaSyncDatabase::Rollback()
       
   198     {
       
   199     __ASSERT_DEBUG(iDatabase.InTransaction(), User::Invariant());
       
   200     iDatabase.Rollback();
       
   201     }
       
   202 
       
   203 TBool CMediaSyncDatabase::OptimizeL(TItemId aObjectId, TUint aType, const TDesC& aUri)
       
   204     {
       
   205     __FLOG(_L8("CMediaSyncDatabase::OptimizeL - Entry"));
       
   206     
       
   207     TBool saveNotification = ETrue;
       
   208     
       
   209     switch (aType)
       
   210         {                
       
   211     case KMssChange:
       
   212         if ( UpdateUriColumnL(aObjectId, KMssAddition, aUri) ||
       
   213              UpdateUriColumnL(aObjectId, KMssChange, aUri) )
       
   214             {
       
   215             saveNotification = EFalse;// ignore this update notification
       
   216             }
       
   217         __FLOG_VA((_L8("OptimizeL - KMssChange ObjectId: %u, Ignore saving: %d"), aObjectId, saveNotification));
       
   218         break;
       
   219                 
       
   220     case KMssPresent:
       
   221         if (RemoveNotificationL(aObjectId, KMssNotPresent))
       
   222             {
       
   223             saveNotification = EFalse;// ignore this present notification
       
   224             }
       
   225         __FLOG_VA((_L8("OptimizeL - KMssPresent ObjectId: %u, Ignore saving: %d"), aObjectId, saveNotification));
       
   226         break;        
       
   227         
       
   228     case KMssRemoval:
       
   229         if (RemoveNotificationL(aObjectId, KMssAddition))
       
   230             {
       
   231             saveNotification = EFalse;// ignore this removal notification
       
   232             }        
       
   233         else
       
   234             {
       
   235             RemoveNotificationL(aObjectId, KMssChange);
       
   236             }
       
   237         __FLOG_VA((_L8("OptimizeL - KMssRemoval ObjectId: %u, Ignore saving: %d"), aObjectId, saveNotification));
       
   238         break;
       
   239         
       
   240     case KMssNotPresent:
       
   241         if (RemoveNotificationL(aObjectId, KMssPresent))
       
   242             {
       
   243             saveNotification = EFalse;// ignore this not present notification
       
   244             }
       
   245         __FLOG_VA((_L8("OptimizeL - KMssNotPresent ObjectId: %u, Ignore saving: %d"), aObjectId, saveNotification));
       
   246         break;
       
   247         
       
   248     default:
       
   249         // Nothing to do
       
   250         break;
       
   251         }
       
   252     
       
   253     __FLOG(_L8("CMediaSyncDatabase::OptimizeL - Exit"));
       
   254     
       
   255     return saveNotification;
       
   256     }
       
   257 
       
   258 void CMediaSyncDatabase::SaveNotificationsL(const RArray<TItemId>& aObjectIdArray, TBool aPresent, CMdESession& aSession)
       
   259     {        
       
   260     iDatabase.Begin();
       
   261    
       
   262     if (aPresent)
       
   263         {
       
   264         __FLOG(_L8("CMediaSyncDatabase::SaveNotificationsL Present - Entry"));
       
   265         SaveAndCheckWithUriL(aObjectIdArray, KMssPresent, aSession);
       
   266         }
       
   267     else
       
   268         {
       
   269         __FLOG(_L8("CMediaSyncDatabase::SaveNotificationsL Not Present - Entry"));
       
   270         SaveWithoutUriL(aObjectIdArray, KMssNotPresent);
       
   271         }      
       
   272     
       
   273     iDatabase.Commit();  
       
   274     
       
   275     __FLOG(_L8("CMediaSyncDatabase::SaveNotificationsL Present - Exit"));
       
   276     }
       
   277 
       
   278 void CMediaSyncDatabase::SaveAddNotificationsL(const RArray<TItemId>& aObjectIdArray, CMdESession& aSession)
       
   279     {
       
   280     __FLOG(_L8("CMediaSyncDatabase::SaveAddNotificationsL - Entry"));
       
   281     
       
   282     CMdENamespaceDef& defaultNamespaceDef = aSession.GetDefaultNamespaceDefL();
       
   283     CMdEObjectDef& imageObjDef = defaultNamespaceDef.GetObjectDefL(MdeConstants::Image::KImageObject); 
       
   284 
       
   285     TInt objectCount = aObjectIdArray.Count();   
       
   286     for (TInt i(0);i < objectCount;i++)
       
   287         {       
       
   288         TItemId objectId = aObjectIdArray[i];
       
   289         CMdEObject* addObject = aSession.GetObjectL(objectId, imageObjDef);
       
   290         if (addObject)
       
   291             {
       
   292             CleanupStack::PushL(addObject);
       
   293             CleanupStack::PushL(TCleanupItem(CMediaSyncDatabase::RollbackTable, &iBatched));
       
   294             iBatched.InsertL();
       
   295             iBatched.SetColL(1, (TUint32)objectId);
       
   296             iBatched.SetColL(2, KMssAddition);
       
   297             iBatched.SetColL(3, addObject->Uri());
       
   298             iBatched.PutL();
       
   299             CleanupStack::Pop(&iBatched);            
       
   300             __FLOG_VA((_L16("CMediaSyncDatabase::SaveAndCheckWithUriL - ObjectId:%u, Type:%u, URI:%S"), objectId, KMssAddition, &addObject->Uri()));
       
   301             CleanupStack::PopAndDestroy(addObject); 
       
   302             }                                 
       
   303         }      
       
   304     
       
   305     __FLOG(_L8("CMediaSyncDatabase::SaveAddNotificationsL - Exit"));
       
   306     }
       
   307 
       
   308 void CMediaSyncDatabase::SaveAndCheckWithUriL(const RArray<TItemId>& aObjectIdArray, TUint aType, CMdESession& aSession)
       
   309     {
       
   310     __FLOG(_L8("CMediaSyncDatabase::SaveAndCheckWithUriL - Entry"));
       
   311     
       
   312     CMdENamespaceDef& defaultNamespaceDef = aSession.GetDefaultNamespaceDefL();
       
   313     CMdEObjectDef& imageObjDef = defaultNamespaceDef.GetObjectDefL(MdeConstants::Image::KImageObject); 
       
   314     CMdEPropertyDef& itemTypePropDef = imageObjDef.GetPropertyDefL(MdeConstants::Object::KItemTypeProperty);    
       
   315 
       
   316     TInt objectCount = aObjectIdArray.Count();   
       
   317     for (TInt i(0);i < objectCount;i++)
       
   318         {       
       
   319         TItemId objectId = aObjectIdArray[i];          
       
   320         CMdEObject* changeObject = aSession.GetObjectL(objectId, imageObjDef);
       
   321         if (changeObject)
       
   322             {
       
   323             CleanupStack::PushL(changeObject);            
       
   324             //only support jpeg format image files             
       
   325             CMdEProperty* itemType = NULL;
       
   326             TInt err = changeObject->Property(itemTypePropDef, itemType);
       
   327             
       
   328             if (err >= KErrNone && itemType != NULL && itemType->TextValueL().Compare(KJpegMime) == 0)
       
   329                 {                        
       
   330                 if (OptimizeL(objectId, aType, changeObject->Uri()))
       
   331                     {                    
       
   332                     CleanupStack::PushL(TCleanupItem(CMediaSyncDatabase::RollbackTable, &iBatched));
       
   333                     iBatched.InsertL();
       
   334                     iBatched.SetColL(1, (TUint32)objectId);
       
   335                     iBatched.SetColL(2, aType);                    
       
   336                     iBatched.SetColL(3, changeObject->Uri());
       
   337                     iBatched.PutL();                    
       
   338                     CleanupStack::Pop(&iBatched);
       
   339                     __FLOG_VA((_L16("CMediaSyncDatabase::SaveAndCheckWithUriL - ObjectId:%u, Type:%u, URI:%S"), objectId, aType, &changeObject->Uri()));
       
   340                     }
       
   341                 }
       
   342             CleanupStack::PopAndDestroy(changeObject);            
       
   343             }
       
   344         }  
       
   345     
       
   346     __FLOG(_L8("CMediaSyncDatabase::SaveAndCheckWithUriL - Exit"));
       
   347     }
       
   348 
       
   349 void CMediaSyncDatabase::SaveWithoutUriL(const RArray<TItemId>& aObjectIdArray, TUint aType)
       
   350     {
       
   351     TInt objectCount = aObjectIdArray.Count();   
       
   352     for (TInt i(0);i < objectCount;i++)
       
   353         {       
       
   354         TItemId objectId = aObjectIdArray[i];        
       
   355         if (OptimizeL(objectId, aType))
       
   356             {
       
   357             CleanupStack::PushL(TCleanupItem(CMediaSyncDatabase::RollbackTable, &iBatched));
       
   358             iBatched.InsertL();
       
   359             iBatched.SetColL(1, (TUint32)objectId);
       
   360             iBatched.SetColL(2, aType);
       
   361             iBatched.PutL();
       
   362             __FLOG_VA((_L8("CMediaSyncDatabase::SaveWithoutUriL - ObjectId:%u, Type: %u"), objectId, aType));
       
   363             CleanupStack::Pop(&iBatched);
       
   364             }
       
   365         }
       
   366     
       
   367     __FLOG(_L8("CMediaSyncDatabase::SaveWithoutUriL - Exit"));
       
   368     }
       
   369 
       
   370 TBool CMediaSyncDatabase::UpdateUriColumnL(TItemId aObjectId, TUint aType, const TDesC& aUri)
       
   371     {
       
   372     __FLOG(_L8("CMediaSyncDatabase::UpdateUriColumnL - Entry"));
       
   373     
       
   374     TBool update = EFalse;
       
   375     
       
   376     iBatched.SetIndex(KSQLCombinedIndex);
       
   377     TDbSeekMultiKey<2> seekKey;
       
   378     seekKey.Add((TUint)aObjectId);
       
   379     seekKey.Add(aType);
       
   380     if (iBatched.SeekL(seekKey))
       
   381         {
       
   382         CleanupStack::PushL(TCleanupItem(CMediaSyncDatabase::RollbackTable, &iBatched));
       
   383         iBatched.UpdateL();                 
       
   384         iBatched.SetColL(3, aUri);
       
   385         iBatched.PutL();        
       
   386         CleanupStack::Pop(&iBatched);
       
   387         update = ETrue;
       
   388         __FLOG_VA((_L16("CMediaSyncDatabase::UpdateUriColumnL - ObjectId:%u, Type:%u, URI:%S"), aObjectId, aType, &aUri));
       
   389         }    
       
   390     
       
   391     __FLOG(_L8("CMediaSyncDatabase::UpdateUriColumnL - Exit"));
       
   392     return update;
       
   393     }
       
   394 
       
   395 void CMediaSyncDatabase::RemoveAllNotificationsL()
       
   396     {        
       
   397     _LIT(KSQLDeleteAllNotifications, "DELETE FROM ImageStore");
       
   398   
       
   399     User::LeaveIfError(iDatabase.Execute(KSQLDeleteAllNotifications));    
       
   400     iDatabase.Compact();    
       
   401     iSavePosition = EFalse;    
       
   402     
       
   403     __FLOG_VA((_L8("CMediaSyncDatabase::RemoveAllNotificationsL")));
       
   404     }
       
   405 
       
   406 TBool CMediaSyncDatabase::RemoveNotificationL(TItemId aObjectId, TUint aType)
       
   407     {
       
   408     TBool remove = EFalse;
       
   409     
       
   410     iBatched.SetIndex(KSQLCombinedIndex);
       
   411     TDbSeekMultiKey<2> seekKey;
       
   412     seekKey.Add((TUint)aObjectId);
       
   413     seekKey.Add(aType);
       
   414     if (iBatched.SeekL(seekKey))
       
   415         {
       
   416         iBatched.DeleteL();
       
   417         CompactDatabase();
       
   418         iSavePosition = EFalse;
       
   419         remove = ETrue;
       
   420         __FLOG_VA((_L8("CMediaSyncDatabase::RemoveNotificationL - ObjectId:%u, Type: %u"), aObjectId, aType));
       
   421         }    
       
   422     return remove;
       
   423     }
       
   424 
       
   425 void CMediaSyncDatabase::CompactDatabase()
       
   426     {
       
   427     if (++iCompactCounter > KCompactThreshold)
       
   428         {
       
   429         iDatabase.Compact();
       
   430         iCompactCounter = 0;
       
   431         }    
       
   432     }
       
   433 
       
   434 void CMediaSyncDatabase::FetchNotificationsL(CMediaSyncDataWriter& aResulWriter, TInt aMaxtFetchCount, TBool& aIsFinished)
       
   435     {
       
   436     __FLOG(_L8("CMediaSyncDatabase::FetchNotificationsL - Entry"));
       
   437     
       
   438     _LIT(KSQLQuery, "SELECT ObjectId, NotificationType, URI FROM ImageStore");
       
   439     
       
   440     RDbView view;
       
   441     CleanupClosePushL(view);
       
   442     
       
   443     User::LeaveIfError(view.Prepare(iDatabase, TDbQuery(KSQLQuery)));
       
   444     User::LeaveIfError(view.EvaluateAll());
       
   445     
       
   446     //goto the last fetch position
       
   447     if (iSavePosition)
       
   448         {
       
   449         view.GotoL(iBookmark);
       
   450         }
       
   451     else        
       
   452         {
       
   453         view.FirstL();   
       
   454         }
       
   455     
       
   456     TInt entrySize = 0;
       
   457     //tranvers records
       
   458     while (view.AtRow() && (aMaxtFetchCount > 0))
       
   459         {
       
   460         view.GetL();        
       
   461         TPtrC16 uri = view.ColDes16(3);
       
   462         
       
   463         entrySize = uri.Size();
       
   464         entrySize += sizeof(TUint32);//object id size
       
   465         entrySize += sizeof(TUint8);//type size 
       
   466         entrySize += sizeof(TUint8);//uri size
       
   467         
       
   468         if (entrySize > aResulWriter.FreeSpaceBytes())
       
   469             {
       
   470             //there is no enought space to save entry
       
   471             break;
       
   472             }
       
   473         else
       
   474             {
       
   475             aResulWriter.AppendEntryL(view.ColUint32(1), (TUint8)view.ColUint32(2), uri);
       
   476             view.NextL();
       
   477             --aMaxtFetchCount;
       
   478             }                                       
       
   479         }
       
   480     
       
   481     //save current fetch position
       
   482     if (view.AtEnd())
       
   483         {
       
   484         iSavePosition = EFalse;
       
   485         aIsFinished = ETrue;
       
   486         }
       
   487     else
       
   488         {
       
   489         iBookmark = view.Bookmark();
       
   490         iSavePosition = ETrue;
       
   491         aIsFinished = EFalse;
       
   492         }
       
   493     CleanupStack::PopAndDestroy(&view);
       
   494     
       
   495     __FLOG(_L8("CMediaSyncDatabase::FetchNotificationsL - Exit"));
       
   496     }
       
   497 
       
   498 void CMediaSyncDatabase::RollbackTable(TAny* aTable)
       
   499     {
       
   500     reinterpret_cast<RDbTable*> (aTable)->Cancel();
       
   501     }