forked from organicmaps/organicmaps
Review fixes
This commit is contained in:
parent
2e7431948b
commit
a03c31fa74
6 changed files with 45 additions and 47 deletions
|
@ -47,4 +47,4 @@ SOURCES += \
|
|||
tag_admixer_test.cpp \
|
||||
tesselator_test.cpp \
|
||||
triangles_tree_coding_test.cpp \
|
||||
ugc_test.cpp \
|
||||
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;
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
//}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue