|
| 1 | +// SPDX-License-Identifier: CC-BY-4.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#include <unistd.h> |
| 5 | + |
| 6 | +#include <cstdlib> |
| 7 | +#include <iostream> |
| 8 | +#include <thread> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include <vortex/data_source.hpp> |
| 12 | +#include <vortex/estimate.hpp> |
| 13 | + |
| 14 | +using vortex::DataSource; |
| 15 | +using vortex::Estimate; |
| 16 | +using vortex::Scan; |
| 17 | +using vortex::Session; |
| 18 | + |
| 19 | +static void print_estimate(const char *what, const Estimate &est) { |
| 20 | + using enum vortex::EstimateType; |
| 21 | + switch (est.type()) { |
| 22 | + case Unknown: |
| 23 | + std::cout << what << ": unknown\n"; |
| 24 | + break; |
| 25 | + case Exact: |
| 26 | + std::cout << what << ": " << est.value() << '\n'; |
| 27 | + break; |
| 28 | + case Inexact: |
| 29 | + std::cout << what << ": at most " << est.value() << '\n'; |
| 30 | + break; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +struct ScanStats { |
| 35 | + size_t partitions = 0; |
| 36 | + size_t arrays = 0; |
| 37 | + size_t rows = 0; |
| 38 | +}; |
| 39 | + |
| 40 | +static ScanStats worker(Scan &scan) { |
| 41 | + ScanStats stats; |
| 42 | + while (auto partition = scan.next_partition()) { |
| 43 | + ++stats.partitions; |
| 44 | + while (auto array = partition->next()) { |
| 45 | + ++stats.arrays; |
| 46 | + stats.rows += array->size(); |
| 47 | + } |
| 48 | + } |
| 49 | + return stats; |
| 50 | +} |
| 51 | + |
| 52 | +bool parse_arguments(int argc, char **argv, size_t &num_threads, std::string_view &files) { |
| 53 | + int opt = 0; |
| 54 | + while ((opt = getopt(argc, argv, "j:")) != -1) { |
| 55 | + switch (opt) { |
| 56 | + case 'j': |
| 57 | + num_threads = static_cast<size_t>(std::atoi(optarg)); |
| 58 | + break; |
| 59 | + default: |
| 60 | + std::cerr << "Multi-threaded file scan\nUsage: scan [-j " |
| 61 | + "threads] <file glob>\n"; |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + if (optind + 1 != argc) { |
| 66 | + std::cerr << "Multi-threaded file scan\nUsage: scan [-j threads] <file " |
| 67 | + "glob>\n"; |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + files = argv[optind]; |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +int main(int argc, char **argv) { |
| 76 | + size_t num_threads = 0; |
| 77 | + std::string_view files; |
| 78 | + if (!parse_arguments(argc, argv, num_threads, files)) { |
| 79 | + return 1; |
| 80 | + } |
| 81 | + std::cout << "Opening files: " << files << '\n'; |
| 82 | + |
| 83 | + const Session session; |
| 84 | + const DataSource ds = DataSource::open(session, {files}); |
| 85 | + |
| 86 | + print_estimate("Data source row count", ds.row_count()); |
| 87 | + |
| 88 | + Scan scan = ds.scan(); |
| 89 | + print_estimate("Partition count", scan.partition_count()); |
| 90 | + |
| 91 | + if (num_threads == 0) { |
| 92 | + num_threads = scan.partition_count().value_or(1); |
| 93 | + } |
| 94 | + |
| 95 | + std::cout << "Starting scan, using " << num_threads << " threads\n"; |
| 96 | + std::vector<std::thread> threads; |
| 97 | + threads.reserve(num_threads); |
| 98 | + std::vector<ScanStats> results(num_threads); |
| 99 | + |
| 100 | + for (size_t i = 0; i < num_threads; ++i) { |
| 101 | + threads.emplace_back([i, &scan, &results] { results[i] = worker(scan); }); |
| 102 | + } |
| 103 | + for (auto &t : threads) { |
| 104 | + t.join(); |
| 105 | + } |
| 106 | + |
| 107 | + ScanStats total; |
| 108 | + for (const auto &r : results) { |
| 109 | + total.partitions += r.partitions; |
| 110 | + total.arrays += r.arrays; |
| 111 | + total.rows += r.rows; |
| 112 | + } |
| 113 | + std::cout << "Finished scan, processed " << total.partitions << " partitions, " << total.arrays |
| 114 | + << " arrays, " << total.rows << " rows\n"; |
| 115 | + return 0; |
| 116 | +} |
0 commit comments