240 self.pending_operations = [] |
240 self.pending_operations = [] |
241 #: (None, 'precommit', 'postcommit', 'uncommitable') |
241 #: (None, 'precommit', 'postcommit', 'uncommitable') |
242 self.commit_state = None |
242 self.commit_state = None |
243 self.pruned_hooks_cache = {} |
243 self.pruned_hooks_cache = {} |
244 |
244 |
|
245 # Entity cache management ################################################# |
|
246 # |
|
247 # The transaction entity cache as held in tx.data it is removed at end the |
|
248 # end of the transaction (commit and rollback) |
|
249 # |
|
250 # XXX transaction level caching may be a pb with multiple repository |
|
251 # instances, but 1. this is probably not the only one :$ and 2. it may be |
|
252 # an acceptable risk. Anyway we could activate it or not according to a |
|
253 # configuration option |
|
254 |
|
255 def set_entity_cache(self, entity): |
|
256 """Add `entity` to the transaction entity cache""" |
|
257 try: |
|
258 self.data['ecache'].setdefault(entity.eid, entity) |
|
259 except KeyError: |
|
260 self.data['ecache'] = ecache = {} |
|
261 ecache[entity.eid] = entity |
|
262 |
|
263 def entity_cache(self, eid): |
|
264 """get cache entity for `eid`""" |
|
265 return self.data['ecache'][eid] |
|
266 |
|
267 def cached_entities(self): |
|
268 """return the whole entity cache""" |
|
269 return self.data.get('ecache', {}).values() |
|
270 |
|
271 def drop_entity_cache(self, eid=None): |
|
272 """drop entity from the cache |
|
273 |
|
274 If eid is None, the whole cache is dropped""" |
|
275 if eid is None: |
|
276 self.data.pop('ecache', None) |
|
277 else: |
|
278 del self.data['ecache'][eid] |
|
279 |
|
280 |
245 |
281 |
246 def tx_attr(attr_name): |
282 def tx_attr(attr_name): |
247 """return a property to forward attribute access to transaction. |
283 """return a property to forward attribute access to transaction. |
248 |
284 |
249 This is to be used by session""" |
285 This is to be used by session""" |
250 @property |
286 @property |
251 def attr_from_tx(session): |
287 def attr_from_tx(session): |
252 return getattr(session._tx, attr_name) |
288 return getattr(session._tx, attr_name) |
253 return attr_from_tx |
289 return attr_from_tx |
|
290 |
|
291 def tx_meth(meth_name): |
|
292 """return a function forwarding calls to transaction. |
|
293 |
|
294 This is to be used by session""" |
|
295 def meth_from_tx(session, *args, **kwargs): |
|
296 return getattr(session._tx, meth_name)(*args, **kwargs) |
|
297 return meth_from_tx |
|
298 |
254 |
299 |
255 class Session(RequestSessionBase): |
300 class Session(RequestSessionBase): |
256 """Repository user session |
301 """Repository user session |
257 |
302 |
258 This tie all together: |
303 This tie all together: |
940 @property |
985 @property |
941 def cursor(self): |
986 def cursor(self): |
942 """return a rql cursor""" |
987 """return a rql cursor""" |
943 return self |
988 return self |
944 |
989 |
945 def set_entity_cache(self, entity): |
990 set_entity_cache = tx_meth('set_entity_cache') |
946 # XXX session level caching may be a pb with multiple repository |
991 entity_cache = tx_meth('entity_cache') |
947 # instances, but 1. this is probably not the only one :$ and 2. it |
992 cache_entities = tx_meth('cached_entities') |
948 # may be an acceptable risk. Anyway we could activate it or not |
993 drop_entity_cache = tx_meth('drop_entity_cache') |
949 # according to a configuration option |
|
950 try: |
|
951 self._tx.data['ecache'].setdefault(entity.eid, entity) |
|
952 except KeyError: |
|
953 self._tx.data['ecache'] = ecache = {} |
|
954 ecache[entity.eid] = entity |
|
955 |
|
956 def entity_cache(self, eid): |
|
957 return self._tx.data['ecache'][eid] |
|
958 |
|
959 def cached_entities(self): |
|
960 return self._tx.data.get('ecache', {}).values() |
|
961 |
|
962 def drop_entity_cache(self, eid=None): |
|
963 if eid is None: |
|
964 self._tx.data.pop('ecache', None) |
|
965 else: |
|
966 del self._tx.data['ecache'][eid] |
|
967 |
994 |
968 def from_controller(self): |
995 def from_controller(self): |
969 """return the id (string) of the controller issuing the request (no |
996 """return the id (string) of the controller issuing the request (no |
970 sense here, always return 'view') |
997 sense here, always return 'view') |
971 """ |
998 """ |