Skip to content

Commit cfef293

Browse files
authored
Merge pull request #21 from adam-p/no-store-time-diff
don't immediately write server time diff to datastore
2 parents e96df62 + 640e120 commit cfef293

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

datastore.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ error::Result<nlohmann::json> Datastore::Get() const {
131131
return json_;
132132
}
133133

134-
Error Datastore::Set(const json::json_pointer& p, json v) {
134+
Error Datastore::Set(const json::json_pointer& p, json v, bool write_store/*=true*/) {
135135
SYNCHRONIZE(mutex_);
136136
MUST_BE_INITIALIZED;
137137

138138
// We will use the transaction mechanism to do the writing. It will also help prevent
139139
// changes to the stored value between the time we check it and the time we set it.
140-
BeginTransaction();
140+
if (write_store) {
141+
BeginTransaction();
142+
}
141143

142144
// Avoid modifying the datastore if the value is the same as what's already there.
143145
bool changed = true;
@@ -151,7 +153,10 @@ Error Datastore::Set(const json::json_pointer& p, json v) {
151153
json_[p] = v;
152154
transaction_dirty_ = changed;
153155

154-
return PassError(EndTransaction(true));
156+
if (write_store) {
157+
return PassError(EndTransaction(true));
158+
}
159+
return nullerr;
155160
}
156161

157162
static string FilePath(const string& file_root, const string& suffix) {

datastore.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ class Datastore {
105105

106106
error::Result<nlohmann::json> Get() const;
107107

108-
// Sets the value v in the datastore at path p.
108+
/// Sets the value v in the datastore at path p.
109+
/// If write_store is false, the change will stored in memory but not written to disk.
110+
/// However, the dirty flag will be set and the change will be in the next write.
109111
/// NOTE: Set is not atomic. If the file operation fails, the intermediate object will still be
110112
/// updated. We may want this to be otherwise in the future, but for now I think that it's preferable.
111113
/// Returns false if the file operation failed.
112-
error::Error Set(const json::json_pointer& p, json v);
114+
error::Error Set(const json::json_pointer& p, json v, bool write_store=true);
113115

114116
protected:
115117
/// Helper for the public Reset methods

userdata.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ datetime::Duration UserData::GetServerTimeDiff() const {
196196
error::Error UserData::SetServerTimeDiff(const datetime::DateTime& serverTimeNow) {
197197
auto localTimeNow = datetime::DateTime::Now();
198198
auto diff = serverTimeNow.Diff(localTimeNow);
199-
return PassError(datastore_.Set(kServerTimeDiffPtr, datetime::DurationToInt64(diff)));
199+
// Updating the server time diff isn't so important that it needs to be written to disk
200+
// immediately. Also, it is generally done outside of a transaction and then followed
201+
// by a transaction, so it can lead to rapid datastore updates (which we suspect can
202+
// cause corruption issues).
203+
return PassError(datastore_.Set(kServerTimeDiffPtr, datetime::DurationToInt64(diff), /*write_store=*/false));
200204
}
201205

202206
datetime::DateTime UserData::ServerTimeToLocal(const datetime::DateTime& server_time) const {

0 commit comments

Comments
 (0)