ICF 3.0.5.47
Technical documentation of ICF Libraries
TMemCachedProducerComp.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#ifndef iproc_TMemCachedProducerComp_included
7#define iproc_TMemCachedProducerComp_included
8
9
10// Qt includes
11#include <QtCore/QList>
12
13// ICF includes
15
16
17namespace iproc
18{
19
20
25template <class Key, class CacheObject>
28 virtual public TILockedProducer<Key, CacheObject>
29{
30public:
33
34 I_BEGIN_BASE_COMPONENT(TMemCachedProducerComp);
35 I_REGISTER_INTERFACE(LockedProducerType);
36 I_TASSIGN(m_slaveCacheEngineCompPtr, "SlaveCacheEngine", "Slave cache engine providing access to cached object", true, "SlaveCacheEngine");
37 I_ASSIGN(m_maxCachedObjectsAttrPtr, "MaxCachedObjects", "Maximal number of cached objects", true, 20);
38 I_END_COMPONENT;
39
40 // reimplemented (iproc::TILockedProducer)
41 virtual const CacheObject* ProduceLockedObject(const Key& key);
42 virtual void UnlockObject(const CacheObject* objectPtr);
43
44protected:
48 void CleanElementList();
49
50private:
51 I_TREF(LockedProducerType, m_slaveCacheEngineCompPtr);
52 I_ATTR(int, m_maxCachedObjectsAttrPtr);
53
54 struct ListElement
55 {
56 bool operator==(const Key& key)
57 {
58 return this->key == key;
59 }
60
61 Key key;
62 const CacheObject* objectPtr;
63 int lockedCount;
64 };
65
66 typedef QList<ListElement> CachedList;
67 typedef QMap<const CacheObject*, typename CachedList::reverse_iterator> ObjectToListMap;
68
69 CachedList m_cachedList;
70 ObjectToListMap m_objectToListMap;
71};
72
73
74// public methods
75
76// reimplemented (iproc::TILockedProducer)
77
78template <class Key, class CacheObject>
80{
81 typename CachedList::iterator foundIter = std::find(m_cachedList.begin(), m_cachedList.end(), key);
82 if (foundIter != m_cachedList.end()){
83 foundIter->lockedCount++;
84
85 return foundIter->objectPtr;
86 }
87
88 if (m_slaveCacheEngineCompPtr != nullptr){
89 const CacheObject* objectPtr = m_slaveCacheEngineCompPtr->ProduceLockedObject(key);
90 if (objectPtr != nullptr){
91 m_cachedList.push_back(ListElement());
92
93 ListElement& element = m_cachedList.back();
94 m_objectToListMap[objectPtr] = m_cachedList.rbegin();
95
96 element.key = key;
97 element.objectPtr = objectPtr;
98 element.lockedCount = 1;
99
100 CleanElementList();
101
102 return objectPtr;
103 }
104 }
105
106 return nullptr;
107}
108
109
110template <class Key, class CacheObject>
112{
113 typename ObjectToListMap::iterator foundIter = m_objectToListMap.find(objectPtr);
114 Q_ASSERT(foundIter != m_objectToListMap.end()); // if locked is done correctly, this element must exist.
115
116 typename CachedList::reverse_iterator objectIter = foundIter.value();
117 Q_ASSERT(objectIter != m_cachedList.rend());
118
119 objectIter->lockedCount--;
120
121 CleanElementList();
122}
123
124
125// protected methods
126
127template <class Key, class CacheObject>
129{
130 int maxCachedObjects = qMax(0, *m_maxCachedObjectsAttrPtr);
131
132 typename CachedList::iterator iter = m_cachedList.begin();
133 while ( (int(m_cachedList.size()) > maxCachedObjects) &&
134 (iter != m_cachedList.end())){
135 Q_ASSERT(m_objectToListMap.find(iter->objectPtr) != m_objectToListMap.end()); // object is present in objects map
136
137 if (iter->lockedCount <= 0){
138 Q_ASSERT(m_slaveCacheEngineCompPtr != nullptr);
139
140 m_slaveCacheEngineCompPtr->UnlockObject(iter->objectPtr);
141
142 m_objectToListMap.erase(iter->objectPtr);
143 iter = m_cachedList.erase(iter);
144 }
145 else{
146 ++iter;
147 }
148 }
149
150 Q_ASSERT(m_cachedList.size() == m_objectToListMap.size()); // this both structures are coupled, the number of elements must be the same
151}
152
153
154} // namespace iproc
155
156
157#endif // !iproc_TMemCachedProducerComp_included
158
159
Base class for component implementation.
Template interface for providers of cached data.
Template implementation iproc::TILockedProducer buffering objects in memory cache.
void CleanElementList()
Remove elements from list if cumulated weight is above defined maximum.
virtual void UnlockObject(const CacheObject *objectPtr)
End of accessing to cached element.
TILockedProducer< Key, CacheObject > LockedProducerType
virtual const CacheObject * ProduceLockedObject(const Key &key)
Begin of accessing to cached element.
This namespace containes interfaces and implementation of data processing concepts.

© Witold Gantzke and Kirill Lepskiy