server/repository.py
changeset 2968 0e3460341023
parent 2893 5989ce0707bc
parent 2967 e7d348134006
child 3072 6fb42c53f6df
equal deleted inserted replaced
2902:dd9f2dd02f85 2968:0e3460341023
   165         # cache eid -> type / source
   165         # cache eid -> type / source
   166         self._type_source_cache = {}
   166         self._type_source_cache = {}
   167         # cache (extid, source uri) -> eid
   167         # cache (extid, source uri) -> eid
   168         self._extid_cache = {}
   168         self._extid_cache = {}
   169         # open some connections pools
   169         # open some connections pools
       
   170         if config.open_connections_pools:
       
   171             self.open_connections_pools()
       
   172 
       
   173     def open_connections_pools(self):
       
   174         config = self.config
   170         self._available_pools = Queue.Queue()
   175         self._available_pools = Queue.Queue()
   171         self._available_pools.put_nowait(pool.ConnectionsPool(self.sources))
   176         self._available_pools.put_nowait(pool.ConnectionsPool(self.sources))
   172         if config.read_instance_schema:
   177         if config.read_instance_schema:
   173             # normal start: load the instance schema from the database
   178             # normal start: load the instance schema from the database
   174             self.fill_schema()
   179             self.fill_schema()
   175         elif config.bootstrap_schema:
   180         elif config.bootstrap_schema:
   176             # usually during repository creation
   181             # usually during repository creation
   177             self.warning("set fs instance'schema as bootstrap schema")
   182             self.warning("set fs instance'schema as bootstrap schema")
   178             config.bootstrap_cubes()
   183             config.bootstrap_cubes()
   179             self.set_schema(self.config.load_schema(), resetvreg=False)
   184             self.set_schema(config.load_schema(), resetvreg=False)
   180             # need to load the Any and CWUser entity types
   185             # need to load the Any and CWUser entity types
   181             self.vreg.schema = self.schema
   186             self.vreg.schema = self.schema
   182             etdirectory = join(CW_SOFTWARE_ROOT, 'entities')
   187             etdirectory = join(CW_SOFTWARE_ROOT, 'entities')
   183             self.vreg.init_registration([etdirectory])
   188             self.vreg.init_registration([etdirectory])
   184             self.vreg.load_file(join(etdirectory, '__init__.py'),
   189             self.vreg.load_file(join(etdirectory, '__init__.py'),
   185                                 'cubicweb.entities.__init__')
   190                                 'cubicweb.entities.__init__')
   186             self.vreg.load_file(join(etdirectory, 'authobjs.py'),
   191             self.vreg.load_file(join(etdirectory, 'authobjs.py'),
   187                                 'cubicweb.entities.authobjs')
   192                                 'cubicweb.entities.authobjs')
       
   193             self.vreg.load_file(join(etdirectory, 'wfobjs.py'),
       
   194                                 'cubicweb.entities.wfobjs')
   188         else:
   195         else:
   189             # test start: use the file system schema (quicker)
   196             # test start: use the file system schema (quicker)
   190             self.warning("set fs instance'schema")
   197             self.warning("set fs instance'schema")
   191             config.bootstrap_cubes()
   198             config.bootstrap_cubes()
   192             self.set_schema(self.config.load_schema())
   199             self.set_schema(config.load_schema())
   193         if not config.creating:
   200         if not config.creating:
   194             if 'CWProperty' in self.schema:
   201             if 'CWProperty' in self.schema:
   195                 self.vreg.init_properties(self.properties())
   202                 self.vreg.init_properties(self.properties())
   196             # call source's init method to complete their initialisation if
   203             # call source's init method to complete their initialisation if
   197             # needed (for instance looking for persistent configuration using an
   204             # needed (for instance looking for persistent configuration using an
   211         self.pools = []
   218         self.pools = []
   212         for i in xrange(config['connections-pool-size']):
   219         for i in xrange(config['connections-pool-size']):
   213             self.pools.append(pool.ConnectionsPool(self.sources))
   220             self.pools.append(pool.ConnectionsPool(self.sources))
   214             self._available_pools.put_nowait(self.pools[-1])
   221             self._available_pools.put_nowait(self.pools[-1])
   215         self._shutting_down = False
   222         self._shutting_down = False
   216         self.hm = vreg['hooks']
   223         self.hm = self.vreg['hooks']
   217         if not (config.creating or config.repairing):
   224         if not (config.creating or config.repairing):
   218             # call instance level initialisation hooks
   225             # call instance level initialisation hooks
   219             self.hm.call_hooks('server_startup', repo=self)
   226             self.hm.call_hooks('server_startup', repo=self)
   220             # register a task to cleanup expired session
   227             # register a task to cleanup expired session
   221             self.looping_task(self.config['session-time']/3.,
   228             self.looping_task(config['session-time']/3., self.clean_sessions)
   222                               self.clean_sessions)
       
   223 
   229 
   224     # internals ###############################################################
   230     # internals ###############################################################
   225 
   231 
   226     def get_source(self, uri, source_config):
   232     def get_source(self, uri, source_config):
   227         source_config['uri'] = uri
   233         source_config['uri'] = uri
   228         return sources.get_source(source_config, self.schema, self)
   234         return sources.get_source(source_config, self.schema, self)
   229 
   235 
   230     def set_schema(self, schema, resetvreg=True):
   236     def set_schema(self, schema, resetvreg=True, rebuildinfered=True):
   231         schema.rebuild_infered_relations()
   237         if rebuildinfered:
       
   238             schema.rebuild_infered_relations()
   232         self.info('set schema %s %#x', schema.name, id(schema))
   239         self.info('set schema %s %#x', schema.name, id(schema))
   233         self.debug(', '.join(sorted(str(e) for e in schema.entities())))
   240         self.debug(', '.join(sorted(str(e) for e in schema.entities())))
   234         self.querier.set_schema(schema)
   241         self.querier.set_schema(schema)
   235         for source in self.sources:
   242         for source in self.sources:
   236             source.set_schema(schema)
   243             source.set_schema(schema)
   949         entity.set_eid(self.system_source.create_eid(session))
   956         entity.set_eid(self.system_source.create_eid(session))
   950         if server.DEBUG & server.DBG_REPO:
   957         if server.DEBUG & server.DBG_REPO:
   951             print 'ADD entity', etype, entity.eid, dict(entity)
   958             print 'ADD entity', etype, entity.eid, dict(entity)
   952         entity._is_saved = False # entity has an eid but is not yet saved
   959         entity._is_saved = False # entity has an eid but is not yet saved
   953         relations = []
   960         relations = []
   954         # if inlined relations are specified, fill entity's related cache to
   961         # init edited_attributes before calling before_add_entity hooks
   955         # avoid unnecessary queries
       
   956         entity.edited_attributes = set(entity)
   962         entity.edited_attributes = set(entity)
   957         for attr in entity.edited_attributes:
   963         if source.should_call_hooks:
       
   964             self.hm.call_hooks('before_add_entity', session, entity=entity)
       
   965         # XXX use entity.keys here since edited_attributes is not updated for
       
   966         # inline relations
       
   967         for attr in entity.keys():
   958             rschema = eschema.subject_relation(attr)
   968             rschema = eschema.subject_relation(attr)
   959             if not rschema.is_final(): # inlined relation
   969             if not rschema.is_final(): # inlined relation
   960                 relations.append((attr, entity[attr]))
   970                 relations.append((attr, entity[attr]))
   961         if source.should_call_hooks:
       
   962             self.hm.call_hooks('before_add_entity', session, entity=entity)
       
   963         entity.set_defaults()
   971         entity.set_defaults()
   964         entity.check(creation=True)
   972         entity.check(creation=True)
   965         source.add_entity(session, entity)
   973         source.add_entity(session, entity)
   966         if source.uri != 'system':
   974         if source.uri != 'system':
   967             extid = source.get_extid(entity)
   975             extid = source.get_extid(entity)