server/session.py
branchstable
changeset 3553 76b4a177a018
parent 3275 5247789df541
child 3589 a5432f99f2d9
child 3986 cc29eddf51ad
equal deleted inserted replaced
3552:8facb3324170 3553:76b4a177a018
   148         if symetric:
   148         if symetric:
   149             self._update_entity_rel_cache_del(object, rtype, 'object', object)
   149             self._update_entity_rel_cache_del(object, rtype, 'object', object)
   150         else:
   150         else:
   151             self._update_entity_rel_cache_del(object, rtype, 'object', subject)
   151             self._update_entity_rel_cache_del(object, rtype, 'object', subject)
   152 
   152 
   153     def _rel_cache(self, eid, rtype, role):
   153     def _update_entity_rel_cache_add(self, eid, rtype, role, targeteid):
   154         try:
   154         try:
   155             entity = self.entity_cache(eid)
   155             entity = self.entity_cache(eid)
   156         except KeyError:
   156         except KeyError:
   157             return
   157             return
   158         return entity.relation_cached(rtype, role)
   158         rcache = entity.relation_cached(rtype, role)
   159 
       
   160     def _update_entity_rel_cache_add(self, eid, rtype, role, targeteid):
       
   161         rcache = self._rel_cache(eid, rtype, role)
       
   162         if rcache is not None:
   159         if rcache is not None:
   163             rset, entities = rcache
   160             rset, entities = rcache
       
   161             rset = rset.copy()
       
   162             entities = list(entities)
   164             rset.rows.append([targeteid])
   163             rset.rows.append([targeteid])
   165             if not isinstance(rset.description, list): # else description not set
   164             if not isinstance(rset.description, list): # else description not set
   166                 rset.description = list(rset.description)
   165                 rset.description = list(rset.description)
   167             rset.description.append([self.describe(targeteid)[0]])
   166             rset.description.append([self.describe(targeteid)[0]])
   168             targetentity = self.entity_from_eid(targeteid)
   167             targetentity = self.entity_from_eid(targeteid)
   170                 targetentity.rset = rset
   169                 targetentity.rset = rset
   171                 targetentity.row = rset.rowcount
   170                 targetentity.row = rset.rowcount
   172                 targetentity.col = 0
   171                 targetentity.col = 0
   173             rset.rowcount += 1
   172             rset.rowcount += 1
   174             entities.append(targetentity)
   173             entities.append(targetentity)
       
   174             entity._related_cache['%s_%s' % (rtype, role)] = (rset, tuple(entities))
   175 
   175 
   176     def _update_entity_rel_cache_del(self, eid, rtype, role, targeteid):
   176     def _update_entity_rel_cache_del(self, eid, rtype, role, targeteid):
   177         rcache = self._rel_cache(eid, rtype, role)
   177         try:
       
   178             entity = self.entity_cache(eid)
       
   179         except KeyError:
       
   180             return
       
   181         rcache = entity.relation_cached(rtype, role)
   178         if rcache is not None:
   182         if rcache is not None:
   179             rset, entities = rcache
   183             rset, entities = rcache
   180             for idx, row in enumerate(rset.rows):
   184             for idx, row in enumerate(rset.rows):
   181                 if row[0] == targeteid:
   185                 if row[0] == targeteid:
   182                     break
   186                     break
   184                 # this may occurs if the cache has been filed by a hook
   188                 # this may occurs if the cache has been filed by a hook
   185                 # after the database update
   189                 # after the database update
   186                 self.debug('cache inconsistency for %s %s %s %s', eid, rtype,
   190                 self.debug('cache inconsistency for %s %s %s %s', eid, rtype,
   187                            role, targeteid)
   191                            role, targeteid)
   188                 return
   192                 return
       
   193             rset = rset.copy()
       
   194             entities = list(entities)
   189             del rset.rows[idx]
   195             del rset.rows[idx]
   190             if isinstance(rset.description, list): # else description not set
   196             if isinstance(rset.description, list): # else description not set
   191                 del rset.description[idx]
   197                 del rset.description[idx]
   192             del entities[idx]
   198             del entities[idx]
   193             rset.rowcount -= 1
   199             rset.rowcount -= 1
       
   200             entity._related_cache['%s_%s' % (rtype, role)] = (rset, tuple(entities))
   194 
   201 
   195     # resource accessors ######################################################
   202     # resource accessors ######################################################
   196 
   203 
   197     def actual_session(self):
   204     def actual_session(self):
   198         """return the original parent session if any, else self"""
   205         """return the original parent session if any, else self"""