forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexJSONHelpers.cxx
More file actions
230 lines (202 loc) · 5.27 KB
/
IndexJSONHelpers.cxx
File metadata and controls
230 lines (202 loc) · 5.27 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
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
// Copyright 2019-2025 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "IndexJSONHelpers.h"
#include <rapidjson/reader.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/error/en.h>
#include <stack>
#include <iostream>
namespace o2::framework
{
namespace
{
struct IndexRecordsReader : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, IndexRecordsReader> {
using Ch = rapidjson::UTF8<>::Ch;
using SizeType = rapidjson::SizeType;
enum struct State {
IN_START,
IN_LIST,
IN_RECORD,
IN_ERROR
};
std::stack<State> states;
std::ostringstream debug;
std::vector<o2::soa::IndexRecord> records;
std::string currentKey;
std::string label;
std::string columnLabel;
o2::soa::IndexKind kind;
int pos;
IndexRecordsReader()
{
debug << ">>> Start" << std::endl;
states.push(State::IN_START);
}
bool StartArray()
{
debug << "StartArray()" << std::endl;
if (states.top() == State::IN_START && currentKey.compare("records") == 0) {
states.push(State::IN_LIST);
return true;
}
states.push(State::IN_ERROR);
return false;
}
bool EndArray(SizeType)
{
debug << "EndArray()" << std::endl;
if (states.top() == State::IN_LIST) {
// records done
states.pop();
return true;
}
states.push(State::IN_ERROR);
return false;
}
bool Key(const Ch* str, SizeType, bool)
{
debug << "Key(" << str << ")" << std::endl;
currentKey = str;
if (states.top() == State::IN_START) {
if (currentKey.compare("records") == 0) {
return true;
}
}
if (states.top() == State::IN_RECORD) {
if (currentKey.compare("label") == 0) {
return true;
}
if (currentKey.compare("column") == 0) {
return true;
}
if (currentKey.compare("kind") == 0) {
return true;
}
if (currentKey.compare("pos") == 0) {
return true;
}
}
states.push(State::IN_ERROR);
return false;
}
bool StartObject()
{
debug << "StartObject()" << std::endl;
if (states.top() == State::IN_START) {
return true;
}
if (states.top() == State::IN_LIST) {
states.push(State::IN_RECORD);
label = "";
kind = soa::IndexKind::IdxInvalid;
pos = -2;
return true;
}
states.push(State::IN_ERROR);
return false;
}
bool EndObject(SizeType)
{
debug << "EndObject()" << std::endl;
if (states.top() == State::IN_RECORD) {
states.pop();
// add a record
records.emplace_back(label, columnLabel, kind, pos);
return true;
}
if (states.top() == State::IN_START) {
return true;
}
states.push(State::IN_ERROR);
return false;
}
bool Uint(unsigned i)
{
debug << "Uint(" << i << ") passed to Int()" << std::endl;
return Int(i);
}
bool Int(int i)
{
debug << "Int(" << i << ")" << std::endl;
if (states.top() == State::IN_RECORD) {
if (currentKey.compare("kind") == 0) {
kind = (soa::IndexKind)i;
return true;
}
if (currentKey.compare("pos") == 0) {
pos = i;
return true;
}
}
states.push(State::IN_ERROR);
return false;
}
bool String(const Ch* str, SizeType, bool)
{
debug << "String(" << str << ")" << std::endl;
if (states.top() == State::IN_RECORD) {
if (currentKey.compare("label") == 0) {
label = str;
return true;
}
if (currentKey.compare("column") == 0) {
columnLabel = str;
return true;
}
}
states.push(State::IN_ERROR);
return false;
}
};
} // namespace
std::vector<o2::soa::IndexRecord> IndexJSONHelpers::read(std::istream& s)
{
rapidjson::Reader reader;
rapidjson::IStreamWrapper isw(s);
IndexRecordsReader irreader;
bool ok = reader.Parse(isw, irreader);
if (!ok) {
throw framework::runtime_error_f("Cannot parse serialized index records vector, error: %s at offset: %d", rapidjson::GetParseError_En(reader.GetParseErrorCode()), reader.GetErrorOffset());
}
return irreader.records;
}
namespace
{
void writeRecords(rapidjson::Writer<rapidjson::OStreamWrapper>& w, std::vector<o2::soa::IndexRecord>& records)
{
for (auto& r : records) {
w.StartObject();
w.Key("label");
w.String(r.label.c_str());
w.Key("column");
w.String(r.columnLabel.c_str());
w.Key("kind");
w.Int((int)r.kind);
w.Key("pos");
w.Int(r.pos);
w.EndObject();
}
}
} // namespace
void IndexJSONHelpers::write(std::ostream& o, std::vector<o2::soa::IndexRecord>& irs)
{
rapidjson::OStreamWrapper osw(o);
rapidjson::Writer<rapidjson::OStreamWrapper> w(osw);
w.StartObject();
w.Key("records");
w.StartArray();
writeRecords(w, irs);
w.EndArray();
w.EndObject();
}
} // namespace o2::framework