]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java
Restored old NoSingleResultException constructors without result count
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / DatabaseIndexing.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.indexing;
13
14 import java.io.IOException;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.function.Consumer;
20 import java.util.stream.Stream;
21
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.IndexRoot;
26 import org.simantics.db.common.request.WriteRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.indexing.internal.IndexChangedWriter;
29 import org.simantics.db.layer0.adapter.GenericRelationIndex;
30 import org.simantics.db.layer0.genericrelation.IndexedRelations;
31 import org.simantics.db.layer0.internal.SimanticsInternal;
32 import org.simantics.db.service.ServerInformation;
33 import org.simantics.utils.FileUtils;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * A facade for Simantics graph database index management facilities.
38  * 
39  * @author Tuukka Lehtonen
40  */
41 public final class DatabaseIndexing {
42
43     private static final boolean DEBUG = IndexPolicy.TRACE_INDEX_MANAGEMENT;
44     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatabaseIndexing.class);
45
46     public static Path getIndexBaseLocation() {
47         return Activator.getDefault().getIndexBaseFile();
48     }
49
50     public static Path getIndexLocation(Session session, Resource relation, Resource input) {
51         if (session == null)
52             throw new NullPointerException("null session");
53         if (relation == null)
54             throw new NullPointerException("null relation");
55         if (input == null)
56             throw new NullPointerException("null input");
57
58         String dir = session.getService(ServerInformation.class).getDatabaseId()
59         + "." + relation.getResourceId()
60         + "." + input.getResourceId();
61
62         return getIndexBaseLocation().resolve(dir);
63     }
64
65     private static Path getAllDirtyFile() {
66         return getIndexBaseLocation().resolve(".dirty");
67     }
68
69     private static Path getChangedFile(Path indexPath) {
70         return indexPath.resolve(".changed");
71     }
72
73     public static void markAllDirty() throws IOException {
74         Path indexBase = getIndexBaseLocation();
75         if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
76             return;
77         if (LOGGER.isDebugEnabled())
78             LOGGER.debug("Marking all indexes dirty");
79         Path allDirtyFile = getAllDirtyFile();
80         if (!Files.exists(allDirtyFile)) {
81             Files.createFile(allDirtyFile);
82             FileUtils.sync(allDirtyFile);
83         }
84     }
85
86     public static void clearAllDirty() throws IOException {
87         if (LOGGER.isDebugEnabled())
88             LOGGER.debug("Clearing dirty state of all indexes");
89
90         Path indexBase = getIndexBaseLocation();
91         if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
92             return;
93
94         forEachIndexPath(indexPath -> {
95             Path p = getChangedFile(indexPath);
96             try {
97                 FileUtils.delete(p);
98             } catch (IOException e) {
99                 LOGGER.error("Could not delete {}", p.toAbsolutePath(), e);
100             }
101         });
102
103         FileUtils.delete(getAllDirtyFile());
104     }
105     
106     /**
107      * Internal to indexing, invoked by {@link IndexedRelationsImpl} which
108      * doesn't want to throw these exceptions forward. Just log it.
109      * 
110      * @param indexPath
111      */
112     static void markIndexChanged(Session session, Path indexPath) {
113         if (LOGGER.isDebugEnabled())
114             LOGGER.debug("Marking index dirty: " + indexPath);
115         Path changedFile = getChangedFile(indexPath);
116         try {
117             
118             // Mark change only once per DB session.
119             if (getIndexChangedWriter(session).markDirty(changedFile)) {
120                 Files.createDirectories(indexPath);
121                 Files.createFile(changedFile);
122                 FileUtils.sync(changedFile);
123             }
124         } catch (IOException e) {
125             LOGGER.error("Could not mark index changed for indexPath={} and changedFile={}", indexPath.toAbsolutePath(), changedFile.toAbsolutePath());
126         }
127     }
128
129     private static IndexChangedWriter getIndexChangedWriter(Session session) {
130         IndexChangedWriter writer = session.peekService(IndexChangedWriter.class);
131         if (writer == null) {
132             synchronized (IndexChangedWriter.class) {
133                 if (writer == null)
134                     session.registerService(IndexChangedWriter.class, writer = new IndexChangedWriter());
135             }
136         }
137         return writer;
138     }
139
140     public static void deleteAllIndexes() throws IOException {
141         Path indexBase = DatabaseIndexing.getIndexBaseLocation();
142
143         ArrayList<Path> filter = new ArrayList<>(2);
144         filter.add(getAllDirtyFile());
145         filter.add(indexBase);
146
147         FileUtils.deleteWithFilter(indexBase, path -> !filter.contains(path));
148         FileUtils.delete(indexBase);
149     }
150
151     public static void deleteIndex(final Resource relation, final Resource modelPart) throws DatabaseException {
152
153         SimanticsInternal.getSession().syncRequest(new WriteRequest() {
154
155                         @Override
156                         public void perform(WriteGraph graph) throws DatabaseException {
157                                 deleteIndex(graph, relation, modelPart);
158                         }
159                 
160         });
161
162     }
163
164     public static void deleteIndex(WriteGraph graph, final Resource relation, final Resource modelPart) throws DatabaseException {
165         
166         Resource model = graph.syncRequest(new IndexRoot(modelPart));
167         GenericRelationIndex index = graph.adapt(relation, GenericRelationIndex.class);
168         IndexedRelations ir = graph.getService(IndexedRelations.class);
169         // Deletes index files
170         ir.reset(null, graph, relation, model);
171         // Notifies DB listeners
172         index.reset(graph, model);
173         
174     }
175     
176     public static void deleteIndex(Path indexPath) throws IOException {
177         if (LOGGER.isDebugEnabled())
178             LOGGER.debug("Deleting index " + indexPath.toAbsolutePath());
179
180         ArrayList<Path> filter = new ArrayList<>(2);
181         filter.add(getChangedFile(indexPath));
182         filter.add(indexPath);
183
184         FileUtils.deleteWithFilter(indexPath, path -> !filter.contains(path));
185         FileUtils.delete(indexPath);
186     }
187
188     public static void validateIndexes() throws IOException {
189         Path indexBase = getIndexBaseLocation();
190         if (LOGGER.isDebugEnabled())
191             LOGGER.debug("Validating indexes at " + indexBase);
192         if (!Files.exists(indexBase))
193             return;
194         if (!Files.isDirectory(indexBase)) {
195             // Make sure that index-base is a valid directory
196             if (LOGGER.isDebugEnabled())
197                 LOGGER.debug(indexBase + " is not a directory! Removing it.");
198             FileUtils.delete(indexBase);
199             Files.createDirectories(indexBase);
200             return;
201         }
202         Path allDirtyFile = getAllDirtyFile();
203         if (Files.isRegularFile(allDirtyFile)) {
204             if (LOGGER.isDebugEnabled())
205                 LOGGER.debug("All indexes marked dirty, removing them.");
206             deleteAllIndexes();
207         } else {
208             forEachIndexPath(indexPath -> {
209                 Path changed = getChangedFile(indexPath);
210                 if (Files.isRegularFile(changed)) {
211                     if (LOGGER.isDebugEnabled())
212                         LOGGER.debug("Index is dirty, removing: " + indexPath);
213                     try {
214                         deleteIndex(indexPath);
215                     } catch (IOException e) {
216                         LOGGER.error("Could not delete index {}", indexPath.toAbsolutePath(), e);
217                     }
218                 }
219             });
220         }
221     }
222
223     private static void forEachIndexPath(Consumer<Path> callback) throws IOException {
224         try (Stream<Path> paths = Files.walk(getIndexBaseLocation(), 1).filter(Files::isDirectory)) {
225             Iterator<Path> iter = paths.iterator();
226             while (iter.hasNext()) {
227                 Path p = iter.next();
228                 callback.accept(p);
229             }
230         }
231     }
232
233 }