MCPcopy Create free account
hub / github.com/comaps/comaps / GpsTrackStorage

Method GpsTrackStorage

libs/map/gps_track_storage.cpp:107–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

105} // namespace
106
107GpsTrackStorage::GpsTrackStorage(string const & filePath) : m_filePath(filePath), m_itemCount(0)
108{
109 auto const createNewFile = [this]
110 {
111 m_stream.open(m_filePath, ios::in | ios::out | ios::binary | ios::trunc);
112
113 if (!m_stream)
114 MYTHROW(OpenException, ("Open file error.", m_filePath));
115
116 if (!WriteVersion(m_stream, kCurrentVersion))
117 MYTHROW(OpenException, ("Write version error.", m_filePath));
118
119 m_itemCount = 0;
120 };
121
122 // Open existing file
123 m_stream.open(m_filePath, ios::in | ios::out | ios::binary);
124
125 if (m_stream)
126 {
127 uint32_t version = 0;
128 if (!ReadVersion(m_stream, version))
129 {
130 LOG(LWARNING, ("Recreating", m_filePath, "because can't read version from it."));
131 m_stream.close();
132 createNewFile();
133 version = kCurrentVersion;
134 }
135
136 if (version == kCurrentVersion)
137 {
138 // Seek to end to get file size
139 m_stream.seekp(0, ios::end);
140 if (!m_stream.good())
141 MYTHROW(OpenException, ("Seek to the end error.", m_filePath));
142
143 auto const fileSize = static_cast<size_t>(m_stream.tellp());
144
145 m_itemCount = GetItemCount(fileSize);
146
147 // Set write position after last item position
148 size_t const offset = GetItemOffset(m_itemCount);
149 m_stream.seekp(offset, ios::beg);
150 if (!m_stream.good())
151 MYTHROW(OpenException, ("Seek to the offset error:", offset, m_filePath));
152
153 LOG(LINFO, ("Restored", m_itemCount, "points from gps track storage"));
154 }
155 else
156 {
157 m_stream.close();
158 // TODO: migration for file m_filePath from version 'version' to version 'kCurrentVersion'
159 // TODO: For now we support only one version, but in future migration may be needed.
160 }
161 }
162
163 if (!m_stream)
164 createNewFile();

Callers

nothing calls this directly

Calls 6

ReadVersionFunction · 0.85
GetItemCountFunction · 0.85
GetItemOffsetFunction · 0.85
openMethod · 0.80
WriteVersionFunction · 0.70
closeMethod · 0.45

Tested by

no test coverage detected