server/hook.py
changeset 3720 5376aaadd16b
parent 3660 7b41a6ba7400
child 3998 94cc7cad3d2d
equal deleted inserted replaced
3678:29f74716fd70 3720:5376aaadd16b
   243 
   243 
   244     def __call__(self):
   244     def __call__(self):
   245         eschema = self.schema.eschema(self._cw.describe(self.eidfrom)[0])
   245         eschema = self.schema.eschema(self._cw.describe(self.eidfrom)[0])
   246         execute = self._cw.unsafe_execute
   246         execute = self._cw.unsafe_execute
   247         for rel in self.subject_relations:
   247         for rel in self.subject_relations:
   248             if eschema.has_subject_relation(rel):
   248             if rel in eschema.subjrels:
   249                 execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   249                 execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   250                         'X %s R, NOT R %s P' % (self.rtype, rel, self.rtype),
   250                         'X %s R, NOT R %s P' % (self.rtype, rel, self.rtype),
   251                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   251                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   252         for rel in self.object_relations:
   252         for rel in self.object_relations:
   253             if eschema.has_object_relation(rel):
   253             if rel in eschema.objrels:
   254                 execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   254                 execute('SET R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   255                         'R %s X, NOT R %s P' % (self.rtype, rel, self.rtype),
   255                         'R %s X, NOT R %s P' % (self.rtype, rel, self.rtype),
   256                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   256                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   257 
   257 
   258 
   258 
   267 
   267 
   268     def __call__(self):
   268     def __call__(self):
   269         eschema = self.schema.eschema(self._cw.describe(self.eidfrom)[0])
   269         eschema = self.schema.eschema(self._cw.describe(self.eidfrom)[0])
   270         execute = self._cw.unsafe_execute
   270         execute = self._cw.unsafe_execute
   271         for rel in self.subject_relations:
   271         for rel in self.subject_relations:
   272             if eschema.has_subject_relation(rel):
   272             if rel in eschema.subjrels:
   273                 execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   273                 execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   274                         'X %s R' % (self.rtype, rel),
   274                         'X %s R' % (self.rtype, rel),
   275                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   275                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   276         for rel in self.object_relations:
   276         for rel in self.object_relations:
   277             if eschema.has_object_relation(rel):
   277             if rel in eschema.objrels:
   278                 execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   278                 execute('DELETE R %s P WHERE X eid %%(x)s, P eid %%(p)s, '
   279                         'R %s X' % (self.rtype, rel),
   279                         'R %s X' % (self.rtype, rel),
   280                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   280                         {'x': self.eidfrom, 'p': self.eidto}, 'x')
   281 
   281 
   282 
   282 
   326 
   326 
   327     def insert_index(self):
   327     def insert_index(self):
   328         """return the index of  the lastest instance which is not a
   328         """return the index of  the lastest instance which is not a
   329         LateOperation instance
   329         LateOperation instance
   330         """
   330         """
   331         for i, op in enumerate(self.session.pending_operations):
   331         # faster by inspecting operation in reverse order for heavy transactions
       
   332         i = None
       
   333         for i, op in enumerate(reversed(self.session.pending_operations)):
   332             if isinstance(op, (LateOperation, SingleLastOperation)):
   334             if isinstance(op, (LateOperation, SingleLastOperation)):
   333                 return i
   335                 continue
   334         return None
   336             return -i or None
       
   337         if i is None:
       
   338             return None
       
   339         return -(i + 1)
   335 
   340 
   336     def handle_event(self, event):
   341     def handle_event(self, event):
   337         """delegate event handling to the opertaion"""
   342         """delegate event handling to the opertaion"""
   338         getattr(self, event)()
   343         getattr(self, event)()
   339 
   344 
   393     """
   398     """
   394     def insert_index(self):
   399     def insert_index(self):
   395         """return the index of  the lastest instance which is not a
   400         """return the index of  the lastest instance which is not a
   396         SingleLastOperation instance
   401         SingleLastOperation instance
   397         """
   402         """
   398         for i, op in enumerate(self.session.pending_operations):
   403         # faster by inspecting operation in reverse order for heavy transactions
       
   404         i = None
       
   405         for i, op in enumerate(reversed(self.session.pending_operations)):
   399             if isinstance(op, SingleLastOperation):
   406             if isinstance(op, SingleLastOperation):
   400                 return i
   407                 continue
   401         return None
   408             return -i or None
       
   409         if i is None:
       
   410             return None
       
   411         return -(i + 1)
   402 
   412 
   403 
   413 
   404 class SingleOperation(Operation):
   414 class SingleOperation(Operation):
   405     """special operation which should be called once"""
   415     """special operation which should be called once"""
   406     def register(self, session):
   416     def register(self, session):
   416         session.add_operation(self, self.insert_index())
   426         session.add_operation(self, self.insert_index())
   417         return equivalent
   427         return equivalent
   418 
   428 
   419     def equivalent_index(self, operations):
   429     def equivalent_index(self, operations):
   420         """return the index of the equivalent operation if any"""
   430         """return the index of the equivalent operation if any"""
   421         equivalents = [i for i, op in enumerate(operations)
   431         for i, op in enumerate(reversed(operations)):
   422                        if op.__class__ is self.__class__]
   432             if op.__class__ is self.__class__:
   423         if equivalents:
   433                 return -(i+1)
   424             return equivalents[0]
       
   425         return None
   434         return None
   426 
   435 
   427 
   436 
   428 class SingleLastOperation(SingleOperation):
   437 class SingleLastOperation(SingleOperation):
   429     """special operation which should be called once and after all other
   438     """special operation which should be called once and after all other