diff options
author | Sergey Yershov <syershov@maps.me> | 2017-06-21 10:14:58 +0300 |
---|---|---|
committer | Yuri Gorshenin <mipt.vi002@gmail.com> | 2017-07-05 16:41:38 +0300 |
commit | a03c31fa74cd3e5772f6ea5d996699d4aa863cd5 (patch) | |
tree | 5a7e0a8b09598e8866d8f818d3b218c39bdc32e5 /generator | |
parent | 2e7431948be3071d401affc15b99a1a751595ab3 (diff) |
Review fixes
Diffstat (limited to 'generator')
-rw-r--r-- | generator/generator_tests/generator_tests.pro | 2 | ||||
-rw-r--r-- | generator/generator_tests/ugc_test.cpp | 7 | ||||
-rw-r--r-- | generator/ugc_db.cpp | 53 | ||||
-rw-r--r-- | generator/ugc_db.hpp | 8 | ||||
-rw-r--r-- | generator/ugc_translator.cpp | 19 | ||||
-rw-r--r-- | generator/ugc_translator.hpp | 3 |
6 files changed, 45 insertions, 47 deletions
diff --git a/generator/generator_tests/generator_tests.pro b/generator/generator_tests/generator_tests.pro index 4834da4c57..6786833f58 100644 --- a/generator/generator_tests/generator_tests.pro +++ b/generator/generator_tests/generator_tests.pro @@ -47,4 +47,4 @@ SOURCES += \ tag_admixer_test.cpp \ tesselator_test.cpp \ triangles_tree_coding_test.cpp \ - ugc_test.cpp \ + ugc_test.cpp \ diff --git a/generator/generator_tests/ugc_test.cpp b/generator/generator_tests/ugc_test.cpp index 548cf8b32f..759fdb42ca 100644 --- a/generator/generator_tests/ugc_test.cpp +++ b/generator/generator_tests/ugc_test.cpp @@ -6,8 +6,7 @@ #include "ugc/types.hpp" - -std::string database(R"LLL( +std::string g_database(R"LLL( PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE agg (id bigint, data blob); @@ -28,7 +27,7 @@ std::string database(R"LLL( UNIT_TEST(UGC_SmokeTest) { generator::UGCDB db(":memory:"); - bool create = db.Exec(database); + bool create = db.Exec(g_database); TEST(create, ("Can't open database")); osm::Id id = osm::Id(1); std::vector<uint8_t> blob; @@ -41,7 +40,7 @@ UNIT_TEST(UGC_SmokeTest) UNIT_TEST(UGC_TranslateRatingTest) { generator::UGCTranslator tr; - tr.CreateRatings(database); + tr.CreateRatings(g_database); osm::Id id = osm::Id(6); ugc::UGC ugc; diff --git a/generator/ugc_db.cpp b/generator/ugc_db.cpp index e30543e7b0..0ad91153c3 100644 --- a/generator/ugc_db.cpp +++ b/generator/ugc_db.cpp @@ -3,25 +3,29 @@ #include "base/logging.hpp" #include "base/macros.hpp" -#include <iostream> +#include <sqlite3.h> -namespace { - struct Results - { - size_t counter = 0; - std::stringstream values; - }; +#include <sstream> + +namespace +{ +struct Results +{ + bool empty = true; + std::ostringstream values; +}; } // namespace namespace generator { - -static int callback(void * results_ptr, int argc, char **argv, char **azColName) +static int callback(void * results_ptr, int argc, char ** argv, char ** azColName) { Results & results = *reinterpret_cast<Results *>(results_ptr); - for(size_t i=0; i<argc; i++) + for (size_t i = 0; i < argc; i++) { - if (results.counter++) + if (results.empty) + results.empty = false; + else results.values << ","; results.values << (argv[i] ? argv[i] : "{}"); @@ -31,36 +35,33 @@ static int callback(void * results_ptr, int argc, char **argv, char **azColName) UGCDB::UGCDB(std::string const & path) { - int rc; - - rc = sqlite3_open(path.c_str(), &m_db); - if(rc) + int rc = sqlite3_open(path.c_str(), &m_db); + if (rc) { LOG(LERROR, ("Can't open database:", sqlite3_errmsg(m_db))); sqlite3_close(m_db); m_db = nullptr; - return; } } UGCDB::~UGCDB() { - if(m_db) + if (m_db) sqlite3_close(m_db); } bool UGCDB::Get(osm::Id const & id, std::vector<uint8_t> & blob) { - if(!m_db) + if (!m_db) return false; - char *zErrMsg = 0; - std::stringstream cmd; Results results; results.values << "["; + + std::ostringstream cmd; cmd << "SELECT data FROM agg WHERE id=" << id.OsmId() << ";"; -// Leaves comment for debug purposes -// std::cout << cmd.str() << std::endl; + + char * zErrMsg = nullptr; auto rc = sqlite3_exec(m_db, cmd.str().c_str(), callback, &results, &zErrMsg); if (rc != SQLITE_OK) { @@ -75,12 +76,13 @@ bool UGCDB::Get(osm::Id const & id, std::vector<uint8_t> & blob) bool UGCDB::Exec(std::string const & statement) { - if(!m_db) + if (!m_db) return false; - char *zErrMsg = 0; + char * zErrMsg = nullptr; auto rc = sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, &zErrMsg); - if( rc!=SQLITE_OK ){ + if (rc != SQLITE_OK) + { LOG(LERROR, ("SQL error:", zErrMsg)); sqlite3_free(zErrMsg); return false; @@ -88,7 +90,6 @@ bool UGCDB::Exec(std::string const & statement) return true; } - bool UGCDB::ValueToBlob(std::string const & src, std::vector<uint8_t> & blob) { blob.assign(src.cbegin(), src.cend()); diff --git a/generator/ugc_db.hpp b/generator/ugc_db.hpp index d24316e207..5c38da8ae8 100644 --- a/generator/ugc_db.hpp +++ b/generator/ugc_db.hpp @@ -9,13 +9,13 @@ #include <string> #include <vector> -#include <sqlite3.h> - namespace osm { class Id; } +struct sqlite3; + namespace generator { DECLARE_EXCEPTION(DBNotFound, RootException); @@ -25,11 +25,13 @@ class UGCDB public: UGCDB(std::string const & path); ~UGCDB(); + WARN_UNUSED_RESULT bool Get(osm::Id const & id, std::vector<uint8_t> & blob); WARN_UNUSED_RESULT bool Exec(std::string const & statement); + private: bool ValueToBlob(std::string const & src, std::vector<uint8_t> & blob); -private: + sqlite3 * m_db = nullptr; }; } // namespace generator diff --git a/generator/ugc_translator.cpp b/generator/ugc_translator.cpp index 8a36e8bd32..b3becb7cdc 100644 --- a/generator/ugc_translator.cpp +++ b/generator/ugc_translator.cpp @@ -6,15 +6,12 @@ namespace generator { -UGCTranslator::UGCTranslator() - : m_dbRatings(":memory:") - , m_dbReviews(":memory:") -{} +UGCTranslator::UGCTranslator() : m_dbRatings(":memory:"), m_dbReviews(":memory:") {} UGCTranslator::UGCTranslator(std::string const & path) - : m_dbRatings(path + ".ratings") - , m_dbReviews(path + ".reviews") -{} + : m_dbRatings(path + ".ratings"), m_dbReviews(path + ".reviews") +{ +} bool UGCTranslator::TranslateUGC(osm::Id const & id, ugc::UGC & ugc) { @@ -35,7 +32,6 @@ void UGCTranslator::CreateReviews(std::string const & data) UNUSED_VALUE(rc); } - bool UGCTranslator::TranslateRating(UGCDB & db, osm::Id const id, ugc::Rating & rating) { std::vector<uint8_t> blob; @@ -47,7 +43,7 @@ bool UGCTranslator::TranslateRating(UGCDB & db, osm::Id const id, ugc::Rating & my::Json jsonRoot(result); size_t size = json_array_size(jsonRoot.get()); - for (size_t i=0; i<size; ++i) + for (size_t i = 0; i < size; ++i) { json_t * el = json_array_get(jsonRoot.get(), i); double ratingValue = 0; @@ -56,7 +52,7 @@ bool UGCTranslator::TranslateRating(UGCDB & db, osm::Id const id, ugc::Rating & FromJSONObject(el, "value", ratingValue); FromJSONObject(el, "criterion_id", translationKeyId); - std::stringstream translationKey; + std::ostringstream translationKey; translationKey << "TranslationKey" << translationKeyId; rating.m_ratings.emplace_back(translationKey.str(), static_cast<float>(ratingValue)); } @@ -64,13 +60,12 @@ bool UGCTranslator::TranslateRating(UGCDB & db, osm::Id const id, ugc::Rating & return true; } - bool UGCTranslator::TranslateReview(UGCDB & db, osm::Id const id, std::vector<ugc::Review> & review) { return true; } -//bool UGCTranslator::TranslateAttribute(UGCDB & db, osm::Id const id, ugc::Attribute & attribute) +// bool UGCTranslator::TranslateAttribute(UGCDB & db, osm::Id const id, ugc::Attribute & attribute) //{ // return false; //} diff --git a/generator/ugc_translator.hpp b/generator/ugc_translator.hpp index 5b47bb71b3..1499120319 100644 --- a/generator/ugc_translator.hpp +++ b/generator/ugc_translator.hpp @@ -17,11 +17,12 @@ public: // For testing only void CreateRatings(std::string const & data); void CreateReviews(std::string const & data); + private: bool TranslateRating(UGCDB & db, osm::Id const id, ugc::Rating & rating); bool TranslateReview(UGCDB & db, osm::Id const id, std::vector<ugc::Review> & review); // bool TranslateAttribute(UGCDB & db, osm::Id const id, ugc::Attribute & attribute); -private: + UGCDB m_dbRatings; UGCDB m_dbReviews; }; |