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 org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatabaseIndexing.class);
45 public static Path getIndexBaseLocation() {
46 return Activator.getDefault().getIndexBaseFile();
49 public static Path getIndexLocation(Session session, Resource relation, Resource input) {
51 throw new NullPointerException("null session");
53 throw new NullPointerException("null relation");
55 throw new NullPointerException("null input");
57 String dir = session.getService(ServerInformation.class).getDatabaseId()
58 + "." + relation.getResourceId()
59 + "." + input.getResourceId();
61 return getIndexBaseLocation().resolve(dir);
64 private static Path getAllDirtyFile() {
65 return getIndexBaseLocation().resolve(".dirty");
68 private static Path getChangedFile(Path indexPath) {
69 return indexPath.resolve(".changed");
72 public static void markAllDirty() throws IOException {
73 Path indexBase = getIndexBaseLocation();
74 if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
76 if (LOGGER.isDebugEnabled())
77 LOGGER.debug("Marking all indexes dirty");
78 Path allDirtyFile = getAllDirtyFile();
79 if (!Files.exists(allDirtyFile)) {
80 Files.createFile(allDirtyFile);
81 FileUtils.sync(allDirtyFile);
85 public static void clearAllDirty() throws IOException {
86 if (LOGGER.isDebugEnabled())
87 LOGGER.debug("Clearing dirty state of all indexes");
89 Path indexBase = getIndexBaseLocation();
90 if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
93 forEachIndexPath(indexPath -> {
94 Path p = getChangedFile(indexPath);
97 } catch (IOException e) {
98 LOGGER.error("Could not delete {}", p.toAbsolutePath(), e);
102 FileUtils.delete(getAllDirtyFile());
106 * Internal to indexing, invoked by {@link IndexedRelationsImpl} which
107 * doesn't want to throw these exceptions forward. Just log it.
111 static void markIndexChanged(Session session, Path indexPath) {
112 if (LOGGER.isDebugEnabled())
113 LOGGER.debug("Marking index dirty: " + indexPath);
114 Path changedFile = getChangedFile(indexPath);
117 // Mark change only once per DB session.
118 if (getIndexChangedWriter(session).markDirty(changedFile)) {
119 Files.createDirectories(indexPath);
120 Files.createFile(changedFile);
121 FileUtils.sync(changedFile);
123 } catch (IOException e) {
124 LOGGER.error("Could not mark index changed for indexPath={} and changedFile={}", indexPath.toAbsolutePath(), changedFile.toAbsolutePath());
128 private static IndexChangedWriter getIndexChangedWriter(Session session) {
129 IndexChangedWriter writer = session.peekService(IndexChangedWriter.class);
130 if (writer == null) {
131 synchronized (IndexChangedWriter.class) {
133 session.registerService(IndexChangedWriter.class, writer = new IndexChangedWriter());
139 public static void deleteAllIndexes() throws IOException {
140 Path indexBase = DatabaseIndexing.getIndexBaseLocation();
142 ArrayList<Path> filter = new ArrayList<>(2);
143 filter.add(getAllDirtyFile());
144 filter.add(indexBase);
146 FileUtils.deleteWithFilter(indexBase, path -> !filter.contains(path));
147 FileUtils.delete(indexBase);
150 public static void deleteIndex(final Resource relation, final Resource modelPart) throws DatabaseException {
152 SimanticsInternal.getSession().syncRequest(new WriteRequest() {
155 public void perform(WriteGraph graph) throws DatabaseException {
156 deleteIndex(graph, relation, modelPart);
163 public static void deleteIndex(WriteGraph graph, final Resource relation, final Resource modelPart) throws DatabaseException {
165 Resource model = graph.syncRequest(new IndexRoot(modelPart));
166 GenericRelationIndex index = graph.adapt(relation, GenericRelationIndex.class);
167 IndexedRelations ir = graph.getService(IndexedRelations.class);
168 // Deletes index files
169 ir.reset(null, graph, relation, model);
170 // Notifies DB listeners
171 index.reset(graph, model);
175 public static void deleteIndex(Path indexPath) throws IOException {
176 if (LOGGER.isDebugEnabled())
177 LOGGER.debug("Deleting index " + indexPath.toAbsolutePath());
179 ArrayList<Path> filter = new ArrayList<>(2);
180 filter.add(getChangedFile(indexPath));
181 filter.add(indexPath);
183 FileUtils.deleteWithFilter(indexPath, path -> !filter.contains(path));
184 FileUtils.delete(indexPath);
187 public static void validateIndexes() throws IOException {
188 Path indexBase = getIndexBaseLocation();
189 if (LOGGER.isDebugEnabled())
190 LOGGER.debug("Validating indexes at " + indexBase);
191 if (!Files.exists(indexBase))
193 if (!Files.isDirectory(indexBase)) {
194 // Make sure that index-base is a valid directory
195 if (LOGGER.isDebugEnabled())
196 LOGGER.debug(indexBase + " is not a directory! Removing it.");
197 FileUtils.delete(indexBase);
198 Files.createDirectories(indexBase);
201 Path allDirtyFile = getAllDirtyFile();
202 if (Files.isRegularFile(allDirtyFile)) {
203 if (LOGGER.isDebugEnabled())
204 LOGGER.debug("All indexes marked dirty, removing them.");
207 forEachIndexPath(indexPath -> {
208 Path changed = getChangedFile(indexPath);
209 if (Files.isRegularFile(changed)) {
210 if (LOGGER.isDebugEnabled())
211 LOGGER.debug("Index is dirty, removing: " + indexPath);
213 deleteIndex(indexPath);
214 } catch (IOException e) {
215 LOGGER.error("Could not delete index {}", indexPath.toAbsolutePath(), e);
222 private static void forEachIndexPath(Consumer<Path> callback) throws IOException {
223 try (Stream<Path> paths = Files.walk(getIndexBaseLocation(), 1).filter(Files::isDirectory)) {
224 Iterator<Path> iter = paths.iterator();
225 while (iter.hasNext()) {
226 Path p = iter.next();