File: yyjson.h

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (69 lines) | stat: -rw-r--r-- 2,236 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once

#ifdef SIMDJSON_COMPETITION_YYJSON

#include "distinct_user_id.h"

namespace distinct_user_id {

struct yyjson_base {
  bool run(yyjson_doc *doc, std::vector<uint64_t> &result) {
    if (!doc) { return false; }
    yyjson_val *root = yyjson_doc_get_root(doc);
    if (!yyjson_is_obj(root)) { return false; }
    yyjson_val *statuses = yyjson_obj_get(root, "statuses");
    if (!yyjson_is_arr(statuses)) { return false; }

    // Walk the document, parsing the tweets as we go
    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");
      if (!yyjson_is_obj(user)) { return false; }
      auto id = yyjson_obj_get(user, "id");
      if (!yyjson_is_uint(id)) { return false; }
      result.push_back(yyjson_get_uint(id));

      // 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) {
        if (!yyjson_is_obj(retweet)) { return false; }
        user = yyjson_obj_get(retweet, "user");
        if (!yyjson_is_obj(user)) { return false; }
        id = yyjson_obj_get(user, "id");
        if (!yyjson_is_uint(id)) { return false; }
        result.push_back(yyjson_get_sint(id));
      }
    }

    return true;
  }

};

struct yyjson : yyjson_base {
  bool run(simdjson::padded_string &json, std::vector<uint64_t> &result) {
    yyjson_doc *doc = yyjson_read(json.data(), json.size(), 0);
    bool b = yyjson_base::run(doc, result);
    yyjson_doc_free(doc);
    return b;
  }
};
BENCHMARK_TEMPLATE(distinct_user_id, yyjson)->UseManualTime();

#if SIMDJSON_COMPETITION_ONDEMAND_INSITU
struct yyjson_insitu : yyjson_base {
  bool run(simdjson::padded_string &json, std::vector<uint64_t> &result) {
    yyjson_doc *doc = yyjson_read_opts(json.data(), json.size(), YYJSON_READ_INSITU, 0, 0);
    bool b = yyjson_base::run(doc, result);
    yyjson_doc_free(doc);
    return b;
  }
};
BENCHMARK_TEMPLATE(distinct_user_id, yyjson_insitu)->UseManualTime();
#endif // SIMDJSON_COMPETITION_ONDEMAND_INSITU

} // namespace distinct_user_id

#endif // SIMDJSON_COMPETITION_YYJSON