Fix antipatern of create/write in loop

issue3516
review14931003
This commit is contained in:
Sergi Almacellas Abellana committed 2016-06-09 17:32:17 +02:00
1 parent d90e992d32
commit f22e4e077c
4 files changed
+42 -42

No files matched your search

+4 -1
View File
@@ -300,6 +300,7 @@ class ActionMixin(ModelSQL):
Action = pool.get('ir.action')
ir_action = cls.__table__()
new_records = []
to_write = []
for values in vlist:
later = {}
action_values = {}
@@ -331,7 +332,9 @@ class ActionMixin(ModelSQL):
transaction.connection, cls._table, action.id)
record = cls(action.id)
new_records.append(record)
cls.write([record], later)
to_write.extend(([record], later))
if to_write:
cls.write(*to_write)
return new_records
@classmethod
+4 -1
View File
@@ -934,6 +934,7 @@ class ModelData(ModelSQL, ModelView):
def sync(cls, records):
pool = Pool()
to_write = []
models_to_write = defaultdict(list)
for data in records:
Model = pool.get(data.model)
values = cls.load_values(data.values)
@@ -942,11 +943,13 @@ class ModelData(ModelSQL, ModelView):
# if they come from version < 3.2
if values != fs_values:
record = Model(data.db_id)
Model.write([record], fs_values)
models_to_write[Model].extend(([record], fs_values))
values = fs_values
to_write.extend([[data], {
'values': cls.dump_values(values),
}])
for Model, values_to_write in models_to_write.iteritems():
Model.write(*values_to_write)
if to_write:
cls.write(*to_write)
+3 -1
View File
@@ -166,6 +166,8 @@ class Property(ModelSQL, ModelView):
raise Exception('Not implemented')
if (val != default_val):
to_create = []
for res_id in ids:
vals = cls._set_values(model, res_id, val, model_field.id)
cls.create([vals])
to_create.append(vals)
cls.create(to_create)
+31 -39
View File
@@ -518,28 +518,20 @@ class Translation(ModelSQL, ModelView):
]):
translations[translation.name] = translation
to_create = []
to_save = []
for record, value in izip(records, values):
translation = translations.get(get_name(record))
if not translation:
to_create.append({
'name': name,
'lang': lang,
'type': ttype,
'src': getattr(record, field_name),
'value': value,
'fuzzy': False,
})
else:
with Transaction().set_context(_check_access=False):
cls.write([translation], {
'src': getattr(record, field_name),
'value': value,
'fuzzy': False,
})
if to_create:
with Transaction().set_context(_check_access=False):
cls.create(to_create)
translation = cls()
translation.name = name
translation.lang = lang
translation.type = ttype
translation.src = getattr(record, field_name)
translation.value = value
translation.fuzzy = False
to_save.append(translation)
with Transaction().set_context(_check_access=False):
cls.save(to_save)
return
Model = pool.get(model_name)
@@ -567,19 +559,15 @@ class Translation(ModelSQL, ModelView):
other_translations.setdefault(translation.res_id, []
).append(translation)
to_create = []
to_save = []
for record, value in izip(records, values):
translation = translations.get(record.id)
if not translation:
to_create.append({
'name': name,
'lang': lang,
'type': ttype,
'res_id': record.id,
'value': value,
'src': getattr(record, field_name),
'fuzzy': False,
})
translation = cls()
translation.name = name
translation.lang = lang
translation.type = ttype
translation.res_id = record.id
else:
with Transaction().set_context(_check_access=False):
cls.write([translation], {
@@ -589,13 +577,16 @@ class Translation(ModelSQL, ModelView):
})
other_langs = other_translations.get(record.id)
if other_langs:
cls.write(other_langs, {
'src': getattr(record, field_name),
'fuzzy': True,
})
if to_create:
with Transaction().set_context(_check_access=False):
cls.create(to_create)
for other_lang in other_langs:
other_lang.src = getattr(record, field_name)
other_lang.fuzzy = True
to_save.append(other_lang)
translation.value = value
translation.src = getattr(record, field_name)
translation.fuzzy = False
to_save.append(translation)
with Transaction().set_context(_check_access=False):
cls.save(to_save)
@classmethod
def delete_ids(cls, model, ttype, ids):
@@ -845,7 +836,7 @@ class Translation(ModelSQL, ModelView):
translation.value = new_translation.value
translation.overriding_module = module
translation.fuzzy = new_translation.fuzzy
translation.save()
return translation
# Make a first loop to retreive translation ids in the right order to
# get better read locality and a full usage of the cache.
@@ -869,7 +860,8 @@ class Translation(ModelSQL, ModelView):
noupdate = False
if '.' in res_id:
override_translation(res_id, translation)
to_save.append(override_translation(res_id,
translation))
continue
model = translation.name.split(',')[0]
@@ -907,7 +899,7 @@ class Translation(ModelSQL, ModelView):
to_save.append(old_translation)
else:
translations.add(old_translation)
cls.save(to_save)
cls.save(filter(None, to_save))
translations |= set(to_save)
if translations: