author | František Kučera <franta-hg@frantovo.cz> |
Tue, 19 Apr 2022 21:00:07 +0200 | |
branch | v_0 |
changeset 3 | 9e16e31fa756 |
parent 2 | d2ba14aa4e20 |
child 4 | 0890135ff1f7 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <regex> |
|
20 |
#include <stdexcept> |
|
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
#include <sstream> |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
22 |
#include <codecvt> |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
#include <iomanip> |
0 | 24 |
|
25 |
#include <relpipe/common/type/typedefs.h> |
|
26 |
#include <relpipe/reader/TypeId.h> |
|
27 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
28 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
29 |
||
30 |
#include <relpipe/writer/Factory.h> |
|
31 |
||
32 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
33 |
||
34 |
#include "Configuration.h" |
|
35 |
||
36 |
namespace relpipe { |
|
37 |
namespace tr { |
|
38 |
namespace serialize { |
|
39 |
||
3
9e16e31fa756
typed values instead of mere strings in the handler + check number of attributes at the end
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
40 |
class SerializeHandler : public relpipe::reader::handlers::RelationalReaderValueHandler { |
0 | 41 |
private: |
42 |
Configuration configuration; |
|
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
shared_ptr<relpipe::writer::RelationalWriter> writer; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
|
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
class RelationContext { |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
public: |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
relpipe::common::type::StringX name; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
std::vector<relpipe::reader::handlers::AttributeMetadata> readerMetadata; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
std::vector<relpipe::writer::AttributeMetadata> writerMetadata; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
} relationContext; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
|
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
class RecordContext { |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
public: |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
std::stringstream buffer; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
shared_ptr<relpipe::writer::RelationalWriter> writer; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
size_t attributeIndex = 0; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
} recordContext; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
|
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
relpipe::common::type::StringX toHex(const std::string& octets) { |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
std::stringstream hex; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
hex << std::hex << std::setfill('0') << std::hex; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
for (size_t i = 0, size = octets.size(); i < size; i++) hex << std::setw(2) << (0xff & octets[i]); |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
return convertor.from_bytes(hex.str()); |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
} |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
|
0 | 67 |
public: |
68 |
||
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
SerializeHandler(shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) { |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
// TODO: configurable relation name |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
// TODO: configurable attribute name |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
// TODO: optional custom attributes with constant value or ordinal number |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
// TODO: optional serialization of only certain relations? and certain fields? |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
// TODO: optional pass-through of certain relations? |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
// TODO: multiple modes? one output record per a) one input record, b) several input records, c) one input relation, … ? |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
|
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
writer->startRelation(L"message",{ |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
{L"data", relpipe::writer::TypeId::STRING} // TODO: octet-string (when supported) instead of HEX |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
}, true); |
0 | 80 |
} |
81 |
||
82 |
virtual ~SerializeHandler() = default; |
|
83 |
||
84 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
relationContext = RelationContext(); |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
|
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
relationContext.name = name; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
relationContext.readerMetadata = attributes; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
|
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
for (relpipe::reader::handlers::AttributeMetadata readerMetadata : attributes) { |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
relationContext.writerMetadata.push_back({readerMetadata.getAttributeName(), writer->toTypeId(readerMetadata.getTypeName())}); |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
} |
0 | 93 |
} |
94 |
||
3
9e16e31fa756
typed values instead of mere strings in the handler + check number of attributes at the end
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
95 |
void attribute(const void* value, const std::type_info& type) override { |
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
if (recordContext.attributeIndex == 0) { |
2
d2ba14aa4e20
initialize record context on first attribute of each record
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
97 |
recordContext = RecordContext(); |
d2ba14aa4e20
initialize record context on first attribute of each record
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
98 |
recordContext.writer.reset(relpipe::writer::Factory::create(recordContext.buffer)); |
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
recordContext.writer->startRelation(relationContext.name, relationContext.writerMetadata, true); |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
100 |
} |
0 | 101 |
|
3
9e16e31fa756
typed values instead of mere strings in the handler + check number of attributes at the end
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
recordContext.writer->writeAttribute(value, type); |
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
103 |
recordContext.attributeIndex++; |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
104 |
|
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
105 |
if (recordContext.attributeIndex % relationContext.readerMetadata.size() == 0) { |
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
106 |
writer->writeAttribute(toHex(recordContext.buffer.str())); |
2
d2ba14aa4e20
initialize record context on first attribute of each record
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
107 |
recordContext.attributeIndex = 0; |
1
c0a1a008982c
first version of serializer
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
108 |
} |
0 | 109 |
} |
110 |
||
111 |
void endOfPipe() { |
|
3
9e16e31fa756
typed values instead of mere strings in the handler + check number of attributes at the end
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
if (recordContext.attributeIndex != 0) throw relpipe::reader::RelpipeReaderException(L"Invalid number of attributes at the end of the pipe – incomplete record."); |
0 | 113 |
} |
114 |
||
115 |
}; |
|
116 |
||
117 |
} |
|
118 |
} |
|
119 |
} |