Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move JSON APIs to C++ #2301

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cpp/perspective/src/cpp/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,8 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("get_sort", &View<t_ctxunit>::get_sort)
.function("get_step_delta", &View<t_ctxunit>::get_step_delta)
.function("get_column_dtype", &View<t_ctxunit>::get_column_dtype)
.function("is_column_only", &View<t_ctxunit>::is_column_only);
.function("is_column_only", &View<t_ctxunit>::is_column_only)
.function("to_columns", &View<t_ctxunit>::to_columns);

class_<View<t_ctx0>>("View_ctx0")
.constructor<std::shared_ptr<Table>, std::shared_ptr<t_ctx0>,
Expand All @@ -1925,7 +1926,9 @@ EMSCRIPTEN_BINDINGS(perspective) {
.function("get_sort", &View<t_ctx0>::get_sort)
.function("get_step_delta", &View<t_ctx0>::get_step_delta)
.function("get_column_dtype", &View<t_ctx0>::get_column_dtype)
.function("is_column_only", &View<t_ctx0>::is_column_only);
.function("is_column_only", &View<t_ctx0>::is_column_only)
.function("to_columns", &View<t_ctx0>::to_columns);
;

class_<View<t_ctx1>>("View_ctx1")
.constructor<std::shared_ptr<Table>, std::shared_ptr<t_ctx1>,
Expand Down
114 changes: 114 additions & 0 deletions cpp/perspective/src/cpp/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include <perspective/view.h>
#include <perspective/arrow_writer.h>
#include <sstream>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <boost/algorithm/string/join.hpp>

#include <arrow/csv/writer.h>
#include <perspective/pyutils.h>
Expand Down Expand Up @@ -408,6 +411,117 @@ View<T>::get_min_max(const std::string& colname) const {
return m_ctx->get_min_max(colname);
}

template <>
std::string
View<t_ctxunit>::to_columns(t_uindex start_row, t_uindex end_row,
t_uindex start_col, t_uindex end_col, bool get_ids) const {
std::vector<t_tscalar> slice
= m_ctx->get_data(start_row, end_row, start_col, end_col);
auto col_names = column_names();
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.StartObject();
// for (auto c = col_names.begin(); c != col_names.end(); ++c) {
// std::stringstream column_name;
// for (auto i = 0; i < c->size() - 1; ++i) {
// column_name << (*c)[i] << "|";
// }

// column_name << c[c->size() - 1];
// // std::cout << column_name.str() << std::endl;
// const std::string& tmp = column_name.str();
// writer.Key(tmp.c_str());
// writer.StartArray();
// writer.EndArray();
// }

writer.EndObject();
return s.GetString();
}

template <>
std::string
View<t_ctx0>::to_columns(t_uindex start_row, t_uindex end_row,
t_uindex start_col, t_uindex end_col, bool get_ids) const {
std::vector<t_tscalar> slice
= m_ctx->get_data(start_row, end_row, start_col, end_col);
auto col_names = column_names();
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.StartObject();
for (auto c = start_col; c < end_col; ++c) {
std::stringstream column_name;

for (auto i = 0; i < col_names[c].size() - 1; ++i) {
column_name << col_names[c][i].get<const char*>() << "|";
}

column_name << col_names[c][col_names[c].size() - 1].get<const char*>();
const std::string& tmp = column_name.str();

writer.Key(tmp.c_str());
writer.StartArray();
for (auto x = start_row; x < end_row; ++x) {
// (ridx - m_start_row) * m_stride + (cidx - m_start_col)
const t_tscalar& scalar
= slice[(x - start_row) * (end_col - start_col)
+ (c - start_col)];
switch (scalar.get_dtype()) {
case DTYPE_INT32:
writer.Int(scalar.get<int32_t>());
break;
case DTYPE_FLOAT64:
writer.Double(scalar.get<double>());
break;
case DTYPE_STR:
writer.String(scalar.get<const char*>());
break;
case DTYPE_TIME:
case DTYPE_DATE:
writer.Double(scalar.get<double>());
break;
default:
break;
}
}
writer.EndArray();
}

if (get_ids) {
writer.Key("__ID__");
writer.StartArray();
for (auto x = start_row; x < end_row; ++x) {
std::pair<t_uindex, t_uindex> pair{x, 0};
std::vector<std::pair<t_uindex, t_uindex>> vec{pair};
const auto keys = m_ctx->get_pkeys(vec);
const t_tscalar& scalar = keys[0];
writer.StartArray();
switch (scalar.get_dtype()) {
case DTYPE_INT32:
writer.Int(scalar.get<int32_t>());
break;
case DTYPE_FLOAT64:
writer.Double(scalar.get<double>());
break;
case DTYPE_STR:
writer.String(scalar.get<const char*>());
break;
case DTYPE_TIME:
case DTYPE_DATE:
writer.Double(scalar.get<double>());
break;
default:
break;
}
writer.EndArray();
}
writer.EndArray();
}

writer.EndObject();
return s.GetString();
}

template <>
std::shared_ptr<t_data_slice<t_ctxunit>>
View<t_ctxunit>::get_data(t_uindex start_row, t_uindex end_row,
Expand Down
3 changes: 3 additions & 0 deletions cpp/perspective/src/include/perspective/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ class PERSPECTIVE_EXPORT View {
std::shared_ptr<t_data_slice<CTX_T>> get_data(t_uindex start_row,
t_uindex end_row, t_uindex start_col, t_uindex end_col) const;

std::string to_columns(t_uindex start_row, t_uindex end_row,
t_uindex start_col, t_uindex end_col, bool get_ids) const;

/**
* @brief Serializes the `View`'s data into the Apache Arrow format
* as a bytestring. Using start/end row and column, retrieve a data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export function createDataListener() {
id: true,
};

columns = await this._view.to_columns(new_window);
columns = await this._view.to_columns_string(new_window);
columns = JSON.parse(columns);
this._last_window = new_window;
this._ids = columns.__ID__;
this._reverse_columns = this._column_paths
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective/src/js/api/view_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ view.prototype.to_arrow = async_queue("to_arrow");

view.prototype.to_columns = async_queue("to_columns");

view.prototype.to_columns_string = async_queue("to_columns_string");

view.prototype.to_csv = async_queue("to_csv");

view.prototype.schema = async_queue("schema");
Expand Down
31 changes: 31 additions & 0 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,37 @@ export default function (Module) {
return to_format.call(this, options, formatters.jsonTableFormatter);
};

view.prototype.to_columns_string = function (options) {
const num_sides = this.sides();
if (num_sides === 0) {
_call_process(this.table.get_id());
options = _parse_format_options.bind(this)(options);
const start_row = options.start_row;
const end_row = options.end_row;
const start_col = options.start_col;
const end_col = options.end_col;
const hidden = this._num_hidden();

const is_formatted = options.formatted;
const get_pkeys = !!options.index;
const get_ids = !!options.id;
const leaves_only = !!options.leaves_only;
const num_sides = this.sides();
const has_row_path = num_sides !== 0 && !this.column_only;
const nidx = SIDES[num_sides];

return this._View.to_columns(
start_row,
end_row,
start_col,
end_col,
get_ids
);
} else {
throw new Error("Not yet implemented");
}
};

/**
* Serializes this view to JSON data in a row-oriented format.
*
Expand Down
Loading