File: PersistHelper.cpp

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (304 lines) | stat: -rw-r--r-- 13,622 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright 2009- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation
 * nor does it submit to any jurisdiction.
 */

#include "PersistHelper.hpp"

#include <sstream>

#include "TemporaryFile.hpp"
#include "ecflow/core/Ecf.hpp"
#include "ecflow/core/File.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/formatter/DefsWriter.hpp"

using namespace std;
using namespace ecf;

bool PersistHelper::test_persist_and_reload(const Defs& theInMemoryDefs,
                                            PrintStyle::Type_t file_type_on_disk,
                                            bool do_compare) {
    // Write parsed file to disk, and reload, then compare defs, they should be the same
    errorMsg_.clear();
    file_size_ = 0;

    TemporaryFile temporary("tmp_%%%%-%%%%-%%%%-%%%%.def");

    {
        // The file MUST be written in the *SAME* form that it was read, Otherwise they will not compare:
        theInMemoryDefs.write_to_file(temporary.path(), file_type_on_disk);
    }

    // Reload the file we just persisted and compare with in memory defs
    Defs savedDef;
    return reload_from_defs_file(theInMemoryDefs, savedDef, temporary.path(), do_compare);
}

bool PersistHelper::test_defs_checkpt_and_reload(const Defs& theInMemoryDefs, bool do_compare) {
    // Write parsed file to disk, and reload, then compare defs, they should be the same
    errorMsg_.clear();
    file_size_ = 0;

    TemporaryFile temporary("tmp_%%%%-%%%%-%%%%-%%%%.def");

    {
        // The file MUST be written in the *SAME* form that it was read, Otherwise they will not compare:
        theInMemoryDefs.write_to_checkpt_file(temporary.path());
    }

    // Reload the file we just persisted and compare with in memory defs
    Defs savedDef;
    bool reload_result = reload_from_defs_file(theInMemoryDefs, savedDef, temporary.path(), do_compare);
    if (reload_result) {
        return savedDef.checkInvariants(errorMsg_);
    }
    return false;
}

bool PersistHelper::test_cereal_checkpt_and_reload(const Defs& theInMemoryDefs, bool do_compare) {
    errorMsg_.clear();
    file_size_ = 0;
    if (!theInMemoryDefs.checkInvariants(errorMsg_)) {
        return false;
    }

    // Save in memory defs as a check pt file, then restore and compare
    Defs reloaded_defs;
    bool reload_result = reload_from_cereal_checkpt_file(theInMemoryDefs, reloaded_defs, do_compare);
    if (reload_result) {
        return reloaded_defs.checkInvariants(errorMsg_);
    }
    return false;
}

bool PersistHelper::test_state_persist_and_reload_with_checkpt(const Defs& theInMemoryDefs) {
    // Write Defs to disk, and reload, then compare defs reloaded checkpt file, they should be the same
    errorMsg_.clear();
    file_size_ = 0;
    if (!theInMemoryDefs.checkInvariants(errorMsg_)) {
        return false;
    }

    DebugEquality debug_equality; // only as affect in DEBUG build

    TemporaryFile temporary("tmp_%%%%-%%%%-%%%%-%%%%.def");

    {
        // The file MUST be written in the *SAME* form that it was read, Otherwise they will not compare:
        theInMemoryDefs.write_to_checkpt_file(temporary.path()); // will save edit history
    }

    Defs reload_strings_def;
    {
        // Open file, and parse as a string.
        std::string defs_as_string;
        if (!File::open(temporary.path(), defs_as_string)) {
            errorMsg_ += "Could not file file: " + temporary.path();
            return false;
        }
        std::string error_msg, warning;
        if (!reload_strings_def.restore_from_string(defs_as_string, error_msg, warning)) {
            errorMsg_ += error_msg;
            return false;
        }
        if (!reload_strings_def.checkInvariants(errorMsg_)) {
            return false;
        }
    }

    // Reload the file we just persisted and compare with in memory defs
    Defs reloaded_defs;
    if (!reload_from_defs_file(theInMemoryDefs, reloaded_defs, temporary.path())) {
        return false;
    }
    if (!reloaded_defs.checkInvariants(errorMsg_)) {
        return false;
    }

    // Save in memory defs as a check pt file, then restore and compare
    Defs reloaded_cereal_checkPt_defs;
    if (!reload_from_cereal_checkpt_file(theInMemoryDefs, reloaded_cereal_checkPt_defs, true)) {
        return false;
    }
    if (!reloaded_cereal_checkPt_defs.checkInvariants(errorMsg_)) {
        return false;
    }

    // Make sure reloading def's file with state is same as the checkpt file
    bool match = reloaded_defs == reloaded_cereal_checkPt_defs;

    if (!match) {
        std::stringstream ss;
        ss << "\nPersistHelper::test_state_persist_and_reload_with_checkpt\n";
        ss << "In reloaded_defs_file and reloaded_checkPt_defs don't match\n";
        ss << "+++++++++++++ in memory defs  ++++++++++++++++++++++++++++\n";
        ss << ecf::as_string(theInMemoryDefs, PrintStyle::MIGRATE);
        ss << "+++++++++++++ reloaded_defs  ++++++++++++++++++++++++++++\n";
        ss << ecf::as_string(reloaded_defs, PrintStyle::MIGRATE);
        ss << "++++++++++++++ reloaded_checkPt_defs  ++++++++++++++++++++++++++++\n";
        ss << ecf::as_string(reloaded_cereal_checkPt_defs, PrintStyle::MIGRATE);
        errorMsg_ += ss.str();
    }
    else {
        if (compare_edit_history_ && !reloaded_defs.compare_edit_history(reloaded_cereal_checkPt_defs)) {
            std::stringstream ss;
            ss << "\nPersistHelper::test_state_persist_and_reload_with_checkpt  compare_edit_history_\n";
            ss << "In reloaded_defs_file and reloaded_checkPt_defs edit history don't match\n";
            ss << "+++++++++++++ in memory defs  ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(theInMemoryDefs, PrintStyle::MIGRATE);
            ss << "+++++++++++++ reloaded_defs  ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(reloaded_defs, PrintStyle::MIGRATE);
            ss << "++++++++++++++ reloaded_checkPt_defs  ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(reloaded_cereal_checkPt_defs, PrintStyle::MIGRATE);
            errorMsg_ += ss.str();
        }
    }
    if (!reloaded_defs.compare_change_no(reloaded_cereal_checkPt_defs)) {
        errorMsg_ += "\nPersistHelper::test_state_persist_and_reload_with_checkpt: Change numbers don't compare "
                     "between reloaded_defs and reloaded_cereal_checkPt_defs \n";
    }

    // Make sure reloading def's file with state is same as the checkpt file
    match = reload_strings_def == reloaded_cereal_checkPt_defs;
    if (!match) {
        std::stringstream ss;
        ss << "\nPersistHelper::test_state_persist_and_reload_with_checkpt\n";
        ss << "In reloaded_defs file AS STRING and reloaded_checkPt_defs don't match\n";
        ss << "+++++++++++++ in memory defs  ++++++++++++++++++++++++++++\n";
        ss << ecf::as_string(theInMemoryDefs, PrintStyle::MIGRATE);
        ss << "+++++++++++++  reload_strings_def  ++++++++++++++++++++++++++++\n";
        ss << ecf::as_string(reload_strings_def, PrintStyle::MIGRATE);
        ss << "++++++++++++++ reloaded_checkPt_defs  ++++++++++++++++++++++++++++\n";
        ss << ecf::as_string(reloaded_cereal_checkPt_defs, PrintStyle::MIGRATE);
        errorMsg_ += ss.str();
    }
    else {
        if (compare_edit_history_ && !reload_strings_def.compare_edit_history(reloaded_cereal_checkPt_defs)) {
            std::stringstream ss;
            ss << "\nPersistHelper::test_state_persist_and_reload_with_checkpt  compare_edit_history_\n";
            ss << "In reloaded_defs_file and reloaded_checkPt_defs edit history don't match\n";
            ss << "+++++++++++++ in memory defs  ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(theInMemoryDefs, PrintStyle::MIGRATE);
            ss << "+++++++++++++ reload_strings_def ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(reload_strings_def, PrintStyle::MIGRATE);
            ss << "++++++++++++++ reloaded_checkPt_defs  ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(reloaded_cereal_checkPt_defs, PrintStyle::MIGRATE);
            errorMsg_ += ss.str();
        }
    }
    if (!reload_strings_def.compare_change_no(reloaded_cereal_checkPt_defs)) {
        errorMsg_ += "\nPersistHelper::test_state_persist_and_reload_with_checkpt: Change numbers don't compare "
                     "between reload_strings_def and reloaded_cereal_checkPt_defs\n";
    }

    return errorMsg_.empty();
}

bool PersistHelper::reload_from_defs_file(const Defs& theInMemoryDefs,
                                          Defs& reloaded_defs,
                                          const std::string& tmpFilename,
                                          bool do_compare) {
    DebugEquality debug_equality; // only as affect in DEBUG build

    std::string warningMsg;
    if (!reloaded_defs.restore(tmpFilename, errorMsg_, warningMsg)) {
        std::stringstream ss;
        ss << "RE-PARSE failed for " << tmpFilename << "\n";
        errorMsg_ += ss.str();
        return false;
    }

    if (do_compare) {
        // Make sure the file we just parsed match's the one we persisted
        bool match = reloaded_defs == theInMemoryDefs;

        if (!match) {
            std::stringstream ss;
            ss << "\nPersistHelper::reload_from_defs_file\n";
            ss << "In memory and reloaded def's don't match\n";
            ss << "+++++++++++++ Saved/reloaded_defs  ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(reloaded_defs, PrintStyle::STATE);
            ss << "++++++++++++++ In memory def ++++++++++++++++++++++++++++\n";
            ss << ecf::as_string(theInMemoryDefs, PrintStyle::STATE);
            errorMsg_ += ss.str();
        }
        else {
            if (compare_edit_history_ && !reloaded_defs.compare_edit_history(theInMemoryDefs)) {
                std::stringstream ss;
                ss << "\nPersistHelper::reload_from_defs_file compare_edit_history_\n";
                ss << "In memory and reloaded def's don't match\n";
                ss << "+++++++++++++ Saved/reloaded_defs  ++++++++++++++++++++++++++++\n";
                ss << ecf::as_string(reloaded_defs, PrintStyle::MIGRATE);
                ss << "++++++++++++++ In memory def ++++++++++++++++++++++++++++\n";
                ss << ecf::as_string(theInMemoryDefs, PrintStyle::MIGRATE);
                errorMsg_ += ss.str();
            }
        }
        if (!reloaded_defs.compare_change_no(theInMemoryDefs)) {
            errorMsg_ += "\nPersistHelper::reload_from_defs_file: Change numbers don't compare between reloaded_defs  "
                         "and theInMemoryDefs  \n";
        }
    }

    file_size_ = fs::file_size(tmpFilename);
    std::remove(tmpFilename.c_str());
    return errorMsg_.empty();
}

bool PersistHelper::reload_from_cereal_checkpt_file(const Defs& theInMemoryDefs, Defs& reloaded_defs, bool do_compare) {
    // make sure edit history is stored
    TemporaryFile temporary("tmp.check_%%%%-%%%%-%%%%-%%%%");

    theInMemoryDefs.cereal_save_as_checkpt(temporary.path());

    DebugEquality debug_equality; // only as effect in DEBUG build

    try {
        // Parse the file we just persisted and load the defs file into memory.
        reloaded_defs.cereal_restore_from_checkpt(temporary.path());

        if (do_compare) {
            // Make sure the checkpoint the file we just parsed corresponds to the one we stored
            bool match = reloaded_defs == theInMemoryDefs;
            if (!match) {
                std::stringstream ss;
                ss << "\nPersistHelper::reload_from_cereal_checkpt_file\n";
                ss << "In memory and reloaded def's don't match\n";
                ss << "+++++++++++++ Saved/reloaded check pt file ++++++++++++++++++++++++++++\n";
                ss << ecf::as_string(reloaded_defs, PrintStyle::STATE);
                ss << "++++++++++++++ In memory def ++++++++++++++++++++++++++++\n";
                ss << ecf::as_string(theInMemoryDefs, PrintStyle::STATE);
                errorMsg_ += ss.str();
            }
            else {
                if (compare_edit_history_ && !reloaded_defs.compare_edit_history(theInMemoryDefs)) {
                    std::stringstream ss;
                    ss << "\nPersistHelper::reload_from_cereal_checkpt_file  compare_edit_history_\n";
                    ss << "In reloaded_defs_file and reloaded_checkPt_defs edit history don't match\n";
                    ss << "+++++++++++++ Saved/reloaded check pt file ++++++++++++++++++++++++++++\n";
                    ss << ecf::as_string(reloaded_defs, PrintStyle::MIGRATE);
                    ss << "++++++++++++++ theInMemoryDefs  ++++++++++++++++++++++++++++\n";
                    ss << ecf::as_string(theInMemoryDefs, PrintStyle::MIGRATE);
                    errorMsg_ += ss.str();
                }
            }
            if (!reloaded_defs.compare_change_no(theInMemoryDefs)) {
                errorMsg_ += "\nPersistHelper::reload_from_cereal_checkpt_file: Change numbers don't compare between "
                             "reloaded_defs and theInMemoryDefs  \n";
            }
        }
    }
    catch (std::exception& e) {
        errorMsg_ = "PersistHelper::reload_from_cereal_checkpt_file: " + string(e.what());
    }

    file_size_ = temporary.size();

    return errorMsg_.empty();
}