8
8
namespace clp_s ::indexer {
9
9
IndexManager::IndexManager (std::optional<clp::GlobalMetadataDBConfig> const & db_config) {
10
10
if (db_config.has_value ()) {
11
- m_table_metadata_db = std::make_unique<MySQLIndexStorage>(
11
+ m_mysql_index_storage = std::make_unique<MySQLIndexStorage>(
12
12
db_config->get_metadata_db_host (),
13
13
db_config->get_metadata_db_port (),
14
14
db_config->get_metadata_db_username (),
15
15
db_config->get_metadata_db_password (),
16
16
db_config->get_metadata_db_name (),
17
17
db_config->get_metadata_table_prefix ()
18
18
);
19
- m_table_metadata_db->open ();
19
+ m_mysql_index_storage->open ();
20
+ m_field_update_callback = [this ](std::string& field_name, NodeType field_type) {
21
+ m_mysql_index_storage->add_field (field_name, field_type);
22
+ };
20
23
m_output_type = OutputType::Database;
21
24
} else {
22
25
throw OperationFailed (ErrorCodeBadParam, __FILENAME__, __LINE__);
@@ -25,32 +28,17 @@ IndexManager::IndexManager(std::optional<clp::GlobalMetadataDBConfig> const& db_
25
28
26
29
IndexManager::~IndexManager () {
27
30
if (m_output_type == OutputType::Database) {
28
- m_table_metadata_db ->close ();
31
+ m_mysql_index_storage ->close ();
29
32
}
30
33
}
31
34
32
- void IndexManager::update_metadata (std::string const & archive_dir, std::string const & archive_id) {
33
- m_table_metadata_db->init (archive_dir);
34
-
35
- auto archive_path = std::filesystem::path (archive_dir) / archive_id;
36
- std::error_code ec;
37
- if (false == std::filesystem::exists (archive_path, ec) || ec) {
38
- throw OperationFailed (ErrorCodeBadParam, __FILENAME__, __LINE__);
39
- }
35
+ void IndexManager::update_metadata (std::string const & table_name, Path const & archive_path) {
36
+ m_mysql_index_storage->init (table_name);
40
37
41
38
ArchiveReader archive_reader;
42
- archive_reader.open (
43
- clp_s::Path{.source = clp_s::InputSource::Filesystem, .path = archive_path.string ()},
44
- NetworkAuthOption{}
45
- );
39
+ archive_reader.open (archive_path, NetworkAuthOption{});
46
40
47
- auto schema_tree = archive_reader.get_schema_tree ();
48
- auto field_pairs = traverse_schema_tree (schema_tree);
49
- if (OutputType::Database == m_output_type) {
50
- for (auto & [name, type] : field_pairs) {
51
- m_table_metadata_db->add_field (name, type);
52
- }
53
- }
41
+ traverse_schema_tree_and_update_metadata (archive_reader.get_schema_tree ());
54
42
}
55
43
56
44
std::string IndexManager::escape_key_name (std::string_view const key_name) {
@@ -100,49 +88,42 @@ std::string IndexManager::escape_key_name(std::string_view const key_name) {
100
88
return escaped_key_name;
101
89
}
102
90
103
- std::vector<std::pair<std::string, clp_s::NodeType>> IndexManager::traverse_schema_tree (
91
+ void IndexManager::traverse_schema_tree_and_update_metadata (
104
92
std::shared_ptr<SchemaTree> const & schema_tree
105
93
) {
106
- std::vector<std::pair<std::string, clp_s::NodeType>> fields;
107
94
if (nullptr == schema_tree) {
108
- return fields ;
95
+ return ;
109
96
}
110
97
111
98
std::string path_buffer;
112
99
// Stack of pairs of node_id and path_length
113
100
std::stack<std::pair<int32_t , uint64_t >> s;
114
- for (auto const & node : schema_tree->get_nodes ()) {
115
- if (constants::cRootNodeId == node.get_parent_id ()
116
- && clp_s::NodeType::Metadata != node.get_type ())
117
- {
118
- s.emplace (node.get_id (), 0 );
119
- break ;
120
- }
121
- }
101
+ s.emplace (schema_tree->get_object_subtree_node_id (), 0 );
122
102
123
103
while (false == s.empty ()) {
124
104
auto [node_id, path_length] = s.top ();
125
105
s.pop ();
126
106
127
107
auto const & node = schema_tree->get_node (node_id);
128
- auto const & children_ids = node.get_children_ids ();
129
108
auto node_type = node.get_type ();
109
+ // TODO: Add support for structured arrays
110
+ if (NodeType::StructuredArray == node_type) {
111
+ continue ;
112
+ }
113
+ auto const & children_ids = node.get_children_ids ();
130
114
path_buffer.resize (path_length);
131
115
if (false == path_buffer.empty ()) {
132
116
path_buffer += " ." ;
133
117
}
134
118
path_buffer += escape_key_name (node.get_key_name ());
135
- if (children_ids.empty () && clp_s::NodeType::Object != node_type
136
- && clp_s::NodeType::Unknown != node_type)
119
+ if (children_ids.empty () && NodeType::Object != node_type && NodeType::Unknown != node_type)
137
120
{
138
- fields. emplace_back (path_buffer, node_type);
121
+ m_field_update_callback (path_buffer, node_type);
139
122
}
140
123
141
124
for (auto child_id : children_ids) {
142
125
s.emplace (child_id, path_buffer.size ());
143
126
}
144
127
}
145
-
146
- return fields;
147
128
}
148
129
} // namespace clp_s::indexer
0 commit comments