mtpfws/mtpfw/inc/cmtpobjectstore.h
changeset 0 d0791faffa3f
child 4 60a94a45d437
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2006-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  @internalComponent
       
    19  */
       
    20 
       
    21 #ifndef CMTPOBJECTSTORE_H
       
    22 #define CMTPOBJECTSTORE_H
       
    23 
       
    24 #include <badesca.h>
       
    25 #include <d32dbms.h>
       
    26 
       
    27 #include "mtpframeworkconst.h"
       
    28 #include "rmtpframework.h"
       
    29 #include "mtpdebug.h"
       
    30 
       
    31 class CFileStore;
       
    32 class CMTPHandleAllocator;
       
    33 class CMTPObjectMetaData;
       
    34 class CMTPPuidMgr;
       
    35 class CMTPReferenceMgr;
       
    36 class CMTPTypeArray;
       
    37 class RMTPObjectMgrQueryContext;
       
    38 class TMTPTypeUint32;
       
    39 class TMTPTypeUint128;
       
    40 class TMTPObjectMgrQueryParams;
       
    41 class CMtpDeltaDataMgr;
       
    42 class CMTPDPIDStore;
       
    43 class CMTPPkgIDStore;
       
    44 /** 
       
    45  Implements the MTP object meta data store, which manages the storage/mapping of object handle and suid, and puid using DMBS.
       
    46  Note, the CMTPObjectMgr acts as a shim class which forwards all the requests to the CMTPObjectStore. 
       
    47  The reason is that later on, the class CMTPObjectStore will also implement the reference manager and puid manager
       
    48  functionalities.  Having a single class implements all these will enable us to use a single database with multiple 
       
    49  tables.
       
    50 
       
    51  @internalComponent
       
    52  
       
    53  */
       
    54 class CMTPObjectStore : public CBase
       
    55 	{
       
    56 	class CDbCompactor : public CBase
       
    57 		{
       
    58 	public:
       
    59 		inline static CDbCompactor* NewL(RDbNamedDatabase* aDatabase)
       
    60 			{
       
    61 			CDbCompactor* self = new (ELeave) CDbCompactor(aDatabase);
       
    62 			return self;
       
    63 			}
       
    64 		inline void Compact()
       
    65 			{
       
    66 			if (!((++iCompactCnt) % KCompactCnt))
       
    67 				{
       
    68 				RDbDatabase::TSize size = iDatabase->Size();
       
    69 				iDatabase->Compact();
       
    70 				}
       
    71 			}
       
    72 		inline ~CDbCompactor()
       
    73 			{
       
    74 			}
       
    75 	private:
       
    76 		inline CDbCompactor(RDbNamedDatabase* aDatabase) : iDatabase(aDatabase)
       
    77 			{
       
    78 			}
       
    79 
       
    80 	private:
       
    81 		RDbNamedDatabase* iDatabase;
       
    82 		TUint8 iCompactCnt;
       
    83 		static const TUint8 KCompactCnt = 32; //Use this value make the performance better haven't test other value
       
    84 		};
       
    85 
       
    86 	class MTraverseAction
       
    87 		{
       
    88 	public:
       
    89 		typedef enum {EOnlyLoadedDP, EAllDP} TActionTarget;
       
    90 		MTraverseAction(TActionTarget aTarget) : iActionTarget(aTarget)
       
    91 			{
       
    92 			}
       
    93 		TActionTarget Target()
       
    94 			{
       
    95 			return iActionTarget;
       
    96 			}
       
    97 		virtual void DoL(RDbTable& aTable) = 0;
       
    98 		
       
    99 	private:
       
   100 		TActionTarget iActionTarget;
       
   101 		};
       
   102 	class TCountAction : public MTraverseAction
       
   103 		{
       
   104 	public:
       
   105 		TCountAction(TUint& aCounter, TActionTarget aTarget = EOnlyLoadedDP) : MTraverseAction(aTarget), iCounter(aCounter)
       
   106 			{
       
   107 			}
       
   108 		void DoL(RDbTable& /*aTable*/)
       
   109 			{
       
   110 			++iCounter;
       
   111 			}
       
   112 	private:
       
   113 		TUint& iCounter;
       
   114 		};
       
   115 
       
   116 	class TGetHandlesAction : public MTraverseAction
       
   117 		{
       
   118 	public:
       
   119 		TGetHandlesAction(RArray<TUint>& aHandles, TActionTarget aTarget = EOnlyLoadedDP) : MTraverseAction(aTarget), iHandles(aHandles)
       
   120 			{
       
   121 			}
       
   122 		void DoL(RDbTable& aTable)
       
   123 			{
       
   124 			iHandles.AppendL(aTable.ColUint32(EObjectStoreHandleId));
       
   125 			}
       
   126 	private:
       
   127 		RArray<TUint>& iHandles;
       
   128 		};
       
   129 
       
   130 	class TGetSuidsAction : public MTraverseAction
       
   131 		{
       
   132 	public:
       
   133 		TGetSuidsAction(CDesCArray& aSuids, TActionTarget aTarget = EOnlyLoadedDP) : MTraverseAction(aTarget), iSuids(aSuids)
       
   134 			{
       
   135 			}
       
   136 		void DoL(RDbTable& aTable)
       
   137 			{
       
   138 			TFileName buf;
       
   139 			RDbColReadStream suid;
       
   140 			suid.OpenLC(aTable, EObjectStoreSUID);
       
   141 			suid.ReadL(buf, aTable.ColLength(EObjectStoreSUID));
       
   142 			suid.Close();
       
   143 			CleanupStack::PopAndDestroy();
       
   144 			iSuids.AppendL(buf);
       
   145 			}
       
   146 	private:
       
   147 		CDesCArray& iSuids;
       
   148 		};
       
   149 	
       
   150 	class TDelAction : public MTraverseAction
       
   151 		{
       
   152 	public:
       
   153 		TDelAction(CMTPObjectStore& aStore, TActionTarget aTarget = EOnlyLoadedDP) : MTraverseAction(aTarget), iStore(aStore)
       
   154 			{
       
   155 			}
       
   156 		void DoL(RDbTable& aTable)
       
   157 			{
       
   158 			aTable.DeleteL();
       
   159 			iStore.IncTranOpsNumL();
       
   160 			}
       
   161 	private:
       
   162 		CMTPObjectStore& iStore;
       
   163 		};
       
   164 	
       
   165 public:
       
   166 
       
   167 	static CMTPObjectStore* NewL();
       
   168 	~CMTPObjectStore();
       
   169 
       
   170 public:
       
   171 
       
   172 	RDbDatabase& Database();
       
   173 	CMTPDPIDStore& DPIDStore() const;
       
   174 	CMTPPkgIDStore& PkgIDStore() const;
       
   175 	CMTPReferenceMgr& ReferenceMgr() const;
       
   176 	CMtpDeltaDataMgr* MtpDeltaDataMgr() const;
       
   177 	void RestorePersistentObjectsL(TUint aDataProviderId);
       
   178 	void RemoveObjectsByStorageIdL(TUint32 aStorageId);
       
   179 	void RemoveNonPersistentObjectsL(TUint aDataProviderId);
       
   180 	void MarkNonPersistentObjectsL(TUint aDataProviderId, TUint32 aStorageId);
       
   181 	void EstablishDBSnapshotL(TUint32 aStorageId);
       
   182 	void CleanDBSnapshotL();
       
   183 	void MarkDPLoadedL(TUint aDataProviderId, TBool aFlag);
       
   184 
       
   185 public:
       
   186 
       
   187 	TUint CountL(const TMTPObjectMgrQueryParams& aParams) const;
       
   188 	void GetObjectHandlesL(const TMTPObjectMgrQueryParams& aParams, RMTPObjectMgrQueryContext& aContext, RArray<TUint>& aHandles) const;
       
   189 	void GetObjectSuidsL(const TMTPObjectMgrQueryParams& aParams, RMTPObjectMgrQueryContext& aContext, CDesCArray& aSuids) const;
       
   190 	void TraverseL(const TMTPObjectMgrQueryParams& aParams, MTraverseAction& action) const;
       
   191 	void CalcFreeHandlesL(TUint aDataProviderId);
       
   192 	void CommitReservedObjectHandleL(CMTPObjectMetaData& aObject);
       
   193 	TUint32 HandleL(const TDesC& aSuid) const;
       
   194 	TUint32 HandleL(TUint32 aSuidHash, const TDesC& aSuid) const;
       
   195 	void InsertObjectL(CMTPObjectMetaData& aObject);
       
   196 	void InsertObjectsL(RPointerArray<CMTPObjectMetaData>& aObjects);
       
   197 	void ModifyObjectL(const CMTPObjectMetaData& aObject);
       
   198 	TBool ObjectL(const TMTPTypeUint32& aHandle, CMTPObjectMetaData& aObject) const;
       
   199 	TBool ObjectL(const TDesC& aSuid, CMTPObjectMetaData& aObject) const;
       
   200 	TBool ObjectExistsL(const TUint32 aHandle);
       
   201 	TMTPTypeUint128 PuidL(const TUint32 aHandle);
       
   202 	TMTPTypeUint128 PuidL(const TDesC& aSuid);
       
   203 
       
   204 	const TPtrC ObjectSuidL(TUint32 aHandle) const;
       
   205 	void RemoveObjectL(const TMTPTypeUint32& aHandle);
       
   206 	void RemoveObjectL(const TDesC& aSuid);
       
   207 	void RemoveObjectsL(const CDesCArray& aSuids);
       
   208 	void RemoveObjectsL(TUint aDataProviderId);
       
   209 	void ReserveObjectHandleL(CMTPObjectMetaData& aObject, TUint64 aSpaceRequired);
       
   210 	void UnreserveObjectHandleL(const CMTPObjectMetaData& aObject);
       
   211 	void IncTranOpsNumL();
       
   212 	TBool IsMediaFormat(TUint32 aFormatCode);
       
   213 
       
   214 	void BeginTransactionL();
       
   215 	void CommitTransactionL();
       
   216 
       
   217 	void CleanL();
       
   218 	TUint ObjectOwnerId(const TMTPTypeUint32& aHandle) const;
       
   219 private:
       
   220 
       
   221 	/**
       
   222 	 Enumeration representing the column fields in the object store DBMS, this definition must exactly match the sequence
       
   223 	 defined in KSQLCreateHandleTableText
       
   224 	 //During insert a object, only the following items is provided in the CMTPObjectMetaData
       
   225 	 //DPId
       
   226 	 //Format
       
   227 	 //FormatSubcode
       
   228 	 //StorageID
       
   229 	 //Modes
       
   230 	 //ParentHandle
       
   231 	 //
       
   232 	 //Handle is allocated by MTP framework
       
   233 	 //NonConsumable is inserted only by FileDP, for objects manged by LicenseeDP, its values is meaningless
       
   234 	 */
       
   235 	enum TObjectStore
       
   236 		{
       
   237 		EObjectStoreHandleId = 1,
       
   238 		EObjectStoreSUIDHash = 2,
       
   239 		EObjectStoreSUID = 3,
       
   240 		EObjectStoreDataProviderId = 4,
       
   241 		EObjectStoreFormatCode = 5,
       
   242 		EObjectStoreFormatSubCode = 6,
       
   243 		EObjectStoreStorageId = 7,
       
   244 		EObjectStoreModes = 8,
       
   245 		EObjectStorePOUID = 9,
       
   246 		EObjectStoreParentHandle = 10,
       
   247 		EObjectStoreDPFlag = 11,
       
   248 		EObjectStoreNonConsumable = 12,
       
   249 		EObjectStoreName = 13
       
   250 		};
       
   251 
       
   252 	class CEnumertingCacheItem : public CBase
       
   253 		{
       
   254 	public:
       
   255 		static CEnumertingCacheItem* NewLC(TUint32 aSuidHash, TUint32 aHandle, TUint32 aFormat, TUint64 aId, TUint8 aDpID)
       
   256 			{
       
   257 			CEnumertingCacheItem* self = new (ELeave) CEnumertingCacheItem(aSuidHash, aHandle, aFormat, aId, aDpID);
       
   258 			CleanupStack::PushL(self);
       
   259 			return self;
       
   260 			}
       
   261 		static CEnumertingCacheItem* NewL(TUint32 aSuidHash, TUint32 aHandle, TUint32 aFormat, TUint64 aId, TUint8 aDpID)
       
   262 			{
       
   263 			CEnumertingCacheItem* self = CEnumertingCacheItem::NewLC(aSuidHash, aHandle, aFormat, aId, aDpID);
       
   264 			CleanupStack::Pop();
       
   265 			return self;
       
   266 			}
       
   267 		static TInt Compare(const CEnumertingCacheItem& aFirst, const CEnumertingCacheItem& aSecond);
       
   268 		CEnumertingCacheItem(TUint32 aSuidHash, TUint32 aHandle, TUint32 aFormat, TUint64 aId, TUint8 aDpID);
       
   269 		~CEnumertingCacheItem()
       
   270 			{
       
   271 			delete iSuid;
       
   272 			}
       
   273 		TUint32 iObjHandleId;
       
   274 		TUint32 iObjSuiIdHash;
       
   275 		TUint32 iFormatcode;
       
   276 		TUint64 iPOUID;
       
   277 		HBufC* iSuid;
       
   278 		TPtrC iSuidPtr;
       
   279 		TUint8 iDpID;
       
   280 		};
       
   281 
       
   282 	CMTPObjectStore();
       
   283 	void ConstructL();
       
   284 
       
   285 private:
       
   286 
       
   287 	TBool LocateByHandleL(const TUint aHandle, const TBool aReadTable = ETrue) const;
       
   288 	TBool LocateBySuidL(const TDesC& aSuid) const;
       
   289 
       
   290 	void BuildObjectMetaDataL(CMTPObjectMetaData& aBuf, const RDbTable& aTable) const;
       
   291 	void InitializeDbL();
       
   292 	void CreateDbL(const TDesC& aFileName);
       
   293 	TInt OpenDb(const TDesC& aFileName);
       
   294 	void CloseDb();
       
   295 
       
   296 	void CreateHandleTableL();
       
   297 	void CreateHandleIndexL();
       
   298 	void CreateStorageTableL();
       
   299 	void CreateStorageIndexL();
       
   300 
       
   301 	void GetFullPathName(const TDesC& aName, TFileName& aFileName) const;
       
   302 
       
   303 	TBool GetObjectL(TUint32 aHandle, CMTPObjectMetaData& aObject) const;
       
   304 
       
   305 	static void DBUpdateFailRecover(TAny* aTable);
       
   306     TBool IsInvalidHandle( TUint32 aHandle ) const;
       
   307     TBool FilterObject(const RDbTable& aCurrRow,const TUint32 aStorageID,const TUint32 aFormatCode,const TUint32 aDpID) const;
       
   308     void DbColWriteStreamL(RDbTable& aTable, TDbColNo aCol, const TDesC16& aDes);
       
   309     void DbColReadStreamL(const RDbTable& aTable, TDbColNo aCol, TDes16& aDes) const;    
       
   310 
       
   311 private:
       
   312 
       
   313 	static const TUint KQueryWindowForeSlots = 512;
       
   314 	static const TUint KQueryWindowRearSlots = 0;
       
   315 	
       
   316 	mutable RDbTable						iBatched;
       
   317 	mutable RDbTable						iBatched_SuidHashID;
       
   318 	mutable RDbNamedDatabase				iDatabase;
       
   319 	mutable TBuf<KMTPMaxSqlStatementLen>	iSqlStatement;
       
   320 	mutable TUint32							iCachedHandle;
       
   321 	mutable TUint32							iCachedSuidHash;
       
   322 	
       
   323 	RArray<TUint>							iNonPersistentDPList;
       
   324 	//The following object is used for enumerating cache, will be closed after the enumeration is completed.
       
   325 	RPointerArray<CEnumertingCacheItem>		iEnumeratingCacheObjList;
       
   326 	RMTPFramework							iSingletons;
       
   327 	
       
   328 	
       
   329 	TUint32									iTransactionOps;
       
   330 	TUint32									iMaxCommitLimit;
       
   331 	TUint32									iMaxCompactLimit;
       
   332 	TBool									iUpdateDeltaDataTable;
       
   333 	TBool									iCacheExist;
       
   334 	
       
   335 	CMTPReferenceMgr*						iReferenceMgr;
       
   336 	CMTPHandleAllocator*					iHandleAllocator;
       
   337 	CEnumertingCacheItem*					iSentinal;
       
   338 	CMTPDPIDStore*							iDPIDStore;
       
   339 	CMTPPkgIDStore*							iPkgIDStore;
       
   340 	CMtpDeltaDataMgr* 						iMtpDeltaDataMgr;
       
   341 	CDbCompactor*							iCompactor;
       
   342 	mutable TFileName                       iSuidBuf;
       
   343 	/**
       
   344 	 FLOGGER debug trace member variable.
       
   345 	 */
       
   346 	__FLOG_DECLARATION_MEMBER_MUTABLE;
       
   347 	};
       
   348 
       
   349 #endif // CMTPOBJECTSTORE_H