Merge lp:~tkluck/simple-scan/fix-cropping-slowness into lp:~simple-scan-team/simple-scan/trunk

Proposed by Timo Kluck
Status: Merged
Merged at revision: 648
Proposed branch: lp:~tkluck/simple-scan/fix-cropping-slowness
Merge into: lp:~simple-scan-team/simple-scan/trunk
Diff against target: 221 lines (+113/-17)
1 file modified
src/autosave-manager.vala (+113/-17)
To merge this branch: bzr merge lp:~tkluck/simple-scan/fix-cropping-slowness
Reviewer Review Type Date Requested Status
Timo Kluck (community) Needs Resubmitting
Robert Ancell Needs Fixing
Review via email: mp+181986@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Robert Ancell (robert-ancell) wrote :

You need to merge with trunk - this branch has merge conflicts.

review: Needs Fixing
Revision history for this message
Robert Ancell (robert-ancell) wrote :

I haven't looked in detail, but does this code migrate from the old format correctly?

Revision history for this message
Timo Kluck (tkluck) wrote :

Good point; I don't think it does. I'll come up with something.

636. By Timo Kluck

Separate updating of autosave pixels from other updates

637. By Timo Kluck

Use files instead of blobs to autosave pixel data

638. By Timo Kluck

remove previous version of autosave database

Revision history for this message
Timo Kluck (tkluck) wrote :

Rebased and handles version upgrade now.

review: Needs Resubmitting
Revision history for this message
Timo Kluck (tkluck) wrote :

Sorry, I was trying to say I'm hereby resubmitting, not tell myself to resubmit this...

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/autosave-manager.vala'
2--- src/autosave-manager.vala 2013-08-25 03:32:01 +0000
3+++ src/autosave-manager.vala 2013-08-26 19:37:26 +0000
4@@ -192,6 +192,24 @@
5 Source.remove (update_timeout);
6 update_timeout = 0;
7
8+ string query = @"
9+ SELECT pixels_filename FROM pages
10+ WHERE process_id = $PID
11+ ";
12+ Sqlite.Statement stmt;
13+ var result = database_connection.prepare_v2 (query, -1, out stmt);
14+ if (result != Sqlite.OK)
15+ warning (@"Error $result while preparing query");
16+ while (stmt.step () != Sqlite.DONE) {
17+ string filename = stmt.column_text (0);
18+ var file = File.new_for_path (filename);
19+ try {
20+ file.delete (null);
21+ } catch (Error e) {
22+ warning("Failed to delete autosave file");
23+ }
24+ }
25+
26 warn_if_fail (database_connection.exec (@"
27 DELETE FROM pages
28 WHERE process_id = $PID
29@@ -212,6 +230,17 @@
30 Sqlite.Database connection;
31 if (Sqlite.Database.open (AUTOSAVE_FILENAME, out connection) != Sqlite.OK)
32 throw new IOError.FAILED ("Could not connect to autosave database");
33+ Sqlite.Statement stmt;
34+ var result = connection.prepare_v2("PRAGMA user_version", -1, out stmt);
35+ if (result != Sqlite.OK)
36+ warning ("Error %d while executing pragma query", result);
37+ while (stmt.step () != Sqlite.DONE) {
38+ var user_version = stmt.column_int (0);
39+ if ( user_version < 1 ) {
40+ connection.exec("DROP TABLE pages");
41+ connection.exec("PRAGMA user_version = 1");
42+ }
43+ }
44 string query = @"
45 CREATE TABLE IF NOT EXISTS pages (
46 id integer PRIMARY KEY,
47@@ -232,9 +261,9 @@
48 crop_width integer,
49 crop_height integer,
50 scan_direction integer,
51- pixels binary
52+ pixels_filename string
53 )";
54- var result = connection.exec(query);
55+ result = connection.exec(query);
56 if (result != Sqlite.OK)
57 warning ("Error %d while executing query", result);
58 return connection;
59@@ -248,6 +277,8 @@
60 page.scan_direction_changed.connect (on_page_changed);
61 page.crop_changed.connect (on_page_changed);
62 page.scan_finished.connect (on_page_changed);
63+ page.scan_finished.connect (on_pixels_changed);
64+ page.pixels_changed.connect (on_pixels_changed);
65 }
66
67 public void on_page_removed (Page page)
68@@ -256,17 +287,41 @@
69 page.size_changed.disconnect (on_page_changed);
70 page.scan_direction_changed.disconnect (on_page_changed);
71 page.crop_changed.disconnect (on_page_changed);
72- page.scan_finished.connect (on_page_changed);
73+ page.scan_finished.disconnect (on_page_changed);
74+ page.pixels_changed.disconnect (on_pixels_changed);
75
76 string query = @"
77+ SELECT pixels_filename FROM pages
78+ WHERE process_id = $PID
79+ AND page_hash = ?2
80+ AND book_hash = ?3
81+ AND book_revision = ?4
82+ ";
83+ Sqlite.Statement stmt;
84+ var result = database_connection.prepare_v2 (query, -1, out stmt);
85+ if (result != Sqlite.OK)
86+ warning (@"Error $result while preparing query");
87+ stmt.bind_int64 (2, direct_hash (page));
88+ stmt.bind_int64 (3, direct_hash (book));
89+ stmt.bind_int64 (4, cur_book_revision);
90+ while (stmt.step () != Sqlite.DONE) {
91+ string filename = stmt.column_text (0);
92+ var file = File.new_for_path (filename);
93+ try {
94+ file.delete (null);
95+ } catch (Error e) {
96+ warning ("Failed to delete autosave file");
97+ }
98+ }
99+
100+ query = @"
101 DELETE FROM pages
102 WHERE process_id = $PID
103 AND page_hash = ?2
104 AND book_hash = ?3
105 AND book_revision = ?4
106 ";
107- Sqlite.Statement stmt;
108- var result = database_connection.prepare_v2 (query, -1, out stmt);
109+ result = database_connection.prepare_v2 (query, -1, out stmt);
110 if (result != Sqlite.OK)
111 warning (@"Error $result while preparing query");
112 stmt.bind_int64 (2, direct_hash (page));
113@@ -311,6 +366,12 @@
114 update_page (page);
115 }
116
117+ public void on_pixels_changed (Page page)
118+ {
119+ if (!page.is_scanning ())
120+ update_page_pixels (page);
121+ }
122+
123 public void on_needs_saving_changed (Book book)
124 {
125 for (var n = 0; n < book.get_n_pages (); n++)
126@@ -356,6 +417,7 @@
127 warning ("Error %d while executing query", result);
128
129 update_page (page);
130+ update_page_pixels (page);
131 }
132
133 private void update_page (Page page)
134@@ -404,8 +466,7 @@
135 crop_width=$crop_width,
136 crop_height=$crop_height,
137 scan_direction=$((int)page.get_scan_direction ()),
138- color_profile=?1,
139- pixels=?2
140+ color_profile=?1
141 WHERE process_id = $PID
142 AND page_hash = ?4
143 AND book_hash = ?5
144@@ -427,15 +488,45 @@
145 if (result != Sqlite.OK)
146 warning ("Error %d while binding text", result);
147
148- if (page.get_pixels () != null)
149+ warn_if_fail (stmt.step () == Sqlite.DONE);
150+ }
151+
152+ private void update_page_pixels (Page page) {
153+ debug ("Updating the pixels in the autosave for a page");
154+
155+ string basename = @"$cur_book_revision-$(direct_hash (book))-$(direct_hash (page)).bin";
156+ string filename = Path.build_filename (AUTOSAVE_DIR, basename);
157+ var file = File.new_for_path (filename);
158+ try {
159+ file.replace_contents (page.get_pixels (), null, false, 0, null, null);
160+ } catch (Error e) {
161+ warning ("Error while saving autosave pixel data");
162+ };
163+ Sqlite.Statement stmt;
164+ string query = @"
165+ UPDATE pages
166+ SET
167+ pixels_filename=?1
168+ WHERE process_id = $PID
169+ AND page_hash = ?2
170+ AND book_hash = ?3
171+ AND book_revision = ?4
172+ ";
173+
174+ var result = database_connection.prepare_v2 (query, -1, out stmt);
175+ if (result != Sqlite.OK)
176 {
177- // (-1) is the special value SQLITE_TRANSIENT
178- result = stmt.bind_blob (2, page.get_pixels (), page.get_pixels ().length, (DestroyNotify)(-1));
179- if (result != Sqlite.OK)
180- warning ("Error %d while binding blob", result);
181+ warning ("Error %d while preparing statement", result);
182+ return;
183 }
184- else
185- warn_if_fail (stmt.bind_null (2) == Sqlite.OK);
186+
187+ stmt.bind_int64 (2, direct_hash (page));
188+ stmt.bind_int64 (3, direct_hash (book));
189+ stmt.bind_int64 (4, cur_book_revision);
190+
191+ result = stmt.bind_text (1, filename);
192+ if (result != Sqlite.OK)
193+ warning ("Error %d while binding string", result);
194
195 warn_if_fail (stmt.step () == Sqlite.DONE);
196 }
197@@ -461,7 +552,7 @@
198 crop_width,
199 crop_height,
200 scan_direction,
201- pixels,
202+ pixels_filename,
203 id
204 FROM pages
205 WHERE process_id = $PID
206@@ -520,8 +611,13 @@
207 new_page.move_crop (crop_x, crop_y);
208 }
209
210- uchar[] new_pixels = new uchar[stmt.column_bytes (17)];
211- Memory.copy (new_pixels, stmt.column_blob (17), stmt.column_bytes (17));
212+ var file = File.new_for_path (stmt.column_text (17));
213+ uchar[] new_pixels;
214+ try {
215+ file.load_contents (null, out new_pixels, null);
216+ } catch (Error e) {
217+ warning ("Error while loading pixel data");
218+ };
219 new_page.set_pixels (new_pixels);
220
221 var id = stmt.column_int (18);

Subscribers

People subscribed via source and target branches