1 package org.simantics.acorn.internal;
4 import java.io.IOException;
5 import java.nio.file.DirectoryStream;
6 import java.nio.file.FileVisitOption;
7 import java.nio.file.FileVisitResult;
8 import java.nio.file.Files;
9 import java.nio.file.Path;
10 import java.nio.file.SimpleFileVisitor;
11 import java.nio.file.attribute.BasicFileAttributes;
12 import java.util.EnumSet;
13 import java.util.Properties;
15 import org.simantics.acorn.GraphClientImpl2;
16 import org.simantics.db.Database;
17 import org.simantics.db.DatabaseUserAgent;
18 import org.simantics.db.ServiceLocator;
19 import org.simantics.db.common.utils.Logger;
20 import org.simantics.db.server.ProCoreException;
23 * @author Tuukka Lehtonen
25 public class AcornDatabase implements Database {
27 private final Path folder;
29 private DatabaseUserAgent userAgent;
31 public AcornDatabase(Path folder) {
36 public DatabaseUserAgent getUserAgent() {
41 public void setUserAgent(DatabaseUserAgent dbUserAgent) {
42 userAgent = dbUserAgent;
46 public Status getStatus() {
51 public File getFolder() {
52 return folder.toFile();
56 public boolean isFolderOk() {
57 return isFolderOk(folder.toFile());
61 public boolean isFolderOk(File aFolder) {
62 if (!aFolder.isDirectory())
68 public boolean isFolderEmpty() {
69 return isFolderEmpty(folder.toFile());
73 public boolean isFolderEmpty(File aFolder) {
74 Path path = aFolder.toPath();
75 if (!Files.isDirectory(path))
77 try (DirectoryStream<Path> folderStream = Files.newDirectoryStream(path)) {
78 return !folderStream.iterator().hasNext();
79 } catch (IOException e) {
80 Logger.defaultLogError("Failed to open folder stream. folder=" + path, e);
86 public void initFolder(Properties properties) throws ProCoreException {
88 Files.createDirectories(folder);
89 } catch (IOException e) {
90 throw new ProCoreException(e);
95 public void deleteFiles() throws ProCoreException {
96 // TODO: somehow check that the acorn client is not active.
101 public void start() throws ProCoreException {
105 public boolean isRunning() throws ProCoreException {
110 public boolean tryToStop() throws ProCoreException {
112 // throw new UnsupportedOperationException();
116 public void connect() throws ProCoreException {
120 public boolean isConnected() throws ProCoreException {
125 public String execute(String command) throws ProCoreException {
126 throw new UnsupportedOperationException("execute(" + command + ")");
130 public void disconnect() throws ProCoreException {
134 public void clone(File to, int revision, boolean saveHistory) throws ProCoreException {
136 throw new UnsupportedOperationException();
140 public Path createFromChangeSets(int revision) throws ProCoreException {
142 throw new UnsupportedOperationException();
146 public void deleteGuard() throws ProCoreException {
148 throw new UnsupportedOperationException();
152 public Path dumpChangeSets() throws ProCoreException {
154 throw new UnsupportedOperationException();
158 public void purgeDatabase() throws ProCoreException {
160 throw new UnsupportedOperationException();
164 public long serverGetTailChangeSetId() throws ProCoreException {
166 // But after purging we don't so beware.
167 // TODO: beware for purge
172 public Session newSession(ServiceLocator locator) throws ProCoreException {
174 return new GraphClientImpl2(this, folder, locator);
175 } catch (IOException e) {
176 throw new ProCoreException(e);
181 public Journal getJournal() throws ProCoreException {
183 throw new UnsupportedOperationException();
186 private static void deleteTree(Path path) throws ProCoreException {
187 if (!Files.exists(path))
190 class Visitor extends SimpleFileVisitor<Path> {
192 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
195 } catch (IOException ioe) {
196 ioe.printStackTrace();
199 return FileVisitResult.CONTINUE;
202 public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
206 } catch (IOException ioe) {
207 ioe.printStackTrace();
210 return FileVisitResult.CONTINUE;
216 Visitor v = new Visitor();
217 EnumSet<FileVisitOption> opts = EnumSet.noneOf(FileVisitOption.class);
218 Files.walkFileTree(path, opts, Integer.MAX_VALUE, v);
219 } catch (IOException e) {
220 throw new ProCoreException("Could not delete " + path, e);
225 public String getCompression() {