[place-recognition] Change database asynchronous queries to synchronous. 71/64571/1
authorMarcin Masternak <m.masternak@samsung.com>
Fri, 1 Apr 2016 16:46:29 +0000 (18:46 +0200)
committerMarcin Masternak <m.masternak@samsung.com>
Fri, 1 Apr 2016 16:46:29 +0000 (18:46 +0200)
Change-Id: Ibde511e03c713008ca731acd65da27ec6dfb5b28
Signed-off-by: Marcin Masternak <m.masternak@samsung.com>
src/place/recognition/user_places/location_logger.cpp
src/place/recognition/user_places/places_detector.cpp
src/place/recognition/user_places/places_detector.h
src/place/recognition/user_places/visit_detector.cpp

index 3a4fb93b7148e3dd0779ed2d3593dda7ccc40df5..3bcd109f85d405d0844a3c6ae9b1040f6db03376 100644 (file)
@@ -193,7 +193,8 @@ int ctx::LocationLogger::db_insert_log(location_event_s location_event)
        data.set(NULL, LOCATION_COLUMN_METHOD, static_cast<int>(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;
 }
index a3ea9a53e6b07d8a88cfd169c67a014845a7e661..98cb399c4aaf6815da7fee96c5a17755f6a1d488 100644 (file)
@@ -74,53 +74,27 @@ bool ctx::PlacesDetector::onTimerExpired(int timerId)
 {
        _D("");
        db_delete_places();
+       db_delete_old_visits();
+       std::vector<Json> 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<Json>& records)
+std::vector<ctx::Json> 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<std::shared_ptr<Place>> 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<Json> 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::Json> ctx::PlacesDetector::db_get_places()
 {
-       bool ret = db_manager::execute(PLACES_DETECTOR_QUERY_ID_GET_PLACES, GET_PLACES_QUERY, this);
+       std::vector<Json> 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::graph_t> 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<Json> 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<Json> 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<Json> records = db_get_places();
+       std::vector<std::shared_ptr<Place>> 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<int>(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");
 }
 
index fb08ee4597f6f3ff23eaee60e12828065dab9c6f..e0461d9b0e40ca519f12fa18703984b3d93a5553 100644 (file)
 
 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<Json> db_get_visits();
+               std::vector<Json> db_get_places();
                void db_insert_place(const Place &place);
                std::shared_ptr<Place> place_from_merged(visits_t &merged_visits);
                std::vector<std::shared_ptr<Place>> 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<Json>& records);
                std::shared_ptr<components_t> merge_visits(const std::vector<visit_s> &visits);
                std::vector<std::shared_ptr<Place>> get_places();
 
index 84abb5e23eb5398b6358a56c2e19793b58d484eb..26c3bb57dbcf7f0c1ca8e90e405d149914fbb331 100644 (file)
@@ -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;
 }