From 89330c31f4651690955890e3a0e902a1bb4949cf Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Fri, 1 Apr 2016 18:46:29 +0200 Subject: [PATCH] [place-recognition] Change database asynchronous queries to synchronous. Change-Id: Ibde511e03c713008ca731acd65da27ec6dfb5b28 Signed-off-by: Marcin Masternak --- .../user_places/location_logger.cpp | 3 +- .../user_places/places_detector.cpp | 65 +++++++------------ .../recognition/user_places/places_detector.h | 18 +---- .../user_places/visit_detector.cpp | 3 +- 4 files changed, 29 insertions(+), 60 deletions(-) diff --git a/src/place/recognition/user_places/location_logger.cpp b/src/place/recognition/user_places/location_logger.cpp index 3a4fb93..3bcd109 100644 --- a/src/place/recognition/user_places/location_logger.cpp +++ b/src/place/recognition/user_places/location_logger.cpp @@ -193,7 +193,8 @@ int ctx::LocationLogger::db_insert_log(location_event_s location_event) data.set(NULL, LOCATION_COLUMN_METHOD, static_cast(location_event.method)); #endif /* TIZEN_ENGINEER_MODE */ - bool ret = ctx::db_manager::insert(0, LOCATION_TABLE_NAME, data); + int64_t row_id; + bool ret = db_manager::insert_sync(LOCATION_TABLE_NAME, data, &row_id); _D("%s -> DB: location table insert result", ret ? "SUCCESS" : "FAIL"); return ret; } diff --git a/src/place/recognition/user_places/places_detector.cpp b/src/place/recognition/user_places/places_detector.cpp index a3ea9a5..98cb399 100644 --- a/src/place/recognition/user_places/places_detector.cpp +++ b/src/place/recognition/user_places/places_detector.cpp @@ -74,53 +74,27 @@ bool ctx::PlacesDetector::onTimerExpired(int timerId) { _D(""); db_delete_places(); + db_delete_old_visits(); + std::vector records = db_get_visits(); + visits_t visits = visits_from_jsons(records); + process_visits(visits); return true; } -void ctx::PlacesDetector::on_query_result_received(unsigned int query_id, int error, std::vector& records) +std::vector ctx::PlacesDetector::db_get_visits() { - // TODO: - // The below "state machine" approach was choosen because it is not possible to use synchronized database queries in the main thread. - // Probably the more elegant approach would be to use event_driven_thread class where synchronized queries are allowed. - // Consider refactoring. - if (error != ERR_NONE) { - _E("on_query_result_received query_id:%d, error:%d", query_id, error); - } - else if (query_id == PLACES_DETECTOR_QUERY_ID_DELETE_PLACES) { - db_delete_old_visits(); - } - else if (query_id == PLACES_DETECTOR_QUERY_ID_DELETE_OLD_VISITS) { - db_get_visits(); - } - else if (query_id == PLACES_DETECTOR_QUERY_ID_GET_VISITS) { - visits_t visits = visits_from_jsons(records); - process_visits(visits); - } - else if (query_id == PLACES_DETECTOR_QUERY_ID_INSERT_VISIT) { - // Don't do anything. It is fine. - } - else if (query_id == PLACES_DETECTOR_QUERY_ID_INSERT_PLACE) { - // Don't do anything. It is fine. - } - else if (query_id == PLACES_DETECTOR_QUERY_ID_GET_PLACES) { - std::vector> db_places = places_from_jsons(records); - detected_places_update(db_places); - } - else { - _E("on_query_result_received unknown query_id:%d", query_id); - } -} - -void ctx::PlacesDetector::db_get_visits() -{ - bool ret = db_manager::execute(PLACES_DETECTOR_QUERY_ID_GET_VISITS, GET_VISITS_QUERY, this); + std::vector records; + bool ret = db_manager::execute_sync(GET_VISITS_QUERY, &records); _D("load visits execute query result: %s", ret ? "SUCCESS" : "FAIL"); + return records; } -void ctx::PlacesDetector::db_get_places() +std::vector ctx::PlacesDetector::db_get_places() { - bool ret = db_manager::execute(PLACES_DETECTOR_QUERY_ID_GET_PLACES, GET_PLACES_QUERY, this); + std::vector records; + bool ret = db_manager::execute_sync(GET_PLACES_QUERY, &records); _D("load places execute query result: %s", ret ? "SUCCESS" : "FAIL"); + return records; } double ctx::PlacesDetector::double_value_from_json(Json &row, const char* key) @@ -389,7 +363,8 @@ std::shared_ptr ctx::PlacesDetector::graph_from_visits(const std:: void ctx::PlacesDetector::db_delete_places() { - bool ret = db_manager::execute(PLACES_DETECTOR_QUERY_ID_DELETE_PLACES, DELETE_PLACES_QUERY, this); + std::vector records; + bool ret = db_manager::execute_sync(DELETE_PLACES_QUERY, &records); _D("delete places execute query result: %s", ret ? "SUCCESS" : "FAIL"); } @@ -407,8 +382,9 @@ void ctx::PlacesDetector::db_delete_older_visits(time_t threshold) std::stringstream query; query << "DELETE FROM " << VISIT_TABLE; query << " WHERE " << VISIT_COLUMN_END_TIME << " < " << threshold; - // query << " AND 0"; // Always false condition. Uncomment it for not deleting any visit during development. - bool ret = db_manager::execute(PLACES_DETECTOR_QUERY_ID_DELETE_OLD_VISITS, query.str().c_str(), this); + // query << " AND 0"; // XXX: Always false condition. Uncomment it for not deleting any visit during development. + std::vector records; + bool ret = db_manager::execute_sync(query.str().c_str(), &records); _D("delete old visits execute query result: %s", ret ? "SUCCESS" : "FAIL"); } @@ -419,7 +395,9 @@ ctx::PlacesDetector::PlacesDetector(bool test_mode_) return; } db_create_table(); - db_get_places(); + std::vector records = db_get_places(); + std::vector> db_places = places_from_jsons(records); + detected_places_update(db_places); } void ctx::PlacesDetector::db_create_table() @@ -442,7 +420,8 @@ void ctx::PlacesDetector::db_insert_place(const Place &place) data.set(NULL, PLACE_COLUMN_WIFI_APS, place.wifi_aps); data.set(NULL, PLACE_COLUMN_CREATE_DATE, static_cast(place.create_date)); - bool ret = db_manager::insert(PLACES_DETECTOR_QUERY_ID_INSERT_PLACE, PLACE_TABLE, data); + int64_t row_id; + bool ret = db_manager::insert_sync(PLACE_TABLE, data, &row_id); _D("insert place execute query result: %s", ret ? "SUCCESS" : "FAIL"); } diff --git a/src/place/recognition/user_places/places_detector.h b/src/place/recognition/user_places/places_detector.h index fb08ee4..e0461d9 100644 --- a/src/place/recognition/user_places/places_detector.h +++ b/src/place/recognition/user_places/places_detector.h @@ -26,16 +26,7 @@ namespace ctx { - enum { - PLACES_DETECTOR_QUERY_ID_DELETE_PLACES = 1, - PLACES_DETECTOR_QUERY_ID_DELETE_OLD_VISITS = 2, - PLACES_DETECTOR_QUERY_ID_GET_VISITS = 3, - PLACES_DETECTOR_QUERY_ID_INSERT_VISIT = 4, - PLACES_DETECTOR_QUERY_ID_INSERT_PLACE = 5, - PLACES_DETECTOR_QUERY_ID_GET_PLACES = 6 - }; - - class PlacesDetector : public ITimerListener, public db_listener_iface { + class PlacesDetector : public ITimerListener { private: bool test_mode; @@ -50,8 +41,8 @@ namespace ctx { void db_delete_places(); void db_delete_old_visits(); void db_delete_older_visits(time_t threshold); - void db_get_visits(); - void db_get_places(); + std::vector db_get_visits(); + std::vector db_get_places(); void db_insert_place(const Place &place); std::shared_ptr place_from_merged(visits_t &merged_visits); std::vector> detected_places; @@ -64,9 +55,6 @@ namespace ctx { static void merge_location(const visits_t &merged_visits, Place &place); PlacesDetector(bool test_mode_ = false); bool onTimerExpired(int timerId); - void on_creation_result_received(unsigned int query_id, int error) {} - void on_insertion_result_received(unsigned int query_id, int error, int64_t row_id) {} - void on_query_result_received(unsigned int query_id, int error, std::vector& records); std::shared_ptr merge_visits(const std::vector &visits); std::vector> get_places(); diff --git a/src/place/recognition/user_places/visit_detector.cpp b/src/place/recognition/user_places/visit_detector.cpp index 84abb5e..26c3bb5 100644 --- a/src/place/recognition/user_places/visit_detector.cpp +++ b/src/place/recognition/user_places/visit_detector.cpp @@ -420,7 +420,8 @@ int ctx::VisitDetector::db_insert_visit(visit_s visit) _D("db: visit table insert interval: (%d, %d)", visit.interval.start, visit.interval.end); #endif /* TIZEN_ENGINEER_MODE */ - bool ret = db_manager::insert(0, VISIT_TABLE, data); + int64_t row_id; + bool ret = db_manager::insert_sync(VISIT_TABLE, data, &row_id); _D("db: visit table insert result: %s", ret ? "SUCCESS" : "FAIL"); return ret; } -- 2.34.1