BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
elasticsearch.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 oxarbitrage, and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 #include <cstddef>
26 #include <string>
27 #include <vector>
28 
29 #include <curl/curl.h>
30 
31 #include <fc/variant_object.hpp>
32 
33 namespace graphene { namespace utilities {
34 
36 {
37 public:
38  curl_wrapper();
39 
40  // Note: the numbers are used in the request() function. If we need to update or add, please check the function
42  {
43  HTTP_GET = 0,
44  HTTP_POST = 1,
45  HTTP_HEAD = 2,
46  HTTP_PUT = 3,
47  HTTP_DELETE = 4,
48  HTTP_PATCH = 5,
49  HTTP_OPTIONS = 6
50  };
51 
53  {
54  static constexpr uint16_t HTTP_200 = 200;
55  static constexpr uint16_t HTTP_401 = 401;
56  static constexpr uint16_t HTTP_413 = 413;
57  };
58 
60  {
61  uint16_t code;
62  std::string content;
63  bool is_200() const;
64  };
65 
67  const std::string& url,
68  const std::string& auth,
69  const std::string& query ) const;
70 
71  http_response get( const std::string& url, const std::string& auth ) const;
72  http_response del( const std::string& url, const std::string& auth ) const;
73  http_response post( const std::string& url, const std::string& auth, const std::string& query ) const;
74  http_response put( const std::string& url, const std::string& auth, const std::string& query ) const;
75 
76 private:
77 
78  static CURL* init_curl();
79  static curl_slist* init_request_headers();
80 
81  struct curl_deleter
82  {
83  void operator()( CURL* p_curl ) const;
84  };
85 
86  struct curl_slist_deleter
87  {
88  void operator()( curl_slist* slist ) const;
89  };
90 
91  std::unique_ptr<CURL, curl_deleter> curl { init_curl() };
92  std::unique_ptr<curl_slist, curl_slist_deleter> request_headers { init_request_headers() };
93 };
94 
95 class es_client
96 {
97 public:
98  es_client( const std::string& p_base_url, const std::string& p_auth ) : base_url(p_base_url), auth(p_auth) {}
99 
100  bool check_status() const;
101  std::string get_version() const;
102  void check_version_7_or_above( bool& result ) const noexcept;
103 
104  bool send_bulk( const std::vector<std::string>& bulk_lines ) const;
105  bool del( const std::string& path ) const;
106  std::string get( const std::string& path ) const;
107  std::string query( const std::string& path, const std::string& query ) const;
108 
110  static constexpr size_t request_size_threshold = 4 * 1024 * 1024; // 4MB
111 private:
112  std::string base_url;
113  std::string auth;
114  curl_wrapper curl;
115 };
116 
117 std::vector<std::string> createBulk(const fc::mutable_variant_object& bulk_header, std::string&& data);
118 
120 {
121  enum class data_type
122  {
124  map_type,
125  array_type // can be simple arrays, object arrays, static_variant arrays, or even nested arrays
126  };
127 
128  static fc::variant adapt( const fc::variant_object& op, uint16_t max_depth );
129 
130  static fc::variant adapt( const fc::variants& v, data_type type, uint16_t max_depth );
131 
132  static fc::variant adapt_map_item( const fc::variants& v, uint16_t max_depth );
133 
134  static fc::variant adapt_static_variant( const fc::variants& v, uint16_t max_depth );
135 
137  static void in_situ_adapt( fc::variants& v, uint16_t max_depth );
138 
140  static void extract_data_from_variant( const fc::variant& v,
142  const std::string& prefix,
143  uint16_t max_depth );
144 
145 };
146 
147 } } // end namespace graphene::utilities
graphene::utilities::es_data_adaptor::adapt
static fc::variant adapt(const fc::variant_object &op, uint16_t max_depth)
Definition: elasticsearch.cpp:264
graphene::utilities::es_client::es_client
es_client(const std::string &p_base_url, const std::string &p_auth)
Definition: elasticsearch.hpp:98
fc::variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:20
graphene::utilities::es_data_adaptor::data_type::array_type
@ array_type
graphene::utilities::es_client::check_status
bool check_status() const
Definition: elasticsearch.cpp:197
graphene::utilities::curl_wrapper::request
http_response request(http_request_method method, const std::string &url, const std::string &auth, const std::string &query) const
Definition: elasticsearch.cpp:121
fc::mutable_variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:108
graphene::utilities::createBulk
std::vector< std::string > createBulk(const fc::mutable_variant_object &bulk_header, std::string &&data)
Definition: elasticsearch.cpp:69
graphene::utilities::curl_wrapper::del
http_response del(const std::string &url, const std::string &auth) const
Definition: elasticsearch.cpp:180
graphene::utilities::es_client::del
bool del(const std::string &path) const
Definition: elasticsearch.cpp:240
graphene::utilities::curl_wrapper::http_response_code
Definition: elasticsearch.hpp:52
graphene::utilities::es_data_adaptor::data_type::map_type
@ map_type
graphene::utilities::es_client::get
std::string get(const std::string &path) const
Definition: elasticsearch.cpp:248
graphene::utilities::es_data_adaptor::data_type
data_type
Definition: elasticsearch.hpp:121
graphene::utilities::curl_wrapper
Definition: elasticsearch.hpp:35
graphene::utilities::curl_wrapper::post
http_response post(const std::string &url, const std::string &auth, const std::string &query) const
Definition: elasticsearch.cpp:185
graphene::utilities::es_client::check_version_7_or_above
void check_version_7_or_above(bool &result) const noexcept
Definition: elasticsearch.cpp:216
graphene::utilities::curl_wrapper::http_response
Definition: elasticsearch.hpp:59
variant_object.hpp
graphene::utilities::curl_wrapper::http_request_method::HTTP_PATCH
@ HTTP_PATCH
graphene::utilities::curl_wrapper::http_response::is_200
bool is_200() const
Definition: elasticsearch.cpp:80
graphene::utilities::es_data_adaptor::adapt_map_item
static fc::variant adapt_map_item(const fc::variants &v, uint16_t max_depth)
Definition: elasticsearch.cpp:431
graphene::utilities::es_client::query
std::string query(const std::string &path, const std::string &query) const
Definition: elasticsearch.cpp:256
graphene::utilities::curl_wrapper::http_response_code::HTTP_200
static constexpr uint16_t HTTP_200
Definition: elasticsearch.hpp:54
graphene::utilities::es_client::request_size_threshold
static constexpr size_t request_size_threshold
When doing bulk operations, call send_bulk when the approximate size of pending data reaches this val...
Definition: elasticsearch.hpp:110
graphene::utilities::curl_wrapper::http_request_method::HTTP_GET
@ HTTP_GET
graphene::utilities::curl_wrapper::http_response_code::HTTP_401
static constexpr uint16_t HTTP_401
Definition: elasticsearch.hpp:55
graphene::utilities::curl_wrapper::http_request_method::HTTP_DELETE
@ HTTP_DELETE
graphene::utilities::curl_wrapper::http_response_code::HTTP_413
static constexpr uint16_t HTTP_413
Definition: elasticsearch.hpp:56
fc::variants
std::vector< variant > variants
Definition: variant.hpp:170
graphene::utilities::curl_wrapper::put
http_response put(const std::string &url, const std::string &auth, const std::string &query) const
Definition: elasticsearch.cpp:191
graphene::utilities::curl_wrapper::http_response::content
std::string content
Definition: elasticsearch.hpp:62
graphene::utilities::es_data_adaptor::data_type::static_variant_type
@ static_variant_type
graphene::utilities::es_data_adaptor
Definition: elasticsearch.hpp:119
graphene::utilities::es_client::get_version
std::string get_version() const
Definition: elasticsearch.cpp:205
graphene::utilities::es_data_adaptor::adapt_static_variant
static fc::variant adapt_static_variant(const fc::variants &v, uint16_t max_depth)
Definition: elasticsearch.cpp:451
graphene::utilities::curl_wrapper::http_request_method
http_request_method
Definition: elasticsearch.hpp:41
graphene::utilities::curl_wrapper::curl_wrapper
curl_wrapper()
Definition: elasticsearch.cpp:103
graphene::utilities::curl_wrapper::get
http_response get(const std::string &url, const std::string &auth) const
Definition: elasticsearch.cpp:175
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
graphene::utilities::curl_wrapper::http_request_method::HTTP_POST
@ HTTP_POST
graphene::utilities::es_data_adaptor::extract_data_from_variant
static void extract_data_from_variant(const fc::variant &v, fc::mutable_variant_object &mv, const std::string &prefix, uint16_t max_depth)
Extract data from v into mv.
Definition: elasticsearch.cpp:412
graphene::utilities::curl_wrapper::http_response::code
uint16_t code
Definition: elasticsearch.hpp:61
graphene::utilities::es_client
Definition: elasticsearch.hpp:95
graphene::utilities::curl_wrapper::http_request_method::HTTP_PUT
@ HTTP_PUT
graphene::utilities::curl_wrapper::http_request_method::HTTP_OPTIONS
@ HTTP_OPTIONS
graphene::utilities::es_client::send_bulk
bool send_bulk(const std::vector< std::string > &bulk_lines) const
Definition: elasticsearch.cpp:232
graphene
Definition: api.cpp:48
graphene::utilities::es_data_adaptor::in_situ_adapt
static void in_situ_adapt(fc::variants &v, uint16_t max_depth)
Update directly, no return.
Definition: elasticsearch.cpp:471
graphene::utilities::curl_wrapper::http_request_method::HTTP_HEAD
@ HTTP_HEAD