1019 try: |
1019 try: |
1020 # bw compat: cnx may be a session, get at the Connection |
1020 # bw compat: cnx may be a session, get at the Connection |
1021 cnx = cnx._cnx |
1021 cnx = cnx._cnx |
1022 except AttributeError: |
1022 except AttributeError: |
1023 pass |
1023 pass |
1024 eid = self.system_source.extid2eid(cnx, extid) |
1024 with cnx.ensure_cnx_set: |
|
1025 eid = self.system_source.extid2eid(cnx, extid) |
1025 if eid is not None: |
1026 if eid is not None: |
1026 self._extid_cache[extid] = eid |
1027 self._extid_cache[extid] = eid |
1027 self._type_source_cache[eid] = (etype, extid, source.uri) |
1028 self._type_source_cache[eid] = (etype, extid, source.uri) |
1028 return eid |
1029 return eid |
1029 if not insert: |
1030 if not insert: |
1030 return |
1031 return |
1031 # no link between extid and eid, create one |
1032 # no link between extid and eid, create one |
1032 try: |
1033 with cnx.ensure_cnx_set: |
1033 eid = self.system_source.create_eid(cnx) |
1034 # write query, ensure connection's mode is 'write' so connections |
1034 self._extid_cache[extid] = eid |
1035 # won't be released until commit/rollback |
1035 self._type_source_cache[eid] = (etype, extid, source.uri) |
1036 cnx.mode = 'write' |
1036 entity = source.before_entity_insertion( |
1037 try: |
1037 cnx, extid, etype, eid, sourceparams) |
1038 eid = self.system_source.create_eid(cnx) |
1038 if source.should_call_hooks: |
1039 self._extid_cache[extid] = eid |
1039 # get back a copy of operation for later restore if necessary, |
1040 self._type_source_cache[eid] = (etype, extid, source.uri) |
1040 # see below |
1041 entity = source.before_entity_insertion( |
1041 pending_operations = cnx.pending_operations[:] |
1042 cnx, extid, etype, eid, sourceparams) |
1042 self.hm.call_hooks('before_add_entity', cnx, entity=entity) |
|
1043 self.add_info(cnx, entity, source, extid) |
|
1044 source.after_entity_insertion(cnx, extid, entity, sourceparams) |
|
1045 if source.should_call_hooks: |
|
1046 self.hm.call_hooks('after_add_entity', cnx, entity=entity) |
|
1047 return eid |
|
1048 except Exception: |
|
1049 # XXX do some cleanup manually so that the transaction has a |
|
1050 # chance to be commited, with simply this entity discarded |
|
1051 self._extid_cache.pop(extid, None) |
|
1052 self._type_source_cache.pop(eid, None) |
|
1053 if 'entity' in locals(): |
|
1054 hook.CleanupDeletedEidsCacheOp.get_instance(cnx).add_data(entity.eid) |
|
1055 self.system_source.delete_info_multi(cnx, [entity]) |
|
1056 if source.should_call_hooks: |
1043 if source.should_call_hooks: |
1057 cnx.pending_operations = pending_operations |
1044 # get back a copy of operation for later restore if |
1058 raise |
1045 # necessary, see below |
|
1046 pending_operations = cnx.pending_operations[:] |
|
1047 self.hm.call_hooks('before_add_entity', cnx, entity=entity) |
|
1048 self.add_info(cnx, entity, source, extid) |
|
1049 source.after_entity_insertion(cnx, extid, entity, sourceparams) |
|
1050 if source.should_call_hooks: |
|
1051 self.hm.call_hooks('after_add_entity', cnx, entity=entity) |
|
1052 return eid |
|
1053 except Exception: |
|
1054 # XXX do some cleanup manually so that the transaction has a |
|
1055 # chance to be commited, with simply this entity discarded |
|
1056 self._extid_cache.pop(extid, None) |
|
1057 self._type_source_cache.pop(eid, None) |
|
1058 if 'entity' in locals(): |
|
1059 hook.CleanupDeletedEidsCacheOp.get_instance(cnx).add_data(entity.eid) |
|
1060 self.system_source.delete_info_multi(cnx, [entity]) |
|
1061 if source.should_call_hooks: |
|
1062 cnx.pending_operations = pending_operations |
|
1063 raise |
1059 |
1064 |
1060 def add_info(self, session, entity, source, extid=None): |
1065 def add_info(self, session, entity, source, extid=None): |
1061 """add type and source info for an eid into the system table, |
1066 """add type and source info for an eid into the system table, |
1062 and index the entity with the full text index |
1067 and index the entity with the full text index |
1063 """ |
1068 """ |