Repository navigation
Expand file tree
/
Copy pathcsv_importer.cpp
More file actions
81 lines (69 loc) · 3.18 KB
/
Copy pathcsv_importer.cpp
File metadata and controls
81 lines (69 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "csv_contract.bindings.h"
#include "csv_package.h"
#include "csv_sdk.h"
namespace csv_import {
namespace {
using huxerui::pluginkit::PluginContext;
std::uint32_t live_importers = 0;
std::optional<huxerui::RawAsset> cell_limit;
void OnActivate(const PluginContext& context) {
if (fixture::active_context) throw std::runtime_error("The CSV package is already active");
auto limit = context.Resources().GetRawResource(csv_resources::raw::cell_limit_txt);
fixture::Record(context, "activate");
cell_limit.emplace(std::move(limit));
fixture::active_context = &context;
}
void OnDeactivate(const PluginContext& context) {
if (fixture::active_context != &context || live_importers || fixture::live_exporters) {
throw std::runtime_error("Expected the original context after all CSV instances were destroyed");
}
(void)fixture::ReadCellLimit(*cell_limit);
fixture::Record(context, "deactivate");
cell_limit.reset();
fixture::active_context = nullptr;
}
template<char Separator>
class CsvImporter final : public TableImporter {
public:
explicit CsvImporter(const PluginContext& context) : context_(context) {
if (fixture::active_context != &context) throw std::runtime_error("Expected the activation's context");
++live_importers;
}
~CsvImporter() override { --live_importers; }
std::string Delimiter() const override { return std::string(1, Separator); }
std::uint32_t ProcessId() const override { return csv_import::ProcessId(); }
huxerui::Task<ImportResult> Import(huxerui::AsyncInputStream input, std::shared_ptr<ImportProgress> receiver) override {
using huxerui::pluginkit::Error;
using huxerui::pluginkit::ErrorCode;
co_await huxerui::Delay(std::chrono::milliseconds(1));
fixture::CsvRecords parser(Separator, fixture::ReadCellLimit(*cell_limit),
std::get<std::int64_t>(context_.Settings().at("max_rows")));
Table table;
const auto append = [&table](fixture::Row row, bool header) {
if (header) table.header = std::move(row);
else table.rows.push_back(std::move(row));
};
auto remaining = MaxImportBytes;
for (;;) {
const auto bytes = co_await fixture::ReadImportChunk(input, remaining);
if (bytes.empty()) break;
if (!parser.Feed(bytes, append)) co_return ImportResult{std::nullopt, {parser.Error()}};
}
if (!parser.Finish(append)) co_return ImportResult{std::nullopt, {parser.Error()}};
const auto rows = parser.Rows();
if (receiver) co_await receiver->OnRows(rows, MaxImportBytes - remaining);
if (!co_await SetImportedRows(rows)) {
throw Error(ErrorCode::PluginFailed, "The host declined the imported row count");
}
co_return ImportResult{std::move(table), {}};
}
private:
const PluginContext& context_;
};
} // namespace
} // namespace csv_import
using huxerui::pluginkit::BindExtension;
HPK_EXPORT_PLUGIN(csv_import::OnActivate, csv_import::OnDeactivate,
BindExtension<csv_import::TableImporter, csv_import::CsvImporter<','>>(csv_import::Importers),
BindExtension<csv_import::TableExporter, csv_import::fixture::CsvExporter>(csv_import::Exporters),
BindExtension<csv_import::TableImporter, csv_import::CsvImporter<';'>>(csv_import::Importers))