Skip to content
Permalink
Browse files

IndexRebuild task (#362)

  • Loading branch information...
gquerret committed Apr 29, 2019
1 parent d62382d commit 2b6411e3e45efd9bd88c405af8771304b8332a29
@@ -31,3 +31,4 @@ RestGen=com.phenix.pct.RestGen
PCTASBroker=com.phenix.pct.PCTASBroker
PCTWSBroker=com.phenix.pct.PCTWSBroker
PCTNSBroker=com.phenix.pct.PCTNSBroker
IndexRebuild=com.phenix.pct.PCTIndexRebuild
@@ -0,0 +1,198 @@
/**
* Copyright 2005-2018 Riverside Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.phenix.pct;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.types.Environment;

/**
* Class for creating Progress databases
*
* @author <a href="mailto:g.querret+PCT@gmail.com">Gilles QUERRET </a>
*/
public class PCTIndexRebuild extends PCT {
private File dbDir = null;
private String dbName = null;
private File outputLog = null;
private List<Index> indexes = new ArrayList<>();
private String cpInternal = null;
private List<PCTRunOption> options = new ArrayList<>();

// Internal use
private int tmpId;
private File tmpFile;

public PCTIndexRebuild() {
tmpId = PCT.nextRandomInt();
tmpFile = new File(System.getProperty(PCT.TMPDIR), "idxbuild" + tmpId + ".txt");
}

/**
* Database name
*/
public void setDBName(String dbName) {
this.dbName = dbName;
}

/**
* Database directory
*/
public void setDbDir(File dbDir) {
this.dbDir = dbDir;
}

public void setOutputLog(File outputLog) {
this.outputLog = outputLog;
}

/**
* Internal code page (-cpinternal attribute)
*/
public void setCpInternal(String cpInternal) {
this.cpInternal = cpInternal;
}

public void addConfiguredIndex(Index index) {
if ((index.table == null) || index.table.isEmpty())
throw new BuildException("Invalid index");
if ((index.index == null) || index.index.isEmpty())
throw new BuildException("Invalid index");
this.indexes.add(index);
}

public void addOption(PCTRunOption option) {
options.add(option);

}


/**
* Do the work
*
* @throws BuildException Something went wrong
*/
@Override
public void execute() {
checkDlcHome();

// Checking dbName and dbDir are defined
if (dbName == null) {
throw new BuildException(Messages.getString("PCTCreateBase.3"));
}
if (indexes.isEmpty())
throw new BuildException("Index list can't be empty");

// Update destDir if not defined
if (dbDir == null) {
dbDir = getProject().getBaseDir();
}

try {
generateIndexFile();
idxBuildCmdLine().execute();
} catch (IOException caught) {
cleanup();
throw new BuildException(caught);
}

}

protected void cleanup() {
deleteFile(tmpFile);
}

public static class Index {
private String table;
private String index;

public void setTable(String table) {
this.table = table;
}

public void setIndex(String index) {
this.index = index;
}
}

private void generateIndexFile() throws IOException {
try (OutputStream os = new FileOutputStream(tmpFile);
Writer w = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(w)) {
bw.write("some");
bw.newLine();
for (Index idx : indexes) {
bw.write(idx.table);
bw.newLine();
bw.write(idx.index);
bw.newLine();
}
bw.write("!");
bw.newLine();
bw.write("y");
bw.newLine();
bw.write("y");
bw.newLine();
}
}

/**
* Creates the _dbutil procopy emptyX command line
*
* @return An ExecTask, ready to be executed
*/
private ExecTask idxBuildCmdLine() {
ExecTask exec = new ExecTask(this);
exec.setFailonerror(true);

File db = new File(dbDir, dbName);
exec.setExecutable(getExecPath("_dbutil").toString());
exec.createArg().setValue(db.getAbsolutePath());
exec.createArg().setValue("-C");
exec.createArg().setValue("idxbuild");
if (outputLog != null)
exec.setOutput(outputLog.getAbsoluteFile());
exec.setInput(tmpFile);
for (PCTRunOption option: options) {
exec.createArg().setValue(option.getName());
if (option.getValue() != null)
exec.createArg().setValue(option.getValue());
}
if (cpInternal != null) {
exec.createArg().setValue("-cpinternal");
exec.createArg().setValue(cpInternal);
}

Environment.Variable var = new Environment.Variable();
var.setKey("DLC"); //$NON-NLS-1$
var.setValue(getDlcHome().toString());
exec.addEnv(var);

return exec;
}

}
@@ -0,0 +1,37 @@
/**
* Copyright 2005-2018 Riverside Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.phenix.pct;

import org.testng.annotations.Test;

/**
* Class for testing IndexRebuild task
*
* @author <a href="mailto:g.querret+PCT@gmail.com">Gilles QUERRET</a>
*/
public class IndexRebuildTest extends BuildFileTestNg {

@Test(groups = {"v10"})
public void test1() {
configureProject("IndexRebuild/test1/build.xml");
executeTarget("init");
expectBuildException("check", "No index yet");
executeTarget("test");
executeTarget("check");
}

}
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<project name="IndexRebuild-test1">
<taskdef resource="PCT.properties" />

<target name="init">
<mkdir dir="db" />
<sports2000 destDir="db" dbName="sp2k" dlcHome="${DLC}" />
<PCTLoadSchema dlcHome="${DLC}" srcFile="index.df">
<DBConnection dbName="sp2k" dbDir="db" singleUser="true" />
</PCTLoadSchema>
</target>

<target name="test">
<IndexRebuild dbDir="db" dbName="sp2k" dlcHome="${DLC}">
<Index table="customer" index="CustomerNew1" />
<Index table="employee" index="EmployeeNew1" />
</IndexRebuild>
</target>

<target name="check">
<PCTRun procedure="test.p" dlcHome="${DLC}">
<DBConnection dbName="sp2k" dbDir="db" singleUser="true" />
</PCTRun>
</target>
</project>
@@ -0,0 +1,10 @@
ADD INDEX "CustomerNew1" ON "customer"
INACTIVE
DESCRIPTION "XXX"
INDEX-FIELD "name" ASCENDING

ADD INDEX "EmployeeNew1" ON "employee"
INACTIVE
DESCRIPTION "XXX"
INDEX-FIELD "lastname" ASCENDING

@@ -0,0 +1,3 @@
find first customer use-index CustomerNew1.
find first employee use-index EmployeeNew1.
return '0'.

0 comments on commit 2b6411e

Please sign in to comment.
You can’t perform that action at this time.