server/repository.py
changeset 7543 570522300e22
parent 7514 32081892850e
child 7562 cdef82ca9eab
equal deleted inserted replaced
7542:86e9632a4e9c 7543:570522300e22
   152                                              config.sources()['system'])
   152                                              config.sources()['system'])
   153         self.sources = [self.system_source]
   153         self.sources = [self.system_source]
   154         self.sources_by_uri = {'system': self.system_source}
   154         self.sources_by_uri = {'system': self.system_source}
   155         # querier helper, need to be created after sources initialization
   155         # querier helper, need to be created after sources initialization
   156         self.querier = querier.QuerierHelper(self, self.schema)
   156         self.querier = querier.QuerierHelper(self, self.schema)
   157         # cache eid -> (type, source, extid)
   157         # cache eid -> (type, physical source, extid, actual source)
   158         self._type_source_cache = {}
   158         self._type_source_cache = {}
   159         # cache (extid, source uri) -> eid
   159         # cache (extid, source uri) -> eid
   160         self._extid_cache = {}
   160         self._extid_cache = {}
   161         # open some connections set
   161         # open some connections set
   162         if config.init_cnxset_pool:
   162         if config.init_cnxset_pool:
   532         This is a public method, not requiring a session id.
   532         This is a public method, not requiring a session id.
   533         """
   533         """
   534         # XXX we may want to check we don't give sensible information
   534         # XXX we may want to check we don't give sensible information
   535         if foreid is None:
   535         if foreid is None:
   536             return self.config[option]
   536             return self.config[option]
   537         _, sourceuri, extid = self.type_and_source_from_eid(foreid)
   537         _, sourceuri, extid, _ = self.type_and_source_from_eid(foreid)
   538         if sourceuri == 'system':
   538         if sourceuri == 'system':
   539             return self.config[option]
   539             return self.config[option]
   540         cnxset = self._get_cnxset()
   540         cnxset = self._get_cnxset()
   541         try:
   541         try:
   542             cnx = cnxset.connection(sourceuri)
   542             cnx = cnxset.connection(sourceuri)
   747                 raise
   747                 raise
   748         finally:
   748         finally:
   749             session.free_cnxset()
   749             session.free_cnxset()
   750 
   750 
   751     def describe(self, sessionid, eid, txid=None):
   751     def describe(self, sessionid, eid, txid=None):
   752         """return a tuple (type, source, extid) for the entity with id <eid>"""
   752         """return a tuple `(type, physical source uri, extid, actual source
       
   753         uri)` for the entity of the given `eid`
       
   754         """
   753         session = self._get_session(sessionid, setcnxset=True, txid=txid)
   755         session = self._get_session(sessionid, setcnxset=True, txid=txid)
   754         try:
   756         try:
   755             return self.type_and_source_from_eid(eid, session)
   757             return self.type_and_source_from_eid(eid, session)
   756         finally:
   758         finally:
   757             session.free_cnxset()
   759             session.free_cnxset()
   952     # data sources handling ###################################################
   954     # data sources handling ###################################################
   953     # * correspondance between eid and (type, source)
   955     # * correspondance between eid and (type, source)
   954     # * correspondance between eid and local id (i.e. specific to a given source)
   956     # * correspondance between eid and local id (i.e. specific to a given source)
   955 
   957 
   956     def type_and_source_from_eid(self, eid, session=None):
   958     def type_and_source_from_eid(self, eid, session=None):
   957         """return a tuple (type, source, extid) for the entity with id <eid>"""
   959         """return a tuple `(type, physical source uri, extid, actual source
       
   960         uri)` for the entity of the given `eid`
       
   961         """
   958         try:
   962         try:
   959             eid = typed_eid(eid)
   963             eid = typed_eid(eid)
   960         except ValueError:
   964         except ValueError:
   961             raise UnknownEid(eid)
   965             raise UnknownEid(eid)
   962         try:
   966         try:
   966                 session = self.internal_session()
   970                 session = self.internal_session()
   967                 free_cnxset = True
   971                 free_cnxset = True
   968             else:
   972             else:
   969                 free_cnxset = False
   973                 free_cnxset = False
   970             try:
   974             try:
   971                 etype, uri, extid = self.system_source.eid_type_source(session,
   975                 etype, uri, extid, auri = self.system_source.eid_type_source(
   972                                                                        eid)
   976                     session, eid)
   973             finally:
   977             finally:
   974                 if free_cnxset:
   978                 if free_cnxset:
   975                     session.free_cnxset()
   979                     session.free_cnxset()
   976         self._type_source_cache[eid] = (etype, uri, extid)
   980             self._type_source_cache[eid] = (etype, uri, extid, auri)
   977         if uri != 'system':
   981             if uri != 'system':
   978             self._extid_cache[(extid, uri)] = eid
   982                 self._extid_cache[(extid, uri)] = eid
   979         return etype, uri, extid
   983             return etype, uri, extid, auri
   980 
   984 
   981     def clear_caches(self, eids):
   985     def clear_caches(self, eids):
   982         etcache = self._type_source_cache
   986         etcache = self._type_source_cache
   983         extidcache = self._extid_cache
   987         extidcache = self._extid_cache
   984         rqlcache = self.querier._rql_cache
   988         rqlcache = self.querier._rql_cache
   985         for eid in eids:
   989         for eid in eids:
   986             try:
   990             try:
   987                 etype, uri, extid = etcache.pop(typed_eid(eid)) # may be a string in some cases
   991                 etype, uri, extid, auri = etcache.pop(typed_eid(eid)) # may be a string in some cases
   988                 rqlcache.pop('%s X WHERE X eid %s' % (etype, eid), None)
   992                 rqlcache.pop('%s X WHERE X eid %s' % (etype, eid), None)
   989                 extidcache.pop((extid, uri), None)
   993                 extidcache.pop((extid, uri), None)
   990             except KeyError:
   994             except KeyError:
   991                 etype = None
   995                 etype = None
   992             rqlcache.pop('Any X WHERE X eid %s' % eid, None)
   996             rqlcache.pop('Any X WHERE X eid %s' % eid, None)
  1016             args[key] = typed_eid(args[key])
  1020             args[key] = typed_eid(args[key])
  1017         return tuple(cachekey)
  1021         return tuple(cachekey)
  1018 
  1022 
  1019     def eid2extid(self, source, eid, session=None):
  1023     def eid2extid(self, source, eid, session=None):
  1020         """get local id from an eid"""
  1024         """get local id from an eid"""
  1021         etype, uri, extid = self.type_and_source_from_eid(eid, session)
  1025         etype, uri, extid, _ = self.type_and_source_from_eid(eid, session)
  1022         if source.uri != uri:
  1026         if source.uri != uri:
  1023             # eid not from the given source
  1027             # eid not from the given source
  1024             raise UnknownEid(eid)
  1028             raise UnknownEid(eid)
  1025         return extid
  1029         return extid
  1026 
  1030 
  1059             session = self.internal_session()
  1063             session = self.internal_session()
  1060             free_cnxset = True
  1064             free_cnxset = True
  1061         eid = self.system_source.extid2eid(session, uri, extid)
  1065         eid = self.system_source.extid2eid(session, uri, extid)
  1062         if eid is not None:
  1066         if eid is not None:
  1063             self._extid_cache[cachekey] = eid
  1067             self._extid_cache[cachekey] = eid
  1064             self._type_source_cache[eid] = (etype, uri, extid)
  1068             self._type_source_cache[eid] = (etype, uri, extid, source.uri)
  1065             if free_cnxset:
  1069             if free_cnxset:
  1066                 session.free_cnxset()
  1070                 session.free_cnxset()
  1067             return eid
  1071             return eid
  1068         if not insert:
  1072         if not insert:
  1069             return
  1073             return
  1077             session = self.internal_session()
  1081             session = self.internal_session()
  1078             free_cnxset = True
  1082             free_cnxset = True
  1079         try:
  1083         try:
  1080             eid = self.system_source.create_eid(session)
  1084             eid = self.system_source.create_eid(session)
  1081             self._extid_cache[cachekey] = eid
  1085             self._extid_cache[cachekey] = eid
  1082             self._type_source_cache[eid] = (etype, uri, extid)
  1086             self._type_source_cache[eid] = (etype, uri, extid, source.uri)
  1083             entity = source.before_entity_insertion(
  1087             entity = source.before_entity_insertion(
  1084                 session, extid, etype, eid, sourceparams)
  1088                 session, extid, etype, eid, sourceparams)
  1085             if source.should_call_hooks:
  1089             if source.should_call_hooks:
  1086                 self.hm.call_hooks('before_add_entity', session, entity=entity)
  1090                 self.hm.call_hooks('before_add_entity', session, entity=entity)
  1087             # XXX call add_info with complete=False ?
  1091             # XXX call add_info with complete=False ?
  1213         else:
  1217         else:
  1214             if source.copy_based_source:
  1218             if source.copy_based_source:
  1215                 suri = 'system'
  1219                 suri = 'system'
  1216             extid = source.get_extid(entity)
  1220             extid = source.get_extid(entity)
  1217             self._extid_cache[(str(extid), suri)] = entity.eid
  1221             self._extid_cache[(str(extid), suri)] = entity.eid
  1218         self._type_source_cache[entity.eid] = (entity.__regid__, suri, extid)
  1222         self._type_source_cache[entity.eid] = (entity.__regid__, suri, extid,
       
  1223                                                source.uri)
  1219         return extid
  1224         return extid
  1220 
  1225 
  1221     def glob_add_entity(self, session, edited):
  1226     def glob_add_entity(self, session, edited):
  1222         """add an entity to the repository
  1227         """add an entity to the repository
  1223 
  1228 
  1374         # and does not use setdefault on purpose. Unless a new release
  1379         # and does not use setdefault on purpose. Unless a new release
  1375         # of the Python interpreter advertises large perf improvements
  1380         # of the Python interpreter advertises large perf improvements
  1376         # in setdefault, this should not be changed without profiling.
  1381         # in setdefault, this should not be changed without profiling.
  1377 
  1382 
  1378         for eid in eids:
  1383         for eid in eids:
  1379             etype, sourceuri, extid = self.type_and_source_from_eid(eid, session)
  1384             etype, sourceuri, extid, _ = self.type_and_source_from_eid(eid, session)
  1380             # XXX should cache entity's cw_metainformation
  1385             # XXX should cache entity's cw_metainformation
  1381             entity = session.entity_from_eid(eid, etype)
  1386             entity = session.entity_from_eid(eid, etype)
  1382             try:
  1387             try:
  1383                 data_by_etype_source[(etype, sourceuri)].append(entity)
  1388                 data_by_etype_source[(etype, sourceuri)].append(entity)
  1384             except KeyError:
  1389             except KeyError: