server/sources/native.py
branchstable
changeset 1952 8e19c813750d
parent 1792 9eadf34fd860
child 1954 9b20f3504af8
equal deleted inserted replaced
1951:f28e7f300d3f 1952:8e19c813750d
     1 """Adapters for native cubicweb sources.
     1 """Adapters for native cubicweb sources.
     2 
     2 
       
     3 Notes:
       
     4 * extid (aka external id, the primary key of an entity in the external source
       
     5   from which it comes from) are stored in a varchar column encoded as a base64
       
     6   string. This is because it should actually be Bytes but we want an index on
       
     7   it for fast querying.
       
     8   
     3 :organization: Logilab
     9 :organization: Logilab
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
    10 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
    11 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
    12 """
     7 __docformat__ = "restructuredtext en"
    13 __docformat__ = "restructuredtext en"
     8 
    14 
     9 from threading import Lock
    15 from threading import Lock
    10 from datetime import datetime
    16 from datetime import datetime
       
    17 from base64 import b64decode, b64encode
    11 
    18 
    12 from logilab.common.cache import Cache
    19 from logilab.common.cache import Cache
    13 from logilab.common.configuration import REQUIRED
    20 from logilab.common.configuration import REQUIRED
    14 from logilab.common.adbh import get_adv_func_helper
    21 from logilab.common.adbh import get_adv_func_helper
    15 
    22 
   454         except:
   461         except:
   455             assert session.pool, 'session has no pool set'
   462             assert session.pool, 'session has no pool set'
   456             raise UnknownEid(eid)
   463             raise UnknownEid(eid)
   457         if res is None:
   464         if res is None:
   458             raise UnknownEid(eid)
   465             raise UnknownEid(eid)
       
   466         if res[-1] is not None:
       
   467             if not isinstance(res, list):
       
   468                 res = list(res)
       
   469             res[-1] = b64decode(res[-1])
   459         return res
   470         return res
   460 
   471 
   461     def extid2eid(self, session, source, lid):
   472     def extid2eid(self, session, source, extid):
   462         """get eid from a local id. An eid is attributed if no record is found"""
   473         """get eid from an external id. Return None if no record found."""
       
   474         assert isinstance(extid, str)
   463         cursor = session.system_sql('SELECT eid FROM entities WHERE '
   475         cursor = session.system_sql('SELECT eid FROM entities WHERE '
   464                                     'extid=%(x)s AND source=%(s)s',
   476                                     'extid=%(x)s AND source=%(s)s',
   465                                     # str() necessary with pg 8.3
   477                                     {'x': b64encode(extid), 's': source.uri})
   466                                     {'x': str(lid), 's': source.uri})
       
   467         # XXX testing rowcount cause strange bug with sqlite, results are there
   478         # XXX testing rowcount cause strange bug with sqlite, results are there
   468         #     but rowcount is 0
   479         #     but rowcount is 0
   469         #if cursor.rowcount > 0:
   480         #if cursor.rowcount > 0:
   470         try:
   481         try:
   471             result = cursor.fetchone()
   482             result = cursor.fetchone()
   497             self._eid_creation_lock.release()
   508             self._eid_creation_lock.release()
   498 
   509 
   499     def add_info(self, session, entity, source, extid=None):
   510     def add_info(self, session, entity, source, extid=None):
   500         """add type and source info for an eid into the system table"""
   511         """add type and source info for an eid into the system table"""
   501         # begin by inserting eid/type/source/extid into the entities table
   512         # begin by inserting eid/type/source/extid into the entities table
   502         attrs = {'type': str(entity.e_schema), 'eid': entity.eid,
   513         if extid is not None:
   503                  'extid': extid, 'source': source.uri, 'mtime': datetime.now()}
   514             assert isinstance(extid, str)
       
   515             extid = b64encode(extid)
       
   516         attrs = {'type': entity.id, 'eid': entity.eid, 'extid': extid,
       
   517                  'source': source.uri, 'mtime': datetime.now()}
   504         session.system_sql(self.sqlgen.insert('entities', attrs), attrs)
   518         session.system_sql(self.sqlgen.insert('entities', attrs), attrs)
   505 
   519 
   506     def delete_info(self, session, eid, etype, uri, extid):
   520     def delete_info(self, session, eid, etype, uri, extid):
   507         """delete system information on deletion of an entity by transfering
   521         """delete system information on deletion of an entity by transfering
   508         record from the entities table to the deleted_entities table
   522         record from the entities table to the deleted_entities table
   509         """
   523         """
   510         attrs = {'eid': eid}
   524         attrs = {'eid': eid}
   511         session.system_sql(self.sqlgen.delete('entities', attrs), attrs)
   525         session.system_sql(self.sqlgen.delete('entities', attrs), attrs)
   512         if self.has_deleted_entitites_table:
   526         if self.has_deleted_entitites_table:
       
   527             if extid is not None:
       
   528                 assert isinstance(extid, str), type(extid)
       
   529                 extid = b64encode(extid)
   513             attrs = {'type': etype, 'eid': eid, 'extid': extid,
   530             attrs = {'type': etype, 'eid': eid, 'extid': extid,
   514                      'source': uri, 'dtime': datetime.now()}
   531                      'source': uri, 'dtime': datetime.now()}
   515             session.system_sql(self.sqlgen.insert('deleted_entities', attrs), attrs)
   532             session.system_sql(self.sqlgen.insert('deleted_entities', attrs), attrs)
   516 
   533 
   517     def fti_unindex_entity(self, session, eid):
   534     def fti_unindex_entity(self, session, eid):