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);
110 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
111 TimeLogger.log(MigrationStateImpl.class, "reading TG into memory from " + modelFile);
112 TransferableGraph1 tg = TransferableGraphFileReader.read(modelFile);
113 TimeLogger.log(MigrationStateImpl.class, "read TG into memory from " + modelFile);
114 setProperty(MigrationStateKeys.CURRENT_TG, tg);
116 } catch (DatabaseException e) {
118 } catch (Throwable t) {
119 throw new DatabaseException(t);
122 } else if (MigrationStateKeys.CURRENT_TGS.equals(key)) {
124 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
125 return (T) initializeTransferableGraphSource(modelFile);
127 } else if (MigrationStateKeys.CURRENT_DATA_CONTAINER.equals(key)) {
131 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
132 DataContainer dc = tgs.getHeader();
133 setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, dc);
136 } catch (DatabaseException e) {
138 } catch (Throwable t) {
139 throw new DatabaseException(t);
142 } else if (MigrationStateKeys.TG_EXTENSIONS.equals(key)) {
146 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
147 Map<String,Variant> extensions = tgs.getExtensions();
148 setProperty(MigrationStateKeys.TG_EXTENSIONS, extensions);
149 return (T)extensions;
151 } catch (DatabaseException e) {
153 } catch (Throwable t) {
154 throw new DatabaseException(t);
157 } else if (MigrationStateKeys.CURRENT_RESOURCE.equals(key) || MigrationStateKeys.CURRENT_ROOT_RESOURCES.equals(key)) {
159 final Session session = getProperty(MigrationStateKeys.SESSION);
160 final IProgressMonitor monitor = probeProperty(MigrationStateKeys.PROGRESS_MONITOR);
161 final boolean updateDependencies = MigrationUtils.getProperty(this, MigrationStateKeys.UPDATE_DEPENDENCIES, Boolean.TRUE);
163 File temporaryTg = exportCurrentTgAsTemporaryFile(session, monitor);
164 if (temporaryTg != null)
165 setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg));
167 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
169 importTransferableGraphSource(monitor, session, updateDependencies, tgs);
170 // Delete temporary file if necessary
171 if (temporaryTg != null)
172 temporaryTg.delete();
173 return getProperty(key);
176 } else if (MigrationStateKeys.UPDATE_DEPENDENCIES.equals(key)) {
187 public <T> void setProperty(String key, T value) {
188 properties.put(key, value);
192 public void dispose() {
193 // Close all possible open file handles
195 StreamingTransferableGraphFileReader tgs = probeProperty(MigrationStateKeys.CURRENT_TGS_READER);
197 } catch (DatabaseException e) {
198 Logger.defaultLogError(e);
202 private static void uncheckedClose(Closeable closeable) {
204 if (closeable != null)
206 } catch (IOException e) {
211 private Resource createTemporaryRoot(WriteGraph graph) throws DatabaseException {
212 Layer0 L0 = Layer0.getInstance(graph);
213 Resource project = SimanticsInternal.getProject();
214 Resource root = graph.getPossibleObject(project, L0.PartOf);
215 Resource temp = Layer0Utils.getPossibleChild(graph, root, "Temp");
217 throw new AssumptionException("Temporary folder 'Temp' not found under " + graph.getPossibleURI(root));
219 Resource indexRoot = graph.newResource();
220 String indexRootName = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date());
221 indexRootName = NameUtils.findFreshName(graph, indexRootName, temp, L0.ConsistsOf, "%s-%d");
222 graph.claim(indexRoot, L0.InstanceOf, L0.IndexRoot);
223 graph.addLiteral(indexRoot, L0.HasName, L0.String, indexRootName, Bindings.STRING);
224 graph.claim(temp, L0.ConsistsOf, indexRoot);
228 private File exportCurrentTgAsTemporaryFile(Session session, IProgressMonitor monitor) throws DatabaseException {
229 TransferableGraph1 tg = probeProperty(MigrationStateKeys.CURRENT_TG);
234 // Write TG back to disk and initialize CURRENT_TGS for the migrated TG.
235 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
236 File tempFile = File.createTempFile("temporary-tgs", ".tg", SimanticsInternal.getTemporaryDirectory());
237 TimeLogger.log(MigrationStateImpl.class, "export temporary TG " + tempFile);
239 DataContainer dc = DataContainers.readHeader(modelFile);
240 TransferableGraphs.writeTransferableGraph(session, dc.format, dc.version, dc.metadata,
241 new TGTransferableGraphSource(tg),
243 new TGExportMonitor(monitor, "Exporting temporary transferable graph"));
245 // Allow potentially large TG structure to be GC'ed.
246 setProperty(MigrationStateKeys.CURRENT_TG, null);
248 TimeLogger.log(MigrationStateImpl.class, "export temporary TG done " + tempFile);
250 } catch (Exception e) {
251 throw new DatabaseException(e);
255 private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException {
257 StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer);
258 TransferableGraphSource tgs = reader.readTG();
259 setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);
260 setProperty(MigrationStateKeys.CURRENT_TGS, tgs);
262 } catch (DatabaseException e) {
264 } catch (IOException e) {
265 throw new DatabaseException("An I/O exception occurred during reading '" + dataContainer.getAbsolutePath() + "'", e);
266 } catch (Throwable t) {
267 throw new DatabaseException(t);
271 private void importTransferableGraphSource(IProgressMonitor monitor, Session session, boolean updateDependencies, TransferableGraphSource tgs) throws DatabaseException {
272 TimeLogger.log(MigrationStateImpl.class, "import TGS " + tgs);
273 final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {
275 public Resource perform(WriteGraph graph) throws DatabaseException {
276 if(!updateDependencies)
277 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
278 return createTemporaryRoot(graph);
282 IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));
283 IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {
285 public Resource getTarget() {
289 public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
290 super.beforeWrite(graph, process);
291 if(!updateDependencies)
292 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
295 public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
296 super.afterWrite(graph, process);
297 Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);
298 if(storeResources != null && storeResources) {
299 long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));
300 setProperty(MigrationStateKeys.RESOURCE_IDS, ids);
305 // Make sure that the supplied advisor is redirected to temp
306 advisor.redirect(indexRoot);
308 String task = "Importing model into database";
309 monitor.subTask(task);
310 ImportResult ir = TransferableGraphs.importGraph1(session, tgs, advisor, new TGImportMonitor(monitor, task));
313 setProperty(MigrationStateKeys.IMPORT_RESULT, ir);
314 setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);
315 setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));
316 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());
317 TimeLogger.log(MigrationStateImpl.class, "imported TGS " + tgs);
321 static class TGImportMonitor implements TGStatusMonitor {
322 private final IProgressMonitor monitor;
323 private final String message;
324 public TGImportMonitor(IProgressMonitor monitor, String message) {
325 this.monitor = monitor;
326 this.message = message;
329 public void status(int percentage) {
330 monitor.subTask(message + " (" + percentage + "%)");
333 public boolean isCanceled() {
334 return monitor.isCanceled();
338 static class TGExportMonitor extends TGProgressMonitor {
339 private final String message;
340 public TGExportMonitor(IProgressMonitor monitor, String message) {
342 this.message = message;
345 protected void workDone(int percentage) {
346 monitor.subTask(message + " (" + percentage + "%)");