faster changelog rebuild

This commit is contained in:
j 2016-01-24 12:05:58 +05:30
parent 4691279b3d
commit fd922cc83c
2 changed files with 16 additions and 10 deletions

View file

@ -45,7 +45,11 @@ class Changelog(db.Model):
sig = sa.Column(sa.String(96)) sig = sa.Column(sa.String(96))
@classmethod @classmethod
def record(cls, user, action, *args): def record(cls, user, action, *args, **kwargs):
commit = True
if '_commit' in kwargs:
commit = kwargs['_commit']
del kwargs['_commit']
c = cls() c = cls()
c.created = datetime.utcnow() c.created = datetime.utcnow()
c.timestamp = datetime2ts(c.created) c.timestamp = datetime2ts(c.created)
@ -55,7 +59,8 @@ class Changelog(db.Model):
_data = str(c.revision) + str(c.timestamp) + c.data _data = str(c.revision) + str(c.timestamp) + c.data
_data = _data.encode() _data = _data.encode()
state.db.session.add(c) state.db.session.add(c)
state.db.session.commit() if commit:
state.db.session.commit()
logger.debug('record change: %s', c.json()) logger.debug('record change: %s', c.json())
@classmethod @classmethod

View file

@ -186,28 +186,29 @@ class User(db.Model):
def rebuild_changelog(self): def rebuild_changelog(self):
Changelog.query.filter_by(user_id=self.id).delete() Changelog.query.filter_by(user_id=self.id).delete()
for item in self.library.get_items().order_by('created'): for item in self.library.get_items().order_by('created'):
Changelog.record(self, 'additem', item.id, item.info) Changelog.record(self, 'additem', item.id, item.info, _commit=False)
Changelog.record(self, 'edititem', item.id, item.meta) Changelog.record(self, 'edititem', item.id, item.meta, _commit=False)
lists = [] lists = []
for l in List.query.filter_by(user_id=self.id, type='static').order_by('index_'): for l in List.query.filter_by(user_id=self.id, type='static').order_by('index_'):
if l.name: if l.name:
lists.append(l.name) lists.append(l.name)
Changelog.record(self, 'addlist', l.name) Changelog.record(self, 'addlist', l.name, _commit=False)
items = [i.id for i in l.get_items().options(load_only('id'))] items = [i.id for i in l.get_items().options(load_only('id'))]
if items: if items:
Changelog.record(self, 'addlistitems', l.name, items) Changelog.record(self, 'addlistitems', l.name, items, _commit=False)
if len(lists) > 1: if len(lists) > 1:
Changelog.record(self, 'orderlists', lists) Changelog.record(self, 'orderlists', lists, _commit=False)
for peer in User.query.filter_by(peered=True): for peer in User.query.filter_by(peered=True):
Changelog.record(self, 'addpeer', peer.id, self.nickname) Changelog.record(self, 'addpeer', peer.id, self.nickname, _commit=False)
if peer.info.get('contact'): if peer.info.get('contact'):
Changelog.record(self, 'editpeer', peer.id, { Changelog.record(self, 'editpeer', peer.id, {
'contact': peer.info.get('contact') 'contact': peer.info.get('contact')
}) }, _commit=False)
if settings.preferences.get('contact'): if settings.preferences.get('contact'):
Changelog.record(self, 'editcontact', settings.preferences.get('contact')) Changelog.record(self, 'editcontact', settings.preferences.get('contact'), _commit=False)
state.db.session.commit()
list_items = sa.Table('listitem', db.metadata, list_items = sa.Table('listitem', db.metadata,
sa.Column('list_id', sa.Integer(), sa.ForeignKey('list.id')), sa.Column('list_id', sa.Integer(), sa.ForeignKey('list.id')),