Skip to content

Commit

Permalink
use template functions for view/context constructors, add init checking
Browse files Browse the repository at this point in the history
  • Loading branch information
sc1f committed Jul 26, 2019
1 parent 8ffe50b commit 879b77c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 143 deletions.
86 changes: 30 additions & 56 deletions cpp/perspective/src/cpp/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@ namespace binding {
return (!item.isUndefined() && !item.isNull());
}

/******************************************************************************
*
* Data Loading
*/

template <>
bool
is_valid_filter(t_dtype type, t_val date_parser, t_val filter_term) {
if (type == DTYPE_DATE || type == DTYPE_TIME) {
t_val parsed_date = date_parser.call<t_val>("parse", filter_term);
return has_value(parsed_date);
} else {
return has_value(filter_term);
}
};

/******************************************************************************
*
* Date Parsing
Expand Down Expand Up @@ -1313,10 +1297,21 @@ namespace binding {
*
* View API
*/

template <>
bool
is_valid_filter(t_dtype type, t_val date_parser, t_val filter_term) {
if (type == DTYPE_DATE || type == DTYPE_TIME) {
t_val parsed_date = date_parser.call<t_val>("parse", filter_term);
return has_value(parsed_date);
} else {
return has_value(filter_term);
}
};

template <>
std::tuple<std::string, std::string, std::vector<t_tscalar>>
make_filter_term(t_dtype type, t_val date_parser, std::vector<t_val> filter) {
// TODO: structure properly
std::string col = filter[0].as<std::string>();
std::string comp_str = filter[1].as<std::string>();
t_filter_op comp = str_to_filter_op(comp_str);
Expand Down Expand Up @@ -1385,6 +1380,7 @@ namespace binding {

bool column_only = false;

// make sure that primary keys are created for column-only views
if (row_pivots.size() == 0 && column_pivots.size() > 0) {
row_pivots.push_back("psp_okey");
column_only = true;
Expand All @@ -1396,11 +1392,14 @@ namespace binding {

for (auto f : js_filter) {
t_dtype type = schema.get_dtype(f[0].as<std::string>());

// validate the filter before it goes into the core engine
if (is_valid_filter(type, date_parser, f[2])) {
filter.push_back(make_filter_term(type, date_parser, f));
}
}

// create the `t_view_config`
t_view_config view_config(row_pivots, column_pivots, aggregates, columns, filter, sort,
filter_op, column_only);

Expand All @@ -1419,44 +1418,16 @@ namespace binding {
return view_config;
}

template <>
std::shared_ptr<View<t_ctx0>>
make_view_zero(std::shared_ptr<Table> table, std::string name, std::string separator,
t_val view_config, t_val date_parser) {
auto schema = table->get_schema();
t_view_config config = make_view_config<t_val>(schema, date_parser, view_config);

auto ctx = make_context_zero(table, schema, config, name);

auto view_ptr = std::make_shared<View<t_ctx0>>(table, ctx, name, separator, config);

return view_ptr;
}

template <>
std::shared_ptr<View<t_ctx1>>
make_view_one(std::shared_ptr<Table> table, std::string name, std::string separator,
t_val view_config, t_val date_parser) {
auto schema = table->get_schema();
t_view_config config = make_view_config<t_val>(schema, date_parser, view_config);

auto ctx = make_context_one(table, schema, config, name);

auto view_ptr = std::make_shared<View<t_ctx1>>(table, ctx, name, separator, config);

return view_ptr;
}

template <>
std::shared_ptr<View<t_ctx2>>
make_view_two(std::shared_ptr<Table> table, std::string name, std::string separator,
template <typename CTX_T>
std::shared_ptr<View<CTX_T>>
make_view(std::shared_ptr<Table> table, std::string name, std::string separator,
t_val view_config, t_val date_parser) {
auto schema = table->get_schema();
t_view_config config = make_view_config<t_val>(schema, date_parser, view_config);

auto ctx = make_context_two(table, schema, config, name);
auto ctx = make_context<CTX_T>(table, schema, config, name);

auto view_ptr = std::make_shared<View<t_ctx2>>(table, ctx, name, separator, config);
auto view_ptr = std::make_shared<View<CTX_T>>(table, ctx, name, separator, config);

return view_ptr;
}
Expand All @@ -1466,8 +1437,9 @@ namespace binding {
* Context API
*/

template <>
std::shared_ptr<t_ctx0>
make_context_zero(std::shared_ptr<Table> table, const t_schema& schema,
make_context(std::shared_ptr<Table> table, const t_schema& schema,
const t_view_config& view_config, std::string name) {
auto columns = view_config.get_columns();
auto filter_op = view_config.get_filter_op();
Expand All @@ -1487,8 +1459,9 @@ namespace binding {
return ctx0;
}

template <>
std::shared_ptr<t_ctx1>
make_context_one(std::shared_ptr<Table> table, const t_schema& schema,
make_context(std::shared_ptr<Table> table, const t_schema& schema,
const t_view_config& view_config, std::string name) {
auto row_pivots = view_config.get_row_pivots();
auto aggspecs = view_config.get_aggspecs();
Expand Down Expand Up @@ -1517,8 +1490,9 @@ namespace binding {
return ctx1;
}

template <>
std::shared_ptr<t_ctx2>
make_context_two(std::shared_ptr<Table> table, const t_schema& schema,
make_context(std::shared_ptr<Table> table, const t_schema& schema,
const t_view_config& view_config, std::string name) {
bool column_only = view_config.is_column_only();
auto row_pivots = view_config.get_row_pivots();
Expand Down Expand Up @@ -1943,9 +1917,9 @@ EMSCRIPTEN_BINDINGS(perspective) {
function("scalar_vec_to_string", &scalar_vec_to_string);
function("table_add_computed_column", &table_add_computed_column<t_val>);
function("col_to_js_typed_array", &col_to_js_typed_array);
function("make_view_zero", &make_view_zero<t_val>);
function("make_view_one", &make_view_one<t_val>);
function("make_view_two", &make_view_two<t_val>);
function("make_view_zero", &make_view<t_ctx0>);
function("make_view_one", &make_view<t_ctx1>);
function("make_view_two", &make_view<t_ctx2>);
function("get_data_slice_zero", &get_data_slice<t_ctx0>, allow_raw_pointers());
function("get_from_data_slice_zero", &get_from_data_slice<t_ctx0>, allow_raw_pointers());
function("get_data_slice_one", &get_data_slice<t_ctx1>, allow_raw_pointers());
Expand Down
29 changes: 19 additions & 10 deletions cpp/perspective/src/cpp/view_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ t_view_config::t_view_config(std::vector<std::string> row_pivots,
tsl::ordered_map<std::string, std::string> aggregates, std::vector<std::string> columns,
std::vector<std::tuple<std::string, std::string, std::vector<t_tscalar>>> filter,
std::vector<std::vector<std::string>> sort, std::string filter_op, bool column_only)
: m_row_pivots(row_pivots)
: m_init(false)
, m_row_pivots(row_pivots)
, m_column_pivots(column_pivots)
, m_aggregates(aggregates)
, m_columns(columns)
Expand All @@ -32,76 +33,92 @@ t_view_config::init(const t_schema& schema) {
fill_aggspecs(schema);
fill_fterm();
fill_sortspec();

m_init = true;
}

void
t_view_config::add_filter_term(
std::tuple<std::string, std::string, std::vector<t_tscalar>> term) {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
m_filter.push_back(term);
}

void
t_view_config::set_row_pivot_depth(std::int32_t depth) {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
m_row_pivot_depth = depth;
}

void
t_view_config::set_column_pivot_depth(std::int32_t depth) {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
m_column_pivot_depth = depth;
}

std::vector<std::string>
t_view_config::get_row_pivots() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_row_pivots;
}

std::vector<std::string>
t_view_config::get_column_pivots() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_column_pivots;
}

std::vector<t_aggspec>
t_view_config::get_aggspecs() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_aggspecs;
}

std::vector<std::string>
t_view_config::get_columns() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_columns;
}

std::vector<t_fterm>
t_view_config::get_fterm() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_fterm;
}

std::vector<t_sortspec>
t_view_config::get_sortspec() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_sortspec;
}

std::vector<t_sortspec>
t_view_config::get_col_sortspec() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_col_sortspec;
}

t_filter_op
t_view_config::get_filter_op() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return str_to_filter_op(m_filter_op);
}

bool
t_view_config::is_column_only() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_column_only;
}

std::int32_t
t_view_config::get_row_pivot_depth() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_row_pivot_depth;
}

std::int32_t
t_view_config::get_column_pivot_depth() const {
PSP_VERBOSE_ASSERT(m_init, "touching uninited object");
return m_column_pivot_depth;
}

Expand Down Expand Up @@ -233,12 +250,4 @@ t_view_config::get_aggregate_index(const std::string& column) const {
return t_index();
}

} // end namespace perspective

namespace std {
std::ostream&
operator<<(std::ostream& os, const perspective::t_view_config& vc) {
os << "t_view_config"; // TODO: finish
return os;
}
} // end namespace std
} // end namespace perspective
Loading

0 comments on commit 879b77c

Please sign in to comment.