+ private File exportCurrentTgAsTemporaryFile(Session session, IProgressMonitor monitor) throws DatabaseException {
+ TransferableGraph1 tg = probeProperty(MigrationStateKeys.CURRENT_TG);
+ if (tg == null)
+ return null;
+
+ try {
+ // Write TG back to disk and initialize CURRENT_TGS for the migrated TG.
+ File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
+ File tempFile = File.createTempFile("temporary-tgs", ".tg", SimanticsInternal.getTemporaryDirectory());
+ TimeLogger.log(MigrationStateImpl.class, "export temporary TG " + tempFile);
+
+ DataContainer dc = DataContainers.readHeader(modelFile);
+ TransferableGraphs.writeTransferableGraph(session, dc.format, dc.version, dc.metadata,
+ new TGTransferableGraphSource(tg),
+ tempFile,
+ new TGExportMonitor(monitor, "Exporting temporary transferable graph"));
+
+ // Allow potentially large TG structure to be GC'ed.
+ setProperty(MigrationStateKeys.CURRENT_TG, null);
+
+ TimeLogger.log(MigrationStateImpl.class, "export temporary TG done " + tempFile);
+ return tempFile;
+ } catch (Exception e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+ private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException {
+ try {
+ StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer);
+ TransferableGraphSource tgs = reader.readTG();
+ setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);
+ setProperty(MigrationStateKeys.CURRENT_TGS, tgs);
+ return tgs;
+ } catch (DatabaseException e) {
+ throw e;
+ } catch (IOException e) {
+ throw new DatabaseException("An I/O exception occurred during reading '" + dataContainer.getAbsolutePath() + "'", e);
+ } catch (Throwable t) {
+ throw new DatabaseException(t);
+ }
+ }
+
+ private void importTransferableGraphSource(IProgressMonitor monitor, Session session, boolean updateDependencies, TransferableGraphSource tgs) throws DatabaseException {
+ TimeLogger.log(MigrationStateImpl.class, "import TGS " + tgs);
+ final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {
+ @Override
+ public Resource perform(WriteGraph graph) throws DatabaseException {
+ if(!updateDependencies)
+ Layer0Utils.setDependenciesIndexingDisabled(graph, true);
+ return createTemporaryRoot(graph);
+ }
+ });
+
+ IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));
+ IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {
+ @Override
+ public Resource getTarget() {
+ return indexRoot;
+ }
+ @Override
+ public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
+ super.beforeWrite(graph, process);
+ if(!updateDependencies)
+ Layer0Utils.setDependenciesIndexingDisabled(graph, true);
+ }
+ @Override
+ public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
+ super.afterWrite(graph, process);
+ Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);
+ if(storeResources != null && storeResources) {
+ long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));
+ setProperty(MigrationStateKeys.RESOURCE_IDS, ids);
+ }
+ }
+ };
+
+ // Make sure that the supplied advisor is redirected to temp
+ advisor.redirect(indexRoot);
+
+ String task = "Importing model into database";
+ monitor.subTask(task);
+ TransferableGraphs.importGraph1(session, tgs, advisor, new TGImportMonitor(monitor, task));
+
+ setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);
+ setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));
+ setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());
+ TimeLogger.log(MigrationStateImpl.class, "imported TGS " + tgs);
+ }
+
+
+ static class TGImportMonitor implements TGStatusMonitor {
+ private final IProgressMonitor monitor;
+ private final String message;
+ public TGImportMonitor(IProgressMonitor monitor, String message) {
+ this.monitor = monitor;
+ this.message = message;
+ }
+ @Override
+ public void status(int percentage) {
+ monitor.subTask(message + " (" + percentage + "%)");
+ }
+ @Override
+ public boolean isCanceled() {
+ return monitor.isCanceled();
+ }
+ }
+
+ static class TGExportMonitor extends TGProgressMonitor {
+ private final String message;
+ public TGExportMonitor(IProgressMonitor monitor, String message) {
+ super(monitor);
+ this.message = message;
+ }
+ @Override
+ protected void workDone(int percentage) {
+ monitor.subTask(message + " (" + percentage + "%)");
+ }
+ }
+