From: Marko Luukkainen Date: Tue, 16 Aug 2022 12:45:33 +0000 (+0300) Subject: Importing TG files creates copies of files without deleting them. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d331e06e5825b124d5a76b1fdc1b45bbb5506407;p=simantics%2Fplatform.git Importing TG files creates copies of files without deleting them. Set TransferableGraphFileReader to delete temporary tg files. Check that MigrationState is disposed where used. gitlab #857 Change-Id: Ic31cf437c26b4a36f45f16455c6a3376fb8fc361 --- diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationStateImpl.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationStateImpl.java index fb8f8c9e7..3e531e368 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationStateImpl.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationStateImpl.java @@ -161,8 +161,16 @@ public class MigrationStateImpl implements MigrationState { final boolean updateDependencies = MigrationUtils.getProperty(this, MigrationStateKeys.UPDATE_DEPENDENCIES, Boolean.TRUE); File temporaryTg = exportCurrentTgAsTemporaryFile(session, monitor); - if (temporaryTg != null) - setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg)); + if (temporaryTg != null) { + TransferableGraphSource tgs = (TransferableGraphSource)properties.get(MigrationStateKeys.CURRENT_TGS); + if (tgs != null) + try { + tgs.close(); + } catch (IOException e) { + Logger.defaultLogError(e); + } + setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg, true)); + } TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS); if (tgs != null) { @@ -253,8 +261,11 @@ public class MigrationStateImpl implements MigrationState { } private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException { + return initializeTransferableGraphSource(dataContainer, false); + } + private TransferableGraphSource initializeTransferableGraphSource(File dataContainer, boolean deleteOnClose) throws DatabaseException { try { - StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer); + StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer, deleteOnClose); TransferableGraphSource tgs = reader.readTG(); setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader); setProperty(MigrationStateKeys.CURRENT_TGS, tgs); diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java index e1421df90..7cd733660 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java @@ -358,11 +358,13 @@ public class MigrationUtils { Collection roots = TransferableGraphUtils.getRoots(tg); if(roots.size() == 1) { + MigrationState state = newState(); try { TGTransferableGraphSource tgSource = new TGTransferableGraphSource(tg); SharedOntologyImportAdvisor advisor = new SharedOntologyImportAdvisor(published); - MigrationState state = newState(); + + state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false); state.setProperty(MigrationStateKeys.CURRENT_TGS, tgSource); state.setProperty(MigrationStateKeys.SESSION, session); @@ -383,6 +385,7 @@ public class MigrationUtils { } catch (Exception e) { throw new DatabaseException(e); } finally { + state.dispose(); session.getService(XSupport.class).setServiceMode(false, false); } diff --git a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphFileReader.java b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphFileReader.java index 8c82c23bc..eb6294e3e 100644 --- a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphFileReader.java +++ b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphFileReader.java @@ -36,14 +36,24 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader { final private static int SIZE = 1<<18; + private boolean deleteOnClose = false; + private File file; + public StreamingTransferableGraphFileReader(File file) throws Exception { + this(file,false); + } + + public StreamingTransferableGraphFileReader(File file, boolean deleteOnClose) throws Exception { super(file, SIZE); if(init) { init=false; @SuppressWarnings("resource") StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(file, 0); for(int i=0;i<40000;i++) r.readTG(); + r.close(); } + this.file = file; + this.deleteOnClose = deleteOnClose; } public StreamingTransferableGraphFileReader(InputStream stream) throws Exception { @@ -53,6 +63,7 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader { @SuppressWarnings("resource") StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(stream, 0); for(int i=0;i<40000;i++) r.readTG(); + r.close(); } } @@ -63,6 +74,7 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader { @SuppressWarnings("resource") StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(channel, 0); for(int i=0;i<40000;i++) r.readTG(); + r.close(); } } @@ -77,6 +89,14 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader { public StreamingTransferableGraphFileReader(File file, int size) throws IOException { super(file, size); } + + @Override + public void close() throws IOException { + super.close(); + if (deleteOnClose && file != null && file.exists()) { + file.delete(); + } + } class FileTransferableGraphSource implements TransferableGraphSource { diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java index e7707dd40..91660a492 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java @@ -68,29 +68,32 @@ public class ModelImporter { state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile); state.setProperty(MigrationStateKeys.SESSION, session); state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor); - - if (includeDependencies) { - try { - ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state); - if (libraryDependenciesBean != null) { - for (ModelDependency dependency : libraryDependenciesBean.dependencies) { - Resource existing = session.sync(new PossibleResource(dependency.uri)); - boolean isExternalEntity = existing != null && session.syncRequest(new IsExternalEntity(existing)); - if (existing == null || isExternalEntity) { - MigrationUtils.importSharedOntology(session, dependency.tg, false); - } - } - } - } catch (AdaptException e) { - Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e)); - } + try { + if (includeDependencies) { + try { + ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state); + if (libraryDependenciesBean != null) { + for (ModelDependency dependency : libraryDependenciesBean.dependencies) { + Resource existing = session.sync(new PossibleResource(dependency.uri)); + boolean isExternalEntity = existing != null && session.syncRequest(new IsExternalEntity(existing)); + if (existing == null || isExternalEntity) { + MigrationUtils.importSharedOntology(session, dependency.tg, false); + } + } + } + } catch (AdaptException e) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e)); + } + } + + MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target); + + Collection resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES); + ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT); + return new MigratedImportResult(resultRoots, result); + } finally { + state.dispose(); } - - MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target); - - Collection resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES); - ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT); - return new MigratedImportResult(resultRoots, result); } };