File tree Expand file tree Collapse file tree 3 files changed +17
-6
lines changed Expand file tree Collapse file tree 3 files changed +17
-6
lines changed Original file line number Diff line number Diff line change @@ -131,13 +131,15 @@ error::Result<nlohmann::json> Datastore::Get() const {
131
131
return json_;
132
132
}
133
133
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 */ ) {
135
135
SYNCHRONIZE (mutex_);
136
136
MUST_BE_INITIALIZED;
137
137
138
138
// We will use the transaction mechanism to do the writing. It will also help prevent
139
139
// 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
+ }
141
143
142
144
// Avoid modifying the datastore if the value is the same as what's already there.
143
145
bool changed = true ;
@@ -151,7 +153,10 @@ Error Datastore::Set(const json::json_pointer& p, json v) {
151
153
json_[p] = v;
152
154
transaction_dirty_ = changed;
153
155
154
- return PassError (EndTransaction (true ));
156
+ if (write_store) {
157
+ return PassError (EndTransaction (true ));
158
+ }
159
+ return nullerr;
155
160
}
156
161
157
162
static string FilePath (const string& file_root, const string& suffix) {
Original file line number Diff line number Diff line change @@ -105,11 +105,13 @@ class Datastore {
105
105
106
106
error::Result<nlohmann::json> Get () const ;
107
107
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.
109
111
// / NOTE: Set is not atomic. If the file operation fails, the intermediate object will still be
110
112
// / updated. We may want this to be otherwise in the future, but for now I think that it's preferable.
111
113
// / 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 );
113
115
114
116
protected:
115
117
// / Helper for the public Reset methods
Original file line number Diff line number Diff line change @@ -196,7 +196,11 @@ datetime::Duration UserData::GetServerTimeDiff() const {
196
196
error::Error UserData::SetServerTimeDiff (const datetime::DateTime& serverTimeNow) {
197
197
auto localTimeNow = datetime::DateTime::Now ();
198
198
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 ));
200
204
}
201
205
202
206
datetime::DateTime UserData::ServerTimeToLocal (const datetime::DateTime& server_time) const {
You can’t perform that action at this time.
0 commit comments