osm_io/osm/apidb_dump/write/
table_data_writers.rs

1use std::collections::HashMap;
2use std::path::Path;
3
4use json::JsonValue;
5use transient_btree_index::{BtreeConfig, BtreeIndex};
6
7use crate::osm::apidb_dump::write::table_data_writer::TableDataWriter;
8
9pub(crate) struct TableDataWriters {
10    pub(crate) acls: TableDataWriter,
11    pub(crate) active_storage_attachments: TableDataWriter,
12    pub(crate) active_storage_blobs: TableDataWriter,
13    pub(crate) active_storage_variant_records: TableDataWriter,
14    pub(crate) ar_internal_metadata: TableDataWriter,
15    pub(crate) changeset_comments: TableDataWriter,
16    pub(crate) changeset_tags: TableDataWriter,
17    pub(crate) changesets: TableDataWriter,
18    pub(crate) changesets_subscribers: TableDataWriter,
19    pub(crate) client_applications: TableDataWriter,
20    pub(crate) current_node_tags: TableDataWriter,
21    pub(crate) current_nodes: TableDataWriter,
22    pub(crate) current_relation_members: TableDataWriter,
23    pub(crate) current_relation_tags: TableDataWriter,
24    pub(crate) current_relations: TableDataWriter,
25    pub(crate) current_way_nodes: TableDataWriter,
26    pub(crate) current_way_tags: TableDataWriter,
27    pub(crate) current_ways: TableDataWriter,
28    pub(crate) delayed_jobs: TableDataWriter,
29    pub(crate) diary_comments: TableDataWriter,
30    pub(crate) diary_entries: TableDataWriter,
31    pub(crate) diary_entry_subscriptions: TableDataWriter,
32    pub(crate) friends: TableDataWriter,
33    pub(crate) gps_points: TableDataWriter,
34    pub(crate) gpx_file_tags: TableDataWriter,
35    pub(crate) gpx_files: TableDataWriter,
36    pub(crate) issue_comments: TableDataWriter,
37    pub(crate) issues: TableDataWriter,
38    pub(crate) languages: TableDataWriter,
39    pub(crate) messages: TableDataWriter,
40    pub(crate) node_tags: TableDataWriter,
41    pub(crate) nodes: TableDataWriter,
42    pub(crate) note_comments: TableDataWriter,
43    pub(crate) notes: TableDataWriter,
44    pub(crate) oauth_access_grants: TableDataWriter,
45    pub(crate) oauth_access_tokens: TableDataWriter,
46    pub(crate) oauth_applications: TableDataWriter,
47    pub(crate) oauth_nonces: TableDataWriter,
48    pub(crate) oauth_tokens: TableDataWriter,
49    pub(crate) redactions: TableDataWriter,
50    pub(crate) relation_members: TableDataWriter,
51    pub(crate) relation_tags: TableDataWriter,
52    pub(crate) relations: TableDataWriter,
53    pub(crate) reports: TableDataWriter,
54    pub(crate) schema_migrations: TableDataWriter,
55    pub(crate) user_blocks: TableDataWriter,
56    pub(crate) user_preferences: TableDataWriter,
57    pub(crate) user_roles: TableDataWriter,
58    pub(crate) user_tokens: TableDataWriter,
59    pub(crate) users: TableDataWriter,
60    pub(crate) way_nodes: TableDataWriter,
61    pub(crate) way_tags: TableDataWriter,
62    pub(crate) ways: TableDataWriter,
63
64    pub(crate) user_index: BtreeIndex<i64, String>,
65    pub(crate) changeset_user_index: BtreeIndex<i64, i64>,
66
67    pub(crate) user_index_buffer: HashMap<i64, String>,
68    pub(crate) changeset_user_index_buffer: HashMap<i64, i64>,
69}
70
71impl TableDataWriters {
72    pub(crate) fn new(template_mapping: JsonValue, output_path: &Path) -> Result<Self, anyhow::Error> {
73        let user_index = BtreeIndex::<i64, String>::with_capacity(BtreeConfig::default(), 0)?;
74        let changeset_user_index = BtreeIndex::<i64, i64>::with_capacity(BtreeConfig::default(), 0)?;
75        let user_index_buffer = HashMap::<i64, String>::new();
76        let changeset_user_index_buffer = HashMap::<i64, i64>::new();
77
78        Ok(TableDataWriters {
79            acls: TableDataWriter::new("public.acls".to_string(), template_mapping["public.acls"].to_string(), output_path).unwrap(),
80            active_storage_attachments: TableDataWriter::new("public.active_storage_attachments".to_string(), template_mapping["public.active_storage_attachments"].to_string(), output_path).unwrap(),
81            active_storage_blobs: TableDataWriter::new("public.active_storage_blobs".to_string(), template_mapping["public.active_storage_blobs"].to_string(), output_path).unwrap(),
82            active_storage_variant_records: TableDataWriter::new("public.active_storage_variant_records".to_string(), template_mapping["public.active_storage_variant_records"].to_string(), output_path).unwrap(),
83            ar_internal_metadata: TableDataWriter::new("public.ar_internal_metadata".to_string(), template_mapping["public.ar_internal_metadata"].to_string(), output_path).unwrap(),
84            changeset_comments: TableDataWriter::new("public.changeset_comments".to_string(), template_mapping["public.changeset_comments"].to_string(), output_path).unwrap(),
85            changeset_tags: TableDataWriter::new("public.changeset_tags".to_string(), template_mapping["public.changeset_tags"].to_string(), output_path).unwrap(),
86            changesets: TableDataWriter::new("public.changesets".to_string(), template_mapping["public.changesets"].to_string(), output_path).unwrap(),
87            changesets_subscribers: TableDataWriter::new("public.changesets_subscribers".to_string(), template_mapping["public.changesets_subscribers"].to_string(), output_path).unwrap(),
88            client_applications: TableDataWriter::new("public.client_applications".to_string(), template_mapping["public.client_applications"].to_string(), output_path).unwrap(),
89            current_node_tags: TableDataWriter::new("public.current_node_tags".to_string(), template_mapping["public.current_node_tags"].to_string(), output_path).unwrap(),
90            current_nodes: TableDataWriter::new("public.current_nodes".to_string(), template_mapping["public.current_nodes"].to_string(), output_path).unwrap(),
91            current_relation_members: TableDataWriter::new("public.current_relation_members".to_string(), template_mapping["public.current_relation_members"].to_string(), output_path).unwrap(),
92            current_relation_tags: TableDataWriter::new("public.current_relation_tags".to_string(), template_mapping["public.current_relation_tags"].to_string(), output_path).unwrap(),
93            current_relations: TableDataWriter::new("public.current_relations".to_string(), template_mapping["public.current_relations"].to_string(), output_path).unwrap(),
94            current_way_nodes: TableDataWriter::new("public.current_way_nodes".to_string(), template_mapping["public.current_way_nodes"].to_string(), output_path).unwrap(),
95            current_way_tags: TableDataWriter::new("public.current_way_tags".to_string(), template_mapping["public.current_way_tags"].to_string(), output_path).unwrap(),
96            current_ways: TableDataWriter::new("public.current_ways".to_string(), template_mapping["public.current_ways"].to_string(), output_path).unwrap(),
97            delayed_jobs: TableDataWriter::new("public.delayed_jobs".to_string(), template_mapping["public.delayed_jobs"].to_string(), output_path).unwrap(),
98            diary_comments: TableDataWriter::new("public.diary_comments".to_string(), template_mapping["public.diary_comments"].to_string(), output_path).unwrap(),
99            diary_entries: TableDataWriter::new("public.diary_entries".to_string(), template_mapping["public.diary_entries"].to_string(), output_path).unwrap(),
100            diary_entry_subscriptions: TableDataWriter::new("public.diary_entry_subscriptions".to_string(), template_mapping["public.diary_entry_subscriptions"].to_string(), output_path).unwrap(),
101            friends: TableDataWriter::new("public.friends".to_string(), template_mapping["public.friends"].to_string(), output_path).unwrap(),
102            gps_points: TableDataWriter::new("public.gps_points".to_string(), template_mapping["public.gps_points"].to_string(), output_path).unwrap(),
103            gpx_file_tags: TableDataWriter::new("public.gpx_file_tags".to_string(), template_mapping["public.gpx_file_tags"].to_string(), output_path).unwrap(),
104            gpx_files: TableDataWriter::new("public.gpx_files".to_string(), template_mapping["public.gpx_files"].to_string(), output_path).unwrap(),
105            issue_comments: TableDataWriter::new("public.issue_comments".to_string(), template_mapping["public.issue_comments"].to_string(), output_path).unwrap(),
106            issues: TableDataWriter::new("public.issues".to_string(), template_mapping["public.issues"].to_string(), output_path).unwrap(),
107            languages: TableDataWriter::new("public.languages".to_string(), template_mapping["public.languages"].to_string(), output_path).unwrap(),
108            messages: TableDataWriter::new("public.messages".to_string(), template_mapping["public.messages"].to_string(), output_path).unwrap(),
109            node_tags: TableDataWriter::new("public.node_tags".to_string(), template_mapping["public.node_tags"].to_string(), output_path).unwrap(),
110            nodes: TableDataWriter::new("public.nodes".to_string(), template_mapping["public.nodes"].to_string(), output_path).unwrap(),
111            note_comments: TableDataWriter::new("public.note_comments".to_string(), template_mapping["public.note_comments"].to_string(), output_path).unwrap(),
112            notes: TableDataWriter::new("public.notes".to_string(), template_mapping["public.notes"].to_string(), output_path).unwrap(),
113            oauth_access_grants: TableDataWriter::new("public.oauth_access_grants".to_string(), template_mapping["public.oauth_access_grants"].to_string(), output_path).unwrap(),
114            oauth_access_tokens: TableDataWriter::new("public.oauth_access_tokens".to_string(), template_mapping["public.oauth_access_tokens"].to_string(), output_path).unwrap(),
115            oauth_applications: TableDataWriter::new("public.oauth_applications".to_string(), template_mapping["public.oauth_applications"].to_string(), output_path).unwrap(),
116            oauth_nonces: TableDataWriter::new("public.oauth_nonces".to_string(), template_mapping["public.oauth_nonces"].to_string(), output_path).unwrap(),
117            oauth_tokens: TableDataWriter::new("public.oauth_tokens".to_string(), template_mapping["public.oauth_tokens"].to_string(), output_path).unwrap(),
118            redactions: TableDataWriter::new("public.redactions".to_string(), template_mapping["public.redactions"].to_string(), output_path).unwrap(),
119            relation_members: TableDataWriter::new("public.relation_members".to_string(), template_mapping["public.relation_members"].to_string(), output_path).unwrap(),
120            relation_tags: TableDataWriter::new("public.relation_tags".to_string(), template_mapping["public.relation_tags"].to_string(), output_path).unwrap(),
121            relations: TableDataWriter::new("public.relations".to_string(), template_mapping["public.relations"].to_string(), output_path).unwrap(),
122            reports: TableDataWriter::new("public.reports".to_string(), template_mapping["public.reports"].to_string(), output_path).unwrap(),
123            schema_migrations: TableDataWriter::new("public.schema_migrations".to_string(), template_mapping["public.schema_migrations"].to_string(), output_path).unwrap(),
124            user_blocks: TableDataWriter::new("public.user_blocks".to_string(), template_mapping["public.user_blocks"].to_string(), output_path).unwrap(),
125            user_preferences: TableDataWriter::new("public.user_preferences".to_string(), template_mapping["public.user_preferences"].to_string(), output_path).unwrap(),
126            user_roles: TableDataWriter::new("public.user_roles".to_string(), template_mapping["public.user_roles"].to_string(), output_path).unwrap(),
127            user_tokens: TableDataWriter::new("public.user_tokens".to_string(), template_mapping["public.user_tokens"].to_string(), output_path).unwrap(),
128            users: TableDataWriter::new("public.users".to_string(), template_mapping["public.users"].to_string(), output_path).unwrap(),
129            way_nodes: TableDataWriter::new("public.way_nodes".to_string(), template_mapping["public.way_nodes"].to_string(), output_path).unwrap(),
130            way_tags: TableDataWriter::new("public.way_tags".to_string(), template_mapping["public.way_tags"].to_string(), output_path).unwrap(),
131            ways: TableDataWriter::new("public.ways".to_string(), template_mapping["public.ways"].to_string(), output_path).unwrap(),
132
133            user_index,
134            changeset_user_index,
135            user_index_buffer,
136            changeset_user_index_buffer,
137        })
138    }
139
140    pub(crate) fn flush_buffers(&mut self) -> Result<(), anyhow::Error> {
141        self.user_index_buffer.iter().for_each(|(user_id, user)| {
142            self.user_index.insert(*user_id, user.to_string()).unwrap();
143        });
144        self.user_index_buffer.clear();
145
146        self.changeset_user_index_buffer.iter().for_each(|(changeset_id, user_id)| {
147            self.changeset_user_index.insert(*changeset_id, *user_id).unwrap();
148        });
149        self.changeset_user_index_buffer.clear();
150        Ok(())
151    }
152
153    pub(crate) fn close(&mut self) -> Result<(), anyhow::Error> {
154        self.acls.close()?;
155        self.active_storage_attachments.close()?;
156        self.active_storage_blobs.close()?;
157        self.active_storage_variant_records.close()?;
158        self.ar_internal_metadata.close()?;
159        self.changeset_comments.close()?;
160        self.changeset_tags.close()?;
161        self.changesets.close()?;
162        self.changesets_subscribers.close()?;
163        self.client_applications.close()?;
164        self.current_node_tags.close()?;
165        self.current_nodes.close()?;
166        self.current_relation_members.close()?;
167        self.current_relation_tags.close()?;
168        self.current_relations.close()?;
169        self.current_way_nodes.close()?;
170        self.current_way_tags.close()?;
171        self.current_ways.close()?;
172        self.delayed_jobs.close()?;
173        self.diary_comments.close()?;
174        self.diary_entries.close()?;
175        self.diary_entry_subscriptions.close()?;
176        self.friends.close()?;
177        self.gps_points.close()?;
178        self.gpx_file_tags.close()?;
179        self.gpx_files.close()?;
180        self.issue_comments.close()?;
181        self.issues.close()?;
182        self.languages.close()?;
183        self.messages.close()?;
184        self.node_tags.close()?;
185        self.nodes.close()?;
186        self.note_comments.close()?;
187        self.notes.close()?;
188        self.oauth_access_grants.close()?;
189        self.oauth_access_tokens.close()?;
190        self.oauth_applications.close()?;
191        self.oauth_nonces.close()?;
192        self.oauth_tokens.close()?;
193        self.redactions.close()?;
194        self.relation_members.close()?;
195        self.relation_tags.close()?;
196        self.relations.close()?;
197        self.reports.close()?;
198        self.schema_migrations.close()?;
199        self.user_blocks.close()?;
200        self.user_preferences.close()?;
201        self.user_roles.close()?;
202        self.user_tokens.close()?;
203        self.users.close()?;
204        self.way_nodes.close()?;
205        self.way_tags.close()?;
206        self.ways.close()?;
207        Ok(())
208    }
209}