org.apache.lucene4.core;bundle-version="4.9.0",
org.apache.lucene4.queryparser;bundle-version="4.9.0",
org.apache.lucene4.analyzers-common;bundle-version="4.9.0",
- org.simantics.db.services;bundle-version="0.8.0"
+ org.simantics.db.services;bundle-version="0.8.0",
+ org.slf4j.api;bundle-version="1.7.25"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.simantics.db.indexing
Bundle-Activator: org.simantics.db.indexing.Activator
package org.simantics.db.indexing;
import java.io.File;
+import java.nio.file.Path;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
// The shared instance
private static Activator plugin;
- private File indexBaseFile;
+ private Path indexBaseFile;
/**
* The constructor
super.start(context);
plugin = this;
IPath state = Platform.getStateLocation(context.getBundle());
- indexBaseFile = state.append("index").toFile();
+ indexBaseFile = state.append("index").toFile().toPath();
}
- public File getIndexBaseFile() {
+ public Path getIndexBaseFile() {
return indexBaseFile;
}
*******************************************************************************/
package org.simantics.db.indexing;
-import java.io.File;
-import java.io.FileFilter;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
import org.simantics.db.Resource;
import org.simantics.db.Session;
import org.simantics.db.WriteGraph;
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.internal.SimanticsInternal;
import org.simantics.db.service.ServerInformation;
import org.simantics.utils.FileUtils;
+import org.slf4j.LoggerFactory;
/**
* A facade for Simantics graph database index management facilities.
public final class DatabaseIndexing {
private static final boolean DEBUG = IndexPolicy.TRACE_INDEX_MANAGEMENT;
+ private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatabaseIndexing.class);
- public static File getIndexBaseLocation() {
- return Activator.getDefault().getIndexBaseFile();
-// Activator activator = Activator.getDefault();
-// Bundle b = Platform.getBundle(Activator.BUNDLE_ID);
-// IPath state = Platform.getStateLocation(b);
-// File path = state.append("index").toFile();
-// return path;
+ public static Path getIndexBaseLocation() {
+ return Activator.getDefault().getIndexBaseFile();
}
- public static File getIndexLocation(Session session, Resource relation, Resource input) {
+ public static Path getIndexLocation(Session session, Resource relation, Resource input) {
if (session == null)
throw new NullPointerException("null session");
if (relation == null)
+ "." + relation.getResourceId()
+ "." + input.getResourceId();
- return new File(getIndexBaseLocation(), dir);
+ return getIndexBaseLocation().resolve(dir);
}
- private static File getAllDirtyFile() {
- return new File(getIndexBaseLocation(), ".dirty");
+ private static Path getAllDirtyFile() {
+ return getIndexBaseLocation().resolve(".dirty");
}
- private static File getChangedFile(File indexPath) {
- return new File(indexPath, ".changed");
+ private static Path getChangedFile(Path indexPath) {
+ return indexPath.resolve(".changed");
}
public static void markAllDirty() throws IOException {
- File indexBase = getIndexBaseLocation();
- if (!indexBase.exists() || !indexBase.isDirectory())
+ Path indexBase = getIndexBaseLocation();
+ if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
return;
- if (DEBUG)
- System.out.println("Marking all indexes dirty");
- File allDirtyFile = getAllDirtyFile();
- if (allDirtyFile.createNewFile()) {
- FileUtils.syncFile(allDirtyFile);
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug("Marking all indexes dirty");
+ Path allDirtyFile = getAllDirtyFile();
+ if (!Files.exists(allDirtyFile)) {
+ Files.createFile(allDirtyFile);
+ FileUtils.sync(allDirtyFile);
}
}
public static void clearAllDirty() throws IOException {
- if (DEBUG)
- System.out.println("Clearing dirty state of all indexes");
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug("Clearing dirty state of all indexes");
- File indexBase = getIndexBaseLocation();
- if (!indexBase.exists() || !indexBase.isDirectory())
+ Path indexBase = getIndexBaseLocation();
+ if (!Files.exists(indexBase) || !Files.isDirectory(indexBase))
return;
- forEachIndexPath(new Procedure<File, IOException>() {
- @Override
- public void execute(File indexPath) throws IOException {
- getChangedFile(indexPath).delete();
+ forEachIndexPath(indexPath -> {
+ Path p = getChangedFile(indexPath);
+ try {
+ FileUtils.delete(p);
+ } catch (IOException e) {
+ LOGGER.error("Could not delete {}", p.toAbsolutePath(), e);
}
});
- getAllDirtyFile().delete();
+ FileUtils.delete(getAllDirtyFile());
}
/**
*
* @param indexPath
*/
- static void markIndexChanged(Session session, File indexPath) {
- if (DEBUG)
- System.out.println("Marking index dirty: " + indexPath);
+ static void markIndexChanged(Session session, Path indexPath) {
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug("Marking index dirty: " + indexPath);
+ Path changedFile = getChangedFile(indexPath);
try {
- 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);
- }
- }
+ Files.createDirectories(indexPath);
+ Files.createFile(changedFile);
+ FileUtils.sync(changedFile);
}
} catch (IOException e) {
- Logger.defaultLogError(e);
+ LOGGER.error("Could not mark index changed for indexPath={} and changedFile={}", indexPath.toAbsolutePath(), changedFile.toAbsolutePath());
}
}
}
public static void deleteAllIndexes() throws IOException {
- File indexBase = DatabaseIndexing.getIndexBaseLocation();
+ Path indexBase = DatabaseIndexing.getIndexBaseLocation();
- ArrayList<String> filter = new ArrayList<>(2);
- filter.add(getAllDirtyFile().getAbsolutePath());
- filter.add(indexBase.getAbsolutePath());
+ ArrayList<Path> filter = new ArrayList<>(2);
+ filter.add(getAllDirtyFile());
+ filter.add(indexBase);
- FileUtils.deleteAllWithFilter(indexBase, filter);
- FileUtils.deleteAll(indexBase);
+ FileUtils.deleteWithFilter(indexBase, path -> !filter.contains(path));
+ FileUtils.delete(indexBase);
}
public static void deleteIndex(final Resource relation, final Resource modelPart) throws DatabaseException {
}
- public static void deleteIndex(File indexPath) throws IOException {
- if (DEBUG)
- System.out.println("Deleting index " + indexPath);
+ public static void deleteIndex(Path indexPath) throws IOException {
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug("Deleting index " + indexPath.toAbsolutePath());
- ArrayList<String> filter = new ArrayList<>(2);
- filter.add(getChangedFile(indexPath).getAbsolutePath());
- filter.add(indexPath.getAbsolutePath());
+ ArrayList<Path> filter = new ArrayList<>(2);
+ filter.add(getChangedFile(indexPath));
+ filter.add(indexPath);
- FileUtils.deleteAllWithFilter(indexPath, filter);
- FileUtils.deleteAll(indexPath);
+ FileUtils.deleteWithFilter(indexPath, path -> !filter.contains(path));
+ FileUtils.delete(indexPath);
}
public static void validateIndexes() throws IOException {
- File indexBase = getIndexBaseLocation();
- if (DEBUG)
- System.out.println("Validating indexes at " + indexBase);
- if (!indexBase.exists())
+ Path indexBase = getIndexBaseLocation();
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug("Validating indexes at " + indexBase);
+ if (!Files.exists(indexBase))
return;
- if (!indexBase.isDirectory()) {
+ if (!Files.isDirectory(indexBase)) {
// Make sure that index-base is a valid directory
- if (DEBUG)
- System.out.println(indexBase + " is not a directory! Removing it.");
- FileUtils.deleteAll(indexBase);
- indexBase.mkdirs();
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug(indexBase + " is not a directory! Removing it.");
+ FileUtils.delete(indexBase);
+ Files.createDirectories(indexBase);
return;
}
- File allDirtyFile = getAllDirtyFile();
- if (allDirtyFile.isFile()) {
- if (DEBUG)
- System.out.println("All indexes marked dirty, removing them.");
+ Path allDirtyFile = getAllDirtyFile();
+ if (Files.isRegularFile(allDirtyFile)) {
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug("All indexes marked dirty, removing them.");
deleteAllIndexes();
} else {
- forEachIndexPath(new Procedure<File, IOException>() {
- @Override
- public void execute(File indexPath) throws IOException {
- File changed = getChangedFile(indexPath);
- if (changed.isFile()) {
- if (DEBUG)
- System.out.println("Index is dirty, removing: " + indexPath);
+ forEachIndexPath(indexPath -> {
+ Path changed = getChangedFile(indexPath);
+ if (Files.isRegularFile(changed)) {
+ if (LOGGER.isDebugEnabled())
+ LOGGER.debug("Index is dirty, removing: " + indexPath);
+ try {
deleteIndex(indexPath);
+ } catch (IOException e) {
+ LOGGER.error("Could not delete index {}", indexPath.toAbsolutePath(), e);
}
}
});
}
}
- interface Procedure<T, E extends Throwable> {
- void execute(T t) throws E;
- }
-
- private static <E extends Throwable> void forEachIndexPath(Procedure<File, E> callback) throws E {
- for (File indexPath : getIndexBaseLocation().listFiles(new FileFilter() {
- @Override
- public boolean accept(File pathname) {
- return pathname.isDirectory();
+ private static void forEachIndexPath(Consumer<Path> callback) throws IOException {
+ try (Stream<Path> paths = Files.walk(getIndexBaseLocation(), 1).filter(Files::isDirectory)) {
+ Iterator<Path> iter = paths.iterator();
+ while (iter.hasNext()) {
+ Path p = iter.next();
+ callback.accept(p);
}
- })) {
- callback.execute(indexPath);
}
}
import org.simantics.db.RequestProcessor;
import org.simantics.db.Resource;
import org.simantics.db.Session;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
return new IllegalStateException("Immutable index cannot be cleared");
}
+
+ @Override
+ protected Logger getLogger() {
+ return LoggerFactory.getLogger(ImmutableIndexedRelationsSearcher.class);
+ }
}
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.simantics.db.service.QueryControl;
import org.simantics.db.service.SerialisationSupport;
import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
*/
public class IndexedRelationsImpl implements IndexedRelations {
+ private static final Logger LOGGER = LoggerFactory.getLogger(IndexedRelationsImpl.class);
+
Map<Object, RWLock> indexLocks = new WeakHashMap<Object, RWLock>();
static class LockHandle {
@Override
public void insert(IProgressMonitor monitor, RequestProcessor processor, GenericRelation relation,
- Resource relationResource, Resource input, Collection<Object[]> documents) {
+ Resource relationResource, Resource input, Collection<Object[]> documents) throws IndexException {
// System.out.println("Inserting to index: " + input + " " + documents);
@Override
public void remove(IProgressMonitor monitor, RequestProcessor processor, GenericRelation relation,
- Resource relationResource, Resource input, String key, Collection<Object> keyValues) {
+ Resource relationResource, Resource input, String key, Collection<Object> keyValues) throws IndexException {
if (relation == null)
throw new IllegalArgumentException("null relation");
@Override
public boolean replace(IProgressMonitor monitor, RequestProcessor processor, GenericRelation relation,
- Resource relationResource, Resource input, String key, Collection<Object> keyValues, Collection<Object[]> documents) {
+ Resource relationResource, Resource input, String key, Collection<Object> keyValues, Collection<Object[]> documents) throws IndexException {
if (relation == null)
throw new IllegalArgumentException("null relation");
IndexedRelationsSearcherBase searcher = makeSearcher(processor, relationResource, input);
LockHandle handle = lock(processor, Pair.make(relationResource, input), true);
-
+ Path path = DatabaseIndexing.getIndexLocation(processor.getSession(), relationResource, input);
try {
searcher.changeState(monitor, processor.getSession(), State.NONE);
if (!searcher.checkState(State.NONE))
throw new IndexException("Could not close index for input " + input + " before removing it");
- File path = DatabaseIndexing.getIndexLocation(processor.getSession(), relationResource, input);
DatabaseIndexing.deleteIndex(path);
-
} catch (IOException e) {
+ LOGGER.error("Could not delete {}", path.toAbsolutePath(), e);
throw new IndexException(e);
} finally {
handle.unlock();
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.adapter.GenericRelation;
import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
*/
public class IndexedRelationsMemorySearcher extends IndexedRelationsSearcherBase {
+ private static final Logger LOGGER = LoggerFactory.getLogger(IndexedRelationsMemorySearcher.class);
+
final IndexedRelationsSearcher backend;
final GenericRelation r;
@Override
Directory getDirectory(Session session) throws IOException {
MemoryIndexing mem = MemoryIndexing.getInstance(session);
- String path = indexPath.getAbsolutePath();
+ String path = indexPath.toAbsolutePath().toString();
return mem.getDirectory(path, Queries.getAnalyzer());
}
@Override
Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
-
- setProblem(null);
-
MemoryIndexing mem = MemoryIndexing.getInstance(session);
changed.clear();
- String path = indexPath.getAbsolutePath();
+ String path = indexPath.toAbsolutePath().toString();
mem.remove(path);
return null;
protected boolean requireChangeInfoOnReplace() {
return false;
}
+
+ @Override
+ protected Logger getLogger() {
+ return LOGGER;
+ }
}
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.adapter.GenericRelation;
import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
* @author Antti Villberg
*/
public class IndexedRelationsSearcher extends IndexedRelationsSearcherBase {
-
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(IndexedRelationsSearcher.class);
+
IndexedRelationsMemorySearcher cache;
IndexedRelationsSearcher(RequestProcessor session, Resource relation, Resource input, GenericRelation r) throws DatabaseException {
MemoryIndexing mem = MemoryIndexing.getInstance(session.getSession());
- String key = indexPath.getAbsolutePath();
+ String key = indexPath.toAbsolutePath().toString();
Map<String,List<Map<String, Object>>> cache = mem.persistentCache.get(key);
if(cache != null) {
MemoryIndexing mem = MemoryIndexing.getInstance(session.getSession());
- String key = indexPath.getAbsolutePath();
+ String key = indexPath.toAbsolutePath().toString();
Map<String,List<Resource>> cache = mem.persistentCacheResources.get(key);
if(cache != null) {
t = cache.bestEffortClear(monitor, session);
if(t != null) return t;
- String key = indexPath.getAbsolutePath();
+ String key = indexPath.toAbsolutePath().toString();
MemoryIndexing mem = MemoryIndexing.getInstance(session);
mem.persistentCache.remove(key);
mem.persistentCacheResources.remove(key);
return null;
}
+
+ @Override
+ protected Logger getLogger() {
+ return LOGGER;
+ }
}
*******************************************************************************/
package org.simantics.db.indexing;
-import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.simantics.db.Resource;
import org.simantics.db.Session;
import org.simantics.db.common.request.SafeName;
-import org.simantics.db.common.utils.Logger;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.indexing.internal.IndexingJob;
import org.simantics.db.service.SerialisationSupport;
import org.simantics.utils.FileUtils;
import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
import gnu.trove.map.hash.THashMap;
}
public void setProblem(Throwable t) {
+ if (t != null)
+ getLogger().error("Setting problem for {} and previous state {}", this, this.state, t);
this.state = State.PROBLEM;
this.exception = t;
}
protected void changeState(IProgressMonitor monitor, Session session, State state, int depth) {
- if(this.state == state) return;
+ if (this.state == state) {
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("Trying to change state {} to the same as previous state {} in depth {} with {}", state, this.state, depth, this);
+ return;
+ }
if (IndexPolicy.TRACE_INDEX_MANAGEMENT)
System.err.println("Index state " + this.state.name() + " => " + state.name() + " " + this);
// Try to exit problem state
if (State.PROBLEM == this.state && depth > 0) {
+ getLogger().info("Try to exit problem state for {} and state {}", this, state);
Throwable t = bestEffortClear(monitor, session);
if(t != null) {
+ getLogger().error("Best effort clear has failed for state {} and this {}", state, this, t);
exception = t;
return;
}
// Managed to get into initial state
this.state = State.NONE;
+ getLogger().info("Managed to get into initial state {}", this.state);
return;
}
// Cannot move into read from no index
- if (State.NONE == this.state && State.READ == state) return;
+ if (State.NONE == this.state && State.READ == state) {
+ getLogger().info("Cannot move into read from no index in {} with state {}", this, state);
+ return;
+ }
// Cannot move into write from no index
- if (State.NONE == this.state && State.WRITE == state) return;
+ if (State.NONE == this.state && State.WRITE == state) {
+ getLogger().info("Cannot move into write from no index in {} with state {}", this, state);
+ return;
+ }
boolean success = false;
}
} catch (Throwable t) {
-
setProblem(t);
-
} finally {
if(!success) {
Resource input;
- File indexPath;
+ Path indexPath;
Directory directory;
}
Directory getDirectory(Session session) throws IOException {
- return FSDirectory.open(indexPath);
+ return FSDirectory.open(indexPath.toFile());
}
abstract String getDescriptor();
try {
initializeIndexImpl(mon, graph, bound, overwrite);
} catch (IOException e) {
+ getLogger().error("Index is in problematic state! {}", this, e);
throw new DatabaseException(e);
}
});
mon.beginTask("Initializing Index", 100);
if (overwrite) {
- mon.subTask("Erasing previous index");
- FileUtils.deleteAll(indexPath);
+ if (Files.exists(indexPath)) {
+ mon.subTask("Erasing previous index");
+ getLogger().info("Erasing previous index {}", indexPath.toAbsolutePath());
+ FileUtils.delete(indexPath);
+ }
}
final AtomicReference<FSDirectory> directory = new AtomicReference<FSDirectory>();
mon.subTask("Start index write");
createDirectory(indexPath);
- directory.set(FSDirectory.open(indexPath));
+ directory.set(FSDirectory.open(indexPath.toFile()));
IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_4_9, Queries.getAnalyzer()).setOpenMode(OpenMode.CREATE);
writer.set(new IndexWriter(directory.get(), conf));
try {
writer.get().addDocument(document);
} catch (CorruptIndexException e) {
+ getLogger().error("Index is corrupted! {}", this, e);
throw new IllegalStateException(e);
} catch (IOException e) {
+ getLogger().error("Index is in problematic state! {}", this, e);
throw new IllegalStateException(e);
} finally {
synchronized (mon) {
try {
s.acquire(INDEXING_THREAD_COUNT);
} catch (InterruptedException e) {
- Logger.defaultLogError(e);
+ getLogger().error("Could not initialize index {}", this, e);
}
// http://www.gossamer-threads.com/lists/lucene/java-dev/47895
System.out.println(getDescriptor() + "Wrote index at " + indexPath + " in " + (1e-9 * (System.nanoTime()-start)) + " seconds.");
} catch (DatabaseException e) {
-
- Logger.defaultLogError(e);
-
+ getLogger().error("Could not initialize index due to db {}", this, e);
} finally {
try {
closeWriter(writer.getAndSet(null));
}
result.add(o);
- } catch (CorruptIndexException e) {
- throw new DatabaseException(e);
- } catch (IOException e) {
- throw new DatabaseException(e);
- }
+ } catch (CorruptIndexException e) {
+ getLogger().error("Index is corrupted! {}", this, e);
+ throw new DatabaseException(e);
+ } catch (IOException e) {
+ getLogger().error("Index is in problematic state! {}", this, e);
+ throw new DatabaseException(e);
+ }
}
result.add(entry);
} catch (CorruptIndexException e) {
+ getLogger().error("Index is corrupted! {}", this, e);
throw new DatabaseException(e);
} catch (IOException e) {
+ getLogger().error("Index is in problematic state! {}", this, e);
throw new DatabaseException(e);
}
-
}
-
return result;
-
}
});
}
ResourceVisitor visitor = new ResourceVisitor();
for (ScoreDoc scoreDoc : docs.scoreDocs) {
-
try {
-
reader.document(scoreDoc.doc, visitor);
result.add(support.getResource(visitor.id));
-
} catch (CorruptIndexException e) {
+ getLogger().error("Index is corrupted! {}", this, e);
throw new DatabaseException(e);
} catch (IOException e) {
+ getLogger().error("Index is in problematic state! {}", this, e);
throw new DatabaseException(e);
}
-
}
-
return result;
-
}
});
}
reader.document(scoreDoc.doc, visitor);
} catch (CorruptIndexException e) {
+ getLogger().error("Index is corrupted! {}", this, e);
throw new DatabaseException(e);
} catch (IOException e) {
+ getLogger().error("Index is in problematic state! {}", this, e);
throw new DatabaseException(e);
}
}
- protected static File getIndexDirectory(Session session, Resource relation, Resource input) {
- File path = DatabaseIndexing.getIndexLocation(session, relation, input);
+ protected static Path getIndexDirectory(Session session, Resource relation, Resource input) {
+ Path path = DatabaseIndexing.getIndexLocation(session, relation, input);
// System.out.println("getIndexDirectory = " + path);
return path;
}
- private static void createDirectory(File path) throws IOException {
- Path p = path.toPath();
- if (Files.exists(p) && !Files.isDirectory(p))
+ private static void createDirectory(Path path) throws IOException {
+ if (Files.exists(path) && !Files.isDirectory(path))
throw new IOException("Could not create index directory " + path + ", a file by that name already exists");
- Files.createDirectories(p);
+ Files.createDirectories(path);
}
- File getIndexPath() {
+ Path getIndexPath() {
return indexPath;
}
boolean isIndexAvailable() {
- return (indexPath.exists() && indexPath.isDirectory());
+ return (Files.exists(indexPath) && Files.isDirectory(indexPath));
}
- Throwable bestEffortClear(IProgressMonitor monitor, Session session) {
- return null;
- }
+ abstract Throwable bestEffortClear(IProgressMonitor monitor, Session session);
/*
* Start from scratch. Clear all caches and rebuild the index.
*/
Throwable clearDirectory(IProgressMonitor monitor, Session session) {
- File file = getIndexPath();
+ Path file = getIndexPath();
try {
-
- for(int i=0;i<15;i++) {
- FileUtils.deleteDir(file);
- if(!file.exists()) {
- return null;
- }
- try {
- Thread.sleep(i*100);
- } catch (InterruptedException e) {
- }
- }
-
+ FileUtils.delete(file);
} catch (Throwable t) {
-
+ getLogger().error("Could not delete directory {}", file.toAbsolutePath(), t);
return t;
-
}
-
- return new IllegalStateException("Failed to delete directory " + file.getAbsolutePath());
-
+ if (Files.exists(file))
+ return new IllegalStateException("Failed to delete directory " + file.toAbsolutePath());
+ return null;
}
private Field[] setFields(Field[] fs, Object[] result) {
System.out.println(getDescriptor() + "index " + fs[i].name() + " = " + value + " : Long");
fs[i].setLongValue((Long) value);
} else {
- Logger.defaultLogError("Can only index Long and String fields, encountered " + value);
+ getLogger().error("Can only index Long and String fields, encountered " + value);
return null;
}
}
return fs;
}
+ protected abstract Logger getLogger();
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + " [" + String.valueOf(schema) + ", " + String.valueOf(relation) + ", " + String.valueOf(input) + ", " + String.valueOf(indexPath) + ", " + String.valueOf(directory) + ", " + String.valueOf(state) + "]";
+ }
}
package org.simantics.db.indexing;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.simantics.db.Resource;
import org.simantics.db.Session;
import org.simantics.db.common.request.Adapt;
-import org.simantics.db.common.utils.Logger;
import org.simantics.db.indexing.IndexedRelationsSearcherBase.State;
import org.simantics.db.layer0.adapter.GenericRelation;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
*/
public class MemoryIndexing {
+ private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MemoryIndexing.class);
+
final private Session session;
final Map<String,Map<String,List<Map<String, Object>>>> persistentCache = new HashMap<String,Map<String,List<Map<String, Object>>>>();
this.session = session;
}
- protected File getIndexDirectory(Resource relation, Resource input) {
+ protected Path getIndexDirectory(Resource relation, Resource input) {
return DatabaseIndexing.getIndexLocation(session, relation, input);
}
public IndexedRelationsSearcher get(RequestProcessor processor, Resource relation, Resource input) {
+ Path location = getIndexDirectory(relation, input);
try {
- File location = getIndexDirectory(relation, input);
- String key = location.getAbsolutePath();
+ String key = location.toAbsolutePath().toString();
IndexedRelationsSearcher searcher = searchers.get(key);
if (searcher == null) {
GenericRelation r = processor.sync(new Adapt<GenericRelation>(relation, GenericRelation.class));
}
return searcher;
} catch (Exception e) {
- Logger.defaultLogError(e);
+ LOGGER.error("Could not get searcher for relation {} and input {} in location {}", relation, input, location, e);
return null;
}
}
public IndexedRelationsSearcherBase getImmutable(RequestProcessor processor, Resource relation, Resource input) {
+ Path location = getIndexDirectory(relation, input);
try {
- File location = getIndexDirectory(relation, input);
- String key = location.getAbsolutePath();
+ String key = location.toAbsolutePath().toString();
IndexedRelationsSearcherBase searcher = immutableSearchers.get(key);
if (searcher == null) {
searcher = new ImmutableIndexedRelationsSearcher(processor, relation, input);
}
return searcher;
} catch (Exception e) {
- Logger.defaultLogError(e);
+ LOGGER.error("Could not get searcher base for relation {} and input {} in location {}", relation, input, location, e);
return null;
}
}
*******************************************************************************/
package org.simantics.db.indexing.internal;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
* @since 1.28.0
*/
public class IndexChangedWriter {
- private Set<String> dirtyFiles = new HashSet<>();
+ private Set<Path> dirtyFiles = new HashSet<>();
- public synchronized boolean markDirty(File dirtyFile) throws IOException {
- return dirtyFiles.add(dirtyFile.getAbsolutePath());
+ public synchronized boolean markDirty(Path dirtyFile) throws IOException {
+ return dirtyFiles.add(dirtyFile);
}
}
import org.simantics.db.common.request.SuperTypeString;
import org.simantics.db.common.request.TypeString;
import org.simantics.db.common.request.UnaryRead;
-import org.simantics.db.common.utils.Logger;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.event.ChangeListener;
import org.simantics.db.exception.DatabaseException;
import org.simantics.operation.Layer0X;
import org.simantics.utils.datastructures.Pair;
import org.simantics.utils.logging.TimeLogger;
+import org.slf4j.LoggerFactory;
public class DependenciesRelation extends UnsupportedRelation implements GenericRelationIndex {
+ private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DependenciesRelation.class);
private static final boolean DEBUG = false;
static final boolean DEBUG_LISTENERS = false;
private static final boolean PROFILE = false;
@Override
public void exception(AsyncReadGraph graph, Throwable throwable) {
- Logger.defaultLogError(throwable);
+ LOGGER.error("Could not compile possible related value for resource {}", resource, throwable);
}
});
@Override
public void exception(AsyncReadGraph graph, Throwable throwable) {
- Logger.defaultLogError(throwable);
+ LOGGER.error("Could not find type for resource {}", resource, throwable);
}
};
@Override
public void exception(AsyncReadGraph graph, Throwable throwable) {
- Logger.defaultLogError(throwable);
+ LOGGER.error("Could not compile for resource {}", resource, throwable);
}
});
if(typeString == null) {
typeString = graph.syncRequest(new SuperTypeString(e.principalType));
if (typeString.isEmpty()) {
- Logger.defaultLogError(new DatabaseException("No name for type " + NameUtils.getURIOrSafeNameInternal(graph, e.resource) + " (" + e.resource + ")"));
+ LOGGER.error("No name for type", new DatabaseException("No name for type " + NameUtils.getURIOrSafeNameInternal(graph, e.resource) + " (" + e.resource + ")"));
}
typeStrings.put(e.principalType, typeString);
}
} catch (Throwable t) {
// Just to know if something unexpected happens here.
- Logger.defaultLogError("Dependencies index update failed for model "
- + model + " and relation " + resource + ".", t);
- t.printStackTrace();
+ LOGGER.error("Dependencies index update failed for model "
+ + model + " and relation " + resource + ".", t);
// NOTE: Last resort: failure to update index
// properly results in removal of the whole index.
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Random;
+import java.util.function.Predicate;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
return read;
}
- public static void delete(Path databaseLocation) throws IOException {
- Files.walkFileTree(databaseLocation, new DeleteDirectoriesVisitor());
+ public static void deleteWithFilter(Path path, Predicate<Path> filter) throws IOException {
+ if (Files.exists(path))
+ Files.walkFileTree(path, new DeleteDirectoriesVisitor(filter));
}
-
+
+ public static void delete(Path path) throws IOException {
+ deleteWithFilter(path, null);
+ }
+
public static void copy(Path from, Path to) throws IOException {
Files.walkFileTree(from, new CopyDirectoriesVisitor(from, to));
}
public static class DeleteDirectoriesVisitor extends SimpleFileVisitor<Path> {
-
+
+ private Predicate<Path> filter;
+
+ public DeleteDirectoriesVisitor(Predicate<Path> filter) {
+ this.filter = filter;
+ }
+
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- Files.delete(file);
- if (Files.exists(file))
+ if (filter != null && !filter.test(file)) {
+ return FileVisitResult.CONTINUE;
+ }
+ if (Files.exists(file)) {
+ Files.delete(file);
+ }
+ if (Files.exists(file)) {
throw new IOException("Could not delete file " + file.toAbsolutePath().toString());
+ }
return FileVisitResult.CONTINUE;
}
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc != null)
throw exc;
- Files.delete(dir);
+ if (filter != null && !filter.test(dir)) {
+ return FileVisitResult.CONTINUE;
+ }
+ if (Files.exists(dir)) {
+ Files.delete(dir);
+ }
if (Files.exists(dir))
throw new IOException("Could not delete file " + dir.toAbsolutePath().toString());
return FileVisitResult.CONTINUE;
raf.getFD().sync();
}
}
+
+ public static void sync(Path path) throws IOException {
+ try (InputStream stream = Files.newInputStream(path, StandardOpenOption.SYNC)) {
+ }
+ }
}