author | František Kučera <franta-hg@frantovo.cz> |
Tue, 11 May 2021 22:03:45 +0200 | |
branch | v_0 |
changeset 28 | bc15f5471b6a |
parent 22 | d07ac873cc89 |
permissions | -rw-r--r-- |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
22
d07ac873cc89
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <memory> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <string> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <vector> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <iostream> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sstream> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <locale> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <codecvt> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
#include <regex> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
#include <relpipe/reader/typedefs.h> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include <relpipe/reader/TypeId.h> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
#include <relpipe/writer/Factory.h> |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
|
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
35 |
#include <relpipe/cli/RelpipeCLIException.h> |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
36 |
|
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
37 |
#include "Configuration.h" |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
38 |
|
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
namespace relpipe { |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
namespace tr { |
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
41 |
namespace cut { |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
|
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
43 |
using namespace std; |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
using namespace relpipe; |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
using namespace relpipe::reader; |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
using namespace relpipe::reader::handlers; |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
|
18
90d4e4f9fde6
fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
48 |
class CutHandler : public RelationalReaderStringHandler { |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
private: |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
50 |
shared_ptr<writer::RelationalWriter> relationalWriter; |
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
51 |
Configuration configuration; |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
52 |
RelationConfiguration* currentFilter = nullptr; |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
53 |
|
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
54 |
vector<integer_t> currentAttributeMapping; |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
55 |
vector<string_t> currentRecord; |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
56 |
integer_t currentAttributeIndex = 0; |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
57 |
|
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
public: |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
|
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
60 |
CutHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
} |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
|
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
63 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
11
9d528c98912d
cut can also do DROP of whole relation: just use regex that matches no attributes like $^
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
64 |
currentRecord.resize(attributes.size()); |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
// TODO: move to a reusable method (or use same metadata on both reader and writer side?) |
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
66 |
vector<writer::AttributeMetadata> allWriterMetadata; |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
for (AttributeMetadata readerMetadata : attributes) { |
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
68 |
allWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
} |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
70 |
|
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
71 |
vector<writer::AttributeMetadata> writerMetadata; |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
72 |
|
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
73 |
using E = RelationConfiguration::ENTITY; |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
74 |
|
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
75 |
currentFilter = nullptr; |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
76 |
for (int i = 0; i < configuration.relationConfigurations.size(); i++) { |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
77 |
if (regex_match(name, configuration.relationConfigurations[i].relationPattern) ^ configuration.relationConfigurations[i].invertMatch[E::RELATION]) { |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
78 |
currentFilter = &configuration.relationConfigurations[i]; |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
79 |
break; |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
80 |
} |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
81 |
} |
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
82 |
|
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
83 |
if (currentFilter) { |
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
84 |
currentAttributeMapping.clear(); |
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
85 |
for (std::wregex attributePattern : currentFilter->attributePatterns) { |
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
86 |
for (int i = 0; i < allWriterMetadata.size(); i++) { |
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
87 |
if (regex_match(allWriterMetadata[i].attributeName, attributePattern) ^ currentFilter->invertMatch[E::ATTRIBUTE]) currentAttributeMapping.push_back(i); |
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
88 |
} |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
89 |
} |
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
90 |
|
11
9d528c98912d
cut can also do DROP of whole relation: just use regex that matches no attributes like $^
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
91 |
if (currentAttributeMapping.empty()) return; // No attribute matches → DROP whole relation |
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
92 |
for (integer_t i : currentAttributeMapping) writerMetadata.push_back(allWriterMetadata[i]); |
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
93 |
} else { |
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
94 |
writerMetadata = allWriterMetadata; |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
95 |
} |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
96 |
|
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
relationalWriter->startRelation(name, writerMetadata, true); |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
} |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
void attribute(const string_t& value) override { |
28
bc15f5471b6a
new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
101 |
if (currentFilter) { |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
102 |
currentRecord[currentAttributeIndex] = value; |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
103 |
currentAttributeIndex++; |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
104 |
|
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
105 |
if (currentAttributeIndex > 0 && currentAttributeIndex % currentRecord.size() == 0) { |
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
106 |
for (integer_t i : currentAttributeMapping) relationalWriter->writeAttribute(currentRecord[i]); |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
107 |
} |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
108 |
|
10
9ec1290b4a9d
first working cut version
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
109 |
currentAttributeIndex = currentAttributeIndex % currentRecord.size(); |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
110 |
} else { |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
111 |
relationalWriter->writeAttribute(value); |
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
112 |
} |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
} |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
114 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
115 |
void endOfPipe() { |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
116 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
117 |
} |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
}; |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
120 |
|
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
} |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
} |
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
123 |
} |