| 170 | conn.close() |
| 171 | |
| 172 | def _doImport(self, conn, inf): |
| 173 | app_metadata_re = re.compile(r'app_metadata=\(.*?\)') |
| 174 | |
| 175 | session = conn.open_session() |
| 176 | session.begin_transaction() |
| 177 | |
| 178 | catalog = session.open_cursor('table:_mdb_catalog') |
| 179 | sizeStorer = session.open_cursor('table:sizeStorer') |
| 180 | wtMeta = session.open_cursor('metadata:', None, 'readonly=false') |
| 181 | |
| 182 | # Get the maximum file ID in the WT catalog: we will be appending |
| 183 | session.create('file:_mtransfer') |
| 184 | newfile_meta = wtMeta['file:_mtransfer'] |
| 185 | self.message(f'''Got new file metadata "{newfile_meta}"''') |
| 186 | session.drop('file:_mtransfer') |
| 187 | file_id = int(re.search(r',id=(\d+),', newfile_meta).group(1)) |
| 188 | |
| 189 | # Get the maximum ID in the MDB catalog: we will be appending |
| 190 | catalog.prev() |
| 191 | maxID = catalog.get_key() |
| 192 | |
| 193 | for export in bson.decode_file_iter(inf, codec_options=codec_options): |
| 194 | if not os.path.exists( |
| 195 | os.path.join(self.dbpath, self.database, export['filename'])): |
| 196 | sys.stderr.write( |
| 197 | f'''File "{export['filename']}" referenced in export missing during import''' |
| 198 | ) |
| 199 | if not self.force: |
| 200 | return |
| 201 | |
| 202 | if not self.force and export['version'] != __version__: |
| 203 | sys.stderr.write( |
| 204 | f'''Database was exported with mtools version {export['version']}, ''' |
| 205 | f'''current version {__version__} may not be compatible''' |
| 206 | ) |
| 207 | return |
| 208 | |
| 209 | # Figure out the new namespace |
| 210 | ns = self.database + '.' + export['collname'] |
| 211 | |
| 212 | # First process the indexes |
| 213 | idxIdent = {} |
| 214 | for idxName, idx in export['indexes'].items(): |
| 215 | ident = self.database + '/' + idx['filename'][:-3] |
| 216 | table_uri = 'table:' + ident |
| 217 | colgroup_uri = 'colgroup:' + ident |
| 218 | file_uri = 'file:' + ident + '.wt' |
| 219 | # Do a regular "session.create" for the table, then overwrite |
| 220 | # the "file:" metadata with the original |
| 221 | app_metadata = app_metadata_re.search(idx['wtmeta_file']).group(0) |
| 222 | # For older style index metadata, update the namespace |
| 223 | app_metadata = re.sub(r'"ns" : ".*?"', f'''"ns" : "{ns}"''', app_metadata) |
| 224 | self.message(f'''For index "{idxName}", app_metadata = "{app_metadata}"''') |
| 225 | wtMeta[table_uri] = (app_metadata + |
| 226 | ',colgroups=,collator=,columns=,key_format=u,value_format=u') |
| 227 | wtMeta[colgroup_uri] = (app_metadata + |
| 228 | ',collator=,columns=,source="' + file_uri + '",type=file') |
| 229 | wtMeta[file_uri] = (idx['wtmeta_file'] + ',' + app_metadata + |