Skip to content

Commit

Permalink
Feat: EXPORT MODEL ~ TO FILE
Browse files Browse the repository at this point in the history
  • Loading branch information
taewhi committed Mar 28, 2024
1 parent 9f7a6a6 commit 7c05de3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion traindb-core/src/main/antlr4/traindb/sql/TrainDBSql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ optionValue
;

exportModel
: K_EXPORT K_MODEL modelName
: K_EXPORT K_MODEL modelName exportToClause?
;

importModel
Expand Down
14 changes: 12 additions & 2 deletions traindb-core/src/main/java/traindb/engine/TrainDBQueryEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,8 @@ public void deleteTasks(Integer cnt) throws Exception {
}

@Override
public TrainDBListResultSet exportModel(String modelName) throws Exception {
public TrainDBListResultSet exportModel(String modelName, String exportFilename)
throws Exception {
T_tracer.startTaskTracer("export model " + modelName);

T_tracer.openTaskTime("find : model");
Expand Down Expand Up @@ -1131,13 +1132,22 @@ public TrainDBListResultSet exportModel(String modelName) throws Exception {
catalogContext.updateTrainingStatus(modelName, "FINISHED");
}

Path outputPath = Paths.get(runner.getModelPath().getParent().toString(), modelName + ".zip");
Path outputPath;
if (exportFilename != null) {
outputPath = Paths.get(exportFilename).toAbsolutePath();
} else {
outputPath = Paths.get(runner.getModelPath().getParent().toString(), modelName + ".zip");
}
runner.exportModel(outputPath.toString());

ObjectMapper mapper = new ObjectMapper();
ZipUtils.addNewFileFromStringToZip("export_metadata.json",
mapper.writeValueAsString(mModel), outputPath);

if (exportFilename != null) {
return TrainDBListResultSet.empty();
}

List<String> header = Arrays.asList("export_model");
List<List<Object>> exportModelInfo = new ArrayList<>();
ByteArray byteArray = convertFileToByteArray(new File(outputPath.toString()));
Expand Down
8 changes: 6 additions & 2 deletions traindb-core/src/main/java/traindb/sql/TrainDBSql.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static TrainDBListResultSet run(TrainDBSqlCommand command, TrainDBSqlRunn
break;
case EXPORT_MODEL:
TrainDBSqlExportModel exportModel = (TrainDBSqlExportModel) command;
return runner.exportModel(exportModel.getModelName());
return runner.exportModel(exportModel.getModelName(), exportModel.getExportFilename());
case IMPORT_MODEL:
TrainDBSqlImportModel importModel = (TrainDBSqlImportModel) command;
return runner.importModel(importModel.getModelName(), importModel.getModelBinaryString());
Expand Down Expand Up @@ -375,8 +375,12 @@ public void exitDescribeTable(TrainDBSqlParser.DescribeTableContext ctx) {
@Override
public void exitExportModel(TrainDBSqlParser.ExportModelContext ctx) {
String modelName = ctx.modelName().getText();
String exportFilename = null;
if (ctx.exportToClause() != null) {
exportFilename = ctx.exportToClause().filenameString().getText();
}
LOG.debug("EXPORT MODEL: name=" + modelName);
commands.add(new TrainDBSqlExportModel(modelName));
commands.add(new TrainDBSqlExportModel(modelName, exportFilename));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@

class TrainDBSqlExportModel extends TrainDBSqlCommand {
private final String modelName;
private final String exportFilename;

TrainDBSqlExportModel(String modelName) {
TrainDBSqlExportModel(String modelName, String exportFilename) {
this.modelName = modelName;
this.exportFilename = exportFilename;
}

String getModelName() {
return modelName;
}

String getExportFilename() {
return exportFilename;
}

@Override
public Type getType() {
return Type.EXPORT_MODEL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void createSynopsis(String synopsisName, TrainDBSqlCommand.SynopsisType synopsis

void deleteTasks(Integer cnt) throws Exception;

TrainDBListResultSet exportModel(String modelName) throws Exception;
TrainDBListResultSet exportModel(String modelName, String exportFilename) throws Exception;

TrainDBListResultSet importModel(String modelName, String modelBinaryString) throws Exception;

Expand Down

0 comments on commit 7c05de3

Please sign in to comment.