diff options
author | Geir Storli <geirst@vespa.ai> | 2024-06-11 17:23:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-11 17:23:25 +0200 |
commit | 26c753d7f527a1908ba53950228b38c2037fd7a9 (patch) | |
tree | 69a847a275ebd13e09e21e378a00baf5ffe56364 | |
parent | 7b537fe1f0e6050c771f8c53e4f8a7d06d0db337 (diff) | |
parent | be23733bc3095cb99c12db08f92b0a6046a6b7f1 (diff) |
Merge pull request #31536 from vespa-engine/havardpe/support-stdin
support using stdin as input
-rw-r--r-- | searchlib/src/apps/vespa-query-analyzer/vespa-query-analyzer.cpp | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/searchlib/src/apps/vespa-query-analyzer/vespa-query-analyzer.cpp b/searchlib/src/apps/vespa-query-analyzer/vespa-query-analyzer.cpp index ea7dd673aa0..6a746d59daa 100644 --- a/searchlib/src/apps/vespa-query-analyzer/vespa-query-analyzer.cpp +++ b/searchlib/src/apps/vespa-query-analyzer/vespa-query-analyzer.cpp @@ -16,13 +16,9 @@ using namespace vespalib::slime::convenience; using vespalib::make_string_short::fmt; -using vespalib::slime::JsonFormat; -using vespalib::slime::ARRAY; -using vespalib::slime::OBJECT; -using vespalib::slime::STRING; -using vespalib::slime::DOUBLE; -using vespalib::slime::BOOL; using namespace search::queryeval; +using namespace vespalib::slime; +using namespace vespalib; //----------------------------------------------------------------------------- @@ -586,9 +582,9 @@ struct Analyzer { data.calc_cost(true); data.normalize(); data.print(); - fprintf(stderr, "distribution key: %d, total_time_ms: %g, estimated ms_per_cost: %g\n", key, total_ms, data.ms_per_cost); + fprintf(stdout, "distribution key: %d, total_time_ms: %g, estimated ms_per_cost: %g\n", key, total_ms, data.ms_per_cost); for (auto [type, time]: time_map) { - fprintf(stderr, "sample type %s used %g ms total\n", Sample::type_to_str(type).c_str(), time); + fprintf(stdout, "sample type %s used %g ms total\n", Sample::type_to_str(type).c_str(), time); } } } @@ -620,18 +616,45 @@ MyApp::parse_params(int argc, char **argv) { return true; } +class StdIn : public Input { +private: + bool _eof = false; + SimpleBuffer _input; +public: + Memory obtain() override { + if ((_input.get().size == 0) && !_eof) { + WritableMemory buf = _input.reserve(4096); + ssize_t res = read(STDIN_FILENO, buf.data, buf.size); + _eof = (res == 0); + assert(res >= 0); // fail on stdio read errors + _input.commit(res); + } + return _input.obtain(); + } + Input &evict(size_t bytes) override { + _input.evict(bytes); + return *this; + } +}; + int MyApp::main() { - vespalib::MappedFileInput file(file_name); - if (!file.valid()) { - fprintf(stderr, "could not read input file: '%s'\n", - file_name.c_str()); - return 1; - } Slime slime; - if(JsonFormat::decode(file, slime) == 0) { - fprintf(stderr, "file contains invalid json: '%s'\n", + std::unique_ptr<Input> input; + if (file_name == "-") { + input = std::make_unique<StdIn>(); + } else { + auto file = std::make_unique<MappedFileInput>(file_name); + if (!file->valid()) { + fprintf(stderr, "could not read input file: '%s'\n", + file_name.c_str()); + return 1; + } + input = std::move(file); + } + if(JsonFormat::decode(*input, slime) == 0) { + fprintf(stderr, "input contains invalid json (%s)\n", file_name.c_str()); return 1; } |