Skip to content

Commit

Permalink
Strategy pattern for Extractor
Browse files Browse the repository at this point in the history
1. Splitted up Converter into Legacy and V2
2. Splitted up Extractor into Interface and Implementation
3. JavaContainerBuilder now instantiates Extractor instead of parser.
  • Loading branch information
tomuben committed Oct 23, 2024
1 parent 3b8102a commit 64e01da
Show file tree
Hide file tree
Showing 25 changed files with 307 additions and 211 deletions.
6 changes: 3 additions & 3 deletions exaudfclient/base/javacontainer/javacontainer.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "base/javacontainer/javacontainer.h"
#include "base/javacontainer/javacontainer_impl.h"
#include "base/javacontainer/script_options/parser.h"
#include "base/javacontainer/script_options/extractor.h"

using namespace SWIGVMContainers;
using namespace std;

JavaVMach::JavaVMach(bool checkOnly, std::unique_ptr<JavaScriptOptions::ScriptOptionsParser> scriptOptionsParser) {
JavaVMach::JavaVMach(bool checkOnly,std::unique_ptr<JavaScriptOptions::Extractor> extractor) {
try {
m_impl = new JavaVMImpl(checkOnly, false, std::move(scriptOptionsParser));
m_impl = new JavaVMImpl(checkOnly, false, std::move(extractor));
} catch (std::exception& err) {
lock_guard<mutex> lock(exception_msg_mtx);
exception_msg = "F-UDF-CL-SL-JAVA-1000: "+std::string(err.what());
Expand Down
4 changes: 2 additions & 2 deletions exaudfclient/base/javacontainer/javacontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JavaVMImpl;

namespace JavaScriptOptions {

struct ScriptOptionsParser;
struct Extractor;

}

Expand All @@ -23,7 +23,7 @@ class JavaVMach: public SWIGVM {
/*
* scriptOptionsParser: JavaVMach takes ownership of ScriptOptionsParser pointer.
*/
JavaVMach(bool checkOnly, std::unique_ptr<JavaScriptOptions::ScriptOptionsParser> scriptOptionsParser);
JavaVMach(bool checkOnly, std::unique_ptr<JavaScriptOptions::Extractor> extractor);
virtual ~JavaVMach() {}
virtual void shutdown();
virtual bool run();
Expand Down
11 changes: 5 additions & 6 deletions exaudfclient/base/javacontainer/javacontainer_builder.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "base/javacontainer/javacontainer_builder.h"
#include "base/javacontainer/script_options/parser_ctpg.h"
#include "base/javacontainer/script_options/parser_legacy.h"
#include "base/javacontainer/script_options/extractor_impl.h"
#include "base/swig_factory/swig_factory_impl.h"

#ifdef ENABLE_JAVA_VM
Expand All @@ -16,13 +15,13 @@ JavaContainerBuilder& JavaContainerBuilder::useCtpgParser() {
}

JavaVMach* JavaContainerBuilder::build() {
std::unique_ptr<JavaScriptOptions::ScriptOptionsParser> parser;
std::unique_ptr<JavaScriptOptions::Extractor> extractor;
if (m_useCtpgParser) {
parser = std::make_unique<JavaScriptOptions::ScriptOptionLinesParserCTPG>(std::make_unique<SwigFactoryImpl>());
extractor = std::make_unique<JavaScriptOptions::tExtractorV2>(std::make_unique<SwigFactoryImpl>());
} else {
parser = std::make_unique<JavaScriptOptions::ScriptOptionLinesParserLegacy>(std::make_unique<SwigFactoryImpl>());
extractor = std::make_unique<JavaScriptOptions::tExtractorLegacy>(std::make_unique<SwigFactoryImpl>());
}
return new JavaVMach(false, std::move(parser));
return new JavaVMach(false, std::move(extractor));
}


Expand Down
14 changes: 6 additions & 8 deletions exaudfclient/base/javacontainer/javacontainer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
#include "base/javacontainer/javacontainer.h"
#include "base/javacontainer/javacontainer_impl.h"
#include "base/javacontainer/script_options/extractor.h"
#include "base/javacontainer/script_options/parser.h"


using namespace SWIGVMContainers;
using namespace std;

JavaVMImpl::JavaVMImpl(bool checkOnly, bool noJNI,
std::unique_ptr<JavaScriptOptions::ScriptOptionsParser> scriptOptionsParser)
std::unique_ptr<JavaScriptOptions::Extractor> extractor)
: m_checkOnly(checkOnly)
, m_exaJavaPath("")
, m_localClasspath("/tmp") // **IMPORTANT**: /tmp needs to be in the classpath, otherwise ExaCompiler crashe with com.exasol.ExaCompilationException: /DATE_STRING.java:3: error: error while writing DATE_STRING: could not create parent directories
Expand All @@ -32,7 +31,7 @@ JavaVMImpl::JavaVMImpl(bool checkOnly, bool noJNI,
stringstream ss;
m_exaJavaPath = "/exaudf/base/javacontainer"; // TODO hardcoded path

parseScriptOptions(std::move(scriptOptionsParser));
parseScriptOptions(std::move(extractor));

m_needsCompilation = checkNeedsCompilation();
if (m_needsCompilation) {
Expand All @@ -49,16 +48,15 @@ JavaVMImpl::JavaVMImpl(bool checkOnly, bool noJNI,
}
}

void JavaVMImpl::parseScriptOptions(std::unique_ptr<JavaScriptOptions::ScriptOptionsParser> scriptOptionsParser) {
JavaScriptOptions::Extractor extractor(*scriptOptionsParser);
void JavaVMImpl::parseScriptOptions(std::unique_ptr<JavaScriptOptions::Extractor> extractor) {

DBG_FUNC_CALL(cerr,extractor.extract(m_scriptCode));
DBG_FUNC_CALL(cerr,extractor->extract(m_scriptCode));

DBG_FUNC_CALL(cerr,setClasspath());

m_jvmOptions = std::move(extractor.moveJvmOptions());
m_jvmOptions = std::move(extractor->moveJvmOptions());

for (set<string>::iterator it = extractor.getJarPaths().begin(); it != extractor.getJarPaths().end();
for (set<string>::iterator it = extractor->getJarPaths().begin(); it != extractor->getJarPaths().end();
++it) {
addJarToClasspath(*it);
}
Expand Down
7 changes: 3 additions & 4 deletions exaudfclient/base/javacontainer/javacontainer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class JavaVMTest;
namespace SWIGVMContainers {

namespace JavaScriptOptions {
struct ScriptOptionsParser;
struct Extractor;
}

class JavaVMImpl {
Expand All @@ -25,8 +25,7 @@ class JavaVMImpl {
/*
* scriptOptionsParser: JavaVMImpl takes ownership of ScriptOptionsParser pointer.
*/
JavaVMImpl(bool checkOnly, bool noJNI,
std::unique_ptr<JavaScriptOptions::ScriptOptionsParser> scriptOptionsParser);
JavaVMImpl(bool checkOnly, bool noJNI, std::unique_ptr<JavaScriptOptions::Extractor> extractor);
~JavaVMImpl() {}
void shutdown();
bool run();
Expand All @@ -43,7 +42,7 @@ class JavaVMImpl {
void setClasspath();
void setJvmOptions();
void addJarToClasspath(const std::string& path);
void parseScriptOptions(std::unique_ptr<JavaScriptOptions::ScriptOptionsParser> scriptOptionsParser);
void parseScriptOptions(std::unique_ptr<JavaScriptOptions::Extractor> extractor);
bool m_checkOnly;
std::string m_exaJavaPath;
std::string m_localClasspath;
Expand Down
7 changes: 5 additions & 2 deletions exaudfclient/base/javacontainer/script_options/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package(default_visibility = ["//visibility:public"])

cc_library(
name = "java_script_option_lines",
hdrs = [":extractor.h", ":parser_legacy.h", ":parser_ctpg.h"],
srcs = [":parser.h", ":converter.h", ":converter.cc", ":parser_legacy.cc", ":extractor.cc",
hdrs = [":extractor.h", ":extractor_impl.h", ":parser_legacy.h", ":parser_ctpg.h",
":converter_v2.h", ":converter_legacy.h"],
srcs = [":parser.h", ":converter.h", ":converter.cc", ":parser_legacy.cc", ":extractor_impl.h",
":extractor_impl.cc", ":converter_legacy.cc", ":converter_legacy.h",
":converter_v2.cc", ":converter_v2.h",
":keywords.h", ":keywords.cc", ":checksum.h", ":checksum.cc", ":parser_ctpg.cc",
":parser_ctpg_script_importer.cc", ":parser_ctpg_script_importer.h", ":string_ops.h", ":string_ops.cc"],
deps = ["@ssl//:ssl", "//base/script_options_parser/legacy:script_option_lines_parser_legacy",
Expand Down
56 changes: 0 additions & 56 deletions exaudfclient/base/javacontainer/script_options/converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,10 @@ namespace SWIGVMContainers {

namespace JavaScriptOptions {

inline uint32_t countBackslashesBackwards(const std::string & s, size_t start) {
uint32_t retVal = 0;
if (start < s.size() && start >= 0) {
while (start >= 0 && s[start--] == '\\') {
retVal++;
}
}
return retVal;
}

Converter::Converter()
: m_jvmOptions()
, m_jarPaths()
, m_whitespace(" \t\f\v") {}

void Converter::convertExternalJar(const std::string & value) {
for (size_t start = 0, delim = 0; ; start = delim + 1) {
delim = value.find(":", start);
if (delim != std::string::npos) {
std::string jar = value.substr(start, delim - start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
}
else {
std::string jar = value.substr(start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
break;
}
}
}

void Converter::convertExternalJarWithEscapeSequences(const std::string & value) {
std::string formattedValue(value);
StringOps::trim(formattedValue);
if (formattedValue.size() > 1 && formattedValue.front() == '\"' && formattedValue.back() == '\"') {
formattedValue = formattedValue.substr(1, formattedValue.size()-2);
}

for (size_t start = 0, delim = 0; ; start = delim + 1) {
size_t search_start = start;
do {
delim = formattedValue.find(":", search_start);
search_start = delim + 1;
} while (delim != std::string::npos && delim != 0 && countBackslashesBackwards(formattedValue, delim-1) % 2 == 1);

if (delim != std::string::npos) {
std::string jar = formattedValue.substr(start, delim - start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
}
else {
std::string jar = formattedValue.substr(start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
break;
}
}
}

void Converter::convertScriptClassName(const std::string & value) {
std::string trimmedValue(value);
StringOps::trim(trimmedValue);
Expand Down
16 changes: 2 additions & 14 deletions exaudfclient/base/javacontainer/script_options/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
#include <string>
#include <vector>
#include <set>
#include <memory>

#include "base/javacontainer/script_options/parser.h"



namespace SWIGVMContainers {

Expand All @@ -19,17 +14,13 @@ class Converter {
public:
Converter();

void convertExternalJar(const std::string & value);

void convertExternalJarWithEscapeSequences(const std::string & value);
virtual void convertExternalJar(const std::string & value) = 0;

void convertScriptClassName(const std::string & value);

void convertJvmOption(const std::string & value);

const std::set<std::string> & getJarPaths() const {
return m_jarPaths;
}
virtual const std::set<std::string> & getJarPaths() const = 0;

std::vector<std::string>&& moveJvmOptions() {
return std::move(m_jvmOptions);
Expand All @@ -38,15 +29,12 @@ class Converter {
private:

std::vector<std::string> m_jvmOptions;

std::set<std::string> m_jarPaths;

const std::string m_whitespace;
};




} //namespace JavaScriptOptions

} //namespace SWIGVMContainers
Expand Down
33 changes: 33 additions & 0 deletions exaudfclient/base/javacontainer/script_options/converter_legacy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "base/javacontainer/script_options/converter_legacy.h"
#include "base/javacontainer/script_options/string_ops.h"
#include <iostream>

namespace SWIGVMContainers {

namespace JavaScriptOptions {

ConverterLegacy::ConverterLegacy()
: Converter()
, m_jarPaths() {}

void ConverterLegacy::convertExternalJar(const std::string & value) {
for (size_t start = 0, delim = 0; ; start = delim + 1) {
delim = value.find(":", start);
if (delim != std::string::npos) {
std::string jar = value.substr(start, delim - start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
}
else {
std::string jar = value.substr(start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
break;
}
}
}


} //namespace JavaScriptOptions

} //namespace SWIGVMContainers
41 changes: 41 additions & 0 deletions exaudfclient/base/javacontainer/script_options/converter_legacy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef SCRIPTOPTIONLINEPARSERCONVERTERLEGACY_H
#define SCRIPTOPTIONLINEPARSERCONVERTERLEGACY_H 1

#include <string>
#include <vector>
#include <set>
#include <memory>

#include "base/javacontainer/script_options/converter.h"



namespace SWIGVMContainers {

namespace JavaScriptOptions {

class ConverterLegacy : public Converter {

public:
ConverterLegacy();

void convertExternalJar(const std::string & value);

const std::set<std::string> & getJarPaths() const {
return m_jarPaths;
}

private:

std::set<std::string> m_jarPaths;

};




} //namespace JavaScriptOptions

} //namespace SWIGVMContainers

#endif //SCRIPTOPTIONLINEPARSERCONVERTER_H
34 changes: 34 additions & 0 deletions exaudfclient/base/javacontainer/script_options/converter_v2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "base/javacontainer/script_options/converter_v2.h"
#include "base/javacontainer/script_options/string_ops.h"
#include <iostream>

namespace SWIGVMContainers {

namespace JavaScriptOptions {

ConverterV2::ConverterV2()
: Converter()
, m_jarPaths() {}

void ConverterV2::convertExternalJar(const std::string & value) {

for (size_t start = 0, delim = 0; ; start = delim + 1) {
delim = value.find(":", start);
if (delim != std::string::npos) {
std::string jar = value.substr(start, delim - start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
}
else {
std::string jar = value.substr(start);
if (m_jarPaths.find(jar) == m_jarPaths.end())
m_jarPaths.insert(jar);
break;
}
}
}


} //namespace JavaScriptOptions

} //namespace SWIGVMContainers
Loading

0 comments on commit 64e01da

Please sign in to comment.