simdjson/benchmark/distinct_user_id/yyjson.h

51 lines
1.6 KiB
C
Raw Normal View History

2021-01-02 14:41:15 +08:00
#pragma once
#ifdef SIMDJSON_COMPETITION_YYJSON
2021-01-02 14:41:15 +08:00
#include "distinct_user_id.h"
2021-01-02 14:41:15 +08:00
namespace distinct_user_id {
2021-01-02 14:41:15 +08:00
struct yyjson {
bool run(const simdjson::padded_string &json, std::vector<uint64_t> &ids) {
2021-01-02 14:41:15 +08:00
// Walk the document, parsing the tweets as we go
yyjson_doc *doc = yyjson_read(json.data(), json.size(), 0);
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-05 05:05:08 +08:00
if (!yyjson_is_arr(statuses)) { return "Statuses is not an array!"; }
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) {
auto user = yyjson_obj_get(tweet, "user");
2021-01-05 05:05:08 +08:00
if (!yyjson_is_obj(user)) { return false; }
2021-01-02 14:41:15 +08:00
auto id = yyjson_obj_get(user, "id");
2021-01-05 05:05:08 +08:00
if (!yyjson_is_uint(id)) { return false; }
ids.push_back(yyjson_get_uint(id));
2021-01-02 14:41:15 +08:00
// Not all tweets have a "retweeted_status", but when they do
// we want to go and find the user within.
auto retweet = yyjson_obj_get(tweet, "retweeted_status");
if (retweet) {
2021-01-05 05:05:08 +08:00
if (!yyjson_is_obj(retweet)) { return false; }
2021-01-02 14:41:15 +08:00
user = yyjson_obj_get(retweet, "user");
2021-01-05 05:05:08 +08:00
if (!yyjson_is_obj(user)) { return false; }
2021-01-02 14:41:15 +08:00
id = yyjson_obj_get(user, "id");
2021-01-05 05:05:08 +08:00
if (!yyjson_is_uint(id)) { return false; }
2021-01-02 14:41:15 +08:00
ids.push_back(yyjson_get_sint(id));
}
}
2021-01-02 14:41:15 +08:00
return true;
}
};
BENCHMARK_TEMPLATE(distinct_user_id, yyjson);
} // namespace distinct_user_id
2021-01-02 14:41:15 +08:00
#endif // SIMDJSON_COMPETITION_YYJSON