don't commit SQL request too fast to improve performences. Fixes #6905

This commit is contained in:
Yann Leboulanger 2012-04-03 19:38:15 +02:00
parent 2fdbe37b58
commit 134d55b285
1 changed files with 30 additions and 19 deletions

View File

@ -34,6 +34,7 @@ import time
import datetime import datetime
from gzip import GzipFile from gzip import GzipFile
from cStringIO import StringIO from cStringIO import StringIO
import gobject
import exceptions import exceptions
import gajim import gajim
@ -106,6 +107,7 @@ class Logger:
def __init__(self): def __init__(self):
self.jids_already_in = [] # holds jids that we already have in DB self.jids_already_in = [] # holds jids that we already have in DB
self.con = None self.con = None
self.commit_timout_id = None
if not os.path.exists(LOG_DB_PATH): if not os.path.exists(LOG_DB_PATH):
# this can happen only the first time (the time we create the db) # this can happen only the first time (the time we create the db)
@ -163,15 +165,25 @@ class Logger:
self.open_db() self.open_db()
self.get_jids_already_in_db() self.get_jids_already_in_db()
def _really_commit(self):
try:
self.con.commit()
except sqlite.OperationalError, e:
print >> sys.stderr, str(e)
self.commit_timout_id = None
return False
def _timeout_commit(self):
if self.commit_timout_id:
return
self.commit_timout_id = gobject.timeout_add(500, self._really_commit)
def simple_commit(self, sql_to_commit): def simple_commit(self, sql_to_commit):
""" """
Helper to commit Helper to commit
""" """
self.cur.execute(sql_to_commit) self.cur.execute(sql_to_commit)
try: self._timeout_commit()
self.con.commit()
except sqlite.OperationalError, e:
print >> sys.stderr, str(e)
def get_jids_already_in_db(self): def get_jids_already_in_db(self):
try: try:
@ -398,12 +410,14 @@ class Logger:
except sqlite.OperationalError, e: except sqlite.OperationalError, e:
raise exceptions.PysqliteOperationalError(str(e)) raise exceptions.PysqliteOperationalError(str(e))
message_id = None message_id = None
try: if write_unread:
self.con.commit() try:
if write_unread: self.con.commit()
message_id = self.cur.lastrowid message_id = self.cur.lastrowid
except sqlite.OperationalError, e: except sqlite.OperationalError, e:
print >> sys.stderr, str(e) print >> sys.stderr, str(e)
else:
self._timeout_commit()
if message_id: if message_id:
self.insert_unread_events(message_id, values[0]) self.insert_unread_events(message_id, values[0])
return message_id return message_id
@ -922,10 +936,7 @@ class Logger:
VALUES (?, ?, ?, ?); VALUES (?, ?, ?, ?);
''', (hash_method, hash_, buffer(data), int(time.time()))) ''', (hash_method, hash_, buffer(data), int(time.time())))
# (1) -- note above # (1) -- note above
try: self._timeout_commit()
self.con.commit()
except sqlite.OperationalError, e:
print >> sys.stderr, str(e)
def update_caps_time(self, method, hash_): def update_caps_time(self, method, hash_):
sql = '''UPDATE caps_cache SET last_seen = %d sql = '''UPDATE caps_cache SET last_seen = %d
@ -963,9 +974,9 @@ class Logger:
# Fill roster tables with the new roster # Fill roster tables with the new roster
for jid in roster: for jid in roster:
self.add_or_update_contact(account_jid, jid, roster[jid]['name'], self.add_or_update_contact(account_jid, jid, roster[jid]['name'],
roster[jid]['subscription'], roster[jid]['ask'], roster[jid]['subscription'], roster[jid]['ask'],
roster[jid]['groups'], commit=False) roster[jid]['groups'], commit=False)
self.con.commit() self._timeout_commit()
# At this point, we are sure the replacement works properly so we can # At this point, we are sure the replacement works properly so we can
# set the new roster_version value. # set the new roster_version value.
@ -987,7 +998,7 @@ class Logger:
self.cur.execute( self.cur.execute(
'DELETE FROM roster_entry WHERE account_jid_id=? AND jid_id=?', 'DELETE FROM roster_entry WHERE account_jid_id=? AND jid_id=?',
(account_jid_id, jid_id)) (account_jid_id, jid_id))
self.con.commit() self._timeout_commit()
def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups, def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups,
commit=True): commit=True):
@ -1022,7 +1033,7 @@ class Logger:
self.convert_human_subscription_values_to_db_api_values(sub), self.convert_human_subscription_values_to_db_api_values(sub),
bool(ask))) bool(ask)))
if commit: if commit:
self.con.commit() self._timeout_commit()
def get_roster(self, account_jid): def get_roster(self, account_jid):
""" """
@ -1075,7 +1086,7 @@ class Logger:
(account_jid_id,)) (account_jid_id,))
self.cur.execute('DELETE FROM roster_group WHERE account_jid_id=?', self.cur.execute('DELETE FROM roster_group WHERE account_jid_id=?',
(account_jid_id,)) (account_jid_id,))
self.con.commit() self._timeout_commit()
def save_if_not_exists(self, with_, direction, tim, msg='', nick=None): def save_if_not_exists(self, with_, direction, tim, msg='', nick=None):
if tim: if tim: