966 if source.support_entity(etype, 1): |
966 if source.support_entity(etype, 1): |
967 return source |
967 return source |
968 else: |
968 else: |
969 raise ETypeNotSupportedBySources(etype) |
969 raise ETypeNotSupportedBySources(etype) |
970 |
970 |
|
971 def init_entity_caches(self, session, entity, source): |
|
972 """add entity to session entities cache and repo's extid cache. |
|
973 Return entity's ext id if the source isn't the system source. |
|
974 """ |
|
975 session.set_entity_cache(entity) |
|
976 suri = source.uri |
|
977 if suri == 'system': |
|
978 extid = None |
|
979 else: |
|
980 extid = source.get_extid(entity) |
|
981 self._extid_cache[(str(extid), suri)] = entity.eid |
|
982 self._type_source_cache[entity.eid] = (entity.__regid__, suri, extid) |
|
983 return extid |
|
984 |
971 def glob_add_entity(self, session, entity): |
985 def glob_add_entity(self, session, entity): |
972 """add an entity to the repository |
986 """add an entity to the repository |
973 |
987 |
974 the entity eid should originaly be None and a unique eid is assigned to |
988 the entity eid should originaly be None and a unique eid is assigned to |
975 the entity instance |
989 the entity instance |
985 eschema = entity.e_schema |
999 eschema = entity.e_schema |
986 etype = str(eschema) |
1000 etype = str(eschema) |
987 source = self.locate_etype_source(etype) |
1001 source = self.locate_etype_source(etype) |
988 # attribute an eid to the entity before calling hooks |
1002 # attribute an eid to the entity before calling hooks |
989 entity.set_eid(self.system_source.create_eid(session)) |
1003 entity.set_eid(self.system_source.create_eid(session)) |
|
1004 # set caches asap |
|
1005 extid = self.init_entity_caches(session, entity, source) |
990 if server.DEBUG & server.DBG_REPO: |
1006 if server.DEBUG & server.DBG_REPO: |
991 print 'ADD entity', etype, entity.eid, dict(entity) |
1007 print 'ADD entity', etype, entity.eid, dict(entity) |
992 relations = [] |
1008 relations = [] |
993 if source.should_call_hooks: |
1009 if source.should_call_hooks: |
994 self.hm.call_hooks('before_add_entity', session, entity=entity) |
1010 self.hm.call_hooks('before_add_entity', session, entity=entity) |
1000 relations.append((attr, entity[attr])) |
1016 relations.append((attr, entity[attr])) |
1001 entity.set_defaults() |
1017 entity.set_defaults() |
1002 if session.is_hook_category_activated('integrity'): |
1018 if session.is_hook_category_activated('integrity'): |
1003 entity.check(creation=True) |
1019 entity.check(creation=True) |
1004 source.add_entity(session, entity) |
1020 source.add_entity(session, entity) |
1005 if source.uri != 'system': |
|
1006 extid = source.get_extid(entity) |
|
1007 self._extid_cache[(str(extid), source.uri)] = entity.eid |
|
1008 else: |
|
1009 extid = None |
|
1010 self.add_info(session, entity, source, extid, complete=False) |
1021 self.add_info(session, entity, source, extid, complete=False) |
1011 entity._is_saved = True # entity has an eid and is saved |
1022 entity._is_saved = True # entity has an eid and is saved |
1012 # prefill entity relation caches |
1023 # prefill entity relation caches |
1013 session.set_entity_cache(entity) |
|
1014 for rschema in eschema.subject_relations(): |
1024 for rschema in eschema.subject_relations(): |
1015 rtype = str(rschema) |
1025 rtype = str(rschema) |
1016 if rtype in schema.VIRTUAL_RTYPES: |
1026 if rtype in schema.VIRTUAL_RTYPES: |
1017 continue |
1027 continue |
1018 if rschema.final: |
1028 if rschema.final: |