Commit 654566558031b3a9f84ac7587eec6f4182d1d49a

Authored by Wiebe Cazemier
1 parent ce161b1f

Start converting session file to v2, for properties

persistencefile.cpp
... ... @@ -320,3 +320,8 @@ void PersistenceFile::closeFile()
320 320 throw std::runtime_error(formatString("Saving '%s' failed: rename of temp file to target failed with: %s", filePath.c_str(), strerror(errno)));
321 321 }
322 322 }
  323 +
  324 +const std::string &PersistenceFile::getFilePath() const
  325 +{
  326 + return this->filePath;
  327 +}
... ...
persistencefile.h
... ... @@ -87,6 +87,8 @@ public:
87 87 void openWrite(const std::string &versionString);
88 88 void openRead();
89 89 void closeFile();
  90 +
  91 + const std::string &getFilePath() const;
90 92 };
91 93  
92 94 #endif // PERSISTENCEFILE_H
... ...
sessionsandsubscriptionsdb.cpp
... ... @@ -41,7 +41,7 @@ SessionsAndSubscriptionsDB::SessionsAndSubscriptionsDB(const std::string &filePa
41 41  
42 42 void SessionsAndSubscriptionsDB::openWrite()
43 43 {
44   - PersistenceFile::openWrite(MAGIC_STRING_SESSION_FILE_V1);
  44 + PersistenceFile::openWrite(MAGIC_STRING_SESSION_FILE_V2);
45 45 }
46 46  
47 47 void SessionsAndSubscriptionsDB::openRead()
... ... @@ -50,11 +50,13 @@ void SessionsAndSubscriptionsDB::openRead()
50 50  
51 51 if (detectedVersionString == MAGIC_STRING_SESSION_FILE_V1)
52 52 readVersion = ReadVersion::v1;
  53 + else if (detectedVersionString == MAGIC_STRING_SESSION_FILE_V2)
  54 + readVersion = ReadVersion::v2;
53 55 else
54 56 throw std::runtime_error("Unknown file version.");
55 57 }
56 58  
57   -SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV1()
  59 +SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV2()
58 60 {
59 61 SessionsAndSubscriptionsResult result;
60 62  
... ... @@ -74,11 +76,11 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV1()
74 76 if (eofFound)
75 77 continue;
76 78  
77   - std::vector<char> reserved(RESERVED_SPACE_SESSIONS_DB_V1);
  79 + std::vector<char> reserved(RESERVED_SPACE_SESSIONS_DB_V2);
78 80  
79 81 for (uint32_t i = 0; i < nrOfSessions; i++)
80 82 {
81   - readCheck(buf.data(), 1, RESERVED_SPACE_SESSIONS_DB_V1, f);
  83 + readCheck(buf.data(), 1, RESERVED_SPACE_SESSIONS_DB_V2, f);
82 84  
83 85 uint32_t usernameLength = readUint32(eofFound);
84 86 readCheck(buf.data(), 1, usernameLength, f);
... ... @@ -188,8 +190,8 @@ void SessionsAndSubscriptionsDB::saveData(const std::vector&lt;std::unique_ptr&lt;Sess
188 190 if (!f)
189 191 return;
190 192  
191   - char reserved[RESERVED_SPACE_SESSIONS_DB_V1];
192   - std::memset(reserved, 0, RESERVED_SPACE_SESSIONS_DB_V1);
  193 + char reserved[RESERVED_SPACE_SESSIONS_DB_V2];
  194 + std::memset(reserved, 0, RESERVED_SPACE_SESSIONS_DB_V2);
193 195  
194 196 const int64_t start_stamp = Session::getProgramStartedAtUnixTimestamp();
195 197 logger->logf(LOG_DEBUG, "Saving program first start time stamp as %ld", start_stamp);
... ... @@ -203,7 +205,7 @@ void SessionsAndSubscriptionsDB::saveData(const std::vector&lt;std::unique_ptr&lt;Sess
203 205  
204 206 writeRowHeader();
205 207  
206   - writeCheck(reserved, 1, RESERVED_SPACE_SESSIONS_DB_V1, f);
  208 + writeCheck(reserved, 1, RESERVED_SPACE_SESSIONS_DB_V2, f);
207 209  
208 210 writeUint32(ses->username.length());
209 211 writeCheck(ses->username.c_str(), 1, ses->username.length(), f);
... ... @@ -294,7 +296,9 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readData()
294 296 return defaultResult;
295 297  
296 298 if (readVersion == ReadVersion::v1)
297   - return readDataV1();
  299 + logger->logf(LOG_WARNING, "File '%s' is version 1, an internal development version that was never finalized. Not reading.", getFilePath().c_str());
  300 + if (readVersion == ReadVersion::v2)
  301 + return readDataV2();
298 302  
299 303 return defaultResult;
300 304 }
... ...
sessionsandsubscriptionsdb.h
... ... @@ -25,7 +25,8 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
25 25 #include "session.h"
26 26  
27 27 #define MAGIC_STRING_SESSION_FILE_V1 "FlashMQRetainedDBv1"
28   -#define RESERVED_SPACE_SESSIONS_DB_V1 32
  28 +#define MAGIC_STRING_SESSION_FILE_V2 "FlashMQRetainedDBv2"
  29 +#define RESERVED_SPACE_SESSIONS_DB_V2 32
29 30  
30 31 /**
31 32 * @brief The SubscriptionForSerializing struct contains the fields we're interested in when saving a subscription.
... ... @@ -51,12 +52,13 @@ class SessionsAndSubscriptionsDB : public PersistenceFile
51 52 enum class ReadVersion
52 53 {
53 54 unknown,
54   - v1
  55 + v1,
  56 + v2
55 57 };
56 58  
57 59 ReadVersion readVersion = ReadVersion::unknown;
58 60  
59   - SessionsAndSubscriptionsResult readDataV1();
  61 + SessionsAndSubscriptionsResult readDataV2();
60 62 void writeRowHeader();
61 63 public:
62 64 SessionsAndSubscriptionsDB(const std::string &filePath);
... ...