-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathexecution_plan_serialization.cpp
More file actions
110 lines (93 loc) · 2.85 KB
/
execution_plan_serialization.cpp
File metadata and controls
110 lines (93 loc) · 2.85 KB
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
// Copyright (C) 2017-2019 Egor Pugin <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "execution_plan.h"
#include <sw/support/serialization.h>
#include <boost/serialization/access.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <primitives/exceptions.h>
#include <fstream>
#include "execution_plan_serialization_boost.h"
static const int serialization_version = 2;
namespace sw
{
namespace driver
{
struct Command;
}
enum SerializationType
{
BoostSerializationBinaryArchive,
BoostSerializationTextArchive,
};
std::tuple<std::unordered_set<std::shared_ptr<builder::Command>>, ExecutionPlan>
ExecutionPlan::load(const path &p, const SwBuilderContext &swctx, int type)
{
std::unordered_set<std::shared_ptr<builder::Command>> commands;
auto load = [&commands](auto &ar)
{
int version;
ar >> version;
if (version != serialization_version)
{
throw SW_RUNTIME_ERROR("Incorrect archive version (" + std::to_string(version) + "), expected (" +
std::to_string(serialization_version) + "), run configure command again");
}
path cp;
ar >> cp;
fs::current_path(cp);
ar >> commands;
};
if (type == 0)
{
std::ifstream ifs(p, std::ios_base::in | std::ios_base::binary);
if (!ifs)
throw SW_RUNTIME_ERROR("Cannot read file: " + normalize_path(p));
boost::archive::binary_iarchive ia(ifs);
load(ia);
}
else if (type == 1)
{
std::ifstream ifs(p);
if (!ifs)
throw SW_RUNTIME_ERROR("Cannot read file: " + normalize_path(p));
boost::archive::text_iarchive ia(ifs);
load(ia);
}
// some setup
for (auto &c : commands)
c->setContext(swctx);
return { commands, create(commands) };
}
void ExecutionPlan::save(const path &p, int type) const
{
fs::create_directories(p.parent_path());
auto save = [this](auto &ar)
{
ar << serialization_version;
ar << fs::current_path();
ar << commands;
};
if (type == 0)
{
std::ofstream ofs(p, std::ios_base::out | std::ios_base::binary);
if (!ofs)
throw SW_RUNTIME_ERROR("Cannot write file: " + normalize_path(p));
boost::archive::binary_oarchive oa(ofs);
save(oa);
}
else if (type == 1)
{
std::ofstream ofs(p);
if (!ofs)
throw SW_RUNTIME_ERROR("Cannot write file: " + normalize_path(p));
boost::archive::text_oarchive oa(ofs);
save(oa);
}
}
}