This will update the amalgamate_demo.cpp file to use On Demand. (#1689)

* This will update the amalgamate_demo.cpp file to use On Demand.

* Making the demo exceptionless.
This commit is contained in:
Daniel Lemire 2021-08-07 12:43:40 -04:00 committed by GitHub
parent 19902abaf8
commit 40813752f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6091 additions and 789 deletions

View File

@ -1,35 +1,70 @@
#include <iostream>
#include "simdjson.h"
#include "simdjson.cpp" #include "simdjson.cpp"
#include "simdjson.h"
#include <iostream>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if(argc < 2) { if (argc < 2) {
std::cerr << "Please specify at least one file name. " << std::endl; std::cerr << "Please specify at least one file name and" << std::endl;
std::cerr << "up to two files." << std::endl;
std::cerr << "The first file should be a JSON document." << std::endl;
std::cerr << "The secod file should container many JSON documents."
<< std::endl;
std::cerr << "Try the test files: jsonexamples/twitter.json "
"jsonexamples/amazon_cellphones.ndjson"
<< std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
const char * filename = argv[1]; const char *filename = argv[1];
simdjson::dom::parser parser;
simdjson::dom::element elem; simdjson::padded_string json;
auto error = parser.load(filename).get(elem); // do the parsing std::cout << "loading: " << filename << std::endl;
auto error = simdjson::padded_string::load(filename).get(json);
if (error) { if (error) {
std::cout << "parse failed" << std::endl; std::cout << "could not load the file " << filename << std::endl;
std::cout << "error code: " << error << std::endl; std::cout << "error code: " << error << std::endl;
std::cout << error << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} else { } else {
std::cout << "parse valid: " << elem << std::endl; std::cout << "loaded: " << json.size() << " bytes." << std::endl;
} }
if(argc == 2) { simdjson::ondemand::parser parser;
simdjson::ondemand::document doc;
error = parser.iterate(json).get(doc);
if (error) {
std::cout << error << std::endl;
return EXIT_FAILURE;
}
simdjson::ondemand::json_type type;
error = doc.type().get(type);
if (error) {
std::cout << error << std::endl;
return EXIT_FAILURE;
}
std::cout << "document has the following type at the root: " << type
<< std::endl;
if (argc == 2) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
// parse_many // iterate_many
const char * filename2 = argv[2]; const char *filename2 = argv[2];
simdjson::dom::document_stream stream; std::cout << "loading: " << filename2 << std::endl;
error = parser.load_many(filename2).get(stream); simdjson::padded_string json2;
error = simdjson::padded_string::load(filename2).get(json2);
if (error) {
std::cout << "could not load the file " << filename2 << std::endl;
std::cout << "error code: " << error << std::endl;
return EXIT_FAILURE;
} else {
std::cout << "loaded: " << json2.size() << " bytes." << std::endl;
}
simdjson::ondemand::document_stream stream;
error = parser.iterate_many(json2).get(stream);
size_t counter{0};
if (!error) { if (!error) {
for (auto result : stream) { for (auto result : stream) {
error = result.error(); error = result.error();
counter++;
} }
} }
if (error) { if (error) {
@ -38,8 +73,8 @@ int main(int argc, char *argv[]) {
std::cout << error << std::endl; std::cout << error << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} else { } else {
std::cout << "parse_many valid" << std::endl; std::cout << "iterate_many valid" << std::endl;
std::cout << "found " << counter << " documents" << std::endl;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff