appfw/apparchitecture/apserv/APSRECCACHE.h
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 1997-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 #if !defined(__APSRECCACHE_H__)
       
    17 #define __APSRECCACHE_H__
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <apmrec.h>
       
    21 #include "APSRECUTIL.H"
       
    22 
       
    23 const TUint KFileHashMapEntries = 40;
       
    24 
       
    25 /**
       
    26 An entry for a recognized file in the recognition cache.
       
    27 The entries is organized as single-linked list.
       
    28 The recognition result itself is reference-counted.
       
    29 @internalComponent
       
    30 */
       
    31 class CRecognitionResultHashMapEntry : public CBase
       
    32 	{
       
    33 public:
       
    34 	static CRecognitionResultHashMapEntry* NewL(const TDesC& aFileName, TTime aLastModified, const TDataRecognitionResult& aResult, CRecognitionResultHashMapEntry* aNext);
       
    35 	~CRecognitionResultHashMapEntry();
       
    36 	
       
    37 	inline const CRecognitionResultHashMapEntry* Next() const { return iNext; }
       
    38 	inline CRecognitionResultHashMapEntry* Next() { return iNext; }
       
    39 	inline TTime LastModified() const { return iLastModified; }
       
    40 	inline const TDesC& FileName() const { return iResult->FileName(); }
       
    41 	inline CRecognitionResult* Result() const { iResult->Open(); return iResult; }
       
    42 		
       
    43 	void UpdateL(TTime aLastModified, const TDataRecognitionResult& aResult);
       
    44 private:
       
    45 	CRecognitionResultHashMapEntry(TTime aLastModified, CRecognitionResult* aResult, CRecognitionResultHashMapEntry* aNext);
       
    46 private:
       
    47 	TTime iLastModified;
       
    48 	CRecognitionResult* iResult;
       
    49 	CRecognitionResultHashMapEntry* iNext;
       
    50 	};
       
    51 
       
    52 /**
       
    53 A hash map for finding and adding recognition results.
       
    54 
       
    55 @internalComponent
       
    56 */
       
    57 class CRecognitionResultHashMap : public CBase
       
    58 	{
       
    59 public:
       
    60 	CRecognitionResultHashMap();
       
    61 	~CRecognitionResultHashMap();
       
    62 	CRecognitionResult* Get(const TDesC& aKey, TTime& aLastModified) const;
       
    63 	TBool AddL(const TDesC& aKey, TTime aLastModified, const TDataRecognitionResult& aResult);
       
    64 	inline TUint NumberOfEntries() const { return iNumberOfEntries; }
       
    65 protected:
       
    66 	TUint GetIndex(const TDesC& aKey) const;
       
    67 private:
       
    68 	inline CRecognitionResultHashMapEntry* Entry(TUint aIndex);
       
    69 	inline const CRecognitionResultHashMapEntry* Entry(TUint aIndex) const;
       
    70 	inline void SetEntry(TUint aIndex, CRecognitionResultHashMapEntry* aEntry);
       
    71 private:
       
    72 	TUint iNumberOfEntries;
       
    73 	CRecognitionResultHashMapEntry* iEntries[KFileHashMapEntries];
       
    74 	};
       
    75 
       
    76 /**
       
    77 A directory entry in the cache.
       
    78 Such an entry doesn't necessarily hold all files of a directory, but
       
    79 only the ones that have been recognized so far.
       
    80 @internalComponent
       
    81 */
       
    82 class CCacheDirectoryEntry : public CBase
       
    83 	{
       
    84 public:
       
    85 	static CCacheDirectoryEntry* NewL(const TDesC& aDirectory);
       
    86 	~CCacheDirectoryEntry();
       
    87 	inline const TDesC& Directory() const { return *iDirectory; }
       
    88 	inline CRecognitionResultHashMap& Files() { return iFiles; } //lint !e1536 Suppress exposing low access member
       
    89 	inline TUint NumberOfEntries() const { return iFiles.NumberOfEntries(); }
       
    90 private:
       
    91 	CCacheDirectoryEntry();
       
    92 public:
       
    93 	static const TInt iOffset;
       
    94 private:
       
    95 	HBufC* iDirectory; // own copy!
       
    96 	CRecognitionResultHashMap iFiles;
       
    97 	TDblQueLink iDlink;
       
    98 	friend class CApsRecognitionCache;
       
    99 	};
       
   100 
       
   101 /**
       
   102 A recognition result cache.
       
   103 The cache holds a list of directories (stored as an RPointerArray). Recently
       
   104 used directories are stored on top of the list, rarely used directories are
       
   105 stored at the bottom. 
       
   106 
       
   107 This strategy improves performance: 
       
   108 search for files is started at the top (principle of locality) 
       
   109 and cleanup is done from the bottom.
       
   110 
       
   111 The files themselves are stored in a hash map (within directories).
       
   112 @internalComponent
       
   113 */
       
   114 class CApsRecognitionCache : public CBase
       
   115 	{
       
   116 public:
       
   117 	CApsRecognitionCache(RFs& aFs);
       
   118 	~CApsRecognitionCache();
       
   119 	void AddL(const TDesC& aDirectory, const TDesC& aFileName, const TDataRecognitionResult& aRecognitionResult);
       
   120 	void AddL(const RFile& aFile, const TDesC& aDirectory, const TDesC& aFileName, const TDataRecognitionResult& aRecognitionResult);
       
   121 	TBool Get(const TDesC& aDirectory, const TDesC& aFileName, TDataRecognitionResult& aRecognitionResult);
       
   122 	CRecognitionResult* Get(const RFile& aFile, const TDesC& aDirectory, const TDesC& aFileName);
       
   123 	void Flush();
       
   124 private:
       
   125 	void DoAddL(const TDesC& aDirectory, const TDesC& aFileName, const TTime& aLastModified, const TDataRecognitionResult& aRecognitionResult);
       
   126 	CRecognitionResult* DoGet(const TDesC& aDirectory, const TDesC& aFileName, const TTime& aLastModified);
       
   127 	TBool CompareDirectories(const TDesC& aDir1, const TDesC& aDir2) const;
       
   128 	void Cleanup();
       
   129 private: // data
       
   130 	RFs& iFs;
       
   131 	TDblQue<CCacheDirectoryEntry> iDirectoryHeader;
       
   132 	TDblQueIter<CCacheDirectoryEntry> iIter;
       
   133 	TUint iNumberOfEntries;
       
   134 	};
       
   135 
       
   136 
       
   137 //inlines
       
   138 
       
   139 inline CRecognitionResultHashMapEntry* CRecognitionResultHashMap::Entry(TUint aIndex)
       
   140 	{
       
   141 	ASSERT(aIndex < KFileHashMapEntries);
       
   142 	return iEntries[aIndex];
       
   143 	}  //lint !e1762 Suppress member function could be made const
       
   144 
       
   145 inline const CRecognitionResultHashMapEntry* CRecognitionResultHashMap::Entry(TUint aIndex) const
       
   146 	{
       
   147 	ASSERT(aIndex < KFileHashMapEntries);
       
   148 	return iEntries[aIndex];
       
   149 	}
       
   150 
       
   151 inline void CRecognitionResultHashMap::SetEntry(TUint aIndex, CRecognitionResultHashMapEntry* aEntry)
       
   152 	{
       
   153 	ASSERT(Entry(aIndex) == aEntry->Next());
       
   154 	iEntries[aIndex] = aEntry;
       
   155 	}
       
   156 
       
   157 #endif // __APSRECCACHE_H__
       
   158