Merge lp:~sam92/openlp/bug-1552563-2.4 into lp:openlp/2.4

Proposed by Samuel Mehrbrodt
Status: Merged
Approved by: Tim Bentley
Approved revision: 2629
Merged at revision: 2628
Proposed branch: lp:~sam92/openlp/bug-1552563-2.4
Merge into: lp:openlp/2.4
Diff against target: 68 lines (+17/-18)
2 files modified
openlp/plugins/songs/lib/mediaitem.py (+17/-17)
tests/functional/openlp_plugins/songs/test_openlpimporter.py (+0/-1)
To merge this branch: bzr merge lp:~sam92/openlp/bug-1552563-2.4
Reviewer Review Type Date Requested Status
Tim Bentley Approve
Raoul Snyman Approve
Review via email: mp+293429@code.launchpad.net

Description of the change

Fix performance regression with Songbook search

The problem was that in each iteration the database was accessed (the song object).
Fixed this by loading all neccessary information directly in the query.

Also this reintroduces natural sorting.

To post a comment you must log in.
Revision history for this message
Samuel Mehrbrodt (sam92) wrote :
Revision history for this message
Raoul Snyman (raoul-snyman) :
review: Approve
Revision history for this message
Tim Bentley (trb143) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openlp/plugins/songs/lib/mediaitem.py'
2--- openlp/plugins/songs/lib/mediaitem.py 2016-01-15 20:37:53 +0000
3+++ openlp/plugins/songs/lib/mediaitem.py 2016-04-29 17:55:06 +0000
4@@ -21,7 +21,6 @@
5 ###############################################################################
6
7 import logging
8-import re
9 import os
10 import shutil
11
12@@ -32,6 +31,7 @@
13 from openlp.core.lib import MediaManagerItem, ItemCapabilities, PluginStatus, ServiceItemContext, \
14 check_item_selected, create_separated_list
15 from openlp.core.lib.ui import create_widget_action
16+from openlp.core.utils import get_natural_key
17 from openlp.plugins.songs.forms.editsongform import EditSongForm
18 from openlp.plugins.songs.forms.songmaintenanceform import SongMaintenanceForm
19 from openlp.plugins.songs.forms.songimportform import SongImportForm
20@@ -251,23 +251,23 @@
21 self.list_view.clear()
22
23 search_keywords = search_keywords.rpartition(' ')
24- search_book = search_keywords[0]
25- search_entry = re.sub(r'[^0-9]', '', search_keywords[2])
26-
27- songbook_entries = (self.plugin.manager.session.query(SongBookEntry)
28- .join(Book)
29- .order_by(Book.name)
30- .order_by(SongBookEntry.entry))
31- for songbook_entry in songbook_entries:
32- if songbook_entry.song.temporary:
33- continue
34- if search_book.lower() not in songbook_entry.songbook.name.lower():
35- continue
36- if search_entry not in songbook_entry.entry:
37- continue
38- song_detail = '%s #%s: %s' % (songbook_entry.songbook.name, songbook_entry.entry, songbook_entry.song.title)
39+ search_book = search_keywords[0] + '%'
40+ search_entry = search_keywords[2] + '%'
41+ search_results = (self.plugin.manager.session.query(SongBookEntry.entry, Book.name, Song.title, Song.id)
42+ .join(Song)
43+ .join(Book)
44+ .filter(Book.name.like(search_book), SongBookEntry.entry.like(search_entry),
45+ Song.temporary.is_(False)).all())
46+
47+ def get_songbook_key(result):
48+ """Get the key to sort by"""
49+ return (get_natural_key(result[1]), get_natural_key(result[0]), get_natural_key(result[2]))
50+
51+ search_results.sort(key=get_songbook_key)
52+ for result in search_results:
53+ song_detail = '%s #%s: %s' % (result[1], result[0], result[2])
54 song_name = QtWidgets.QListWidgetItem(song_detail)
55- song_name.setData(QtCore.Qt.UserRole, songbook_entry.song.id)
56+ song_name.setData(QtCore.Qt.UserRole, result[3])
57 self.list_view.addItem(song_name)
58
59 def on_clear_text_button_click(self):
60
61=== modified file 'tests/functional/openlp_plugins/songs/test_openlpimporter.py'
62--- tests/functional/openlp_plugins/songs/test_openlpimporter.py 2016-04-27 18:45:39 +0000
63+++ tests/functional/openlp_plugins/songs/test_openlpimporter.py 2016-04-29 17:55:06 +0000
64@@ -73,4 +73,3 @@
65 self.assertIsNone(importer.do_import(), 'do_import should return None when import_source is not a list')
66 self.assertEqual(mocked_import_wizard.progress_bar.setMaximum.called, False,
67 'setMaximum on import_wizard.progress_bar should not have been called')
68-

Subscribers

People subscribed via source and target branches