server/sources/__init__.py
branchtls-sprint
changeset 1263 01152fffd593
parent 1251 af40e615dc89
child 1398 5fe84a5f7035
--- a/server/sources/__init__.py	Mon Apr 06 12:37:45 2009 +0200
+++ b/server/sources/__init__.py	Tue Apr 07 09:30:23 2009 +0200
@@ -1,14 +1,35 @@
 """cubicweb server sources support
 
 :organization: Logilab
-:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 __docformat__ = "restructuredtext en"
 
+from datetime import datetime, timedelta
 from logging import getLogger
 
 from cubicweb import set_log_methods
+from cubicweb.server.sqlutils import SQL_PREFIX
+
+
+class TimedCache(dict):
+    def __init__(self, ttlm, ttls=0):
+        # time to live in minutes
+        self.ttl = timedelta(0, ttlm*60 + ttls, 0)
+        
+    def __setitem__(self, key, value):
+        dict.__setitem__(self, key, (datetime.now(), value))
+        
+    def __getitem__(self, key):
+        return dict.__getitem__(self, key)[1]
+    
+    def clear_expired(self):
+        now_ = datetime.now()
+        ttl = self.ttl
+        for key, (timestamp, value) in self.items():
+            if now_ - timestamp > ttl:
+                del self[key]
 
 
 class AbstractSource(object):
@@ -58,7 +79,7 @@
         pass
     
     def __repr__(self):
-        return '<%s source>' % self.uri
+        return '<%s source @%#x>' % (self.uri, id(self))
 
     def __cmp__(self, other):
         """simple comparison function to get predictable source order, with the
@@ -117,8 +138,8 @@
     def eid2extid(self, eid, session=None):
         return self.repo.eid2extid(self, eid, session)
 
-    def extid2eid(self, value, etype, session=None, insert=True):
-        return self.repo.extid2eid(self, value, etype, session, insert)
+    def extid2eid(self, value, etype, session=None, **kwargs):
+        return self.repo.extid2eid(self, value, etype, session, **kwargs)
 
     PUBLIC_KEYS = ('adapter', 'uri')
     def remove_sensitive_information(self, sourcedef):
@@ -139,15 +160,18 @@
         if not myeids:
             return
         # delete relations referencing one of those eids
+        eidcolum = SQL_PREFIX + 'eid'
         for rschema in self.schema.relations():
             if rschema.is_final() or rschema.type == 'identity':
                 continue
             if rschema.inlined:
+                column = SQL_PREFIX + rschema.type
                 for subjtype in rschema.subjects():
+                    table = SQL_PREFIX + str(subjtype)
                     for objtype in rschema.objects(subjtype):
                         if self.support_entity(objtype):
-                            sql = 'UPDATE %s SET %s = NULL WHERE eid IN (%s);' % (
-                                subjtype, rschema.type, myeids)
+                            sql = 'UPDATE %s SET %s=NULL WHERE %s IN (%s);' % (
+                                table, column, eidcolum, myeids)
                             session.system_sql(sql)
                             break
                 continue