diff -r 6e82ae192c3a -r 3ae5cb0b4c02 mtpdataproviders/mtpimagedp/src/cmtpimagedpobjectpropertymgr.cpp --- a/mtpdataproviders/mtpimagedp/src/cmtpimagedpobjectpropertymgr.cpp Mon May 03 13:19:02 2010 +0300 +++ b/mtpdataproviders/mtpimagedp/src/cmtpimagedpobjectpropertymgr.cpp Fri Jun 25 16:54:01 2010 +0800 @@ -51,6 +51,9 @@ // Class constants. __FLOG_STMT(_LIT8(KComponent,"MTPImageDpPropertyMgr");) +// Indicate how many cache can be stored +const TUint KCacheThreshold = 16; + /** The properties cache table content. */ @@ -112,13 +115,10 @@ break; } } - - iObjectHandle = KMTPHandleNone; } void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::ResetL() { - iObjectHandle = KMTPHandleNone; SetUint(EImagePixWidth, 0); SetUint(EImagePixHeight, 0); SetUint(EImageBitDepth, 0); @@ -137,11 +137,6 @@ return iElementsUint[iElements[aId].iOffset]; } -TUint CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::ObjectHandle() const - { - return iObjectHandle; - } - void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::SetDesCL(TUint aId, const TDesC& aValue) { const TElementMetaData& KElement(iElements[aId]); @@ -157,11 +152,6 @@ iElementsUint[iElements[aId].iOffset] = aValue; } -void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::SetObjectHandle(TUint aObjectHandle) - { - iObjectHandle = aObjectHandle; - } - CMTPImageDpObjectPropertyMgr* CMTPImageDpObjectPropertyMgr::NewL(MMTPDataProviderFramework& aFramework, CMTPImageDataProvider& aDataProvider) { CMTPImageDpObjectPropertyMgr* self = new (ELeave) CMTPImageDpObjectPropertyMgr(aFramework, aDataProvider); @@ -175,7 +165,7 @@ iFramework(aFramework), iDataProvider(aDataProvider), iFs(aFramework.Fs()), - iObjectMgr(aFramework.ObjectMgr()) + iObjectMgr(aFramework.ObjectMgr()) { __FLOG_OPEN(KMTPSubsystem, KComponent); } @@ -183,7 +173,6 @@ void CMTPImageDpObjectPropertyMgr::ConstructL(MMTPDataProviderFramework& /*aFramework*/) { __FLOG(_L8("CMTPImageDpObjectPropertyMgr::ConstructL - Entry")); - iPropertiesCache = CMTPImagePropertiesCache::NewL(); iMetaDataSession = CMdESession::NewL(*this); __FLOG(_L8("CMTPImageDpObjectPropertyMgr::ConstructL - Exit")); } @@ -191,10 +180,13 @@ CMTPImageDpObjectPropertyMgr::~CMTPImageDpObjectPropertyMgr() { __FLOG(_L8("CMTPImageDpObjectPropertyMgr::~CMTPImageDpObjectPropertyMgr - Entry")); - delete iPropertiesCache; delete iObject; delete iMetaDataSession; delete iThumbnailCache.iThumbnailData; + + //Clear propreties cache map + ClearAllCache(); + iPropretiesCacheMap.Close(); __FLOG(_L8("CMTPImageDpObjectPropertyMgr::~CMTPImageDpObjectPropertyMgr - Exit")); __FLOG_CLOSE; } @@ -212,26 +204,12 @@ /** * determine whether the cache hit is occured */ - if (iPropertiesCache->ObjectHandle() == iObjectInfo->Uint(CMTPObjectMetaData::EHandle)) + iCacheHit = FindPropertiesCache(iObjectInfo->Uint(CMTPObjectMetaData::EHandle)); + if (!iCacheHit) { - iCacheHit = ETrue; - } - else - { - iCacheHit = EFalse; - - /** - * if cache miss, we should clear the cache content - */ - ClearCacheL(); - - //need parse image file by our self if fail to get properties from MdS - iNeedParse = ETrue; - - //clear previous Mde object delete iObject; - iObject = NULL; - } + iObject = NULL; + } } else { @@ -241,13 +219,27 @@ * other operations will not use cache, such as setobjectvalue/setobjectproplist */ if (aSaveToCache) - { + { TUint objectHandle = iObjectInfo->Uint(CMTPObjectMetaData::EHandle); - if (iPropertiesCache->ObjectHandle() != objectHandle) + if (FindPropertiesCache(objectHandle)) + { + __FLOG_VA((_L16("SetCurrentObjectL - find object in cache:%u"), objectHandle)); + iCurrentPropertiesCache->ResetL(); + } + else { - iPropertiesCache->ResetL(); + if (iPropretiesCacheMap.Count() > KCacheThreshold) + { + // Find the first object handle from cache map and then destory it + RHashMap::TIter iter(iPropretiesCacheMap); + DestroyPropertiesCahce(*iter.NextKey()); + __FLOG_VA((_L16("SetCurrentObjectL - destory object:%u"), objectHandle)); + } + + iCurrentPropertiesCache = CMTPImagePropertiesCache::NewL(); + iPropretiesCacheMap.Insert(objectHandle, iCurrentPropertiesCache); + __FLOG_VA((_L16("SetCurrentObjectL - create new object:%u"), objectHandle)); } - iPropertiesCache->SetObjectHandle(objectHandle); } } @@ -304,13 +296,22 @@ iObjectInfo->SetUint(CMTPObjectMetaData::EParentHandle, aValue); break; case EMTPObjectPropCodeWidth: - iPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixWidth, aValue); + if (iCurrentPropertiesCache != NULL) + { + iCurrentPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixWidth, aValue); + } break; case EMTPObjectPropCodeHeight: - iPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixHeight, aValue); + if (iCurrentPropertiesCache != NULL) + { + iCurrentPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixHeight, aValue); + } break; case EMTPObjectPropCodeImageBitDepth: - iPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImageBitDepth, aValue); + if (iCurrentPropertiesCache != NULL) + { + iCurrentPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImageBitDepth, aValue); + } break; default: //nothing to do @@ -363,7 +364,10 @@ break; case EMTPObjectPropCodeDateCreated://MdS property - iPropertiesCache->SetDesCL(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EDateCreated, aValue); + if (iCurrentPropertiesCache != NULL) + { + iCurrentPropertiesCache->SetDesCL(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EDateCreated, aValue); + } break; default: @@ -403,16 +407,18 @@ aValue = KThumbFormatCode; break; case EMTPObjectPropCodeProtectionStatus: - iFs.Entry(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), entry); - if (entry.IsReadOnly()) + { + TInt err = iFs.Entry(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), entry); + if ( err == KErrNone && entry.IsReadOnly()) { aValue = EMTPProtectionReadOnly; } else { aValue = EMTPProtectionNoProtection; - } - break; + } + } + break; default: aValue = 0;//initialization //ingore the failure if we can't get properties form MdS @@ -437,55 +443,46 @@ aValue = iObjectInfo->Uint(CMTPObjectMetaData::EParentHandle); break; - case EMTPObjectPropCodeRepresentativeSampleSize: - aValue = MTPImageDpUtilits::GetThumbnailSize(*iObjectInfo); - if (aValue == 0) + case EMTPObjectPropCodeRepresentativeSampleSize: + { + __FLOG_VA((_L16("Query smaple size from MdS - URI:%S"), &iObjectInfo->DesC(CMTPObjectMetaData::ESuid))); + ClearThumnailCache(); + /** + * try to query thumbnail from TNM, and then store thumbnail to cache + */ + TEntry fileEntry; + TInt err = iFs.Entry(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), fileEntry); + if (err == KErrNone) { - __FLOG_VA((_L16("Query smaple size from MdS - URI:%S"), &iObjectInfo->DesC(CMTPObjectMetaData::ESuid))); - ClearThumnailCache(); + if(fileEntry.FileSize() > KFileSizeMax || !alwaysCreate) + { + iDataProvider.ThumbnailManager().GetThumbMgr()->SetFlagsL(CThumbnailManager::EDoNotCreate); + } + else + { + iDataProvider.ThumbnailManager().GetThumbMgr()->SetFlagsL(CThumbnailManager::EDefaultFlags); + } + /** - * try to query thumbnail from TNM, and then store thumbnail to cache + * trap the leave to avoid return general error when PC get object property list */ - TEntry fileEntry; - TInt err = iFs.Entry(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), fileEntry); + TRAP(err, iDataProvider.ThumbnailManager().GetThumbnailL(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), iThumbnailCache.iThumbnailData, err)); if (err == KErrNone) { - if(fileEntry.FileSize() > KFileSizeMax || !alwaysCreate) - { - iDataProvider.ThumbnailManager().GetThumbMgr()->SetFlagsL(CThumbnailManager::EDoNotCreate); - } - else - { - iDataProvider.ThumbnailManager().GetThumbMgr()->SetFlagsL(CThumbnailManager::EDefaultFlags); - } - - /** - * trap the leave to avoid return general error when PC get object property list - */ - TRAP(err, iDataProvider.ThumbnailManager().GetThumbnailL(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), iThumbnailCache.iThumbnailData, err)); - if (err == KErrNone) + iThumbnailCache.iObjectHandle = iObjectInfo->Uint(CMTPObjectMetaData::EHandle); + if (iThumbnailCache.iThumbnailData != NULL) { - iThumbnailCache.iObjectHandle = iObjectInfo->Uint(CMTPObjectMetaData::EHandle); - if (iThumbnailCache.iThumbnailData != NULL) - { - aValue = static_cast(iThumbnailCache.iThumbnailData->Size()); - } - - if (aValue > 0) - { - //update metadata column - MTPImageDpUtilits::UpdateObjectThumbnailSizeL(iFramework, *iObjectInfo, aValue); - } - else - { - //trigger initiator to re-query thumbnail again if the thumbnail size of response is zero - aValue = KThumbCompressedSize; - } - - __FLOG_VA((_L16("Cache miss:GetThumbnailSize - URI:%S, Size:%u"), &iObjectInfo->DesC(CMTPObjectMetaData::ESuid), aValue)); + aValue = static_cast(iThumbnailCache.iThumbnailData->Size()); + } + + if (aValue <= 0) + { + //trigger initiator to re-query thumbnail again if the thumbnail size of response is zero + aValue = KThumbCompressedSize; } } - } + } + } break; case EMTPObjectPropCodeRepresentativeSampleHeight: @@ -618,17 +615,10 @@ TRAP(err, iDataProvider.ThumbnailManager().GetThumbnailL(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), iThumbnailCache.iThumbnailData, err)); if (err == KErrNone) { - TInt size = MTPImageDpUtilits::GetThumbnailSize(*iObjectInfo); iThumbnailCache.iObjectHandle = iObjectInfo->Uint(CMTPObjectMetaData::EHandle); if (iThumbnailCache.iThumbnailData != NULL) { aValue.SetByDesL(*iThumbnailCache.iThumbnailData); - if (size == 0) - { - //update metadata column - MTPImageDpUtilits::UpdateObjectThumbnailSizeL(iFramework, *iObjectInfo, iThumbnailCache.iThumbnailData->Size()); - __FLOG_VA((_L16("Cache miss:GetThumbnailSize - URI:%S, Size:%u"), &iObjectInfo->DesC(CMTPObjectMetaData::ESuid), size)); - } } } } @@ -654,19 +644,19 @@ switch (aProperty) { case EMTPObjectPropCodeDateCreated: - (*(static_cast(aValue))).SetL(iPropertiesCache->DesC(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EDateCreated)); + (*(static_cast(aValue))).SetL(iCurrentPropertiesCache->DesC(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EDateCreated)); break; case EMTPObjectPropCodeWidth: - *static_cast(aValue) = iPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixWidth); + *static_cast(aValue) = iCurrentPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixWidth); break; case EMTPObjectPropCodeHeight: - *static_cast(aValue) = iPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixHeight); + *static_cast(aValue) = iCurrentPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixHeight); break; case EMTPObjectPropCodeImageBitDepth: - *static_cast(aValue) = iPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImageBitDepth); + *static_cast(aValue) = iCurrentPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImageBitDepth); break; default: @@ -972,9 +962,18 @@ iMdeSessionError = aError; } -void CMTPImageDpObjectPropertyMgr::ClearCacheL() +void CMTPImageDpObjectPropertyMgr::ClearAllCache() { - iPropertiesCache->ResetL(); + while (iPropretiesCacheMap.Count()) + { + RHashMap::TIter iter(iPropretiesCacheMap); + DestroyPropertiesCahce(*iter.NextKey()); + }; + } + +void CMTPImageDpObjectPropertyMgr::ClearCache(TUint aHandle) + { + DestroyPropertiesCahce(aHandle); } void CMTPImageDpObjectPropertyMgr::OpenMdeObjectL() @@ -1000,3 +999,32 @@ iThumbnailCache.iObjectHandle = KMTPHandleNone; } + +TBool CMTPImageDpObjectPropertyMgr::FindPropertiesCache(TUint aObjectHandle) + { + TBool ret = EFalse; + CMTPImagePropertiesCache** ppCache = iPropretiesCacheMap.Find(aObjectHandle); + if (ppCache) + { + iCurrentPropertiesCache = *ppCache; + ret = (iCurrentPropertiesCache != NULL) ? ETrue : EFalse; + } + else + { + iCurrentPropertiesCache = NULL; + ret = EFalse; + } + + return ret; + } + +void CMTPImageDpObjectPropertyMgr::DestroyPropertiesCahce(TUint aObjectHandle) + { + CMTPImagePropertiesCache** ppCache = iPropretiesCacheMap.Find(aObjectHandle); + if (ppCache) + { + CMTPImagePropertiesCache* pCache = *ppCache; + delete pCache; + iPropretiesCacheMap.Remove(aObjectHandle); + } + }