update transaction data api stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 11 Jun 2009 19:07:47 +0200
branchstable
changeset 2101 08003e0354a7
parent 2100 89b825cdec74
child 2102 268659907769
update transaction data api
devtools/fake.py
goa/__init__.py
server/hooks.py
server/querier.py
server/repository.py
server/schemahooks.py
server/sources/extlite.py
server/ssplanner.py
sobjects/hooks.py
sobjects/notification.py
sobjects/supervising.py
--- a/devtools/fake.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/devtools/fake.py	Thu Jun 11 19:07:47 2009 +0200
@@ -169,12 +169,12 @@
         self.user = user or FakeUser()
         self.is_internal_session = False
         self.is_super_session = self.user.eid == -1
-        self._query_data = {}
+        self.transaction_data = {}
 
     def execute(self, *args):
         pass
     def commit(self, *args):
-        self._query_data.clear()
+        self.transaction_data.clear()
     def close(self, *args):
         pass
     def system_sql(self, sql, args=None):
--- a/goa/__init__.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/goa/__init__.py	Thu Jun 11 19:07:47 2009 +0200
@@ -101,36 +101,36 @@
         # activate entity caching on the server side
 
         def set_entity_cache(self, entity):
-            self._query_data.setdefault('_eid_cache', {})[entity.eid] = entity
+            self.transaction_data.setdefault('_eid_cache', {})[entity.eid] = entity
 
         def entity_cache(self, eid):
-            return self._query_data['_eid_cache'][eid]
+            return self.transaction_data['_eid_cache'][eid]
 
         def drop_entity_cache(self, eid=None):
             if eid is None:
-                self._query_data['_eid_cache'] = {}
-            elif '_eid_cache' in self._query_data:
-                self._query_data['_eid_cache'].pop(eid, None)
+                self.transaction_data['_eid_cache'] = {}
+            elif '_eid_cache' in self.transaction_data:
+                self.transaction_data['_eid_cache'].pop(eid, None)
 
         def datastore_get(self, key):
             if isinstance(key, basestring):
                 key = Key(key)
             try:
-                gentity = self._query_data['_key_cache'][key]
+                gentity = self.transaction_data['_key_cache'][key]
                 #self.critical('cached %s', gentity)
             except KeyError:
                 gentity = Get(key)
                 #self.critical('Get %s', gentity)
-                self._query_data.setdefault('_key_cache', {})[key] = gentity
+                self.transaction_data.setdefault('_key_cache', {})[key] = gentity
             return gentity
 
         def clear_datastore_cache(self, key=None):
             if key is None:
-                self._query_data['_key_cache'] = {}
+                self.transaction_data['_key_cache'] = {}
             else:
                 if isinstance(key, basestring):
                     key = Key(key)
-                self._query_data['_key_cache'].pop(key, None)
+                self.transaction_data['_key_cache'].pop(key, None)
 
         from cubicweb.server.session import Session
         Session.set_entity_cache = set_entity_cache
--- a/server/hooks.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/server/hooks.py	Thu Jun 11 19:07:47 2009 +0200
@@ -18,7 +18,8 @@
 from cubicweb.server.repository import FTIndexEntityOp
 
 def relation_deleted(session, eidfrom, rtype, eidto):
-    session.add_query_data('pendingrelations', (eidfrom, rtype, eidto))
+    session.transaction_data.setdefault('pendingrelations', []).append(
+        (eidfrom, rtype, eidto))
 
 
 # base meta-data handling #####################################################
@@ -41,7 +42,7 @@
 class SetCreatorOp(PreCommitOperation):
 
     def precommit_event(self):
-        if self.eid in self.session.query_data('pendingeids', ()):
+        if self.eid in self.session.transaction_data.get('pendingeids', ()):
             # entity have been created and deleted in the same transaction
             return
         ueid = self.session.user.eid
@@ -138,7 +139,7 @@
 
     def precommit_event(self):
         session = self.session
-        if not self.eid in session.query_data('pendingeids', ()):
+        if not self.eid in session.transaction_data.get('pendingeids', ()):
             etype = session.describe(self.eid)[0]
             session.unsafe_execute('DELETE %s X WHERE X eid %%(x)s, NOT %s'
                                    % (etype, self.relation),
@@ -166,7 +167,7 @@
         eidfrom, rtype, eidto = self.rdef
         # first check related entities have not been deleted in the same
         # transaction
-        pending = self.session.query_data('pendingeids', ())
+        pending = self.session.transaction_data.get('pendingeids', ())
         if eidfrom in pending:
             return
         if eidto in pending:
@@ -217,7 +218,7 @@
 
     def precommit_event(self):
         # recheck pending eids
-        if self.eid in self.session.query_data('pendingeids', ()):
+        if self.eid in self.session.transaction_data.get('pendingeids', ()):
             return
         if self.session.unsafe_execute(*self._rql()).rowcount < 1:
             etype = self.session.describe(self.eid)[0]
@@ -274,7 +275,7 @@
 def cardinalitycheck_before_del_relation(session, eidfrom, rtype, eidto):
     """check cardinalities are satisfied"""
     card = rproperty(session, rtype, eidfrom, eidto, 'cardinality')
-    pendingeids = session.query_data('pendingeids', ())
+    pendingeids = session.transaction_data.get('pendingeids', ())
     if card[0] in '1+' and not eidfrom in pendingeids:
         checkrel_if_necessary(session, CheckSRelationOp, rtype, eidfrom)
     if card[1] in '1+' and not eidto in pendingeids:
@@ -423,7 +424,7 @@
                                {'name': str(entity.e_schema)})
         # if there is an initial state and the entity's state is not set,
         # use the initial state as a default state
-        pendingeids = session.query_data('pendingeids', ())
+        pendingeids = session.transaction_data.get('pendingeids', ())
         if rset and not entity.eid in pendingeids and not entity.in_state:
             session.unsafe_execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
                                    {'x' : entity.eid, 's' : rset[0][0]}, 'x')
@@ -505,7 +506,7 @@
                           key=key, value=value)
 
 def before_del_eproperty(session, eid):
-    for eidfrom, rtype, eidto in session.query_data('pendingrelations', ()):
+    for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()):
         if rtype == 'for_user' and eidfrom == eid:
             # if for_user was set, delete has already been handled
             break
--- a/server/querier.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/server/querier.py	Thu Jun 11 19:07:47 2009 +0200
@@ -298,7 +298,7 @@
         localchecks = {}
         if rqlst.where is not None:
             varkwargs = var_kwargs(rqlst.where, self.args)
-            neweids = self.session.query_data('neweids', ())
+            neweids = self.session.transaction_data.get('neweids', ())
         else:
             varkwargs = None
         restricted_vars = set()
--- a/server/repository.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/server/repository.py	Thu Jun 11 19:07:47 2009 +0200
@@ -60,13 +60,19 @@
         """the observed connections pool has been rollbacked,
         remove inserted eid from repository type/source cache
         """
-        self.repo.clear_caches(self.session.query_data('pendingeids', ()))
+        try:
+            self.repo.clear_caches(self.session.transaction_data['pendingeids'])
+        except KeyError:
+            pass
 
     def rollback_event(self):
         """the observed connections pool has been rollbacked,
         remove inserted eid from repository type/source cache
         """
-        self.repo.clear_caches(self.session.query_data('neweids', ()))
+        try:
+            self.repo.clear_caches(self.session.transaction_data['neweids'])
+        except KeyError:
+            pass
 
 
 class FTIndexEntityOp(LateOperation):
@@ -80,7 +86,7 @@
     def precommit_event(self):
         session = self.session
         entity = self.entity
-        if entity.eid in session.query_data('pendingeids', ()):
+        if entity.eid in session.transaction_data.get('pendingeids', ()):
             return # entity added and deleted in the same transaction
         session.repo.system_source.fti_unindex_entity(session, entity.eid)
         for container in entity.fti_containers():
@@ -864,7 +870,8 @@
         self.system_source.add_info(session, entity, source, extid)
         if complete:
             entity.complete(entity.e_schema.indexable_attributes())
-        session.add_query_data('neweids', entity.eid)
+        new = session.transaction_data.setdefault('neweids', set())
+        new.add(entity.eid)
         # now we can update the full text index
         if self.do_fti:
             FTIndexEntityOp(session, entity=entity)
@@ -881,7 +888,7 @@
         * setup cache update operation
         """
         self.system_source.fti_unindex_entity(session, eid)
-        pending = session.query_data('pendingeids', set(), setdefault=True)
+        pending = session.transaction_data.setdefault('pendingeids', set())
         pending.add(eid)
         CleanupEidTypeCacheOp(session)
 
@@ -918,7 +925,7 @@
 
     def index_entity(self, session, entity):
         """full text index a modified entity"""
-        alreadydone = session.query_data('indexedeids', set(), setdefault=True)
+        alreadydone = session.transaction_data.setdefault('indexedeids', set())
         if entity.eid in alreadydone:
             self.info('skipping reindexation of %s, already done', entity.eid)
             return
--- a/server/schemahooks.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/server/schemahooks.py	Thu Jun 11 19:07:47 2009 +0200
@@ -37,7 +37,7 @@
 
 def get_constraints(session, entity):
     constraints = []
-    for cstreid in session.query_data(entity.eid, ()):
+    for cstreid in session.transaction_data.get(entity.eid, ()):
         cstrent = session.entity(cstreid)
         cstr = CONSTRAINTS[cstrent.type].deserialize(cstrent.value)
         cstr.eid = cstreid
@@ -62,7 +62,8 @@
     # is done by the dbhelper)
     session.pool.source('system').create_index(session, table, column)
     session.info('added index on %s(%s)', table, column)
-    session.add_query_data('createdattrs', '%s.%s' % (etype, rtype))
+    session.transaction_data.setdefault('createdattrs', []).append(
+        '%s.%s' % (etype, rtype))
 
 
 class SchemaOperation(Operation):
@@ -107,8 +108,8 @@
     """actually remove a database from the application's schema"""
     table = None # make pylint happy
     def precommit_event(self):
-        dropped = self.session.query_data('droppedtables',
-                                          default=set(), setdefault=True)
+        dropped = self.session.transaction_data.setdefault('droppedtables',
+                                                           set())
         if self.table in dropped:
             return # already processed
         dropped.add(self.table)
@@ -208,7 +209,7 @@
     * delete the associated relation type when necessary
     """
     subjschema, rschema, objschema = session.repo.schema.schema_by_eid(rdefeid)
-    pendings = session.query_data('pendingeids', ())
+    pendings = session.transaction_data.get('pendingeids', ())
     # first delete existing relation if necessary
     if rschema.is_final():
         rdeftype = 'CWAttribute'
@@ -472,14 +473,14 @@
             except KeyError:
                 alreadythere = False
             if not (alreadythere or
-                    key in session.query_data('createdattrs', ())):
+                    key in session.transaction_data.get('createdattrs', ())):
                 add_inline_relation_column(session, subj, rtype)
         else:
             # need to create the relation if no relation definition in the
             # schema and if it has not been added during other event of the same
             # transaction
             if not (rschema.subjects() or
-                    rtype in session.query_data('createdtables', ())):
+                    rtype in session.transaction_data.get('createdtables', ())):
                 try:
                     rschema = schema[rtype]
                     tablesql = rschema2sql(rschema)
@@ -494,7 +495,8 @@
                 for sql in tablesql.split(';'):
                     if sql.strip():
                         self.session.system_sql(sql)
-                session.add_query_data('createdtables', rtype)
+                session.transaction_data.setdefault('createdtables', []).append(
+                    rtype)
 
 def after_add_enfrdef(session, entity):
     AddCWRelationPreCommitOp(session, entity=entity)
@@ -620,13 +622,14 @@
         if not inlined:
             # need to create the relation if it has not been already done by another
             # event of the same transaction
-            if not rschema.type in session.query_data('createdtables', ()):
+            if not rschema.type in session.transaction_data.get('createdtables', ()):
                 tablesql = rschema2sql(rschema)
                 # create the necessary table
                 for sql in tablesql.split(';'):
                     if sql.strip():
                         sqlexec(sql)
-                session.add_query_data('createdtables', rschema.type)
+                session.transaction_data.setdefault('createdtables', []).append(
+                    rschema.type)
             # copy existant data
             column = SQL_PREFIX + rtype
             for etype in rschema.subjects():
@@ -697,7 +700,7 @@
         session = self.session
         # when the relation is added in the same transaction, the constraint object
         # is created by AddEN?FRDefPreCommitOp, there is nothing to do here
-        if rdef.eid in session.query_data('neweids', ()):
+        if rdef.eid in session.transaction_data.get('neweids', ()):
             self.cancelled = True
             return
         self.cancelled = False
@@ -773,7 +776,7 @@
 
 
 def before_delete_constrained_by(session, fromeid, rtype, toeid):
-    if not fromeid in session.query_data('pendingeids', ()):
+    if not fromeid in session.transaction_data.get('pendingeids', ()):
         schema = session.repo.schema
         entity = session.eid_rset(toeid).get_entity(0, 0)
         subjtype, rtype, objtype = schema.schema_by_eid(fromeid)
@@ -786,8 +789,8 @@
 
 
 def after_add_constrained_by(session, fromeid, rtype, toeid):
-    if fromeid in session.query_data('neweids', ()):
-        session.add_query_data(fromeid, toeid)
+    if fromeid in session.transaction_data.get('neweids', ()):
+        session.transaction_data.setdefault(fromeid, []).append(toeid)
 
 
 # schema permissions synchronization ##########################################
@@ -908,7 +911,7 @@
 
     skip the operation if the related type is being deleted
     """
-    if subject in session.query_data('pendingeids', ()):
+    if subject in session.transaction_data.get('pendingeids', ()):
         return
     perm = rtype.split('_', 1)[0]
     if session.describe(object)[0] == 'CWGroup':
--- a/server/sources/extlite.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/server/sources/extlite.py	Thu Jun 11 19:07:47 2009 +0200
@@ -243,7 +243,7 @@
         """delete a relation from the source"""
         rschema = self.schema.rschema(rtype)
         if rschema.inlined:
-            if subject in session.query_data('pendingeids', ()):
+            if subject in session.transaction_data.get('pendingeids', ()):
                 return
             table = SQL_PREFIX + session.describe(subject)[0]
             column = SQL_PREFIX + rtype
--- a/server/ssplanner.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/server/ssplanner.py	Thu Jun 11 19:07:47 2009 +0200
@@ -440,7 +440,7 @@
         session = self.plan.session
         delete = session.repo.glob_delete_entity
         # register pending eids first to avoid multiple deletion
-        pending = session.query_data('pendingeids', set(), setdefault=True)
+        pending = session.transaction_data.setdefault('pendingeids', set())
         actual = todelete - pending
         pending |= actual
         for eid in actual:
--- a/sobjects/hooks.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/sobjects/hooks.py	Thu Jun 11 19:07:47 2009 +0200
@@ -26,7 +26,7 @@
     beid = None # make pylint happy
     def precommit_event(self):
         session = self.session
-        if not self.beid in session.query_data('pendingeids', ()):
+        if not self.beid in session.transaction_data.get('pendingeids', ()):
             if not session.unsafe_execute('Any X WHERE X bookmarked_by U, X eid %(x)s',
                                           {'x': self.beid}, 'x'):
                 session.unsafe_execute('DELETE Bookmark X WHERE X eid %(x)s',
--- a/sobjects/notification.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/sobjects/notification.py	Thu Jun 11 19:07:47 2009 +0200
@@ -6,6 +6,7 @@
 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
 """
 __docformat__ = "restructuredtext en"
+_ = unicode
 
 from base64 import b64encode, b64decode
 from itertools import repeat
@@ -28,7 +29,6 @@
 from cubicweb.server.hookhelper import SendMailOp
 from cubicweb.server.hooksmanager import Hook
 
-_ = unicode
 
 class RecipientsFinder(Component):
     """this component is responsible to find recipients of a notification
--- a/sobjects/supervising.py	Thu Jun 11 19:04:20 2009 +0200
+++ b/sobjects/supervising.py	Thu Jun 11 19:07:47 2009 +0200
@@ -36,7 +36,8 @@
                 # don't record last_login_time update which are done
                 # automatically at login time
                 return False
-        self.session.add_query_data('pendingchanges', (self._event(), args))
+        self.session.transaction_data.setdefault('pendingchanges', []).append(
+            (self._event(), args))
         return True
 
     def _event(self):
@@ -54,10 +55,8 @@
             # may raise an error during deletion process, for instance due to
             # missing required relation
             title = '#%s' % eid
-        self.session.add_query_data('pendingchanges',
-                                    ('delete_entity',
-                                     (eid, str(entity.e_schema),
-                                      title)))
+        self.session.transaction_data.setdefault('pendingchanges', []).append(
+            ('delete_entity', (eid, str(entity.e_schema), title)))
         return True