From: Hannu Niemistö Date: Tue, 21 Mar 2017 08:33:45 +0000 (+0200) Subject: Merge "(refs #7102) Fixed comparator NodeEventHandler.TreePreOrderComparator" X-Git-Tag: v1.28.0~45 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=965fa411938344172cff82a2ec0b6c50b7cb2b7c;hp=a00b9cf3edf556c32bc6ab74c74e19ae76975dc0 Merge "(refs #7102) Fixed comparator NodeEventHandler.TreePreOrderComparator" --- diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java b/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java index 1358c4023..5e4ef406d 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/Charts.java @@ -48,9 +48,9 @@ public final class Charts { public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException { try { - Resource model = graph.syncRequest(new PossibleIndexRoot(subscriptionItem)); - if (model == null) { - throw new DatabaseException("There is no model for " + subscriptionItem); + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem)); + if (indexRoot == null) { + throw new DatabaseException("There is no index root for " + subscriptionItem); } ItemManager im = new ItemManager(data.history.getItems()); diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/TrendSupport.java b/bundles/org.simantics.charts/src/org/simantics/charts/TrendSupport.java index a396d10da..aeae9f5d3 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/TrendSupport.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/TrendSupport.java @@ -41,7 +41,7 @@ import org.simantics.utils.FileUtils; * * @author Tuukka Lehtonen */ -public class TrendSupport { +public class TrendSupport implements ITrendSupport { // Input private IDynamicExperiment experiment; @@ -170,4 +170,13 @@ public class TrendSupport { return finalPath.toFile(); } + @Override + public void setChartData(ReadGraph graph) throws DatabaseException { + } + + @Override + public ChartData getChartData() { + return chartData; + } + } diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java index c8efdc250..abd6e1ba5 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java @@ -14,6 +14,7 @@ package org.simantics.db.indexing; import java.io.File; import java.io.FileFilter; import java.io.IOException; +import java.util.ArrayList; import org.simantics.db.Resource; import org.simantics.db.Session; @@ -22,6 +23,7 @@ import org.simantics.db.common.request.IndexRoot; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.indexing.internal.IndexChangedWriter; import org.simantics.db.layer0.adapter.GenericRelationIndex; import org.simantics.db.layer0.genericrelation.IndexedRelations; import org.simantics.db.layer0.internal.SimanticsInternal; @@ -75,7 +77,10 @@ public final class DatabaseIndexing { return; if (DEBUG) System.out.println("Marking all indexes dirty"); - getAllDirtyFile().createNewFile(); + File allDirtyFile = getAllDirtyFile(); + if (allDirtyFile.createNewFile()) { + FileUtils.syncFile(allDirtyFile); + } } public static void clearAllDirty() throws IOException { @@ -85,39 +90,61 @@ public final class DatabaseIndexing { File indexBase = getIndexBaseLocation(); if (!indexBase.exists() || !indexBase.isDirectory()) return; - delete(getAllDirtyFile()); forEachIndexPath(new Procedure() { @Override public void execute(File indexPath) throws IOException { - delete(getChangedFile(indexPath)); + getChangedFile(indexPath).delete(); } }); - } + getAllDirtyFile().delete(); + } + /** * Internal to indexing, invoked by {@link IndexedRelationsImpl} which * doesn't want to throw these exceptions forward. Just log it. * * @param indexPath */ - static void markIndexChanged(File indexPath) { - if (!indexPath.exists()) - throw new IllegalArgumentException("index path " + indexPath + " does not exist"); - if (!indexPath.isDirectory()) - throw new IllegalArgumentException("index path " + indexPath + " is not a directory"); + static void markIndexChanged(Session session, File indexPath) { + if (DEBUG) + System.out.println("Marking index dirty: " + indexPath); try { - if (DEBUG) - System.out.println("Marking index dirty: " + indexPath); - getChangedFile(indexPath).createNewFile(); + File changedFile = getChangedFile(indexPath); + // Mark change only once per DB session. + if (getIndexChangedWriter(session).markDirty(changedFile)) { + if (indexPath.mkdirs()) { + if (changedFile.createNewFile()) { + FileUtils.syncFile(changedFile); + } + } + } } catch (IOException e) { Logger.defaultLogError(e); } } + private static IndexChangedWriter getIndexChangedWriter(Session session) { + IndexChangedWriter writer = session.peekService(IndexChangedWriter.class); + if (writer == null) { + synchronized (IndexChangedWriter.class) { + if (writer == null) + session.registerService(IndexChangedWriter.class, writer = new IndexChangedWriter()); + } + } + return writer; + } + public static void deleteAllIndexes() throws IOException { File indexBase = DatabaseIndexing.getIndexBaseLocation(); - delete(indexBase); + + ArrayList filter = new ArrayList<>(2); + filter.add(getAllDirtyFile().getAbsolutePath()); + filter.add(indexBase.getAbsolutePath()); + + FileUtils.deleteAllWithFilter(indexBase, filter); + FileUtils.deleteAll(indexBase); } public static void deleteIndex(final Resource relation, final Resource modelPart) throws DatabaseException { @@ -148,7 +175,13 @@ public final class DatabaseIndexing { public static void deleteIndex(File indexPath) throws IOException { if (DEBUG) System.out.println("Deleting index " + indexPath); - delete(indexPath); + + ArrayList filter = new ArrayList<>(2); + filter.add(getChangedFile(indexPath).getAbsolutePath()); + filter.add(indexPath.getAbsolutePath()); + + FileUtils.deleteAllWithFilter(indexPath, filter); + FileUtils.deleteAll(indexPath); } public static void validateIndexes() throws IOException { @@ -161,7 +194,7 @@ public final class DatabaseIndexing { // Make sure that index-base is a valid directory if (DEBUG) System.out.println(indexBase + " is not a directory! Removing it."); - delete(indexBase); + FileUtils.deleteAll(indexBase); indexBase.mkdirs(); return; } @@ -169,7 +202,6 @@ public final class DatabaseIndexing { if (allDirtyFile.isFile()) { if (DEBUG) System.out.println("All indexes marked dirty, removing them."); - delete(allDirtyFile); deleteAllIndexes(); } else { forEachIndexPath(new Procedure() { @@ -186,12 +218,6 @@ public final class DatabaseIndexing { } } - - private static void delete(File fileOrDir) throws IOException { - if (fileOrDir.exists()) - FileUtils.deleteAll(fileOrDir); - } - interface Procedure { void execute(T t) throws E; } diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsImpl.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsImpl.java index aaadc4704..1b4d62a1d 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsImpl.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsImpl.java @@ -392,13 +392,13 @@ public class IndexedRelationsImpl implements IndexedRelations { try { + DatabaseIndexing.markIndexChanged(processor.getSession(), searcher.getIndexPath()); if(!searcher.startAccess(null, processor.getSession(), true)) { // Could not write index for some reason. Ignore and let the next index query reinitialize the index. return; } searcher.insertIndex(progress.newChild(40), relation, 1, documents); - DatabaseIndexing.markIndexChanged(searcher.getIndexPath()); } catch (InvalidResourceReferenceException e) { throw new IndexException(e); @@ -429,13 +429,13 @@ public class IndexedRelationsImpl implements IndexedRelations { LockHandle handle = lock(processor, Pair.make(relationResource, input), true); try { + DatabaseIndexing.markIndexChanged(processor.getSession(), searcher.getIndexPath()); if(!searcher.startAccess(null, processor.getSession(), true)) { // Could not write index for some reason. Ignore and let the next index query reinitialize the index. return; } searcher.removeIndex(progress.newChild(40), relation, processor, key, keyValues); - DatabaseIndexing.markIndexChanged(searcher.getIndexPath()); } catch (DatabaseException e) { throw new IndexException(e); @@ -492,13 +492,12 @@ public class IndexedRelationsImpl implements IndexedRelations { try { + DatabaseIndexing.markIndexChanged(processor.getSession(), searcher.getIndexPath()); if(!searcher.startAccess(null, processor.getSession(), true)) { // Could not write index for some reason. Ignore and let the next index query reinitialize the index. return true; } - didChange = searcher.replaceIndex(progress.newChild(40), key, keyValues, relation, 1, documents); - if(didChange) - DatabaseIndexing.markIndexChanged(searcher.getIndexPath()); + searcher.replaceIndex(progress.newChild(40), key, keyValues, relation, 1, documents); } catch (InvalidResourceReferenceException e) { throw new IndexException(e); diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexChangedWriter.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexChangedWriter.java new file mode 100644 index 000000000..c0e97a2e4 --- /dev/null +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexChangedWriter.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2017 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.db.indexing.internal; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +/** + * An internal in-memory container for telling whether a certain index has been + * changed since the platform was last started up. + * + * @author Jussi Koskela + * @since 1.28.0 + */ +public class IndexChangedWriter { + private Set dirtyFiles = new HashSet<>(); + + public synchronized boolean markDirty(File dirtyFile) throws IOException { + return dirtyFiles.add(dirtyFile.getAbsolutePath()); + } +} diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java index 538f22478..adc51c16d 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java @@ -1349,6 +1349,18 @@ public class Layer0Utils { return null; } + public static Resource getPossiblePredicateByLabel(ReadGraph graph, Resource instance, String predicateName) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + for(Resource type : graph.getPrincipalTypes(instance)) { + Map domainOf = getDomainOf(graph, type); + for(Resource r : domainOf.values()) { + String label = graph.getPossibleRelatedValue(r, L0.HasLabel, Bindings.STRING); + if(predicateName.equals(label)) + return r; + } + } + return null; + } public static void claimLiteralDataboard(WriteGraph graph, Resource container, Resource property, String valueText) throws DatabaseException { diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/SymbolLibraryComposite.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/SymbolLibraryComposite.java index 7d172318b..4ec3258a7 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/SymbolLibraryComposite.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/SymbolLibraryComposite.java @@ -987,18 +987,20 @@ public class SymbolLibraryComposite extends Composite { final boolean expanded = grp.getExpanded(); final boolean visible = grp.getVisible(); final boolean filterChanged = !objectEquals(filter, lastFilter); + final ISymbolGroup symbolGroup = (ISymbolGroup) grp.getData(SymbolLibraryKeys.KEY_GROUP); + final boolean filterMatchesGroup = filter != null && filter.select(viewer, null, symbolGroup); // Find out how much data would be shown with the new filter. - viewer.setFilter(filter); + viewer.setFilter(filterMatchesGroup ? null : filter); Object[] elements = viewer.getFilteredElements(); - ISymbolGroup symbolGroup = (ISymbolGroup) grp.getData(SymbolLibraryKeys.KEY_GROUP); - boolean filterMatchesGroup = filter != null && filter.select(viewer, null, symbolGroup); boolean shouldBeVisible = !groupFiltered && (elements.length > 0 || filterMatchesGroup); boolean shouldBeExpanded = shouldBeVisible && (filter != null || userExpanded); -// System.out.format("%40s: visible/should be = %5s %5s, expanded/user expanded/should be = %5s %5s %5s\n", +// System.out.format("%40s: filterMatchesGroup(%s) = %s, visible/should be = %5s %5s, expanded/user expanded/should be = %5s %5s %5s\n", // grp.getText(), +// symbolGroup.getName(), +// String.valueOf(filterMatchesGroup), // String.valueOf(visible), // String.valueOf(shouldBeVisible), // String.valueOf(expanded), diff --git a/bundles/org.simantics.history/src/org/simantics/history/HistorySampler.java b/bundles/org.simantics.history/src/org/simantics/history/HistorySampler.java index 73df6dc61..1c45a220f 100644 --- a/bundles/org.simantics.history/src/org/simantics/history/HistorySampler.java +++ b/bundles/org.simantics.history/src/org/simantics/history/HistorySampler.java @@ -15,9 +15,10 @@ public class HistorySampler { public synchronized static TDoubleArrayList sample( HistorySamplerItem item, double from, double end, double timeWindow, double timeStep, boolean resample ) throws HistoryException, IOException { try { - item.open(); + // If there is something pending at this point, flush before opening for read if(item.collector != null) item.collector.flush(); + item.open(); return sample(item.iter, from, end, timeWindow, timeStep, resample); } finally { item.close(); @@ -25,10 +26,18 @@ public class HistorySampler { } public static TDoubleArrayList sample( StreamIterator iter, double from, double end, double timeWindow, double timeStep, boolean resample ) throws HistoryException, IOException { + return sample(iter, from, end, timeWindow, timeStep, resample, 0.0); + } + + public static TDoubleArrayList sample( StreamIterator iter, double from, double end, double timeWindow, double timeStep, boolean resample, Double sampleFrom ) throws HistoryException, IOException { ExportInterpolation numberInterpolation = ExportInterpolation.LINEAR_INTERPOLATION; - double startTime = 0.0; + double startTime = from; + if(sampleFrom != null) { + // This option can be used do define the offset of sampling. Samples will be sampleFrom + n * timeStep + startTime = sampleFrom; + } TDoubleArrayList result = new TDoubleArrayList(); diff --git a/bundles/org.simantics.modeling/scl/Simantics/Simulation.scl b/bundles/org.simantics.modeling/scl/Simantics/Simulation.scl index 4935fafcd..72166303e 100644 --- a/bundles/org.simantics.modeling/scl/Simantics/Simulation.scl +++ b/bundles/org.simantics.modeling/scl/Simantics/Simulation.scl @@ -6,10 +6,15 @@ importJava "org.simantics.simulation.experiment.ExperimentUtil" where stepExperiment :: IExperiment -> Double -> () simulateExperiment :: IExperiment -> Boolean -> () disposeExperiment :: IExperiment -> () - possibleActiveRunVariable ::Resource -> Variable + getExperimentState :: IExperiment -> ExperimentState + possibleActiveRunVariable ::Resource -> Maybe Variable importJava "org.simantics.simulation.experiment.IExperiment" where data IExperiment + +importJava "org.simantics.simulation.experiment.ExperimentState" where + data ExperimentState + INITIALIZING, RUNNING, STOPPED, DISPOSED :: ExperimentState experimentFromRun :: Variable -> IExperiment experimentFromRun run = (run#iExperiment) :: IExperiment \ No newline at end of file diff --git a/bundles/org.simantics.project/src/org/simantics/project/ProjectFeatures.java b/bundles/org.simantics.project/src/org/simantics/project/ProjectFeatures.java index 7fedc55d1..bb2ba5c2c 100644 --- a/bundles/org.simantics.project/src/org/simantics/project/ProjectFeatures.java +++ b/bundles/org.simantics.project/src/org/simantics/project/ProjectFeatures.java @@ -14,8 +14,6 @@ package org.simantics.project; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -188,22 +186,25 @@ public class ProjectFeatures { return false; } - private Collection sortTopologically(Collection toSort) { - ArrayList sorted = new ArrayList(toSort); - Collections.sort(sorted, new Comparator() { - Set visited = new HashSet(); - @Override - public int compare(IProjectFeatureExtension e1, IProjectFeatureExtension e2) { - visited.clear(); - if (deepRequires(visited, e1, e2)) - return 1; - visited.clear(); - if (deepRequires(visited, e2, e1)) - return -1; - return 0; + private void requiresDFS(IProjectFeatureExtension ext, ArrayList result, Set visited) { + if(visited.add(ext)) { + Set reqs = required.getValues(ext); + if(reqs != null) { + for(IProjectFeatureExtension req : reqs) { + requiresDFS(req, result, visited); + } } - }); - return sorted; + result.add(ext); + } + } + + private Collection sortTopologically(Collection toSort) { + ArrayList result = new ArrayList<>(); + Set visited = new HashSet<>(); + for(IProjectFeatureExtension ext : toSort) { + requiresDFS(ext, result, visited); + } + return result; } private Collection requiredExtensions(IProjectFeatureExtension[] allExtensions, Collection includedExtensions) throws ProjectException { @@ -234,7 +235,8 @@ public class ProjectFeatures { if (injectionTargetExt != null) { changed = true; includedProjectFeatureIds.add(ext.getId()); - result.add(ext); + if(!result.contains(ext)) + result.add(ext); required.add(injectionTargetExt, ext); } } diff --git a/bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java b/bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java index e861bc09d..b07304ed1 100644 --- a/bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java +++ b/bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java @@ -94,7 +94,7 @@ public class ProjectFeatureRegistry implements IProjectFeatureRegistry, IExtensi String description = StringUtils.safeString(el.getAttribute("description")); boolean published = "true".equalsIgnoreCase(el.getAttribute("published")); Collection requires = readProjectFeatureReferenceCollection(el, "requires"); - Collection injections = readInjectedDependencies(el); + Collection injections = readInjectedDependencies(el, id); Collection installGroups = readGroupReferenceCollection(el, "installGroup"); ProjectFeatureExtension ext = new ProjectFeatureExtension(el, id, label, description, published, requires, injections, installGroups); @@ -111,19 +111,17 @@ public class ProjectFeatureRegistry implements IProjectFeatureRegistry, IExtensi this.extensions = newExtensions.toArray(new IProjectFeatureExtension[newExtensions.size()]); } - private Collection readInjectedDependencies(IConfigurationElement element) { + private Collection readInjectedDependencies(IConfigurationElement element, String id) { Collection result = new ArrayList(); for (IConfigurationElement child : element.getChildren(INJECT_DEPENDENCY)) { - String id = StringUtils.safeString(child.getAttribute("id")); - if (id.isEmpty()) - // Invalid extension - return null; - String targetId = StringUtils.safeString(child.getAttribute("targetId")); if (targetId.isEmpty()) // Invalid extension return null; + + result.add(new InjectedDependency(new ProjectFeatureReference(id, false), new ProjectFeatureReference(targetId, false))); + } return result; diff --git a/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/Experiment.java b/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/Experiment.java index b4222df01..41125d0a0 100644 --- a/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/Experiment.java +++ b/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/Experiment.java @@ -19,6 +19,8 @@ import org.simantics.databoard.accessor.Accessor; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.Listener; +import org.simantics.db.request.ExternalRead; import org.simantics.utils.datastructures.ListenerList; public abstract class Experiment implements IExperiment { @@ -64,7 +66,7 @@ public abstract class Experiment implements IExperiment { @Override public ExperimentState getState(ReadGraph graph) throws DatabaseException { - throw new UnsupportedOperationException(); + return graph.syncRequest(EXPERIMENT_STATE_READ); } public void changeState(ExperimentState newState) { @@ -136,4 +138,37 @@ public abstract class Experiment implements IExperiment { return identifier; } + static class ExperimentStateRead implements ExternalRead, Runnable { + + final private Experiment experiment; + private Listener listener = null; + + ExperimentStateRead(Experiment experiment) { + this.experiment = experiment; + } + + @Override + public void register(ReadGraph graph, final Listener procedure) { + //System.out.println("IcTrackerRequest.register: " + procedure); + listener = procedure; + procedure.execute(experiment.state); + } + + @Override + public void unregistered() { + //System.out.println("IcTrackerRequest.unregister: " + listener); + listener = null; + } + + @Override + public void run() { + Listener l = listener; + //System.out.println("IcTrackerRequest.run: " + l); + if (l != null) + l.execute(experiment.state); + } + } + + private ExperimentStateRead EXPERIMENT_STATE_READ = new ExperimentStateRead(this); + } diff --git a/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java b/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java index 24f6065c5..a819ef8ed 100644 --- a/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java +++ b/bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java @@ -47,6 +47,10 @@ public final class ExperimentUtil { ((IDynamicExperiment)experiment).simulate(enabled); } + public static ExperimentState getExperimentState(ReadGraph graph, IExperiment experiment) throws DatabaseException { + return experiment.getState(graph); + } + public static void disposeExperiment(final IExperiment experiment) { if(experiment instanceof IDynamicExperiment) { diff --git a/bundles/org.simantics.utils.thread/META-INF/MANIFEST.MF b/bundles/org.simantics.utils.thread/META-INF/MANIFEST.MF index c71e25156..44435fa6e 100644 --- a/bundles/org.simantics.utils.thread/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.utils.thread/META-INF/MANIFEST.MF @@ -6,5 +6,4 @@ Bundle-Version: 1.1.0.qualifier Export-Package: org.simantics.utils.threads, org.simantics.utils.threads.logger Bundle-Vendor: VTT Technical Research Centre of Finland -Require-Bundle: org.eclipse.swt;bundle-version="3.6.0";resolution:=optional Bundle-RequiredExecutionEnvironment: JavaSE-1.8 diff --git a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/Executors2.java b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/Executors2.java deleted file mode 100644 index 66af9794b..000000000 --- a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/Executors2.java +++ /dev/null @@ -1,60 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.utils.threads; - -import java.util.concurrent.Executor; - -import org.eclipse.swt.widgets.Display; - -public class Executors2 { - - public static Executor createSWTExecutor(Display display, boolean async) { - return async ? new SWTExecutorAsync(display) : new SWTExecutorSync(display); - } - -} - -class SWTExecutorAsync implements Executor { - - Display display; - public SWTExecutorAsync(Display display) - { - this.display = display; - } - - @Override - public void execute(Runnable command) { - // Don't accept work if the SWT thread is disposed. - if (display.isDisposed()) - throw new RuntimeException("The SWT thread has been disposed"); - display.asyncExec(command); - } - -} - -class SWTExecutorSync implements Executor { - - Display display; - public SWTExecutorSync(Display display) - { - this.display = display; - } - - @Override - public void execute(Runnable command) { - // Don't accept work if the SWT thread is disposed. - if (display.isDisposed()) - throw new RuntimeException("The SWT thread has been disposed"); - display.syncExec(command); - } - -} - diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/FileService.java b/bundles/org.simantics.utils/src/org/simantics/utils/FileService.java index c500c6635..ab3d249d3 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/FileService.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/FileService.java @@ -22,6 +22,31 @@ public interface FileService { } } + @FunctionalInterface + public static interface FileOperation { + IOperation perform(File file); + + default void perform(File... files) { + for (File f : files) + perform(f); + } + } + + public static class DeleteOperation implements FileOperation { + private final FileService service; + private final DeleteOption[] options; + + public DeleteOperation(FileService service, DeleteOption... options) { + this.service = service; + this.options = options; + } + + @Override + public IOperation perform(File file) { + return service.scheduleDeleteIfExists(file, options); + } + } + /** * Schedules a file to be deleted at some point in the future when the * system allows it to be deleted if ever. @@ -33,4 +58,16 @@ public interface FileService { */ IOperation scheduleDeleteIfExists(File file, DeleteOption... options); + /** + * @param options + * the deletion options to be used by the returned FileOperation + * @return a FileOperation that runs + * {@link #scheduleDeleteIfExists(File, DeleteOption...)} for any + * files given to it + * @since 1.28.0 + */ + default FileOperation deleteOperation(DeleteOption... options) { + return new DeleteOperation(this, options); + } + } diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java index b3ec46a30..4e68f29c4 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java @@ -1013,4 +1013,10 @@ public class FileUtils { return FileVisitResult.CONTINUE; } } + + public static void syncFile(File file) throws IOException { + try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) { + raf.getFD().sync(); + } + } }