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.exception.AssumptionException;
37 import org.simantics.db.exception.DatabaseException;
38 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
39 import org.simantics.db.layer0.internal.SimanticsInternal;
40 import org.simantics.db.layer0.util.Layer0Utils;
41 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
42 import org.simantics.db.layer0.util.TGProgressMonitor;
43 import org.simantics.db.layer0.util.TGTransferableGraphSource;
44 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
45 import org.simantics.db.request.Read;
46 import org.simantics.db.service.ManagementSupport;
47 import org.simantics.db.service.SerialisationSupport;
48 import org.simantics.graph.db.IImportAdvisor;
49 import org.simantics.graph.db.IImportAdvisor2;
50 import org.simantics.graph.db.ImportResult;
51 import org.simantics.graph.db.StreamingTransferableGraphFileReader;
52 import org.simantics.graph.db.TGStatusMonitor;
53 import org.simantics.graph.db.TransferableGraphImporter;
54 import org.simantics.graph.db.TransferableGraphSource;
55 import org.simantics.graph.db.TransferableGraphs;
56 import org.simantics.graph.db.WrapperAdvisor;
57 import org.simantics.graph.representation.TransferableGraph1;
58 import org.simantics.graph.representation.TransferableGraphFileReader;
59 import org.simantics.layer0.Layer0;
60 import org.simantics.utils.logging.TimeLogger;
62 public class MigrationStateImpl implements MigrationState {
64 final private HashMap<String, Object> properties = new HashMap<String, Object>();
66 @SuppressWarnings("unchecked")
68 public <T> T probeProperty(String key) throws DatabaseException {
69 return (T)properties.get(key);
72 @SuppressWarnings("unchecked")
74 public <T> T getProperty(String key) throws DatabaseException {
76 T property = (T)properties.get(key);
77 if(property != null) return property;
79 if(MigrationStateKeys.BASE_URI.equals(key)) {
80 throw new IllegalStateException("Base URI needs to be supplied for migration.");
81 } else if (MigrationStateKeys.SESSION.equals(key)) {
82 throw new IllegalStateException("Session needs to be supplied for migration.");
83 } else if (MigrationStateKeys.MODEL_FILE.equals(key)) {
84 throw new IllegalStateException("Model file needs to be supplied for migration.");
85 } else if (MigrationStateKeys.CURRENT_TG.equals(key)) {
87 final Resource resource = probeProperty(MigrationStateKeys.CURRENT_RESOURCE);
88 final Collection<Resource> roots = probeProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
90 Session session = getProperty(MigrationStateKeys.SESSION);
91 TransferableGraph1 tg = session.syncRequest(new Read<TransferableGraph1>() {
93 public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
94 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, roots, true, false);
95 TransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
96 return TransferableGraphs.create(graph, source);
100 MigrationUtils.clearTempResource(session, resource);
101 setProperty(MigrationStateKeys.CURRENT_RESOURCE, null);
102 setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, null);
103 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, null);
104 setProperty(MigrationStateKeys.CURRENT_TG, tg);
108 TransferableGraphFileReader reader = null;
110 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
111 reader = new TransferableGraphFileReader(modelFile);
112 TimeLogger.log(MigrationStateImpl.class, "reading TG into memory from " + modelFile);
113 TransferableGraph1 tg = reader.readTG();
114 TimeLogger.log(MigrationStateImpl.class, "read TG into memory from " + modelFile);
115 setProperty(MigrationStateKeys.CURRENT_TG, tg);
117 } catch (DatabaseException e) {
119 } catch (Throwable t) {
120 throw new DatabaseException(t);
122 uncheckedClose(reader);
125 } else if (MigrationStateKeys.CURRENT_TGS.equals(key)) {
127 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
128 return (T) initializeTransferableGraphSource(modelFile);
130 } else if (MigrationStateKeys.CURRENT_DATA_CONTAINER.equals(key)) {
134 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
135 DataContainer dc = tgs.getHeader();
136 setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, dc);
139 } catch (DatabaseException e) {
141 } catch (Throwable t) {
142 throw new DatabaseException(t);
145 } else if (MigrationStateKeys.TG_EXTENSIONS.equals(key)) {
149 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
150 Map<String,Variant> extensions = tgs.getExtensions();
151 setProperty(MigrationStateKeys.TG_EXTENSIONS, extensions);
152 return (T)extensions;
154 } catch (DatabaseException e) {
156 } catch (Throwable t) {
157 throw new DatabaseException(t);
160 } else if (MigrationStateKeys.CURRENT_RESOURCE.equals(key) || MigrationStateKeys.CURRENT_ROOT_RESOURCES.equals(key)) {
162 final Session session = getProperty(MigrationStateKeys.SESSION);
163 final IProgressMonitor monitor = probeProperty(MigrationStateKeys.PROGRESS_MONITOR);
164 final boolean updateDependencies = MigrationUtils.getProperty(this, MigrationStateKeys.UPDATE_DEPENDENCIES, Boolean.TRUE);
166 File temporaryTg = exportCurrentTgAsTemporaryFile(session, monitor);
167 if (temporaryTg != null)
168 setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg));
170 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
172 importTransferableGraphSource(monitor, session, updateDependencies, tgs);
173 // Delete temporary file if necessary
174 if (temporaryTg != null)
175 temporaryTg.delete();
176 return getProperty(key);
179 } else if (MigrationStateKeys.UPDATE_DEPENDENCIES.equals(key)) {
190 public <T> void setProperty(String key, T value) {
191 properties.put(key, value);
195 public void dispose() {
196 // Close all possible open file handles
198 StreamingTransferableGraphFileReader tgs = probeProperty(MigrationStateKeys.CURRENT_TGS_READER);
200 } catch (DatabaseException e) {
201 Logger.defaultLogError(e);
205 private static void uncheckedClose(Closeable closeable) {
207 if (closeable != null)
209 } catch (IOException e) {
214 private Resource createTemporaryRoot(WriteGraph graph) throws DatabaseException {
215 Layer0 L0 = Layer0.getInstance(graph);
216 Resource project = SimanticsInternal.getProject();
217 Resource root = graph.getPossibleObject(project, L0.PartOf);
218 Resource temp = Layer0Utils.getPossibleChild(graph, root, "Temp");
220 throw new AssumptionException("Temporary folder 'Temp' not found under " + graph.getPossibleURI(root));
222 Resource indexRoot = graph.newResource();
223 String indexRootName = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date());
224 graph.claim(indexRoot, L0.InstanceOf, L0.IndexRoot);
225 graph.addLiteral(indexRoot, L0.HasName, L0.String, indexRootName, Bindings.STRING);
226 graph.claim(temp, L0.ConsistsOf, indexRoot);
230 private File exportCurrentTgAsTemporaryFile(Session session, IProgressMonitor monitor) throws DatabaseException {
231 TransferableGraph1 tg = probeProperty(MigrationStateKeys.CURRENT_TG);
236 // Write TG back to disk and initialize CURRENT_TGS for the migrated TG.
237 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
238 File tempFile = File.createTempFile("temporary-tgs", ".tg", SimanticsInternal.getTemporaryDirectory());
239 TimeLogger.log(MigrationStateImpl.class, "export temporary TG " + tempFile);
241 DataContainer dc = DataContainers.readHeader(modelFile);
242 TransferableGraphs.writeTransferableGraph(session, dc.format, dc.version, dc.metadata,
243 new TGTransferableGraphSource(tg),
245 new TGExportMonitor(monitor, "Exporting temporary transferable graph"));
247 // Allow potentially large TG structure to be GC'ed.
248 setProperty(MigrationStateKeys.CURRENT_TG, null);
250 TimeLogger.log(MigrationStateImpl.class, "export temporary TG done " + tempFile);
252 } catch (Exception e) {
253 throw new DatabaseException(e);
257 private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException {
259 StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer);
260 TransferableGraphSource tgs = reader.readTG();
261 setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);
262 setProperty(MigrationStateKeys.CURRENT_TGS, tgs);
264 } catch (DatabaseException e) {
266 } catch (IOException e) {
267 throw new DatabaseException("An I/O exception occurred during reading '" + dataContainer.getAbsolutePath() + "'", e);
268 } catch (Throwable t) {
269 throw new DatabaseException(t);
273 private void importTransferableGraphSource(IProgressMonitor monitor, Session session, boolean updateDependencies, TransferableGraphSource tgs) throws DatabaseException {
274 TimeLogger.log(MigrationStateImpl.class, "import TGS " + tgs);
275 final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {
277 public Resource perform(WriteGraph graph) throws DatabaseException {
278 if(!updateDependencies)
279 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
280 return createTemporaryRoot(graph);
284 IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));
285 IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {
287 public Resource getTarget() {
291 public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
292 super.beforeWrite(graph, process);
293 if(!updateDependencies)
294 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
297 public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
298 super.afterWrite(graph, process);
299 Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);
300 if(storeResources != null && storeResources) {
301 long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));
302 setProperty(MigrationStateKeys.RESOURCE_IDS, ids);
307 // Make sure that the supplied advisor is redirected to temp
308 advisor.redirect(indexRoot);
310 String task = "Importing model into database";
311 monitor.subTask(task);
312 ImportResult ir = TransferableGraphs.importGraph1(session, tgs, advisor, new TGImportMonitor(monitor, task));
314 setProperty(MigrationStateKeys.IMPORT_RESULT, ir);
315 setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);
316 setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));
317 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());
318 TimeLogger.log(MigrationStateImpl.class, "imported TGS " + tgs);
322 static class TGImportMonitor implements TGStatusMonitor {
323 private final IProgressMonitor monitor;
324 private final String message;
325 public TGImportMonitor(IProgressMonitor monitor, String message) {
326 this.monitor = monitor;
327 this.message = message;
330 public void status(int percentage) {
331 monitor.subTask(message + " (" + percentage + "%)");
334 public boolean isCanceled() {
335 return monitor.isCanceled();
339 static class TGExportMonitor extends TGProgressMonitor {
340 private final String message;
341 public TGExportMonitor(IProgressMonitor monitor, String message) {
343 this.message = message;
346 protected void workDone(int percentage) {
347 monitor.subTask(message + " (" + percentage + "%)");