1 package org.simantics.acorn.internal;
4 import java.io.IOException;
5 import java.nio.channels.FileChannel;
6 import java.nio.channels.FileLock;
7 import java.nio.file.DirectoryStream;
8 import java.nio.file.FileAlreadyExistsException;
9 import java.nio.file.FileVisitOption;
10 import java.nio.file.FileVisitResult;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.nio.file.SimpleFileVisitor;
14 import java.nio.file.StandardOpenOption;
15 import java.nio.file.attribute.BasicFileAttributes;
16 import java.util.EnumSet;
17 import java.util.Properties;
19 import org.simantics.acorn.GraphClientImpl2;
20 import org.simantics.db.Database;
21 import org.simantics.db.DatabaseUserAgent;
22 import org.simantics.db.ServiceLocator;
23 import org.simantics.db.server.DatabaseStartException;
24 import org.simantics.db.server.ProCoreException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * @author Tuukka Lehtonen
31 public class AcornDatabase implements Database {
33 private static final Logger LOGGER = LoggerFactory.getLogger(AcornDatabase.class);
35 private static final String LOCK_FILE_NAME = "lock";
37 private final Path folder;
38 private final Path lockFile;
40 private GraphClientImpl2 currentClient;
42 private DatabaseUserAgent userAgent;
44 private FileChannel lockFileChannel;
46 private FileLock lock;
48 private boolean isRunning;
50 public AcornDatabase(Path folder) {
52 this.lockFile = folder.resolve(LOCK_FILE_NAME);
56 public DatabaseUserAgent getUserAgent() {
61 public void setUserAgent(DatabaseUserAgent dbUserAgent) {
62 userAgent = dbUserAgent;
66 public Status getStatus() {
71 public File getFolder() {
72 return folder.toFile();
76 public boolean isFolderOk() {
77 return isFolderOk(folder.toFile());
81 public boolean isFolderOk(File aFolder) {
82 if (!aFolder.isDirectory())
88 public boolean isFolderEmpty() {
89 return isFolderEmpty(folder.toFile());
93 public boolean isFolderEmpty(File aFolder) {
94 Path path = aFolder.toPath();
95 if (!Files.isDirectory(path))
97 try (DirectoryStream<Path> folderStream = Files.newDirectoryStream(path)) {
98 return !folderStream.iterator().hasNext();
99 } catch (IOException e) {
100 LOGGER.error("Failed to open folder stream. folder=" + path, e);
106 public void initFolder(Properties properties) throws ProCoreException {
108 Files.createDirectories(folder);
109 } catch (IOException e) {
110 throw new ProCoreException(e);
115 public void deleteFiles() throws ProCoreException {
120 public synchronized void start() throws ProCoreException {
122 lockFileChannel = lockFile.getFileSystem().provider().newFileChannel(lockFile,
123 EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE));
125 lock = lockFileChannel.tryLock();
127 safeLoggingClose(lockFileChannel, lockFile);
128 throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!");
132 } catch (IOException e) {
133 LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
134 safeLoggingClose(lockFileChannel, lockFile);
139 public boolean isRunning() throws ProCoreException {
144 public synchronized boolean tryToStop() throws ProCoreException {
148 safeLoggingClose(lock, lockFile);
150 safeLoggingClose(lockFileChannel, lockFile);
151 lockFileChannel = null;
152 Files.deleteIfExists(lockFile);
154 } catch (IOException e) {
155 LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
161 public void connect() throws ProCoreException {
165 public boolean isConnected() throws ProCoreException {
170 public String execute(String command) throws ProCoreException {
171 throw new UnsupportedOperationException("execute(" + command + ")");
175 public void disconnect() throws ProCoreException {
179 public void clone(File to, int revision, boolean saveHistory) throws ProCoreException {
181 throw new UnsupportedOperationException();
185 public Path createFromChangeSets(int revision) throws ProCoreException {
187 throw new UnsupportedOperationException();
191 public void deleteGuard() throws ProCoreException {
193 throw new UnsupportedOperationException();
197 public Path dumpChangeSets() throws ProCoreException {
199 throw new UnsupportedOperationException();
203 public void purgeDatabase() throws ProCoreException {
204 if(currentClient == null) throw new IllegalStateException("No current session.");
205 currentClient.purgeDatabase();
209 public long serverGetTailChangeSetId() throws ProCoreException {
210 if(currentClient == null) throw new IllegalStateException("No current session.");
211 return currentClient.getTailChangeSetId();
215 public Session newSession(ServiceLocator locator) throws ProCoreException {
217 if(currentClient != null) throw new DatabaseStartException(folder.toFile(), "A session is already running. Only one session is supported.");
218 currentClient = new GraphClientImpl2(this, folder, locator);
219 return currentClient;
220 } catch (IOException e) {
221 throw new ProCoreException(e);
226 public Journal getJournal() throws ProCoreException {
228 throw new UnsupportedOperationException();
231 private static void deleteTree(Path path) throws ProCoreException {
232 if (!Files.exists(path))
235 class Visitor extends SimpleFileVisitor<Path> {
237 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
240 } catch (IOException ioe) {
241 ioe.printStackTrace();
244 return FileVisitResult.CONTINUE;
247 public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
251 } catch (IOException ioe) {
252 ioe.printStackTrace();
255 return FileVisitResult.CONTINUE;
261 Visitor v = new Visitor();
262 EnumSet<FileVisitOption> opts = EnumSet.noneOf(FileVisitOption.class);
263 Files.walkFileTree(path, opts, Integer.MAX_VALUE, v);
264 } catch (IOException e) {
265 throw new ProCoreException("Could not delete " + path, e);
270 public String getCompression() {
274 private static void safeLoggingClose(AutoCloseable closeable, Path file) {
275 if (closeable == null)
277 try (AutoCloseable c = closeable) {
278 } catch (Exception e) {
279 LOGGER.error("Failed to close " + closeable.getClass() + " of " + file.toAbsolutePath());