package framework.sciql;

import framework.AdbmsSystem;
import framework.QueryExecutor;
import framework.QueryGenerator;
import framework.context.BenchmarkContext;
import java.io.File;
import java.io.IOException;
import org.asqldb.util.TimerUtil;
import util.IO;

/**
 *
 * @author George Merticariu
 */
public class SciQLSystem extends AdbmsSystem {

    public static final String KEY_SCIQL_HOME = "sciql.home";
    public static final String KEY_SCIQL_DBFARM = "sciql.dbfarm";

    protected String sciqlHome;
    protected String sciqlDbfarm;
    protected String sciqlBinDir;
    protected String sciqlMclientPath;
    protected String merovingianLockFile;

    public SciQLSystem(String propertiesPath) throws IOException {
        super(propertiesPath, "SciQL");
        this.sciqlHome = getValue(KEY_SCIQL_HOME);
        this.sciqlDbfarm = getValue(KEY_SCIQL_DBFARM);
        this.sciqlBinDir = IO.concatPaths(sciqlHome, "bin");
        this.sciqlMclientPath = IO.concatPaths(sciqlBinDir, "mclient");
        this.startCommand = new String[]{sciqlBinDir + "/monetdbd", "start", sciqlDbfarm};
        this.stopCommand = new String[]{sciqlBinDir + "/monetdbd", "stop", sciqlDbfarm};
        this.merovingianLockFile = IO.concatPaths(sciqlDbfarm, ".merovingian_lock");
    }

    @Override
    public void restartSystem() throws Exception {
        TimerUtil.clearTimers();
        TimerUtil.startTimer("time");
        System.out.print("restarting monetdb... ");
        SciQLConnection.close();

        if (executeShellCommand(stopCommand) != 0) {
            // ignore, it may be already stopped
        }
        waitUntilLockRemoved();
        Thread.sleep(500);

        if (executeShellCommand(startCommand) != 0) {
            executeShellCommand(stopCommand);
            waitUntilLockRemoved();
            if (executeShellCommand(startCommand) != 0) {
                throw new Exception("Failed starting monetdb.");
            }
        }
        Thread.sleep(500);

        SciQLConnection.open(this);
        String res = TimerUtil.stopTimer("time");
        System.out.println("ok, " + res + ".");
    }

    private void waitUntilLockRemoved() throws Exception {
        File lock = new File(merovingianLockFile);
        int count = 0;
        while (lock.exists()) {
            Thread.sleep(500);
            ++count;
            if (count > 60) {
                throw new Exception("Failed restarting monetdb after 30s.");
            }
        }
    }

    public String getSciqlHome() {
        return sciqlHome;
    }

    public String getSciqlDbfarm() {
        return sciqlDbfarm;
    }

    public String getMclientPath() {
        return sciqlMclientPath;
    }

    @Override
    public QueryGenerator getQueryGenerator(BenchmarkContext benchmarkContext) {
        return new SciQLQueryGenerator(benchmarkContext);
    }

    @Override
    public QueryExecutor getQueryExecutor(BenchmarkContext benchmarkContext) throws IOException {
        return new SciQLQueryExecutor(benchmarkContext, this);
    }
}

@@ -349,203 +349,8 @@
  %7B%0A%0A
-    public static final String KEY_SCIQL_HOME = %22sciql.home%22;%0A    public static final String KEY_SCIQL_DBFARM = %22sciql.dbfarm%22;%0A%0A    protected String sciqlHome;%0A    protected String sciqlDbfarm;%0A
     
@@ -572,114 +572,8 @@
 %22);%0A
-        this.sciqlHome = getValue(KEY_SCIQL_HOME);%0A        this.sciqlDbfarm = getValue(KEY_SCIQL_DBFARM);%0A
     
@@ -610,25 +610,26 @@
 atPaths(
-sciqlHome
+installDir
 , %22bin%22)
@@ -735,32 +735,47 @@
  = new String%5B%5D%7B
+IO.concatPaths(
 sciqlBinDir + %22/
@@ -761,37 +761,35 @@
 aths(sciqlBinDir
- +
+,
  %22
-/
 monetdbd%22, %22star
@@ -777,24 +777,25 @@
 , %22monetdbd%22
+)
 , %22start%22, s
@@ -793,27 +793,23 @@
 start%22, 
-sciqlDbfarm
+dataDir
 %7D;%0A     
@@ -843,16 +843,31 @@
 tring%5B%5D%7B
+IO.concatPaths(
 sciqlBin
@@ -873,13 +873,11 @@
 nDir
- +
+,
  %22
-/
 mone
@@ -881,16 +881,17 @@
 onetdbd%22
+)
 , %22stop%22
@@ -892,27 +892,23 @@
 %22stop%22, 
-sciqlDbfarm
+dataDir
 %7D;%0A     
@@ -952,27 +952,23 @@
 atPaths(
-sciqlDbfarm
+dataDir
 , %22.mero
@@ -1359,35 +1359,8 @@
 d();
-%0A        Thread.sleep(500);
 %0A%0A  
@@ -1415,152 +1415,8 @@
 ) %7B%0A
-            executeShellCommand(stopCommand);%0A            waitUntilLockRemoved();%0A            if (executeShellCommand(startCommand) != 0) %7B%0A    
     
@@ -1468,38 +1468,24 @@
 monetdb.%22);%0A
-            %7D%0A
         %7D%0A  
@@ -1765,31 +1765,8 @@
 e);%0A
-        int count = 0;%0A
     
@@ -1836,291 +1836,9 @@
     
-    ++count;%0A            if (count %3E 60) %7B%0A                throw new Exception(%22Failed restarting monetdb after 30s.%22);%0A            %7D%0A        %7D%0A    %7D%0A%0A    public String getSciqlHome() %7B%0A        return sciqlHome;%0A    %7D%0A%0A    public String getSciqlDbfarm() %7B%0A        return sciqlDbfarm;
+%7D
 %0A   

update SciQL system controller