-rw-r--r-- | libopie/pim/obackendfactory.h | 20 | ||||
-rw-r--r-- | libopie/pim/opimaccessbackend.h | 40 | ||||
-rw-r--r-- | libopie/pim/opimaccesstemplate.h | 57 | ||||
-rw-r--r-- | libopie/pim/opimcache.h | 117 | ||||
-rw-r--r-- | libopie/pim/orecordlist.h | 20 | ||||
-rw-r--r-- | libopie/pim/otemplatebase.h | 11 |
6 files changed, 248 insertions, 17 deletions
diff --git a/libopie/pim/obackendfactory.h b/libopie/pim/obackendfactory.h index f11f029..89b8c58 100644 --- a/libopie/pim/obackendfactory.h +++ b/libopie/pim/obackendfactory.h | |||
@@ -18,2 +18,9 @@ | |||
18 | * $Log$ | 18 | * $Log$ |
19 | * Revision 1.3 2002/10/10 17:08:58 zecke | ||
20 | * The Cache is finally in place | ||
21 | * I tested it with my todolist and it 'works' for 10.000 todos the hits are awesome ;) | ||
22 | * The read ahead functionality does not make sense for XMLs backends because most of the stuff is already in memory. While using readahead on SQL makes things a lot faster.... | ||
23 | * I still have to fully implement read ahead | ||
24 | * This change is bic but sc | ||
25 | * | ||
19 | * Revision 1.2 2002/10/08 09:27:36 eilers | 26 | * Revision 1.2 2002/10/08 09:27:36 eilers |
@@ -30,4 +37,4 @@ | |||
30 | */ | 37 | */ |
31 | #ifndef __OPIE_BACKENDFACTORY_H_ | 38 | #ifndef OPIE_BACKENDFACTORY_H_ |
32 | #define __OPIE_BACKENDFACTORY_H_ | 39 | #define OPIE_BACKENDFACTORY_H_ |
33 | 40 | ||
@@ -40,5 +47,6 @@ | |||
40 | 47 | ||
41 | #ifdef __USE_SQL | 48 | /*#ifdef __USE_SQL |
42 | #include "otodoaccesssql.h" | 49 | #include "otodoaccesssql.h" |
43 | #endif | 50 | #endif |
51 | */ | ||
44 | 52 | ||
@@ -74,9 +82,9 @@ class OBackendFactory | |||
74 | case TODO: | 82 | case TODO: |
75 | #ifdef __USE_SQL | 83 | /*#ifdef __USE_SQL |
76 | if ( backend == "sql" ) | 84 | if ( backend == "sql" ) |
77 | return (T*) new OTodoAccessBackendSQL(""); | 85 | return (T*) new OTodoAccessBackendSQL(""); |
78 | #else | 86 | #else*/ |
79 | if ( backend == "sql" ) | 87 | if ( backend == "sql" ) |
80 | qWarning ("OBackendFactory:: sql Backend not implemented! Using XML instead!"); | 88 | qWarning ("OBackendFactory:: sql Backend not implemented! Using XML instead!"); |
81 | #endif | 89 | //#endif |
82 | 90 | ||
diff --git a/libopie/pim/opimaccessbackend.h b/libopie/pim/opimaccessbackend.h index c27acbb..27d3cb8 100644 --- a/libopie/pim/opimaccessbackend.h +++ b/libopie/pim/opimaccessbackend.h | |||
@@ -5,2 +5,3 @@ | |||
5 | 5 | ||
6 | #include <opie/otemplatebase.h> | ||
6 | #include <opie/opimrecord.h> | 7 | #include <opie/opimrecord.h> |
@@ -19,2 +20,3 @@ class OPimAccessBackend { | |||
19 | public: | 20 | public: |
21 | typedef OTemplateBase<T> Frontend; | ||
20 | OPimAccessBackend(); | 22 | OPimAccessBackend(); |
@@ -56,2 +58,4 @@ public: | |||
56 | 58 | ||
59 | virtual T find(int uid, const QArray<int>& items, | ||
60 | uint current, Frontend::CacheDirection )const ; | ||
57 | /** | 61 | /** |
@@ -76,2 +80,17 @@ public: | |||
76 | 80 | ||
81 | /* | ||
82 | * setTheFrontEnd!!! | ||
83 | */ | ||
84 | void setFrontend( Frontend* front ); | ||
85 | |||
86 | protected: | ||
87 | void cache( const T& t )const; | ||
88 | |||
89 | /** | ||
90 | * use a prime number here! | ||
91 | */ | ||
92 | void setSaneCacheSize( int ); | ||
93 | |||
94 | private: | ||
95 | Frontend* m_front; | ||
77 | 96 | ||
@@ -81,3 +100,3 @@ template <class T> | |||
81 | OPimAccessBackend<T>::OPimAccessBackend() { | 100 | OPimAccessBackend<T>::OPimAccessBackend() { |
82 | 101 | m_front = 0l; | |
83 | } | 102 | } |
@@ -87,2 +106,21 @@ OPimAccessBackend<T>::~OPimAccessBackend() { | |||
87 | } | 106 | } |
107 | template <class T> | ||
108 | void OPimAccessBackend<T>::setFrontend( Frontend* fr ) { | ||
109 | m_front = fr; | ||
110 | } | ||
111 | template <class T> | ||
112 | void OPimAccessBackend<T>::cache( const T& t )const { | ||
113 | if (m_front ) | ||
114 | m_front->cache( t ); | ||
115 | } | ||
116 | template <class T> | ||
117 | void OPimAccessBackend<T>::setSaneCacheSize( int size) { | ||
118 | if (m_front ) | ||
119 | m_front->setSaneCacheSize( size ); | ||
120 | } | ||
121 | template <class T> | ||
122 | T OPimAccessBackend<T>::find( int uid, const QArray<int>&, | ||
123 | uint, Frontend::CacheDirection )const { | ||
124 | return find( uid ); | ||
125 | } | ||
88 | 126 | ||
diff --git a/libopie/pim/opimaccesstemplate.h b/libopie/pim/opimaccesstemplate.h index 31ab516..92d7192 100644 --- a/libopie/pim/opimaccesstemplate.h +++ b/libopie/pim/opimaccesstemplate.h | |||
@@ -9,2 +9,3 @@ | |||
9 | 9 | ||
10 | #include "opimcache.h" | ||
10 | #include "otemplatebase.h" | 11 | #include "otemplatebase.h" |
@@ -25,2 +26,3 @@ public: | |||
25 | typedef OPimAccessBackend<T> BackEnd; | 26 | typedef OPimAccessBackend<T> BackEnd; |
27 | typedef OPimCache<T> Cache; | ||
26 | 28 | ||
@@ -75,2 +77,8 @@ public: | |||
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 */ |
@@ -101,2 +109,8 @@ public: | |||
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: |
@@ -113,2 +127,3 @@ protected: | |||
113 | BackEnd* m_backEnd; | 127 | BackEnd* m_backEnd; |
128 | Cache m_cache; | ||
114 | 129 | ||
@@ -120,3 +135,4 @@ OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) | |||
120 | { | 135 | { |
121 | 136 | if (end ) | |
137 | end->setFrontend( this ); | ||
122 | } | 138 | } |
@@ -129,2 +145,3 @@ 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(); |
@@ -156,2 +173,22 @@ 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; |
@@ -165,2 +202,3 @@ 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 ); |
@@ -169,3 +207,3 @@ 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 | } |
@@ -173,2 +211,3 @@ 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 ); |
@@ -177,2 +216,3 @@ 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 ); |
@@ -181,3 +221,3 @@ template <class T> | |||
181 | void OPimAccessTemplate<T>::invalidateCache() { | 221 | void OPimAccessTemplate<T>::invalidateCache() { |
182 | 222 | m_cache.invalidate(); | |
183 | } | 223 | } |
@@ -194,2 +234,13 @@ 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 | } |
diff --git a/libopie/pim/opimcache.h b/libopie/pim/opimcache.h new file mode 100644 index 0000000..067f6e7 --- a/dev/null +++ b/libopie/pim/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/libopie/pim/orecordlist.h b/libopie/pim/orecordlist.h index b6fa7fa..08f5c85 100644 --- a/libopie/pim/orecordlist.h +++ b/libopie/pim/orecordlist.h | |||
@@ -70,2 +70,3 @@ private: | |||
70 | T m_record; | 70 | T m_record; |
71 | bool m_direction :1; | ||
71 | 72 | ||
@@ -127,2 +128,4 @@ ORecordListIterator<T>::ORecordListIterator() { | |||
127 | m_record = T(); | 128 | m_record = T(); |
129 | /* forward */ | ||
130 | m_direction = TRUE; | ||
128 | } | 131 | } |
@@ -141,2 +144,3 @@ ORecordListIterator<T>::ORecordListIterator( const ORecordListIterator<T>& it) { | |||
141 | m_record = it.m_record; | 144 | m_record = it.m_record; |
145 | m_direction = it.m_direction; | ||
142 | } | 146 | } |
@@ -158,7 +162,5 @@ T ORecordListIterator<T>::operator*() { | |||
158 | if (!m_end ) | 162 | if (!m_end ) |
159 | /* FIXME | 163 | m_record = m_temp->find( m_uids[m_current], m_uids, m_current, |
160 | * until the cache is in place | 164 | m_direction ? Base::Forward : |
161 | * we do the uid match uid check | 165 | Base::Reverse ); |
162 | */ | ||
163 | m_record = m_temp->find( m_uids[m_current] ); | ||
164 | else | 166 | else |
@@ -171,2 +173,3 @@ template <class T> | |||
171 | ORecordListIterator<T> &ORecordListIterator<T>::operator++() { | 173 | ORecordListIterator<T> &ORecordListIterator<T>::operator++() { |
174 | m_direction = true; | ||
172 | if (m_current < m_uids.count() ) { | 175 | if (m_current < m_uids.count() ) { |
@@ -181,2 +184,3 @@ template <class T> | |||
181 | ORecordListIterator<T> &ORecordListIterator<T>::operator--() { | 184 | ORecordListIterator<T> &ORecordListIterator<T>::operator--() { |
185 | m_direction = false; | ||
182 | if ( m_current > 0 ) { | 186 | if ( m_current > 0 ) { |
@@ -209,3 +213,4 @@ ORecordListIterator<T>::ORecordListIterator( const QArray<int> uids, | |||
209 | const Base* t ) | 213 | const Base* t ) |
210 | : m_uids( uids ), m_current( 0 ), m_temp( t ), m_end( false ) | 214 | : m_uids( uids ), m_current( 0 ), m_temp( t ), m_end( false ), |
215 | m_direction( false ) | ||
211 | { | 216 | { |
@@ -256,3 +261,4 @@ template <class T> | |||
256 | T ORecordList<T>::operator[]( uint i ) { | 261 | T ORecordList<T>::operator[]( uint i ) { |
257 | return m_acc->find( m_ids[i] ); | 262 | /* forward */ |
263 | return m_acc->find( m_ids[i], m_ids, i ); | ||
258 | } | 264 | } |
diff --git a/libopie/pim/otemplatebase.h b/libopie/pim/otemplatebase.h index f71417b..b855919 100644 --- a/libopie/pim/otemplatebase.h +++ b/libopie/pim/otemplatebase.h | |||
@@ -3,2 +3,4 @@ | |||
3 | 3 | ||
4 | #include <qarray.h> | ||
5 | |||
4 | #include "opimrecord.h" | 6 | #include "opimrecord.h" |
@@ -11,2 +13,3 @@ class OTemplateBase { | |||
11 | public: | 13 | public: |
14 | enum CacheDirection { Forward=0, Reverse }; | ||
12 | OTemplateBase() { | 15 | OTemplateBase() { |
@@ -17,2 +20,10 @@ public: | |||
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 | }; |