@Override
public ServerI getServer(String address, Properties properties) throws DatabaseException {
- return new ServerI() {
-
- @Override
- public void stop() throws DatabaseException {
- // nop
- }
-
- @Override
- public void start() throws DatabaseException {
- // nop
- }
-
- @Override
- public boolean isActive() throws DatabaseException {
- return true;
- }
-
- @Override
- public String getAddress() throws DatabaseException {
- return address;
- }
-
- @Override
- public String executeAndDisconnect(String command) throws DatabaseException {
- return "";
- }
-
- @Override
- public String execute(String command) throws DatabaseException {
- return "";
- }
- };
+ return new AcornServerI(address);
}
@Override
Path dbFolder = Paths.get(address);
return new AcornManagement(dbFolder, properties);
}
+
+ private static class AcornServerI implements ServerI {
+
+ private String address;
+
+ public AcornServerI(String address) {
+ this.address = address;
+ }
+
+ @Override
+ public void stop() throws DatabaseException {
+ AcornDatabaseManager.getDatabase(Paths.get(address)).tryToStop();
+ }
+
+ @Override
+ public void start() throws DatabaseException {
+ AcornDatabaseManager.getDatabase(Paths.get(address)).start();
+ }
+
+ @Override
+ public boolean isActive() throws DatabaseException {
+ return AcornDatabaseManager.getDatabase(Paths.get(address)).isRunning();
+ }
+
+ @Override
+ public String getAddress() throws DatabaseException {
+ return address;
+ }
+
+ @Override
+ public String executeAndDisconnect(String command) throws DatabaseException {
+ return "";
+ }
+
+ @Override
+ public String execute(String command) throws DatabaseException {
+ return "";
+ }
+ }
}
public void create() throws DatabaseException {
db.initFolder(properties);
if (!exist())
- throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());
+ throw new DatabaseException("Failed to create Acorn database. folder=" + db.getFolder());
}
@Override
}
public void makeSnapshot(boolean force) throws IOException {
- clusters.makeSnapshot(locator, force);
+ if (safeToMakeSnapshot)
+ clusters.makeSnapshot(locator, force);
}
public <T> T clone(ClusterUID uid, ClusterCreator creator) throws DatabaseException {
private boolean closed = false;
private boolean isClosing = false;
+ // Add check to make sure if it safe to make snapshot (used with cancel which is not yet supported and may cause corrupted head.state writing)
+ private boolean safeToMakeSnapshot = true;
@Override
public void close() throws ProCoreException {
if(!closed && !isClosing) {
isClosing = true;
try {
- makeSnapshot(true);
+ makeSnapshot(true);
mainProgram.close();
clusters.shutdown();
public long cancelCommit(long transactionId, long changeSetId,
byte[] metadata, OnChangeSetUpdate onChangeSetUpdate)
throws ProCoreException {
- System.err.println("GraphClientImpl2.cancelCommit() called!! this is experimental and might cause havoc!");
- try {
- undo(new long[] {changeSetId}, onChangeSetUpdate);
- } catch (SDBException e) {
- e.printStackTrace();
- throw new ProCoreException(e);
- }
- clusters.state.headChangeSetId++;
- return clusters.state.headChangeSetId;
+ safeToMakeSnapshot = false;
+ throw new UnsupportedOperationException("org.simantics.acorn.GraphClientImpl2.cancelCommit() is not supported operation! Closing down to prevent further havoc");
+// System.err.println("GraphClientImpl2.cancelCommit() called!! this is experimental and might cause havoc!");
+// try {
+// undo(new long[] {changeSetId}, onChangeSetUpdate);
+// } catch (SDBException e) {
+// e.printStackTrace();
+// throw new ProCoreException(e);
+// }
+// clusters.state.headChangeSetId++;
+// return clusters.state.headChangeSetId;
}
@Override
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;
private DatabaseUserAgent userAgent;
+ private RandomAccessFile raLockFile;
+
+ private FileLock lock;
+
+ private boolean isRunning;
+
public AcornDatabase(Path folder) {
this.folder = folder;
}
@Override
public void deleteFiles() throws ProCoreException {
- // TODO: somehow check that the acorn client is not active.
deleteTree(folder);
}
@Override
public void start() throws ProCoreException {
+ Path lockFile = folder.resolve("lock");
+ try {
+ if (!Files.exists(lockFile))
+ Files.createFile(lockFile);
+
+ raLockFile = new RandomAccessFile(lockFile.toFile(), "rw");
+ lock = raLockFile.getChannel().tryLock();
+ if (lock == null) {
+ throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!");
+ }
+
+ isRunning = true;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
@Override
public boolean isRunning() throws ProCoreException {
- return true;
+ return isRunning;
}
@Override
public boolean tryToStop() throws ProCoreException {
+ try {
+ lock.release();
+ raLockFile.close();
+
+ Files.deleteIfExists(folder.resolve("lock"));
+
+ isRunning = false;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
return true;
-// throw new UnsupportedOperationException();
}
@Override
@Override
public boolean isConnected() throws ProCoreException {
- return true;
+ return isRunning;
}
@Override