X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.acorn%2Fsrc%2Forg%2Fsimantics%2Facorn%2Finternal%2FAcornDatabase.java;h=a423fbdbe23b815871688fb17cb2c6fb165550f7;hb=33b349001037197a526a49e5820f0317dc74d934;hp=63c73dd634580e2c3f1add6c4d34b6d866ff926f;hpb=7332cc3e400d07a96de7aeb181383f72f6973b90;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/internal/AcornDatabase.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/internal/AcornDatabase.java index 63c73dd63..a423fbdbe 100644 --- a/bundles/org.simantics.acorn/src/org/simantics/acorn/internal/AcornDatabase.java +++ b/bundles/org.simantics.acorn/src/org/simantics/acorn/internal/AcornDatabase.java @@ -2,6 +2,8 @@ package org.simantics.acorn.internal; import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileLock; import java.nio.file.DirectoryStream; import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; @@ -16,20 +18,36 @@ import org.simantics.acorn.GraphClientImpl2; import org.simantics.db.Database; import org.simantics.db.DatabaseUserAgent; import org.simantics.db.ServiceLocator; -import org.simantics.db.common.utils.Logger; +import org.simantics.db.server.DatabaseStartException; import org.simantics.db.server.ProCoreException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Tuukka Lehtonen */ public class AcornDatabase implements Database { + private static final Logger LOGGER = LoggerFactory.getLogger(AcornDatabase.class); + + private static final String LOCK_FILE_NAME = "lock"; + private final Path folder; + private final Path lockFile; + + private GraphClientImpl2 currentClient; private DatabaseUserAgent userAgent; + private RandomAccessFile raLockFile; + + private FileLock lock; + + private boolean isRunning; + public AcornDatabase(Path folder) { this.folder = folder; + this.lockFile = folder.resolve(LOCK_FILE_NAME); } @Override @@ -77,7 +95,7 @@ public class AcornDatabase implements Database { try (DirectoryStream folderStream = Files.newDirectoryStream(path)) { return !folderStream.iterator().hasNext(); } catch (IOException e) { - Logger.defaultLogError("Failed to open folder stream. folder=" + path, e); + LOGGER.error("Failed to open folder stream. folder=" + path, e); return false; } } @@ -93,23 +111,46 @@ public class AcornDatabase implements Database { @Override public void deleteFiles() throws ProCoreException { - // TODO: somehow check that the acorn client is not active. deleteTree(folder); } @Override - public void start() throws ProCoreException { + public synchronized void start() throws ProCoreException { + try { + raLockFile = new RandomAccessFile(lockFile.toFile(), "rw"); + lock = raLockFile.getChannel().tryLock(); + if (lock == null) { + safeLoggingClose(raLockFile, lockFile); + throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!"); + } + isRunning = true; + } catch (IOException e) { + LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e); + safeLoggingClose(raLockFile, lockFile); + throw new ProCoreException("Failed to start database at " + folder.toAbsolutePath(), e); + } } @Override public boolean isRunning() throws ProCoreException { - return true; + return isRunning; } @Override - public boolean tryToStop() throws ProCoreException { + public synchronized boolean tryToStop() throws ProCoreException { + if (!isRunning) + return false; + try { + safeLoggingClose(lock, lockFile); + lock = null; + safeLoggingClose(raLockFile, lockFile); + raLockFile = null; + Files.deleteIfExists(lockFile); + isRunning = false; + } catch (IOException e) { + LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e); + } return true; -// throw new UnsupportedOperationException(); } @Override @@ -118,7 +159,7 @@ public class AcornDatabase implements Database { @Override public boolean isConnected() throws ProCoreException { - return true; + return isRunning; } @Override @@ -156,22 +197,22 @@ public class AcornDatabase implements Database { @Override public void purgeDatabase() throws ProCoreException { - // TODO: implement - throw new UnsupportedOperationException(); + if(currentClient == null) throw new IllegalStateException("No current session."); + currentClient.purgeDatabase(); } @Override public long serverGetTailChangeSetId() throws ProCoreException { - // "We have it all" - // But after purging we don't so beware. - // TODO: beware for purge - return 1; + if(currentClient == null) throw new IllegalStateException("No current session."); + return currentClient.getTailChangeSetId(); } @Override public Session newSession(ServiceLocator locator) throws ProCoreException { try { - return new GraphClientImpl2(this, folder, locator); + if(currentClient != null) throw new DatabaseStartException(folder.toFile(), "A session is already running. Only one session is supported."); + currentClient = new GraphClientImpl2(this, folder, locator); + return currentClient; } catch (IOException e) { throw new ProCoreException(e); } @@ -226,4 +267,13 @@ public class AcornDatabase implements Database { return "LZ4"; } + private static void safeLoggingClose(AutoCloseable closeable, Path file) { + if (closeable == null) + return; + try (AutoCloseable c = closeable) { + } catch (Exception e) { + LOGGER.error("Failed to close " + closeable.getClass() + " of " + file.toAbsolutePath()); + } + } + }