Line data Source code
1 : //
2 : // DUNE DAQ modification notice:
3 : // This file has been modified from the original ATLAS oks_utils source for the DUNE DAQ project.
4 : // Fork baseline commit: c2e7dfc7 (2022-03-30).
5 : // Renamed since fork: yes (from src/bin/oks_merge.cpp to apps/oks_merge.cxx).
6 : //
7 :
8 : /**
9 : * \file oks_merge.cpp
10 : *
11 : * This file is part of the OKS package.
12 : * https://gitlab.cern.ch/atlas-tdaq-software/oks
13 : *
14 : * This file contains the implementation of the OKS application to merge several data files into a single data files.
15 : */
16 :
17 :
18 : #include "oks/kernel.hpp"
19 :
20 : using namespace dunedaq;
21 : using namespace dunedaq::oks;
22 :
23 : ////////////////////////////////////////////////////////////////////////////////////////////////////
24 :
25 : #include "ers/ers.hpp"
26 :
27 0 : ERS_DECLARE_ISSUE(
28 : oks_merge,
29 : BadCommandLine,
30 : "Bad command line: \"" << what << '\"',
31 : ((const char*)what)
32 : )
33 :
34 0 : ERS_DECLARE_ISSUE(
35 : oks_merge,
36 : BadQuery,
37 : "Failed to parse query \"" << query << "\" in class \"" << class_name << '\"',
38 : ((const char*)query)
39 : ((const char*)class_name)
40 : )
41 :
42 0 : ERS_DECLARE_ISSUE(
43 : oks_merge,
44 : BadClass,
45 : "Cannot find class \"" << class_name << '\"',
46 : ((const char*)class_name)
47 : )
48 :
49 : /** Failed to read OKS database file. */
50 :
51 0 : ERS_DECLARE_ISSUE(
52 : oks_merge,
53 : CaughtException,
54 : "Caught " << what << " exception: \'" << text << '\'',
55 : ((const char *)what)
56 : ((const char *)text)
57 : )
58 :
59 : ////////////////////////////////////////////////////////////////////////////////////////////////////
60 :
61 : std::string appTitle;
62 :
63 0 : void printUsage(const char *appName, std::ostream& s)
64 : {
65 0 : s << appTitle << "\n"
66 : "Usage: " << appName << "\n"
67 : " [--class name-of-class [--query query [--print-references recursion-depth [class-name*] [--]]]\n"
68 : " [--version]\n"
69 : " [--help]\n"
70 : " [--out-data-file data_file] [--out-data-file schema_file] inputfiles*\n"
71 : "\n"
72 : " Options:\n"
73 : " -o | --out-data-file datafilename the out filename to store oks objects from input files\n"
74 : " -s | --out-schema-file schemafilename the out filename to store oks classes from input files\n"
75 : " -c | --class class_name dump given class (all objects or matching some query)\n"
76 : " -q | --query query print objects matching query (can only be used with class)\n"
77 : " -r | --print-references N C1 C2 ... CX print objects referenced by found objects (can only be used with query), where:\n"
78 : " * the parameter N defines recursion depth for referenced objects (> 0)\n"
79 : " * the optional set of names {C1 .. CX} defines [sub-]classes for above objects\n"
80 : " -v | --version print version\n"
81 : " -h | --help print this text\n"
82 : "\n"
83 : " Description:\n"
84 : " Merges several oks files into single schema and data files.\n"
85 0 : "\n";
86 0 : }
87 :
88 : enum __OksMergeExitStatus__ {
89 : __Success__ = 0,
90 : __NoDataFilesLoaded__,
91 : __NoSchemaFilesLoaded__,
92 : __BadCommandLine__,
93 : __NoOutputSchemaFile__,
94 : __BadQuery__,
95 : __NoSuchClass__,
96 : __ExceptionCaught__
97 : };
98 :
99 :
100 : static void
101 0 : no_param(const char * s)
102 : {
103 0 : Oks::error_msg("oks_merge") << "no parameter(s) for command line argument \'" << s << "\' provided\n\n";
104 0 : exit(__BadCommandLine__);
105 : }
106 :
107 : int
108 0 : main(int argc, char *argv[])
109 : {
110 0 : appTitle = "OKS merge. OKS kernel version ";
111 0 : appTitle += OksKernel::GetVersion();
112 :
113 0 : const char * appName = "oks_merge";
114 :
115 0 : std::string out_data_file;
116 0 : std::string out_schema_file;
117 :
118 0 : const char * class_name = nullptr;
119 0 : const char * query = nullptr;
120 0 : long recursion_depth = 0;
121 0 : std::vector<std::string> ref_classes;
122 :
123 :
124 0 : if(argc == 1) {
125 0 : printUsage(appName, std::cerr);
126 : return __BadCommandLine__;
127 : }
128 :
129 0 : OksKernel kernel;
130 0 : kernel.set_use_strict_repository_paths(false);
131 :
132 0 : try {
133 :
134 0 : for(int i = 1; i < argc; i++) {
135 0 : const char * cp = argv[i];
136 :
137 0 : if(!strcmp(cp, "-h") || !strcmp(cp, "--help")) {
138 0 : printUsage(appName, std::cout);
139 : return __Success__;
140 : }
141 0 : else if(!strcmp(cp, "-v") || !strcmp(cp, "--version")) {
142 0 : std::cout << appTitle << std::endl;
143 : return __Success__;
144 : }
145 0 : else if(!strcmp(cp, "-o") || !strcmp(cp, "--out-data-file")) {
146 0 : if(++i == argc) { no_param(cp); } else { out_data_file = argv[i]; }
147 : }
148 0 : else if(!strcmp(cp, "-s") || !strcmp(cp, "--out-schema-file")) {
149 0 : if(++i == argc) { no_param(cp); } else { out_schema_file = argv[i]; }
150 : }
151 0 : else if(!strcmp(cp, "-c") || !strcmp(cp, "--class")) {
152 0 : if(++i == argc) { no_param(cp); } else { class_name = argv[i]; }
153 : }
154 0 : else if(!strcmp(cp, "-q") || !strcmp(cp, "--query")) {
155 0 : if(++i == argc) { no_param(cp); } else { query = argv[i]; }
156 : }
157 0 : else if(!strcmp(cp, "-r") || !strcmp(cp, "--print-references")) {
158 0 : if(++i == argc) { no_param(cp); } else { recursion_depth = atol(argv[i]); }
159 0 : int j = 0;
160 0 : for(; j < argc - i - 1; ++j) {
161 0 : if(argv[i+1+j][0] != '-') { ref_classes.push_back(argv[i+1+j]); } else { break; }
162 : }
163 0 : i += j;
164 0 : }
165 0 : else if(strcmp(cp, "--")) {
166 0 : kernel.load_file(cp);
167 : }
168 : }
169 :
170 0 : if(!out_data_file.empty() && kernel.data_files().empty()) {
171 0 : ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"There were no data files loaded"));
172 0 : return __NoDataFilesLoaded__;
173 : }
174 :
175 0 : if(!out_schema_file.empty() && kernel.schema_files().empty()) {
176 0 : ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"There were no schema files loaded"));
177 0 : return __NoSchemaFilesLoaded__;
178 : }
179 :
180 0 : if(query && !class_name) {
181 0 : ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"Query can only be executed when class name is provided (use -c option)"));
182 0 : return __BadCommandLine__;
183 : }
184 :
185 :
186 0 : OksFile * data_file_h = 0;
187 0 : OksFile * schema_file_h = 0;
188 :
189 0 : if(!out_data_file.empty()) {
190 0 : data_file_h = kernel.new_data(out_data_file);
191 0 : kernel.set_active_data(data_file_h);
192 : }
193 :
194 0 : if(!out_schema_file.empty()) {
195 0 : schema_file_h = kernel.new_schema(out_schema_file);
196 0 : kernel.set_active_schema(schema_file_h);
197 : }
198 :
199 0 : if(!data_file_h && !schema_file_h) {
200 0 : ers::fatal(oks_merge::BadCommandLine(ERS_HERE,"There is no out schema file name defined"));
201 0 : return __NoOutputSchemaFile__;
202 : }
203 :
204 0 : if(schema_file_h) {
205 0 : for(OksClass::Map::const_iterator j = kernel.classes().begin(); j != kernel.classes().end(); ++j) {
206 0 : j->second->set_file(schema_file_h, false);
207 : }
208 :
209 0 : kernel.save_schema(schema_file_h);
210 : }
211 :
212 0 : if(data_file_h) {
213 0 : if(schema_file_h) {
214 0 : data_file_h->add_include_file(out_schema_file);
215 : }
216 :
217 0 : if(class_name && *class_name) {
218 0 : if(OksClass * c = kernel.find_class(class_name)) {
219 0 : if(query && *query) {
220 0 : OksQuery * q = new OksQuery(c, query);
221 0 : if(q->good()) {
222 0 : OksObject::List * objs = c->execute_query(q);
223 0 : size_t num = (objs ? objs->size() : 0);
224 0 : std::cout << "Found " << num << " matching query \"" << query << "\" in class \"" << class_name << "\"";
225 0 : if(num) {
226 0 : OksObject::FSet refs;
227 0 : std::cout << ':' << std::endl;
228 0 : while(!objs->empty()) {
229 0 : OksObject * o = objs->front();
230 0 : objs->pop_front();
231 0 : if(recursion_depth > 0) {
232 0 : oks::ClassSet all_ref_classes;
233 0 : kernel.get_all_classes(ref_classes, all_ref_classes);
234 0 : o->references(refs, recursion_depth, true, &all_ref_classes);
235 0 : std::cout << " - " << o << " references " << refs.size() << " objects" << std::endl;
236 0 : }
237 : else {
238 0 : refs.insert(o);
239 : }
240 : }
241 0 : delete objs;
242 0 : for(OksObject::FSet::iterator i = refs.begin(); i != refs.end(); ++i) {
243 0 : const_cast<OksObject *>(*i)->set_file(data_file_h, false);
244 : }
245 0 : }
246 : else {
247 0 : std::cout << std::endl;
248 : }
249 : }
250 : else {
251 0 : ers::fatal(oks_merge::BadQuery(ERS_HERE, query, class_name));
252 0 : return __BadQuery__;
253 : }
254 0 : delete q;
255 : }
256 : else {
257 0 : std::cout << *c << std::endl;
258 : }
259 : }
260 : else {
261 0 : ers::fatal(oks_merge::BadClass(ERS_HERE, class_name));
262 0 : return __NoSuchClass__;
263 : }
264 : }
265 : else {
266 0 : for(OksObject::Set::const_iterator j = kernel.objects().begin(); j != kernel.objects().end(); ++j) {
267 0 : (*j)->set_file(data_file_h, false);
268 : }
269 : }
270 :
271 0 : kernel.save_data(data_file_h);
272 : }
273 :
274 : }
275 :
276 0 : catch (oks::exception & ex) {
277 0 : ers::fatal(oks_merge::CaughtException(ERS_HERE, "OKS", ex.what()));
278 0 : return __ExceptionCaught__;
279 0 : }
280 :
281 0 : catch (std::exception & ex) {
282 0 : ers::fatal(oks_merge::CaughtException(ERS_HERE, "Standard C++", ex.what()));
283 0 : return __ExceptionCaught__;
284 0 : }
285 :
286 0 : catch (...) {
287 0 : ers::fatal(oks_merge::CaughtException(ERS_HERE, "unknown", ""));
288 0 : return __ExceptionCaught__;
289 0 : }
290 :
291 : return __Success__;
292 0 : }
|