TFileCachedProducerCompBase.h
Go to the documentation of this file.
1 #ifndef iproc_TFileCachedProducerCompBase_included
2 #define iproc_TFileCachedProducerCompBase_included
3 
4 // Old C includes
5 #include <stdio.h>
6 
7 // Qt includes
8 #include <QtCore/QList>
9 
10 // ACF includes
11 #include "ifile/IFilePersistence.h"
12 #include "icomp/CComponentBase.h"
13 
14 #include "iproc/TILockedProducer.h"
15 
16 
17 namespace iproc
18 {
19 
20 
26 template <class Key, class CacheObject>
28  public icomp::CComponentBase,
29  virtual public TILockedProducer<Key, CacheObject>
30 {
31 public:
34 
35  I_BEGIN_BASE_COMPONENT(TFileCachedProducerCompBase);
36  I_REGISTER_INTERFACE(LockedProducerType);
37  I_ASSIGN(m_cacheLoaderCompPtr, "CacheLoader", "Loads and saves cached object to temporary file", true, "CacheLoader");
38  I_TASSIGN(m_slaveCacheEngineCompPtr, "SlaveCacheEngine", "Slave cache engine providing access to cached object", true, "SlaveCacheEngine");
39  I_END_COMPONENT;
40 
41  // reimplemented (iproc::TILockedProducer)
42  virtual const CacheObject* ProduceLockedObject(const Key& key);
43  virtual void UnlockObject(const CacheObject* objectPtr);
44 
45 protected:
46  bool PushKeyBack(const Key& key);
47 
51  void CleanFileList();
52 
53  // methods for optional ovelloading
54  virtual void OnCacheFileSaved(const Key& key, const QString& cacheFilePath);
55  virtual void OnCacheFileRemoved(const Key& key, const QString& cacheFilePath);
56 
57  // abstract methods
61  virtual QString CalcCacheFilePath(const Key& key) const = 0;
65  virtual int GetMaxCachedFilesCount() const = 0;
66 
67 private:
68  I_REF(ifile::IFilePersistence, m_cacheLoaderCompPtr);
69  I_TREF(LockedProducerType, m_slaveCacheEngineCompPtr);
70 
71  typedef QMap<Key, QString> KeyToFileNameMap;
72  typedef QList<Key> RecentlyUsedKeys;
73  typedef QSet<const CacheObject*> OwnedObjects;
74 
75  mutable KeyToFileNameMap m_keyToFileNameMap;
76  mutable RecentlyUsedKeys m_recentlyUsedKeys;
77  mutable OwnedObjects m_ownedObjects;
78 };
79 
80 
81 // public methods
82 
83 // reimplemented (iproc::TILockedProducer)
84 
85 template <class Key, class CacheObject>
87 {
88  if (m_cacheLoaderCompPtr.IsValid()){
89  QString cacheFilePath;
90  typename KeyToFileNameMap::iterator foundIter = m_keyToFileNameMap.find(key);
91  if (foundIter != m_keyToFileNameMap.end()){
92  cacheFilePath = foundIter.value();
93  Q_ASSERT(!cacheFilePath.isEmpty());
94  }
95  else{
96  cacheFilePath = CalcCacheFilePath(key);
97  }
98 
99  if (!cacheFilePath.isEmpty()){
100  if (m_cacheLoaderCompPtr->IsOperationSupported(
101  NULL,
102  &cacheFilePath,
103  ifile::IFilePersistence::QF_LOAD | ifile::IFilePersistence::QF_FILE,
104  false)){
105  istd::TDelPtr<CacheObject> objectPtr(new CacheObject);
106 
107  if (objectPtr.IsValid()){
108  if (m_cacheLoaderCompPtr->LoadFromFile(*objectPtr, cacheFilePath) == ifile::IFilePersistence::OS_OK){
109  m_ownedObjects.insert(objectPtr.GetPtr());
110 
111  return objectPtr.PopPtr();
112  }
113  else if (foundIter != m_keyToFileNameMap.end()){
114  m_keyToFileNameMap.erase(foundIter);
115  }
116  }
117  }
118  }
119  }
120 
121  if (m_slaveCacheEngineCompPtr.IsValid()){
122  const CacheObject* objectPtr = m_slaveCacheEngineCompPtr->ProduceLockedObject(key);
123  if (objectPtr != NULL){
124  QString cacheFilePath = CalcCacheFilePath(key);
125  if (!cacheFilePath.isEmpty() && m_cacheLoaderCompPtr.IsValid()){
126  if (m_cacheLoaderCompPtr->SaveToFile(*const_cast<CacheObject*>(objectPtr), cacheFilePath) == ifile::IFilePersistence::OS_OK){
127  OnCacheFileSaved(key, cacheFilePath);
128 
129  m_keyToFileNameMap[key] = cacheFilePath;
130  m_recentlyUsedKeys.push_back(key);
131 
133  }
134  }
135 
136  return objectPtr;
137  }
138  }
139 
140  return NULL;
141 }
142 
143 
144 template <class Key, class CacheObject>
146 {
147  Q_ASSERT(objectPtr != NULL);
148 
149  typename OwnedObjects::iterator foundIter = m_ownedObjects.find(objectPtr);
150  if (foundIter != m_ownedObjects.end()){
151  m_ownedObjects.remove(objectPtr);
152 
153  delete objectPtr;
154 
155  return;
156  }
157 
158  if (m_slaveCacheEngineCompPtr.IsValid()){
159  m_slaveCacheEngineCompPtr->UnlockObject(objectPtr);
160  }
161 }
162 
163 
164 // protected methods
165 
166 template <class Key, class CacheObject>
168 {
169  if (m_keyToFileNameMap.find(key) == m_keyToFileNameMap.end()){
170  QString cacheFilePath = CalcCacheFilePath(key);
171  if (!cacheFilePath.isEmpty()){
172  m_keyToFileNameMap[key] = cacheFilePath;
173  m_recentlyUsedKeys.push_back(key);
174 
175  return true;
176  }
177  }
178 
179  return false;
180 }
181 
182 
183 template <class Key, class CacheObject>
185 {
186  int maxCachedFiles = GetMaxCachedFilesCount();
187  Q_ASSERT(maxCachedFiles >= 0);
188 
189  while (int(m_recentlyUsedKeys.size()) > maxCachedFiles){
190  const QString& key = m_recentlyUsedKeys.front();
191 
192  typename KeyToFileNameMap::iterator foundIter = m_keyToFileNameMap.find(key);
193  if (foundIter != m_keyToFileNameMap.end()){
194  const QString& cacheFilePath = foundIter.value();
195 
196  if (remove(cacheFilePath.toLocal8Bit()) != 0){
197  OnCacheFileRemoved(key, cacheFilePath);
198  }
199 
200  m_keyToFileNameMap.erase(foundIter);
201  }
202 
203  m_recentlyUsedKeys.pop_front();
204  }
205 }
206 
207 
208 template <class Key, class CacheObject>
209 void TFileCachedProducerCompBase<Key, CacheObject>::OnCacheFileSaved(const Key& /*key*/, const QString& /*cacheFilePath*/)
210 {
211 }
212 
213 
214 template <class Key, class CacheObject>
215 void TFileCachedProducerCompBase<Key, CacheObject>::OnCacheFileRemoved(const Key& /*key*/, const QString& /*cacheFilePath*/)
216 {
217 }
218 
219 
220 } // namespace iproc
221 
222 
223 #endif // !iproc_TFileCachedProducerCompBase_included
224 
225 
virtual void OnCacheFileRemoved(const Key &key, const QString &cacheFilePath)
virtual int GetMaxCachedFilesCount() const =0
Get number of maximal cached files.
virtual void UnlockObject(const CacheObject *objectPtr)
End of accessing to cached element.
bool IsValid() const
Type * PopPtr()
virtual QString CalcCacheFilePath(const Key &key) const =0
Calculate unique file path of temporary file cache object.
TILockedProducer< Key, CacheObject > LockedProducerType
void CleanFileList()
Removes cache files if there is too many of them.
Type * GetPtr() const
Template interface for providers of cached data.
#define NULL
virtual const CacheObject * ProduceLockedObject(const Key &key)
Begin of accessing to cached element.
Template base implementation iproc::TILockedProducer caching (buffering) objects using file system...
virtual void OnCacheFileSaved(const Key &key, const QString &cacheFilePath)

© 2007-2011 Witold Gantzke and Kirill Lepskiy