1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.indexing;
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;
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;
37 * A facade for Simantics graph database index management facilities.
39 * @author Tuukka Lehtonen
41 public final class DatabaseIndexing {
43 private static final boolean DEBUG = IndexPolicy.TRACE_INDEX_MANAGEMENT;
44 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatabaseIndexing.class);
46 public static Path getIndexBaseLocation() {
47 return Activator.getDefault().getIndexBaseFile();
50 public static Path getIndexLocation(Session session, Resource relation, Resource input) {
52 throw new NullPointerException("null session");
54 throw new NullPointerException("null relation");
56 throw new NullPointerException("null input");
58 String dir = session.getService(ServerInformation.class).getDatabaseId()
59 + "." + relation.getResourceId()
60 + "." + input.getResourceId();
62 return getIndexBaseLocation().resolve(dir);
65 private static Path getAllDirtyFile() {
66 return getIndexBaseLocation().resolve(".dirty");
69 private static Path getChangedFile(Path indexPath) {
70 return indexPath.resolve(".changed");
73 public static void markAllDirty() throws IOException {
74 Path indexBase = getIndexBaseLocation();
75 if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
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);
86 public static void clearAllDirty() throws IOException {
87 if (LOGGER.isDebugEnabled())
88 LOGGER.debug("Clearing dirty state of all indexes");
90 Path indexBase = getIndexBaseLocation();
91 if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
94 forEachIndexPath(indexPath -> {
95 Path p = getChangedFile(indexPath);
98 } catch (IOException e) {
99 LOGGER.error("Could not delete {}", p.toAbsolutePath(), e);
103 FileUtils.delete(getAllDirtyFile());
107 * Internal to indexing, invoked by {@link IndexedRelationsImpl} which
108 * doesn't want to throw these exceptions forward. Just log it.
112 static void markIndexChanged(Session session, Path indexPath) {
113 if (LOGGER.isDebugEnabled())
114 LOGGER.debug("Marking index dirty: " + indexPath);
115 Path changedFile = getChangedFile(indexPath);
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);
124 } catch (IOException e) {
125 LOGGER.error("Could not mark index changed for indexPath={} and changedFile={}", indexPath.toAbsolutePath(), changedFile.toAbsolutePath());
129 private static IndexChangedWriter getIndexChangedWriter(Session session) {
130 IndexChangedWriter writer = session.peekService(IndexChangedWriter.class);
131 if (writer == null) {
132 synchronized (IndexChangedWriter.class) {
134 session.registerService(IndexChangedWriter.class, writer = new IndexChangedWriter());
140 public static void deleteAllIndexes() throws IOException {
141 Path indexBase = DatabaseIndexing.getIndexBaseLocation();
143 ArrayList<Path> filter = new ArrayList<>(2);
144 filter.add(getAllDirtyFile());
145 filter.add(indexBase);
147 FileUtils.deleteWithFilter(indexBase, path -> !filter.contains(path));
148 FileUtils.delete(indexBase);
151 public static void deleteIndex(final Resource relation, final Resource modelPart) throws DatabaseException {
153 SimanticsInternal.getSession().syncRequest(new WriteRequest() {
156 public void perform(WriteGraph graph) throws DatabaseException {
157 deleteIndex(graph, relation, modelPart);
164 public static void deleteIndex(WriteGraph graph, final Resource relation, final Resource modelPart) throws DatabaseException {
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);
176 public static void deleteIndex(Path indexPath) throws IOException {
177 if (LOGGER.isDebugEnabled())
178 LOGGER.debug("Deleting index " + indexPath.toAbsolutePath());
180 ArrayList<Path> filter = new ArrayList<>(2);
181 filter.add(getChangedFile(indexPath));
182 filter.add(indexPath);
184 FileUtils.deleteWithFilter(indexPath, path -> !filter.contains(path));
185 FileUtils.delete(indexPath);
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))
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);
202 Path allDirtyFile = getAllDirtyFile();
203 if (Files.isRegularFile(allDirtyFile)) {
204 if (LOGGER.isDebugEnabled())
205 LOGGER.debug("All indexes marked dirty, removing them.");
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);
214 deleteIndex(indexPath);
215 } catch (IOException e) {
216 LOGGER.error("Could not delete index {}", indexPath.toAbsolutePath(), e);
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();