2021-01-02 14:41:15 +08:00
|
|
|
#pragma once
|
|
|
|
|
2021-01-05 04:21:50 +08:00
|
|
|
#ifdef SIMDJSON_COMPETITION_YYJSON
|
|
|
|
|
2021-01-02 14:41:15 +08:00
|
|
|
#include "find_tweet.h"
|
|
|
|
|
|
|
|
namespace find_tweet {
|
|
|
|
|
2021-01-06 03:41:07 +08:00
|
|
|
struct yyjson_base {
|
2021-01-07 06:38:12 +08:00
|
|
|
using StringType=std::string_view;
|
|
|
|
|
2021-01-06 03:41:07 +08:00
|
|
|
bool run(yyjson_doc *doc, uint64_t find_id, std::string_view &result) {
|
2021-01-02 14:41:15 +08:00
|
|
|
if (!doc) { return false; }
|
|
|
|
yyjson_val *root = yyjson_doc_get_root(doc);
|
2021-01-05 05:05:08 +08:00
|
|
|
if (!yyjson_is_obj(root)) { return false; }
|
2021-01-02 14:41:15 +08:00
|
|
|
yyjson_val *statuses = yyjson_obj_get(root, "statuses");
|
2021-01-11 03:52:07 +08:00
|
|
|
if (!yyjson_is_arr(statuses)) { return false; }
|
2021-01-02 14:41:15 +08:00
|
|
|
|
2021-01-06 03:41:07 +08:00
|
|
|
// Walk the document, parsing the tweets as we go
|
2021-01-02 14:41:15 +08:00
|
|
|
size_t tweet_idx, tweets_max;
|
|
|
|
yyjson_val *tweet;
|
|
|
|
yyjson_arr_foreach(statuses, tweet_idx, tweets_max, tweet) {
|
2021-01-05 05:05:08 +08:00
|
|
|
if (!yyjson_is_obj(tweet)) { return false; }
|
2021-01-02 14:41:15 +08:00
|
|
|
auto id = yyjson_obj_get(tweet, "id");
|
2021-01-05 05:05:08 +08:00
|
|
|
if (!yyjson_is_uint(id)) { return false; }
|
2021-01-05 04:21:50 +08:00
|
|
|
if (yyjson_get_uint(id) == find_id) {
|
2021-01-06 03:31:34 +08:00
|
|
|
auto text = yyjson_obj_get(tweet, "text");
|
2021-01-05 05:05:08 +08:00
|
|
|
if (yyjson_is_str(id)) { return false; }
|
2021-01-06 03:41:07 +08:00
|
|
|
result = { yyjson_get_str(text), yyjson_get_len(text) };
|
2021-01-02 14:41:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-06 03:41:07 +08:00
|
|
|
struct yyjson : yyjson_base {
|
|
|
|
bool run(simdjson::padded_string &json, uint64_t find_id, std::string_view &result) {
|
|
|
|
return yyjson_base::run(yyjson_read(json.data(), json.size(), 0), find_id, result);
|
|
|
|
}
|
|
|
|
};
|
2021-01-06 02:45:49 +08:00
|
|
|
BENCHMARK_TEMPLATE(find_tweet, yyjson)->UseManualTime();
|
2021-01-02 14:41:15 +08:00
|
|
|
|
2021-01-06 03:41:07 +08:00
|
|
|
struct yyjson_insitu : yyjson_base {
|
|
|
|
bool run(simdjson::padded_string &json, uint64_t find_id, std::string_view &result) {
|
|
|
|
return yyjson_base::run(yyjson_read_opts(json.data(), json.size(), YYJSON_READ_INSITU, 0, 0), find_id, result);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
BENCHMARK_TEMPLATE(find_tweet, yyjson_insitu)->UseManualTime();
|
|
|
|
|
2021-01-02 14:41:15 +08:00
|
|
|
} // namespace find_tweet
|
2021-01-05 04:21:50 +08:00
|
|
|
|
|
|
|
#endif // SIMDJSON_COMPETITION_YYJSON
|