server/hook.py
changeset 5082 d6fd82a5a4e8
parent 5072 072ae171aeb0
child 5093 8d073d2e089d
equal deleted inserted replaced
5052:c9dbd95333f7 5082:d6fd82a5a4e8
   448         return self.session.repo.config
   448         return self.session.repo.config
   449 
   449 
   450 set_log_methods(Operation, getLogger('cubicweb.session'))
   450 set_log_methods(Operation, getLogger('cubicweb.session'))
   451 
   451 
   452 
   452 
       
   453 def set_operation(session, datakey, value, opcls, **opkwargs):
       
   454     """Search for session.transaction_data[`datakey`] (expected to be a set):
       
   455 
       
   456     * if found, simply append `value`
       
   457 
       
   458     * else, initialize it to set([`value`]) and instantiate the given `opcls`
       
   459       operation class with additional keyword arguments.
       
   460 
       
   461     You should use this instead of creating on operation for each `value`,
       
   462     since handling operations becomes coslty on massive data import.
       
   463     """
       
   464     try:
       
   465         session.transaction_data[datakey].add(value)
       
   466     except KeyError:
       
   467         opcls(session, *opkwargs)
       
   468         session.transaction_data[datakey] = set((value,))
       
   469 
       
   470 
   453 class LateOperation(Operation):
   471 class LateOperation(Operation):
   454     """special operation which should be called after all possible (ie non late)
   472     """special operation which should be called after all possible (ie non late)
   455     operations
   473     operations
   456     """
   474     """
   457     def insert_index(self):
   475     def insert_index(self):
   527 class RQLPrecommitOperation(Operation):
   545 class RQLPrecommitOperation(Operation):
   528     def precommit_event(self):
   546     def precommit_event(self):
   529         execute = self.session.execute
   547         execute = self.session.execute
   530         for rql in self.rqls:
   548         for rql in self.rqls:
   531             execute(*rql)
   549             execute(*rql)
       
   550 
       
   551 
       
   552 class CleanupNewEidsCacheOp(SingleLastOperation):
       
   553     """on rollback of a insert query we have to remove from repository's
       
   554     type/source cache eids of entities added in that transaction.
       
   555 
       
   556     NOTE: querier's rqlst/solutions cache may have been polluted too with
       
   557     queries such as Any X WHERE X eid 32 if 32 has been rollbacked however
       
   558     generated queries are unpredictable and analysing all the cache probably
       
   559     too expensive. Notice that there is no pb when using args to specify eids
       
   560     instead of giving them into the rql string.
       
   561     """
       
   562 
       
   563     def rollback_event(self):
       
   564         """the observed connections pool has been rollbacked,
       
   565         remove inserted eid from repository type/source cache
       
   566         """
       
   567         try:
       
   568             self.session.repo.clear_caches(
       
   569                 self.session.transaction_data['neweids'])
       
   570         except KeyError:
       
   571             pass
       
   572 
       
   573 class CleanupDeletedEidsCacheOp(SingleLastOperation):
       
   574     """on commit of delete query, we have to remove from repository's
       
   575     type/source cache eids of entities deleted in that transaction.
       
   576     """
       
   577 
       
   578     def commit_event(self):
       
   579         """the observed connections pool has been rollbacked,
       
   580         remove inserted eid from repository type/source cache
       
   581         """
       
   582         try:
       
   583             self.session.repo.clear_caches(
       
   584                 self.session.transaction_data['pendingeids'])
       
   585         except KeyError:
       
   586             pass