-rw-r--r-- | libopie2/opiepim/core/opimaccesstemplate.h | 57 | ||||
-rw-r--r-- | libopie2/opiepim/core/opimcache.h | 117 | ||||
-rw-r--r-- | libopie2/opiepim/core/otemplatebase.h | 11 |
3 files changed, 182 insertions, 3 deletions
diff --git a/libopie2/opiepim/core/opimaccesstemplate.h b/libopie2/opiepim/core/opimaccesstemplate.h index 31ab516..92d7192 100644 --- a/libopie2/opiepim/core/opimaccesstemplate.h +++ b/libopie2/opiepim/core/opimaccesstemplate.h | |||
@@ -4,12 +4,13 @@ | |||
4 | #include <qarray.h> | 4 | #include <qarray.h> |
5 | 5 | ||
6 | #include <opie/opimrecord.h> | 6 | #include <opie/opimrecord.h> |
7 | #include <opie/opimaccessbackend.h> | 7 | #include <opie/opimaccessbackend.h> |
8 | #include <opie/orecordlist.h> | 8 | #include <opie/orecordlist.h> |
9 | 9 | ||
10 | #include "opimcache.h" | ||
10 | #include "otemplatebase.h" | 11 | #include "otemplatebase.h" |
11 | 12 | ||
12 | /** | 13 | /** |
13 | * Thats the frontend to our OPIE PIM | 14 | * Thats the frontend to our OPIE PIM |
14 | * Library. Either you want to use it's | 15 | * Library. Either you want to use it's |
15 | * interface or you want to implement | 16 | * interface or you want to implement |
@@ -20,12 +21,13 @@ | |||
20 | 21 | ||
21 | template <class T = OPimRecord > | 22 | template <class T = OPimRecord > |
22 | class OPimAccessTemplate : public OTemplateBase<T> { | 23 | class OPimAccessTemplate : public OTemplateBase<T> { |
23 | public: | 24 | public: |
24 | typedef ORecordList<T> List; | 25 | typedef ORecordList<T> List; |
25 | typedef OPimAccessBackend<T> BackEnd; | 26 | typedef OPimAccessBackend<T> BackEnd; |
27 | typedef OPimCache<T> Cache; | ||
26 | 28 | ||
27 | /** | 29 | /** |
28 | * our sort order | 30 | * our sort order |
29 | * should be safe explaining | 31 | * should be safe explaining |
30 | */ | 32 | */ |
31 | enum SortOrder { WildCards = 0, IgnoreCase = 1, | 33 | enum SortOrder { WildCards = 0, IgnoreCase = 1, |
@@ -70,12 +72,18 @@ public: | |||
70 | 72 | ||
71 | /** | 73 | /** |
72 | * find the OPimRecord uid | 74 | * find the OPimRecord uid |
73 | */ | 75 | */ |
74 | virtual T find( int uid )const; | 76 | virtual T find( int uid )const; |
75 | 77 | ||
78 | /** | ||
79 | * read ahead cache find method ;) | ||
80 | */ | ||
81 | virtual T find( int uid, const QArray<int>&, | ||
82 | uint current, CacheDirection dir = Forward )const; | ||
83 | |||
76 | /* invalidate cache here */ | 84 | /* invalidate cache here */ |
77 | /** | 85 | /** |
78 | * clears the backend and invalidates the backend | 86 | * clears the backend and invalidates the backend |
79 | */ | 87 | */ |
80 | virtual void clear() ; | 88 | virtual void clear() ; |
81 | 89 | ||
@@ -96,40 +104,49 @@ public: | |||
96 | virtual bool remove( int uid ); | 104 | virtual bool remove( int uid ); |
97 | 105 | ||
98 | /** | 106 | /** |
99 | * replace T from backend | 107 | * replace T from backend |
100 | */ | 108 | */ |
101 | virtual bool replace( const T& t) ; | 109 | virtual bool replace( const T& t) ; |
110 | |||
111 | /** | ||
112 | * @internal | ||
113 | */ | ||
114 | void cache( const T& )const; | ||
115 | void setSaneCacheSize( int ); | ||
102 | protected: | 116 | protected: |
103 | /** | 117 | /** |
104 | * invalidate the cache | 118 | * invalidate the cache |
105 | */ | 119 | */ |
106 | void invalidateCache(); | 120 | void invalidateCache(); |
107 | 121 | ||
108 | void setBackEnd( BackEnd* end ); | 122 | void setBackEnd( BackEnd* end ); |
109 | /** | 123 | /** |
110 | * returns the backend | 124 | * returns the backend |
111 | */ | 125 | */ |
112 | BackEnd* backEnd(); | 126 | BackEnd* backEnd(); |
113 | BackEnd* m_backEnd; | 127 | BackEnd* m_backEnd; |
128 | Cache m_cache; | ||
114 | 129 | ||
115 | }; | 130 | }; |
116 | 131 | ||
117 | template <class T> | 132 | template <class T> |
118 | OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) | 133 | OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) |
119 | : OTemplateBase<T>(), m_backEnd( end ) | 134 | : OTemplateBase<T>(), m_backEnd( end ) |
120 | { | 135 | { |
121 | 136 | if (end ) | |
137 | end->setFrontend( this ); | ||
122 | } | 138 | } |
123 | template <class T> | 139 | template <class T> |
124 | OPimAccessTemplate<T>::~OPimAccessTemplate() { | 140 | OPimAccessTemplate<T>::~OPimAccessTemplate() { |
125 | qWarning("~OPimAccessTemplate<T>"); | 141 | qWarning("~OPimAccessTemplate<T>"); |
126 | delete m_backEnd; | 142 | delete m_backEnd; |
127 | } | 143 | } |
128 | template <class T> | 144 | template <class T> |
129 | bool OPimAccessTemplate<T>::load() { | 145 | bool OPimAccessTemplate<T>::load() { |
146 | invalidateCache(); | ||
130 | return m_backEnd->load(); | 147 | return m_backEnd->load(); |
131 | } | 148 | } |
132 | template <class T> | 149 | template <class T> |
133 | bool OPimAccessTemplate<T>::reload() { | 150 | bool OPimAccessTemplate<T>::reload() { |
134 | return m_backEnd->reload(); | 151 | return m_backEnd->reload(); |
135 | } | 152 | } |
@@ -151,46 +168,80 @@ OPimAccessTemplate<T>::queryByExample( const T& t, int sortOrder ) { | |||
151 | List lis(ints, this ); | 168 | List lis(ints, this ); |
152 | return lis; | 169 | return lis; |
153 | } | 170 | } |
154 | template <class T> | 171 | template <class T> |
155 | T OPimAccessTemplate<T>::find( int uid ) const{ | 172 | T OPimAccessTemplate<T>::find( int uid ) const{ |
156 | T t = m_backEnd->find( uid ); | 173 | T t = m_backEnd->find( uid ); |
174 | cache( t ); | ||
175 | return t; | ||
176 | } | ||
177 | template <class T> | ||
178 | T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar, | ||
179 | uint current, CacheDirection dir )const { | ||
180 | /* | ||
181 | * better do T.isEmpty() | ||
182 | * after a find this way we would | ||
183 | * avoid two finds in QCache... | ||
184 | */ | ||
185 | // qWarning("find it now %d", uid ); | ||
186 | if (m_cache.contains( uid ) ) { | ||
187 | qWarning("m cache contains %d", uid); | ||
188 | return m_cache.find( uid ); | ||
189 | } | ||
190 | |||
191 | T t = m_backEnd->find( uid, ar, current, dir ); | ||
192 | qWarning("found it and cache it now %d", uid); | ||
193 | cache( t ); | ||
157 | return t; | 194 | return t; |
158 | } | 195 | } |
159 | template <class T> | 196 | template <class T> |
160 | void OPimAccessTemplate<T>::clear() { | 197 | void OPimAccessTemplate<T>::clear() { |
161 | invalidateCache(); | 198 | invalidateCache(); |
162 | m_backEnd->clear(); | 199 | m_backEnd->clear(); |
163 | } | 200 | } |
164 | template <class T> | 201 | template <class T> |
165 | bool OPimAccessTemplate<T>::add( const T& t ) { | 202 | bool OPimAccessTemplate<T>::add( const T& t ) { |
203 | cache( t ); | ||
166 | return m_backEnd->add( t ); | 204 | return m_backEnd->add( t ); |
167 | } | 205 | } |
168 | template <class T> | 206 | template <class T> |
169 | bool OPimAccessTemplate<T>::remove( const T& t ) { | 207 | bool OPimAccessTemplate<T>::remove( const T& t ) { |
170 | return m_backEnd->remove( t.uid() ); | 208 | return remove( t.uid() ); |
171 | } | 209 | } |
172 | template <class T> | 210 | template <class T> |
173 | bool OPimAccessTemplate<T>::remove( int uid ) { | 211 | bool OPimAccessTemplate<T>::remove( int uid ) { |
212 | m_cache.remove( uid ); | ||
174 | return m_backEnd->remove( uid ); | 213 | return m_backEnd->remove( uid ); |
175 | } | 214 | } |
176 | template <class T> | 215 | template <class T> |
177 | bool OPimAccessTemplate<T>::replace( const T& t ) { | 216 | bool OPimAccessTemplate<T>::replace( const T& t ) { |
217 | m_cache.replace( t ); | ||
178 | return m_backEnd->replace( t ); | 218 | return m_backEnd->replace( t ); |
179 | } | 219 | } |
180 | template <class T> | 220 | template <class T> |
181 | void OPimAccessTemplate<T>::invalidateCache() { | 221 | void OPimAccessTemplate<T>::invalidateCache() { |
182 | 222 | m_cache.invalidate(); | |
183 | } | 223 | } |
184 | template <class T> | 224 | template <class T> |
185 | OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() { | 225 | OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() { |
186 | return m_backEnd; | 226 | return m_backEnd; |
187 | } | 227 | } |
188 | template <class T> | 228 | template <class T> |
189 | bool OPimAccessTemplate<T>::wasChangedExternally()const { | 229 | bool OPimAccessTemplate<T>::wasChangedExternally()const { |
190 | return false; | 230 | return false; |
191 | } | 231 | } |
192 | template <class T> | 232 | template <class T> |
193 | void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) { | 233 | void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) { |
194 | m_backEnd = end; | 234 | m_backEnd = end; |
235 | if (m_backEnd ) | ||
236 | m_backEnd->setFrontend( this ); | ||
237 | } | ||
238 | template <class T> | ||
239 | void OPimAccessTemplate<T>::cache( const T& t ) const{ | ||
240 | /* hacky we need to work around the const*/ | ||
241 | ((OPimAccessTemplate<T>*)this)->m_cache.add( t ); | ||
242 | } | ||
243 | template <class T> | ||
244 | void OPimAccessTemplate<T>::setSaneCacheSize( int size ) { | ||
245 | m_cache.setSize( size ); | ||
195 | } | 246 | } |
196 | #endif | 247 | #endif |
diff --git a/libopie2/opiepim/core/opimcache.h b/libopie2/opiepim/core/opimcache.h new file mode 100644 index 0000000..067f6e7 --- a/dev/null +++ b/libopie2/opiepim/core/opimcache.h | |||
@@ -0,0 +1,117 @@ | |||
1 | #ifndef OPIE_PIM_CACHE_H | ||
2 | #define OPIE_PIM_CACHE_H | ||
3 | |||
4 | #include <qintcache.h> | ||
5 | |||
6 | #include "opimrecord.h" | ||
7 | |||
8 | template <class T = OPimRecord> | ||
9 | class OPimCacheItem { | ||
10 | public: | ||
11 | OPimCacheItem( const T& t = T() ); | ||
12 | ~OPimCacheItem(); | ||
13 | |||
14 | T record()const; | ||
15 | void setRecord( const T& ); | ||
16 | private: | ||
17 | T m_t; | ||
18 | }; | ||
19 | |||
20 | /** | ||
21 | * OPimCache for caching the items | ||
22 | * We support adding, removing | ||
23 | * and finding | ||
24 | */ | ||
25 | template <class T = OPimRecord> | ||
26 | class OPimCache { | ||
27 | public: | ||
28 | typedef OPimCacheItem<T> Item; | ||
29 | OPimCache(); | ||
30 | ~OPimCache(); | ||
31 | |||
32 | bool contains(int uid)const; | ||
33 | void invalidate(); | ||
34 | void setSize( int size ); | ||
35 | |||
36 | T find(int uid )const; | ||
37 | void add( const T& ); | ||
38 | void remove( int uid ); | ||
39 | void replace( const T& ); | ||
40 | |||
41 | private: | ||
42 | QIntCache<Item> m_cache; | ||
43 | }; | ||
44 | |||
45 | // Implementation | ||
46 | template <class T> | ||
47 | OPimCacheItem<T>::OPimCacheItem( const T& t ) | ||
48 | : m_t(t) { | ||
49 | } | ||
50 | template <class T> | ||
51 | OPimCacheItem<T>::~OPimCacheItem() { | ||
52 | |||
53 | } | ||
54 | template <class T> | ||
55 | T OPimCacheItem<T>::record()const { | ||
56 | return m_t; | ||
57 | } | ||
58 | template <class T> | ||
59 | void OPimCacheItem<T>::setRecord( const T& t ) { | ||
60 | m_t = t; | ||
61 | } | ||
62 | // Cache | ||
63 | template <class T> | ||
64 | OPimCache<T>::OPimCache() { | ||
65 | m_cache.setAutoDelete( TRUE ); | ||
66 | } | ||
67 | template <class T> | ||
68 | OPimCache<T>::~OPimCache() { | ||
69 | |||
70 | } | ||
71 | template <class T> | ||
72 | bool OPimCache<T>::contains(int uid )const { | ||
73 | Item* it = m_cache.find( uid, FALSE ); | ||
74 | if (!it) | ||
75 | return false; | ||
76 | return true; | ||
77 | } | ||
78 | template <class T> | ||
79 | void OPimCache<T>::invalidate() { | ||
80 | m_cache.clear(); | ||
81 | } | ||
82 | template <class T> | ||
83 | void OPimCache<T>::setSize( int size ) { | ||
84 | m_cache.setMaxCost( size ); | ||
85 | } | ||
86 | template <class T> | ||
87 | T OPimCache<T>::find(int uid )const { | ||
88 | Item *it = m_cache.find( uid ); | ||
89 | if (it) | ||
90 | return it->record(); | ||
91 | return T(); | ||
92 | } | ||
93 | template <class T> | ||
94 | void OPimCache<T>::add( const T& t ) { | ||
95 | Item* it = 0l; | ||
96 | it = m_cache.find(t.uid(), FALSE ); | ||
97 | |||
98 | if (it ) | ||
99 | it->setRecord( t ); | ||
100 | |||
101 | it = new Item( t ); | ||
102 | if (!m_cache.insert( t.uid(), it ) ) | ||
103 | delete it; | ||
104 | } | ||
105 | template <class T> | ||
106 | void OPimCache<T>::remove( int uid ) { | ||
107 | m_cache.remove( uid ); | ||
108 | } | ||
109 | template <class T> | ||
110 | void OPimCache<T>::replace( const T& t) { | ||
111 | Item *it = m_cache.find( t.uid() ); | ||
112 | if ( it ) { | ||
113 | it->setRecord( t ); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | #endif | ||
diff --git a/libopie2/opiepim/core/otemplatebase.h b/libopie2/opiepim/core/otemplatebase.h index f71417b..b855919 100644 --- a/libopie2/opiepim/core/otemplatebase.h +++ b/libopie2/opiepim/core/otemplatebase.h | |||
@@ -1,21 +1,32 @@ | |||
1 | #ifndef OPIE_TEMPLATE_BASE_H | 1 | #ifndef OPIE_TEMPLATE_BASE_H |
2 | #define OPIE_TEMPLATE_BASE_H | 2 | #define OPIE_TEMPLATE_BASE_H |
3 | 3 | ||
4 | #include <qarray.h> | ||
5 | |||
4 | #include "opimrecord.h" | 6 | #include "opimrecord.h" |
5 | 7 | ||
6 | /** | 8 | /** |
7 | * internal template base | 9 | * internal template base |
8 | */ | 10 | */ |
9 | template <class T = OPimRecord> | 11 | template <class T = OPimRecord> |
10 | class OTemplateBase { | 12 | class OTemplateBase { |
11 | public: | 13 | public: |
14 | enum CacheDirection { Forward=0, Reverse }; | ||
12 | OTemplateBase() { | 15 | OTemplateBase() { |
13 | }; | 16 | }; |
14 | virtual ~OTemplateBase() { | 17 | virtual ~OTemplateBase() { |
15 | } | 18 | } |
16 | virtual T find( int uid )const = 0; | 19 | virtual T find( int uid )const = 0; |
17 | 20 | ||
21 | /** | ||
22 | * read ahead find | ||
23 | */ | ||
24 | virtual T find( int uid, const QArray<int>& items, | ||
25 | uint current, CacheDirection dir = Forward )const = 0; | ||
26 | virtual void cache( const T& )const = 0; | ||
27 | virtual void setSaneCacheSize( int ) = 0; | ||
28 | |||
18 | }; | 29 | }; |
19 | 30 | ||
20 | 31 | ||
21 | #endif | 32 | #endif |