diff options
author | Constantin Shalnev <c.shalnev@corp.mail.ru> | 2015-12-10 18:02:55 +0300 |
---|---|---|
committer | Constantin Shalnev <c.shalnev@corp.mail.ru> | 2016-01-13 15:54:27 +0300 |
commit | 77cf1357d01ab9a3346d1f1f0f2e433d21bb7e5c (patch) | |
tree | dd6564bd9f9a7f99558d2a48970042fa5daa2184 /map/gps_track.cpp | |
parent | 206ec64ce542d5f0e8be396f86b4568d455bc09f (diff) |
Implemented GpsTrackStorage
Diffstat (limited to 'map/gps_track.cpp')
-rw-r--r-- | map/gps_track.cpp | 75 |
1 files changed, 19 insertions, 56 deletions
diff --git a/map/gps_track.cpp b/map/gps_track.cpp index 04ac1039dc..a753ab957b 100644 --- a/map/gps_track.cpp +++ b/map/gps_track.cpp @@ -139,7 +139,7 @@ void GpsTrack::ScheduleTask() ProcessPoints(); } - CloseFile(); + m_storage.reset(); }); } @@ -147,50 +147,18 @@ void GpsTrack::ScheduleTask() m_cv.notify_one(); } -void GpsTrack::LazyInitFile() +void GpsTrack::InitStorageIfNeed() { - if (m_file) - return; - - m_file = make_unique<GpsTrackFile>(); - - // Open or create gps track file - try - { - if (!m_file->Open(m_filePath, m_maxItemCount)) - { - if (!m_file->Create(m_filePath, m_maxItemCount)) - { - LOG(LINFO, ("Cannot open or create GpsTrackFile:", m_filePath)); - m_file.reset(); - } - else - { - LOG(LINFO, ("GpsTrackFile has been created:", m_filePath)); - } - } - } - catch (RootException & e) - { - LOG(LINFO, ("GpsTrackFile has caused exception:", e.Msg())); - m_file.reset(); - } -} - -void GpsTrack::CloseFile() -{ - if (!m_file) + if (m_storage) return; try { - m_file->Close(); - m_file.reset(); + m_storage = make_unique<GpsTrackStorage>(m_filePath, m_maxItemCount); } catch (RootException & e) { - LOG(LINFO, ("GpsTrackFile.Close has caused exception:", e.Msg())); - m_file.reset(); + LOG(LINFO, ("Storage has not been created:", e.Msg())); } } @@ -200,25 +168,24 @@ void GpsTrack::InitCollection(hours duration) m_collection = make_unique<GpsTrackCollection>(m_maxItemCount, duration); - LazyInitFile(); - - if (!m_file) + InitStorageIfNeed(); + if (!m_storage) return; try { - m_file->ForEach([this](TItem const & info)->bool + m_storage->ForEach([this](TItem const & info)->bool { pair<size_t, size_t> evictedIds; m_collection->Add(info, evictedIds); return true; }); } - catch (GpsTrackFile::ReadFileException & e) + catch (RootException & e) { - LOG(LINFO, ("GpsTrackFile.ForEach has caused exception:", e.Msg())); + LOG(LINFO, ("Storage has caused exception:", e.Msg())); m_collection->Clear(); - m_file.reset(); + m_storage.reset(); } } @@ -240,7 +207,7 @@ void GpsTrack::ProcessPoints() if (!m_collection && HasCallback()) InitCollection(duration); - UpdateFile(needClear, points); + UpdateStorage(needClear, points); if (!m_collection) return; @@ -254,27 +221,23 @@ bool GpsTrack::HasCallback() return m_callback != nullptr; } -void GpsTrack::UpdateFile(bool needClear, vector<TItem> const & points) +void GpsTrack::UpdateStorage(bool needClear, vector<TItem> const & points) { - // Update file, if need - // If file exception happens, then drop the file. - - LazyInitFile(); - - if (!m_file) + InitStorageIfNeed(); + if (!m_storage) return; try { if (needClear) - m_file->Clear(); + m_storage->Clear(); - m_file->Append(points); + m_storage->Append(points); } catch (RootException & e) { - LOG(LINFO, ("GpsTrackFile.Append has caused exception:", e.Msg())); - m_file.reset(); + LOG(LINFO, ("Storage has caused exception:", e.Msg())); + m_storage.reset(); } } |