rippled
DatabaseBody.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_NET_DATABASEBODY_H
21 #define RIPPLE_NET_DATABASEBODY_H
22 
23 #include <ripple/core/DatabaseCon.h>
24 #include <boost/asio/io_service.hpp>
25 #include <boost/asio/spawn.hpp>
26 #include <boost/beast/http/message.hpp>
27 #include <soci/sqlite3/soci-sqlite3.h>
28 
29 namespace ripple {
30 
32 {
33  // Algorithm for storing buffers when parsing.
34  class reader;
35 
36  // The type of the @ref message::body member.
37  class value_type;
38 
43  static std::uint64_t
44  size(value_type const& body);
45 };
46 
48 {
49  // This body container holds a connection to the
50  // database, and also caches the size when set.
51 
52  friend class reader;
53  friend struct DatabaseBody;
54 
55  // The cached file size
57  boost::filesystem::path path_;
63  uint64_t handler_count_ = 0;
64  uint64_t part_ = 0;
65  bool closing_ = false;
66 
67 public:
69  ~value_type() = default;
70 
72  value_type() = default;
73 
75  bool
76  is_open() const
77  {
78  return bool{conn_};
79  }
80 
83  size() const
84  {
85  return file_size_;
86  }
87 
89  void
90  close();
91 
100  void
101  open(
102  boost::filesystem::path path,
103  Config const& config,
104  boost::asio::io_service& io_service,
105  boost::system::error_code& ec);
106 };
107 
114 {
115  value_type& body_; // The body we are writing to
116 
117  static const uint32_t FLUSH_SIZE = 50000000;
118  static const uint8_t MAX_HANDLERS = 3;
119  static const uint16_t MAX_ROW_SIZE_PAD = 500;
120 
121 public:
122  // Constructor.
123  //
124  // This is called after the header is parsed and
125  // indicates that a non-zero sized body may be present.
126  // `h` holds the received message headers.
127  // `b` is an instance of `DatabaseBody`.
128  //
129  template <bool isRequest, class Fields>
130  explicit reader(
131  boost::beast::http::header<isRequest, Fields>& h,
132  value_type& b);
133 
134  // Initializer
135  //
136  // This is called before the body is parsed and
137  // gives the reader a chance to do something that might
138  // need to return an error code. It informs us of
139  // the payload size (`content_length`) which we can
140  // optionally use for optimization.
141  //
142  void
143  init(boost::optional<std::uint64_t> const&, boost::system::error_code& ec);
144 
145  // This function is called one or more times to store
146  // buffer sequences corresponding to the incoming body.
147  //
148  template <class ConstBufferSequence>
150  put(ConstBufferSequence const& buffers, boost::system::error_code& ec);
151 
152  void
153  do_put(std::string data);
154 
155  // This function is called when writing is complete.
156  // It is an opportunity to perform any final actions
157  // which might fail, in order to return an error code.
158  // Operations that might fail should not be attempted in
159  // destructors, since an exception thrown from there
160  // would terminate the program.
161  //
162  void
163  finish(boost::system::error_code& ec);
164 };
165 
166 } // namespace ripple
167 
168 #include <ripple/net/impl/DatabaseBody.ipp>
169 
170 #endif // RIPPLE_NET_DATABASEBODY_H
ripple::DatabaseBody::value_type::open
void open(boost::filesystem::path path, Config const &config, boost::asio::io_service &io_service, boost::system::error_code &ec)
Open a file at the given path with the specified mode.
ripple::DatabaseBody::reader::init
void init(boost::optional< std::uint64_t > const &, boost::system::error_code &ec)
std::string
STL class.
std::shared_ptr< boost::asio::io_service::strand >
ripple::DatabaseBody::reader::MAX_ROW_SIZE_PAD
static const uint16_t MAX_ROW_SIZE_PAD
Definition: DatabaseBody.h:119
ripple::DatabaseBody::reader::do_put
void do_put(std::string data)
ripple::DatabaseBody::value_type
Definition: DatabaseBody.h:47
ripple::DatabaseBody::value_type::part_
uint64_t part_
Definition: DatabaseBody.h:64
ripple::DatabaseBody::value_type::size
std::uint64_t size() const
Returns the size of the file if open.
Definition: DatabaseBody.h:83
ripple::DatabaseBody::value_type::strand_
std::shared_ptr< boost::asio::io_service::strand > strand_
Definition: DatabaseBody.h:60
ripple::DatabaseBody::reader::FLUSH_SIZE
static const uint32_t FLUSH_SIZE
Definition: DatabaseBody.h:117
ripple::DatabaseBody::value_type::handler_count_
uint64_t handler_count_
Definition: DatabaseBody.h:63
ripple::DatabaseBody::value_type::m_
std::mutex m_
Definition: DatabaseBody.h:61
ripple::DatabaseBody::reader::reader
reader(boost::beast::http::header< isRequest, Fields > &h, value_type &b)
ripple::DatabaseBody::value_type::~value_type
~value_type()=default
Destructor.
ripple::Config
Definition: Config.h:66
ripple::DatabaseBody::reader::put
std::size_t put(ConstBufferSequence const &buffers, boost::system::error_code &ec)
ripple::DatabaseBody::value_type::close
void close()
Close the file if open.
ripple::DatabaseBody::value_type::value_type
value_type()=default
Constructor.
std::uint64_t
ripple::DatabaseBody
Definition: DatabaseBody.h:31
ripple::DatabaseBody::value_type::conn_
std::unique_ptr< DatabaseCon > conn_
Definition: DatabaseBody.h:58
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::DatabaseBody::reader::MAX_HANDLERS
static const uint8_t MAX_HANDLERS
Definition: DatabaseBody.h:118
ripple::DatabaseBody::value_type::closing_
bool closing_
Definition: DatabaseBody.h:65
ripple::DatabaseBody::value_type::c_
std::condition_variable c_
Definition: DatabaseBody.h:62
ripple::DatabaseBody::value_type::file_size_
std::uint64_t file_size_
Definition: DatabaseBody.h:56
std::condition_variable
ripple::DatabaseBody::reader::body_
value_type & body_
Definition: DatabaseBody.h:115
ripple::DatabaseBody::size
static std::uint64_t size(value_type const &body)
Returns the size of the body.
std::mutex
STL class.
std::size_t
ripple::DatabaseBody::value_type::path_
boost::filesystem::path path_
Definition: DatabaseBody.h:57
ripple::DatabaseBody::reader
Algorithm for storing buffers when parsing.
Definition: DatabaseBody.h:113
ripple::DatabaseBody::value_type::batch_
std::string batch_
Definition: DatabaseBody.h:59
std::unique_ptr
STL class.
ripple::DatabaseBody::reader::finish
void finish(boost::system::error_code &ec)
ripple::DatabaseBody::value_type::is_open
bool is_open() const
Returns true if the file is open.
Definition: DatabaseBody.h:76