Merge lp:~stolowski/unity-scope-click/ensure-one-reader into lp:unity-scope-click/devel

Proposed by Paweł Stołowski
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 348
Merged at revision: 350
Proposed branch: lp:~stolowski/unity-scope-click/ensure-one-reader
Merge into: lp:unity-scope-click/devel
Diff against target: 256 lines (+50/-36)
8 files modified
libclickscope/click/departments-db.cpp (+24/-12)
libclickscope/click/departments-db.h (+3/-3)
libclickscope/tests/test_departments-db.cpp (+14/-12)
scope/clickapps/apps-scope.cpp (+2/-2)
scope/clickstore/store-scope.cpp (+2/-2)
scope/tests/test_apps_query.cpp (+2/-2)
scope/tests/test_helpers.h (+2/-2)
scope/tests/test_query.cpp (+1/-1)
To merge this branch: bzr merge lp:~stolowski/unity-scope-click/ensure-one-reader
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) Approve
PS Jenkins bot (community) continuous-integration Approve
dobey (community) Approve
Review via email: mp+227361@code.launchpad.net

Commit message

Ensure click apps scope opens an existing database only and doesn't attempt to initialize it.

Description of the change

Ensure click apps scope opens an existing database only and doesn't attempt to initialize it - this is not needed since we provide an initialized and prepopulated database. Also make sure the reader process in the database stress test just reads from the db.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
dobey (dobey) :
review: Needs Fixing
348. By Paweł Stołowski

Fixed error message.

Revision history for this message
dobey (dobey) :
review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alejandro J. Cura (alecu) wrote :

Tested on device, looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libclickscope/click/departments-db.cpp'
2--- libclickscope/click/departments-db.cpp 2014-07-17 10:49:54 +0000
3+++ libclickscope/click/departments-db.cpp 2014-07-21 13:53:45 +0000
4@@ -39,21 +39,40 @@
5 namespace click
6 {
7
8-std::unique_ptr<click::DepartmentsDb> DepartmentsDb::create_db()
9+std::unique_ptr<click::DepartmentsDb> DepartmentsDb::open(bool create)
10 {
11 auto const path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
12 if (!path.isEmpty())
13 {
14 QDir("/").mkpath(path);
15 const std::string dbpath = path.toStdString() + "/click-departments.db";
16- return std::unique_ptr<DepartmentsDb>(new DepartmentsDb(dbpath));
17+ return std::unique_ptr<DepartmentsDb>(new DepartmentsDb(dbpath, create));
18 }
19 throw std::runtime_error("Cannot determine cache directory");
20 }
21
22-DepartmentsDb::DepartmentsDb(const std::string& name)
23+DepartmentsDb::DepartmentsDb(const std::string& name, bool create)
24 {
25- init_db(name);
26+ db_ = QSqlDatabase::addDatabase("QSQLITE");
27+ db_.setDatabaseName(QString::fromStdString(name));
28+ if (!db_.open())
29+ {
30+ throw std::runtime_error("Cannot open departments database");
31+ }
32+
33+ if (create)
34+ {
35+ init_db();
36+ }
37+ else
38+ {
39+ QSqlQuery query;
40+ // check for existence of meta table to see if we're dealing with uninitialized database
41+ if (!query.exec("SELECT 1 FROM meta"))
42+ {
43+ throw std::runtime_error("Invalid departments database");
44+ }
45+ }
46
47 delete_pkgmap_query_.reset(new QSqlQuery(db_));
48 delete_depts_query_.reset(new QSqlQuery(db_));
49@@ -84,15 +103,8 @@
50 {
51 }
52
53-void DepartmentsDb::init_db(const std::string& name)
54+void DepartmentsDb::init_db()
55 {
56- db_ = QSqlDatabase::addDatabase("QSQLITE");
57- db_.setDatabaseName(QString::fromStdString(name));
58- if (!db_.open())
59- {
60- throw std::runtime_error("Cannot open departments database");
61- }
62-
63 QSqlQuery query;
64
65 // FIXME: for some reason enabling foreign keys gives errors about number of arguments of prepared queries when doing query.exec(); do not enable
66
67=== modified file 'libclickscope/click/departments-db.h'
68--- libclickscope/click/departments-db.h 2014-07-17 06:28:31 +0000
69+++ libclickscope/click/departments-db.h 2014-07-21 13:53:45 +0000
70@@ -59,7 +59,7 @@
71 }
72 };
73
74- DepartmentsDb(const std::string& name);
75+ DepartmentsDb(const std::string& name, bool create = true);
76 DepartmentsDb(const DepartmentsDb& other) = delete;
77 DepartmentsDb& operator=(const DepartmentsDb&) = delete;
78 virtual ~DepartmentsDb();
79@@ -80,10 +80,10 @@
80
81 void store_departments(const click::DepartmentList& depts, const std::string& locale);
82
83- static std::unique_ptr<DepartmentsDb> create_db();
84+ static std::unique_ptr<DepartmentsDb> open(bool create = true);
85
86 protected:
87- void init_db(const std::string& name);
88+ void init_db();
89 void store_departments_(const click::DepartmentList& depts, const std::string& locale);
90 static void report_db_error(const QSqlError& error, const std::string& message);
91
92
93=== modified file 'libclickscope/tests/test_departments-db.cpp'
94--- libclickscope/tests/test_departments-db.cpp 2014-07-17 10:49:54 +0000
95+++ libclickscope/tests/test_departments-db.cpp 2014-07-21 13:53:45 +0000
96@@ -39,8 +39,8 @@
97 class DepartmentsDbCheck : public DepartmentsDb
98 {
99 public:
100- DepartmentsDbCheck(const std::string& name)
101- : DepartmentsDb(name)
102+ DepartmentsDbCheck(const std::string& name, bool create)
103+ : DepartmentsDb(name, create)
104 {
105 }
106
107@@ -63,11 +63,11 @@
108 class DepartmentsDbTest: public ::testing::Test
109 {
110 public:
111- const std::string db_path = TEST_DIR "/departments-db-test.sqlite";
112+ const std::string db_path = ":memory:";
113
114 void SetUp() override
115 {
116- db.reset(new DepartmentsDbCheck(db_path));
117+ db.reset(new DepartmentsDbCheck(db_path, true));
118 db->store_department_name("tools", "", "Tools");
119 db->store_department_name("office", "", "Office");
120
121@@ -90,11 +90,6 @@
122 db->store_package_mapping("game2", "fps");
123 }
124
125- void TearDown() override
126- {
127- unlink(db_path.c_str());
128- }
129-
130 protected:
131 std::unique_ptr<DepartmentsDbCheck> db;
132 };
133@@ -329,7 +324,7 @@
134 {
135 // populate the db initially to make sure reader doesn't fail if it's faster than writer
136 {
137- DepartmentsDb db(db_path);
138+ DepartmentsDb db(db_path, true);
139 populate_departments(&db, 1);
140 }
141
142@@ -342,7 +337,7 @@
143 {
144 SCOPED_TRACE("writer");
145 std::unique_ptr<DepartmentsDb> db;
146- ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path)));
147+ ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path, true)));
148
149 populate_departments(db.get(), NUM_OF_WRITE_OPS);
150
151@@ -359,7 +354,7 @@
152 {
153 SCOPED_TRACE("reader");
154 std::unique_ptr<DepartmentsDb> db;
155- ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path)));
156+ ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path, false)));
157
158 for (int i = 0; i < NUM_OF_READ_OPS; i++)
159 {
160@@ -382,3 +377,10 @@
161 }
162 }
163
164+TEST(DepartmentsDb, testOpenFailsOnUnitializedDb)
165+{
166+ ASSERT_THROW(
167+ {
168+ DepartmentsDb db(":memory:", false);
169+ }, std::runtime_error);
170+}
171
172=== modified file 'scope/clickapps/apps-scope.cpp'
173--- scope/clickapps/apps-scope.cpp 2014-07-15 11:56:05 +0000
174+++ scope/clickapps/apps-scope.cpp 2014-07-21 13:53:45 +0000
175@@ -52,11 +52,11 @@
176 index.reset(new click::Index(client));
177 try
178 {
179- depts_db = click::DepartmentsDb::create_db();
180+ depts_db = click::DepartmentsDb::open(false);
181 }
182 catch (const std::runtime_error& e)
183 {
184- std::cerr << "Failed to get cache directory" << std::endl;
185+ std::cerr << "Failed to open departments db: " << e.what() << std::endl;
186 }
187 }
188
189
190=== modified file 'scope/clickstore/store-scope.cpp'
191--- scope/clickstore/store-scope.cpp 2014-07-16 21:49:11 +0000
192+++ scope/clickstore/store-scope.cpp 2014-07-21 13:53:45 +0000
193@@ -58,11 +58,11 @@
194
195 try
196 {
197- depts_db = click::DepartmentsDb::create_db();
198+ depts_db = click::DepartmentsDb::open(true);
199 }
200 catch (const std::runtime_error& e)
201 {
202- std::cerr << "Failed to get cache directory" << std::endl;
203+ std::cerr << "Failed to open departments db: " << e.what() << std::endl;
204 }
205 }
206
207
208=== modified file 'scope/tests/test_apps_query.cpp'
209--- scope/tests/test_apps_query.cpp 2014-07-17 20:05:57 +0000
210+++ scope/tests/test_apps_query.cpp 2014-07-21 13:53:45 +0000
211@@ -182,7 +182,7 @@
212 {
213 auto clickif = std::make_shared<MockClickInterface>();
214 auto ptrCat = std::make_shared<FakeCategory>("id", "", "", renderer);
215- auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:");
216+ auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:", true);
217
218 // query for root of the departments tree
219 {
220@@ -220,7 +220,7 @@
221 {
222 auto clickif = std::make_shared<MockClickInterface>();
223 auto ptrCat = std::make_shared<FakeCategory>("id", "", "", renderer);
224- auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:");
225+ auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:", true);
226
227 // query for a leaf department
228 {
229
230=== modified file 'scope/tests/test_helpers.h'
231--- scope/tests/test_helpers.h 2014-07-16 11:53:51 +0000
232+++ scope/tests/test_helpers.h 2014-07-21 13:53:45 +0000
233@@ -56,8 +56,8 @@
234 class MockDepartmentsDb : public click::DepartmentsDb
235 {
236 public:
237- MockDepartmentsDb(const std::string& name)
238- : click::DepartmentsDb(name)
239+ MockDepartmentsDb(const std::string& name, bool create)
240+ : click::DepartmentsDb(name, create)
241 {
242 }
243
244
245=== modified file 'scope/tests/test_query.cpp'
246--- scope/tests/test_query.cpp 2014-07-17 23:15:30 +0000
247+++ scope/tests/test_query.cpp 2014-07-21 13:53:45 +0000
248@@ -297,7 +297,7 @@
249 std::make_shared<click::Department>("1-2", "Department three", "http://three.com", false)
250 });
251 DepartmentList init_departments({dept1});
252- auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:");
253+ auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:", true);
254
255 EXPECT_CALL(*depts_db, store_department_name(_, _, _)).Times(3);
256 EXPECT_CALL(*depts_db, store_department_mapping(_, _)).Times(2);

Subscribers

People subscribed via source and target branches

to all changes: