]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/internal/AcornDatabase.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / internal / AcornDatabase.java
1 package org.simantics.acorn.internal;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.RandomAccessFile;
6 import java.nio.channels.FileLock;
7 import java.nio.file.DirectoryStream;
8 import java.nio.file.FileVisitOption;
9 import java.nio.file.FileVisitResult;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.nio.file.SimpleFileVisitor;
13 import java.nio.file.attribute.BasicFileAttributes;
14 import java.util.EnumSet;
15 import java.util.Properties;
16 import java.util.stream.Stream;
17
18 import org.simantics.acorn.GraphClientImpl2;
19 import org.simantics.db.Database;
20 import org.simantics.db.DatabaseUserAgent;
21 import org.simantics.db.ServiceLocator;
22 import org.simantics.db.server.DatabaseStartException;
23 import org.simantics.db.server.ProCoreException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import fi.vtt.simantics.procore.internal.StaticSessionProperties;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class AcornDatabase implements Database {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(AcornDatabase.class);
35
36     private static final String LOCK_FILE_NAME = "lock";
37
38     private final Path folder;
39     private final Path lockFile;
40
41     private GraphClientImpl2 currentClient;
42
43     private DatabaseUserAgent userAgent;
44
45     private RandomAccessFile raLockFile;
46
47     private FileLock lock;
48
49     private boolean isRunning;
50
51     public AcornDatabase(Path folder) {
52         this.folder = folder;
53         this.lockFile = folder.resolve(LOCK_FILE_NAME);
54     }
55
56     @Override
57     public DatabaseUserAgent getUserAgent() {
58         return userAgent;
59     }
60
61     @Override
62     public void setUserAgent(DatabaseUserAgent dbUserAgent) {
63         userAgent = dbUserAgent;
64     }
65
66     @Override
67     public Status getStatus() {
68         return Status.Local;
69     }
70
71     @Override
72     public File getFolder() {
73         return folder.toFile();
74     }
75
76     @Override
77     public boolean isFolderOk() {
78         return isFolderOk(folder.toFile());
79     }
80
81     @Override
82     public boolean isFolderOk(File aFolder) {
83         if (!aFolder.isDirectory())
84             return false;
85         return true;
86     }
87
88     @Override
89     public boolean isFolderEmpty() {
90         return isFolderEmpty(folder.toFile());
91     }
92
93     @Override
94     public boolean isFolderEmpty(File aFolder) {
95         Path path = aFolder.toPath();
96         if (!Files.isDirectory(path))
97             return false;
98         try (DirectoryStream<Path> folderStream = Files.newDirectoryStream(path)) {
99             return !folderStream.iterator().hasNext();
100         } catch (IOException e) {
101             LOGGER.error("Failed to open folder stream. folder=" + path, e);
102             return false;
103         }
104     }
105
106     @Override
107     public void initFolder(Properties properties) throws ProCoreException {
108         try {
109             Files.createDirectories(folder);
110         } catch (IOException e) {
111             throw new ProCoreException(e);
112         }
113     }
114
115     @Override
116     public void deleteFiles() throws ProCoreException {
117         deleteTree(folder);
118         File vgPath = StaticSessionProperties.virtualGraphStoragePath;
119         if (vgPath != null) {
120             try (Stream<Path> vgs = Files.list(vgPath.toPath())) {
121                 for (Path p : vgs.toArray(Path[]::new))
122                     deleteTree(p);
123             } catch (IOException e) {
124                 throw new ProCoreException(e);
125             }
126         }
127     }
128
129     @Override
130     public synchronized void start() throws ProCoreException {
131         try {
132             raLockFile = new RandomAccessFile(lockFile.toFile(), "rw");
133             lock = raLockFile.getChannel().tryLock();
134             if (lock == null) {
135                 safeLoggingClose(raLockFile, lockFile);
136                 throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!");
137             }
138             isRunning = true;
139         } catch (IOException e) {
140             LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
141             safeLoggingClose(raLockFile, lockFile);
142             throw new ProCoreException("Failed to start database at " + folder.toAbsolutePath(), e);
143         }
144     }
145
146     @Override
147     public boolean isRunning() throws ProCoreException {
148         return isRunning;
149     }
150
151     @Override
152     public synchronized boolean tryToStop() throws ProCoreException {
153         if (!isRunning)
154             return false;
155         try {
156             safeLoggingClose(lock, lockFile);
157             lock = null;
158             safeLoggingClose(raLockFile, lockFile);
159             raLockFile = null;
160             Files.deleteIfExists(lockFile);
161             isRunning = false;
162             safeLoggingClose(currentClient, currentClient.getDbFolder());
163             currentClient = null;
164         } catch (IOException e) {
165             LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
166         }
167         return true;
168     }
169
170     @Override
171     public void connect() throws ProCoreException {
172     }
173
174     @Override
175     public boolean isConnected() throws ProCoreException {
176         return isRunning;
177     }
178
179     @Override
180     public String execute(String command) throws ProCoreException {
181         throw new UnsupportedOperationException("execute(" + command + ")");
182     }
183
184     @Override
185     public void disconnect() throws ProCoreException {
186     }
187
188     @Override
189     public void clone(File to, int revision, boolean saveHistory) throws ProCoreException {
190         // TODO: implement
191         throw new UnsupportedOperationException();
192     }
193
194     @Override
195     public Path createFromChangeSets(int revision) throws ProCoreException {
196         // TODO: implement
197         throw new UnsupportedOperationException();
198     }
199
200     @Override
201     public void deleteGuard() throws ProCoreException {
202         // TODO: implement
203         throw new UnsupportedOperationException();
204     }
205
206     @Override
207     public Path dumpChangeSets() throws ProCoreException {
208         // TODO: implement
209         throw new UnsupportedOperationException();
210     }
211
212     @Override
213     public void purgeDatabase() throws ProCoreException {
214         if(currentClient == null) throw new IllegalStateException("No current session.");
215         currentClient.purgeDatabase();
216     }
217
218     @Override
219     public long serverGetTailChangeSetId() throws ProCoreException {
220         if(currentClient == null) throw new IllegalStateException("No current session.");
221         return currentClient.getTailChangeSetId();
222     }
223
224     @Override
225     public Session newSession(ServiceLocator locator) throws ProCoreException {
226         try {
227                 if(currentClient != null) throw new DatabaseStartException(folder.toFile(), "A session is already running. Only one session is supported.");
228                 currentClient = new GraphClientImpl2(this, folder, locator); 
229             return currentClient;
230         } catch (IOException e) {
231             throw new ProCoreException(e);
232         }
233     }
234
235     @Override
236     public Journal getJournal() throws ProCoreException {
237         // TODO: implement
238         throw new UnsupportedOperationException();
239     }
240
241     static class Visitor extends SimpleFileVisitor<Path> {
242         @Override
243         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
244             try {
245                 Files.delete(file);
246             } catch (IOException ioe) {
247                 LOGGER.error("Failed to delete file {}", file, ioe);
248                 throw ioe;
249             }
250             return FileVisitResult.CONTINUE;
251         }
252         @Override
253         public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
254             if (e == null) {
255                 try {
256                     Files.delete(dir);
257                 } catch (IOException ioe) {
258                     LOGGER.error("Failed to delete directory {}", dir, ioe);
259                     throw ioe;
260                 }
261                 return FileVisitResult.CONTINUE;
262             }
263             throw e;
264         }
265     }
266
267     private static void deleteTree(Path path) throws ProCoreException {
268         if (!Files.exists(path))
269             return;
270         try {
271             Files.walkFileTree(path, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new Visitor());
272         } catch (IOException e) {
273             throw new ProCoreException("Could not delete " + path, e);
274         }
275     }
276
277         @Override
278         public String getCompression() {
279                 return "LZ4";
280         }
281
282     private static void safeLoggingClose(AutoCloseable closeable, Path file) {
283         if (closeable == null)
284             return;
285         try (AutoCloseable c = closeable) {
286         } catch (Exception e) {
287             LOGGER.error("Failed to close " + closeable.getClass() + " of " + file.toAbsolutePath(), e);
288         }
289     }
290
291     private static void safeLoggingClose(Database.Session session, Path file) {
292         if (session == null)
293             return;
294         try {
295             session.close();
296         } catch (Exception e) {
297             LOGGER.error("Failed to close " + session.getClass() + " of " + file.toAbsolutePath(), e);
298         }
299     }
300
301 }