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
=== modified file 'libclickscope/click/departments-db.cpp'
--- libclickscope/click/departments-db.cpp 2014-07-17 10:49:54 +0000
+++ libclickscope/click/departments-db.cpp 2014-07-21 13:53:45 +0000
@@ -39,21 +39,40 @@
39namespace click39namespace click
40{40{
4141
42std::unique_ptr<click::DepartmentsDb> DepartmentsDb::create_db()42std::unique_ptr<click::DepartmentsDb> DepartmentsDb::open(bool create)
43{43{
44 auto const path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);44 auto const path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
45 if (!path.isEmpty())45 if (!path.isEmpty())
46 {46 {
47 QDir("/").mkpath(path);47 QDir("/").mkpath(path);
48 const std::string dbpath = path.toStdString() + "/click-departments.db";48 const std::string dbpath = path.toStdString() + "/click-departments.db";
49 return std::unique_ptr<DepartmentsDb>(new DepartmentsDb(dbpath));49 return std::unique_ptr<DepartmentsDb>(new DepartmentsDb(dbpath, create));
50 }50 }
51 throw std::runtime_error("Cannot determine cache directory");51 throw std::runtime_error("Cannot determine cache directory");
52}52}
5353
54DepartmentsDb::DepartmentsDb(const std::string& name)54DepartmentsDb::DepartmentsDb(const std::string& name, bool create)
55{55{
56 init_db(name);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 if (create)
64 {
65 init_db();
66 }
67 else
68 {
69 QSqlQuery query;
70 // check for existence of meta table to see if we're dealing with uninitialized database
71 if (!query.exec("SELECT 1 FROM meta"))
72 {
73 throw std::runtime_error("Invalid departments database");
74 }
75 }
5776
58 delete_pkgmap_query_.reset(new QSqlQuery(db_));77 delete_pkgmap_query_.reset(new QSqlQuery(db_));
59 delete_depts_query_.reset(new QSqlQuery(db_));78 delete_depts_query_.reset(new QSqlQuery(db_));
@@ -84,15 +103,8 @@
84{103{
85}104}
86105
87void DepartmentsDb::init_db(const std::string& name)106void DepartmentsDb::init_db()
88{107{
89 db_ = QSqlDatabase::addDatabase("QSQLITE");
90 db_.setDatabaseName(QString::fromStdString(name));
91 if (!db_.open())
92 {
93 throw std::runtime_error("Cannot open departments database");
94 }
95
96 QSqlQuery query;108 QSqlQuery query;
97109
98 // FIXME: for some reason enabling foreign keys gives errors about number of arguments of prepared queries when doing query.exec(); do not enable110 // FIXME: for some reason enabling foreign keys gives errors about number of arguments of prepared queries when doing query.exec(); do not enable
99111
=== modified file 'libclickscope/click/departments-db.h'
--- libclickscope/click/departments-db.h 2014-07-17 06:28:31 +0000
+++ libclickscope/click/departments-db.h 2014-07-21 13:53:45 +0000
@@ -59,7 +59,7 @@
59 }59 }
60 };60 };
6161
62 DepartmentsDb(const std::string& name);62 DepartmentsDb(const std::string& name, bool create = true);
63 DepartmentsDb(const DepartmentsDb& other) = delete;63 DepartmentsDb(const DepartmentsDb& other) = delete;
64 DepartmentsDb& operator=(const DepartmentsDb&) = delete;64 DepartmentsDb& operator=(const DepartmentsDb&) = delete;
65 virtual ~DepartmentsDb();65 virtual ~DepartmentsDb();
@@ -80,10 +80,10 @@
8080
81 void store_departments(const click::DepartmentList& depts, const std::string& locale);81 void store_departments(const click::DepartmentList& depts, const std::string& locale);
8282
83 static std::unique_ptr<DepartmentsDb> create_db();83 static std::unique_ptr<DepartmentsDb> open(bool create = true);
8484
85protected:85protected:
86 void init_db(const std::string& name);86 void init_db();
87 void store_departments_(const click::DepartmentList& depts, const std::string& locale);87 void store_departments_(const click::DepartmentList& depts, const std::string& locale);
88 static void report_db_error(const QSqlError& error, const std::string& message);88 static void report_db_error(const QSqlError& error, const std::string& message);
8989
9090
=== modified file 'libclickscope/tests/test_departments-db.cpp'
--- libclickscope/tests/test_departments-db.cpp 2014-07-17 10:49:54 +0000
+++ libclickscope/tests/test_departments-db.cpp 2014-07-21 13:53:45 +0000
@@ -39,8 +39,8 @@
39class DepartmentsDbCheck : public DepartmentsDb39class DepartmentsDbCheck : public DepartmentsDb
40{40{
41public:41public:
42 DepartmentsDbCheck(const std::string& name)42 DepartmentsDbCheck(const std::string& name, bool create)
43 : DepartmentsDb(name)43 : DepartmentsDb(name, create)
44 {44 {
45 }45 }
4646
@@ -63,11 +63,11 @@
63class DepartmentsDbTest: public ::testing::Test63class DepartmentsDbTest: public ::testing::Test
64{64{
65public:65public:
66 const std::string db_path = TEST_DIR "/departments-db-test.sqlite";66 const std::string db_path = ":memory:";
6767
68 void SetUp() override68 void SetUp() override
69 {69 {
70 db.reset(new DepartmentsDbCheck(db_path));70 db.reset(new DepartmentsDbCheck(db_path, true));
71 db->store_department_name("tools", "", "Tools");71 db->store_department_name("tools", "", "Tools");
72 db->store_department_name("office", "", "Office");72 db->store_department_name("office", "", "Office");
7373
@@ -90,11 +90,6 @@
90 db->store_package_mapping("game2", "fps");90 db->store_package_mapping("game2", "fps");
91 }91 }
9292
93 void TearDown() override
94 {
95 unlink(db_path.c_str());
96 }
97
98protected:93protected:
99 std::unique_ptr<DepartmentsDbCheck> db;94 std::unique_ptr<DepartmentsDbCheck> db;
100};95};
@@ -329,7 +324,7 @@
329{324{
330 // populate the db initially to make sure reader doesn't fail if it's faster than writer325 // populate the db initially to make sure reader doesn't fail if it's faster than writer
331 {326 {
332 DepartmentsDb db(db_path);327 DepartmentsDb db(db_path, true);
333 populate_departments(&db, 1);328 populate_departments(&db, 1);
334 }329 }
335330
@@ -342,7 +337,7 @@
342 {337 {
343 SCOPED_TRACE("writer");338 SCOPED_TRACE("writer");
344 std::unique_ptr<DepartmentsDb> db;339 std::unique_ptr<DepartmentsDb> db;
345 ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path)));340 ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path, true)));
346341
347 populate_departments(db.get(), NUM_OF_WRITE_OPS);342 populate_departments(db.get(), NUM_OF_WRITE_OPS);
348343
@@ -359,7 +354,7 @@
359 {354 {
360 SCOPED_TRACE("reader");355 SCOPED_TRACE("reader");
361 std::unique_ptr<DepartmentsDb> db;356 std::unique_ptr<DepartmentsDb> db;
362 ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path)));357 ASSERT_NO_THROW(db.reset(new DepartmentsDb(db_path, false)));
363358
364 for (int i = 0; i < NUM_OF_READ_OPS; i++)359 for (int i = 0; i < NUM_OF_READ_OPS; i++)
365 {360 {
@@ -382,3 +377,10 @@
382 }377 }
383}378}
384379
380TEST(DepartmentsDb, testOpenFailsOnUnitializedDb)
381{
382 ASSERT_THROW(
383 {
384 DepartmentsDb db(":memory:", false);
385 }, std::runtime_error);
386}
385387
=== modified file 'scope/clickapps/apps-scope.cpp'
--- scope/clickapps/apps-scope.cpp 2014-07-15 11:56:05 +0000
+++ scope/clickapps/apps-scope.cpp 2014-07-21 13:53:45 +0000
@@ -52,11 +52,11 @@
52 index.reset(new click::Index(client));52 index.reset(new click::Index(client));
53 try53 try
54 {54 {
55 depts_db = click::DepartmentsDb::create_db();55 depts_db = click::DepartmentsDb::open(false);
56 }56 }
57 catch (const std::runtime_error& e)57 catch (const std::runtime_error& e)
58 {58 {
59 std::cerr << "Failed to get cache directory" << std::endl;59 std::cerr << "Failed to open departments db: " << e.what() << std::endl;
60 }60 }
61}61}
6262
6363
=== modified file 'scope/clickstore/store-scope.cpp'
--- scope/clickstore/store-scope.cpp 2014-07-16 21:49:11 +0000
+++ scope/clickstore/store-scope.cpp 2014-07-21 13:53:45 +0000
@@ -58,11 +58,11 @@
5858
59 try59 try
60 {60 {
61 depts_db = click::DepartmentsDb::create_db();61 depts_db = click::DepartmentsDb::open(true);
62 }62 }
63 catch (const std::runtime_error& e)63 catch (const std::runtime_error& e)
64 {64 {
65 std::cerr << "Failed to get cache directory" << std::endl;65 std::cerr << "Failed to open departments db: " << e.what() << std::endl;
66 }66 }
67}67}
6868
6969
=== modified file 'scope/tests/test_apps_query.cpp'
--- scope/tests/test_apps_query.cpp 2014-07-17 20:05:57 +0000
+++ scope/tests/test_apps_query.cpp 2014-07-21 13:53:45 +0000
@@ -182,7 +182,7 @@
182{182{
183 auto clickif = std::make_shared<MockClickInterface>();183 auto clickif = std::make_shared<MockClickInterface>();
184 auto ptrCat = std::make_shared<FakeCategory>("id", "", "", renderer);184 auto ptrCat = std::make_shared<FakeCategory>("id", "", "", renderer);
185 auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:");185 auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:", true);
186186
187 // query for root of the departments tree187 // query for root of the departments tree
188 {188 {
@@ -220,7 +220,7 @@
220{220{
221 auto clickif = std::make_shared<MockClickInterface>();221 auto clickif = std::make_shared<MockClickInterface>();
222 auto ptrCat = std::make_shared<FakeCategory>("id", "", "", renderer);222 auto ptrCat = std::make_shared<FakeCategory>("id", "", "", renderer);
223 auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:");223 auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:", true);
224224
225 // query for a leaf department225 // query for a leaf department
226 {226 {
227227
=== modified file 'scope/tests/test_helpers.h'
--- scope/tests/test_helpers.h 2014-07-16 11:53:51 +0000
+++ scope/tests/test_helpers.h 2014-07-21 13:53:45 +0000
@@ -56,8 +56,8 @@
56class MockDepartmentsDb : public click::DepartmentsDb56class MockDepartmentsDb : public click::DepartmentsDb
57{57{
58public:58public:
59 MockDepartmentsDb(const std::string& name)59 MockDepartmentsDb(const std::string& name, bool create)
60 : click::DepartmentsDb(name)60 : click::DepartmentsDb(name, create)
61 {61 {
62 }62 }
6363
6464
=== modified file 'scope/tests/test_query.cpp'
--- scope/tests/test_query.cpp 2014-07-17 23:15:30 +0000
+++ scope/tests/test_query.cpp 2014-07-21 13:53:45 +0000
@@ -297,7 +297,7 @@
297 std::make_shared<click::Department>("1-2", "Department three", "http://three.com", false)297 std::make_shared<click::Department>("1-2", "Department three", "http://three.com", false)
298 });298 });
299 DepartmentList init_departments({dept1});299 DepartmentList init_departments({dept1});
300 auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:");300 auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:", true);
301301
302 EXPECT_CALL(*depts_db, store_department_name(_, _, _)).Times(3);302 EXPECT_CALL(*depts_db, store_department_name(_, _, _)).Times(3);
303 EXPECT_CALL(*depts_db, store_department_mapping(_, _)).Times(2);303 EXPECT_CALL(*depts_db, store_department_mapping(_, _)).Times(2);

Subscribers

People subscribed via source and target branches

to all changes: