1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.migration;
14 import java.io.Closeable;
16 import java.io.IOException;
17 import java.text.DateFormat;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.HashMap;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.simantics.databoard.Bindings;
26 import org.simantics.databoard.binding.mutable.Variant;
27 import org.simantics.databoard.container.DataContainer;
28 import org.simantics.databoard.container.DataContainers;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Session;
32 import org.simantics.db.WriteGraph;
33 import org.simantics.db.WriteOnlyGraph;
34 import org.simantics.db.common.request.WriteResultRequest;
35 import org.simantics.db.common.utils.Logger;
36 import org.simantics.db.common.utils.NameUtils;
37 import org.simantics.db.exception.AssumptionException;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
40 import org.simantics.db.layer0.internal.SimanticsInternal;
41 import org.simantics.db.layer0.util.Layer0Utils;
42 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
43 import org.simantics.db.layer0.util.TGProgressMonitor;
44 import org.simantics.db.layer0.util.TGTransferableGraphSource;
45 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
46 import org.simantics.db.request.Read;
47 import org.simantics.db.service.ManagementSupport;
48 import org.simantics.db.service.SerialisationSupport;
49 import org.simantics.graph.db.IImportAdvisor;
50 import org.simantics.graph.db.IImportAdvisor2;
51 import org.simantics.graph.db.ImportResult;
52 import org.simantics.graph.db.StreamingTransferableGraphFileReader;
53 import org.simantics.graph.db.TGStatusMonitor;
54 import org.simantics.graph.db.TransferableGraphImporter;
55 import org.simantics.graph.db.TransferableGraphSource;
56 import org.simantics.graph.db.TransferableGraphs;
57 import org.simantics.graph.db.WrapperAdvisor;
58 import org.simantics.graph.representation.TransferableGraph1;
59 import org.simantics.graph.representation.TransferableGraphFileReader;
60 import org.simantics.layer0.Layer0;
61 import org.simantics.utils.logging.TimeLogger;
63 public class MigrationStateImpl implements MigrationState {
65 final private HashMap<String, Object> properties = new HashMap<String, Object>();
67 @SuppressWarnings("unchecked")
69 public <T> T probeProperty(String key) throws DatabaseException {
70 return (T)properties.get(key);
73 @SuppressWarnings("unchecked")
75 public <T> T getProperty(String key) throws DatabaseException {
77 T property = (T)properties.get(key);
78 if(property != null) return property;
80 if(MigrationStateKeys.BASE_URI.equals(key)) {
81 throw new IllegalStateException("Base URI needs to be supplied for migration.");
82 } else if (MigrationStateKeys.SESSION.equals(key)) {
83 throw new IllegalStateException("Session needs to be supplied for migration.");
84 } else if (MigrationStateKeys.MODEL_FILE.equals(key)) {
85 throw new IllegalStateException("Model file needs to be supplied for migration.");
86 } else if (MigrationStateKeys.CURRENT_TG.equals(key)) {
88 final Resource resource = probeProperty(MigrationStateKeys.CURRENT_RESOURCE);
89 final Collection<Resource> roots = probeProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
91 Session session = getProperty(MigrationStateKeys.SESSION);
92 TransferableGraph1 tg = session.syncRequest(new Read<TransferableGraph1>() {
94 public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
95 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, roots, true, false);
96 TransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
97 return TransferableGraphs.create(graph, source);
100 if (resource != null)
101 MigrationUtils.clearTempResource(session, resource);
102 setProperty(MigrationStateKeys.CURRENT_RESOURCE, null);
103 setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, null);
104 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, null);
105 setProperty(MigrationStateKeys.CURRENT_TG, tg);
109 TransferableGraphFileReader reader = null;
111 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
112 reader = new TransferableGraphFileReader(modelFile);
113 TimeLogger.log(MigrationStateImpl.class, "reading TG into memory from " + modelFile);
114 TransferableGraph1 tg = reader.readTG();
115 TimeLogger.log(MigrationStateImpl.class, "read TG into memory from " + modelFile);
116 setProperty(MigrationStateKeys.CURRENT_TG, tg);
118 } catch (DatabaseException e) {
120 } catch (Throwable t) {
121 throw new DatabaseException(t);
123 uncheckedClose(reader);
126 } else if (MigrationStateKeys.CURRENT_TGS.equals(key)) {
128 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
129 return (T) initializeTransferableGraphSource(modelFile);
131 } else if (MigrationStateKeys.CURRENT_DATA_CONTAINER.equals(key)) {
135 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
136 DataContainer dc = tgs.getHeader();
137 setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, dc);
140 } catch (DatabaseException e) {
142 } catch (Throwable t) {
143 throw new DatabaseException(t);
146 } else if (MigrationStateKeys.TG_EXTENSIONS.equals(key)) {
150 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
151 Map<String,Variant> extensions = tgs.getExtensions();
152 setProperty(MigrationStateKeys.TG_EXTENSIONS, extensions);
153 return (T)extensions;
155 } catch (DatabaseException e) {
157 } catch (Throwable t) {
158 throw new DatabaseException(t);
161 } else if (MigrationStateKeys.CURRENT_RESOURCE.equals(key) || MigrationStateKeys.CURRENT_ROOT_RESOURCES.equals(key)) {
163 final Session session = getProperty(MigrationStateKeys.SESSION);
164 final IProgressMonitor monitor = probeProperty(MigrationStateKeys.PROGRESS_MONITOR);
165 final boolean updateDependencies = MigrationUtils.getProperty(this, MigrationStateKeys.UPDATE_DEPENDENCIES, Boolean.TRUE);
167 File temporaryTg = exportCurrentTgAsTemporaryFile(session, monitor);
168 if (temporaryTg != null)
169 setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg));
171 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
173 importTransferableGraphSource(monitor, session, updateDependencies, tgs);
174 // Delete temporary file if necessary
175 if (temporaryTg != null)
176 temporaryTg.delete();
177 return getProperty(key);
180 } else if (MigrationStateKeys.UPDATE_DEPENDENCIES.equals(key)) {
191 public <T> void setProperty(String key, T value) {
192 properties.put(key, value);
196 public void dispose() {
197 // Close all possible open file handles
199 StreamingTransferableGraphFileReader tgs = probeProperty(MigrationStateKeys.CURRENT_TGS_READER);
201 } catch (DatabaseException e) {
202 Logger.defaultLogError(e);
206 private static void uncheckedClose(Closeable closeable) {
208 if (closeable != null)
210 } catch (IOException e) {
215 private Resource createTemporaryRoot(WriteGraph graph) throws DatabaseException {
216 Layer0 L0 = Layer0.getInstance(graph);
217 Resource project = SimanticsInternal.getProject();
218 Resource root = graph.getPossibleObject(project, L0.PartOf);
219 Resource temp = Layer0Utils.getPossibleChild(graph, root, "Temp");
221 throw new AssumptionException("Temporary folder 'Temp' not found under " + graph.getPossibleURI(root));
223 Resource indexRoot = graph.newResource();
224 String indexRootName = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date());
225 indexRootName = NameUtils.findFreshName(graph, indexRootName, temp, L0.ConsistsOf, "%s-%d");
226 graph.claim(indexRoot, L0.InstanceOf, L0.IndexRoot);
227 graph.addLiteral(indexRoot, L0.HasName, L0.String, indexRootName, Bindings.STRING);
228 graph.claim(temp, L0.ConsistsOf, indexRoot);
232 private File exportCurrentTgAsTemporaryFile(Session session, IProgressMonitor monitor) throws DatabaseException {
233 TransferableGraph1 tg = probeProperty(MigrationStateKeys.CURRENT_TG);
238 // Write TG back to disk and initialize CURRENT_TGS for the migrated TG.
239 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
240 File tempFile = File.createTempFile("temporary-tgs", ".tg", SimanticsInternal.getTemporaryDirectory());
241 TimeLogger.log(MigrationStateImpl.class, "export temporary TG " + tempFile);
243 DataContainer dc = DataContainers.readHeader(modelFile);
244 TransferableGraphs.writeTransferableGraph(session, dc.format, dc.version, dc.metadata,
245 new TGTransferableGraphSource(tg),
247 new TGExportMonitor(monitor, "Exporting temporary transferable graph"));
249 // Allow potentially large TG structure to be GC'ed.
250 setProperty(MigrationStateKeys.CURRENT_TG, null);
252 TimeLogger.log(MigrationStateImpl.class, "export temporary TG done " + tempFile);
254 } catch (Exception e) {
255 throw new DatabaseException(e);
259 private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException {
261 StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer);
262 TransferableGraphSource tgs = reader.readTG();
263 setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);
264 setProperty(MigrationStateKeys.CURRENT_TGS, tgs);
266 } catch (DatabaseException e) {
268 } catch (IOException e) {
269 throw new DatabaseException("An I/O exception occurred during reading '" + dataContainer.getAbsolutePath() + "'", e);
270 } catch (Throwable t) {
271 throw new DatabaseException(t);
275 private void importTransferableGraphSource(IProgressMonitor monitor, Session session, boolean updateDependencies, TransferableGraphSource tgs) throws DatabaseException {
276 TimeLogger.log(MigrationStateImpl.class, "import TGS " + tgs);
277 final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {
279 public Resource perform(WriteGraph graph) throws DatabaseException {
280 if(!updateDependencies)
281 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
282 return createTemporaryRoot(graph);
286 IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));
287 IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {
289 public Resource getTarget() {
293 public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
294 super.beforeWrite(graph, process);
295 if(!updateDependencies)
296 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
299 public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
300 super.afterWrite(graph, process);
301 Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);
302 if(storeResources != null && storeResources) {
303 long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));
304 setProperty(MigrationStateKeys.RESOURCE_IDS, ids);
309 // Make sure that the supplied advisor is redirected to temp
310 advisor.redirect(indexRoot);
312 String task = "Importing model into database";
313 monitor.subTask(task);
314 ImportResult ir = TransferableGraphs.importGraph1(session, tgs, advisor, new TGImportMonitor(monitor, task));
317 setProperty(MigrationStateKeys.IMPORT_RESULT, ir);
318 setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);
319 setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));
320 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());
321 TimeLogger.log(MigrationStateImpl.class, "imported TGS " + tgs);
325 static class TGImportMonitor implements TGStatusMonitor {
326 private final IProgressMonitor monitor;
327 private final String message;
328 public TGImportMonitor(IProgressMonitor monitor, String message) {
329 this.monitor = monitor;
330 this.message = message;
333 public void status(int percentage) {
334 monitor.subTask(message + " (" + percentage + "%)");
337 public boolean isCanceled() {
338 return monitor.isCanceled();
342 static class TGExportMonitor extends TGProgressMonitor {
343 private final String message;
344 public TGExportMonitor(IProgressMonitor monitor, String message) {
346 this.message = message;
349 protected void workDone(int percentage) {
350 monitor.subTask(message + " (" + percentage + "%)");