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 
31 // DatabaseBody needs to meet requirements
32 // from asio which is why some conventions
33 // used elsewhere in this code base are not
34 // followed.
36 {
37  // Algorithm for storing buffers when parsing.
38  class reader;
39 
40  // The type of the @ref message::body member.
41  class value_type;
42 
47  static std::uint64_t
48  size(value_type const& body);
49 };
50 
52 {
53  // This body container holds a connection to the
54  // database, and also caches the size when set.
55 
56  friend class reader;
57  friend struct DatabaseBody;
58 
59  // The cached file size
61  boost::filesystem::path path_;
69  bool closing_ = false;
70 
71 public:
73  ~value_type() = default;
74 
76  value_type() = default;
77 
79  bool
80  is_open() const
81  {
82  return static_cast<bool>(conn_);
83  }
84 
87  size() const
88  {
89  return fileSize_;
90  }
91 
93  void
94  close();
95 
106  void
107  open(
108  boost::filesystem::path const& path,
109  Config const& config,
110  boost::asio::io_service& io_service,
111  boost::system::error_code& ec);
112 };
113 
120 {
121  value_type& body_; // The body we are writing to
122 
123  static constexpr std::uint32_t FLUSH_SIZE = 50000000;
124  static constexpr std::uint8_t MAX_HANDLERS = 3;
125  static constexpr std::uint16_t MAX_ROW_SIZE_PAD = 500;
126 
127 public:
128  // Constructor.
129  //
130  // This is called after the header is parsed and
131  // indicates that a non-zero sized body may be present.
132  // `h` holds the received message headers.
133  // `b` is an instance of `DatabaseBody`.
134  //
135  template <bool isRequest, class Fields>
136  explicit reader(
137  boost::beast::http::header<isRequest, Fields>& h,
138  value_type& b);
139 
140  // Initializer
141  //
142  // This is called before the body is parsed and
143  // gives the reader a chance to do something that might
144  // need to return an error code. It informs us of
145  // the payload size (`content_length`) which we can
146  // optionally use for optimization.
147  //
148  // Note: boost::Beast calls init() and requires a
149  // boost::optional (not a std::optional) as the
150  // parameter.
151  void
152  init(boost::optional<std::uint64_t> const&, boost::system::error_code& ec);
153 
154  // This function is called one or more times to store
155  // buffer sequences corresponding to the incoming body.
156  //
157  template <class ConstBufferSequence>
159  put(ConstBufferSequence const& buffers, boost::system::error_code& ec);
160 
161  void
162  do_put(std::string const& data);
163 
164  // This function is called when writing is complete.
165  // It is an opportunity to perform any final actions
166  // which might fail, in order to return an error code.
167  // Operations that might fail should not be attempted in
168  // destructors, since an exception thrown from there
169  // would terminate the program.
170  //
171  void
172  finish(boost::system::error_code& ec);
173 };
174 
175 } // namespace ripple
176 
177 #include <ripple/net/impl/DatabaseBody.ipp>
178 
179 #endif // RIPPLE_NET_DATABASEBODY_H
ripple::DatabaseBody::reader::do_put
void do_put(std::string const &data)
ripple::DatabaseBody::reader::MAX_HANDLERS
static constexpr std::uint8_t MAX_HANDLERS
Definition: DatabaseBody.h:124
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::value_type
Definition: DatabaseBody.h:51
ripple::DatabaseBody::value_type::size
std::uint64_t size() const
Returns the size of the file if open.
Definition: DatabaseBody.h:87
ripple::DatabaseBody::value_type::strand_
std::shared_ptr< boost::asio::io_service::strand > strand_
Definition: DatabaseBody.h:64
ripple::DatabaseBody::value_type::m_
std::mutex m_
Definition: DatabaseBody.h:65
ripple::DatabaseBody::reader::reader
reader(boost::beast::http::header< isRequest, Fields > &h, value_type &b)
ripple::DatabaseBody::value_type::fileSize_
std::uint64_t fileSize_
Definition: DatabaseBody.h:60
ripple::DatabaseBody::value_type::open
void open(boost::filesystem::path const &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::value_type::~value_type
~value_type()=default
Destructor.
ripple::Config
Definition: Config.h:89
ripple::DatabaseBody::reader::FLUSH_SIZE
static constexpr std::uint32_t FLUSH_SIZE
Definition: DatabaseBody.h:123
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:35
ripple::DatabaseBody::value_type::conn_
std::unique_ptr< DatabaseCon > conn_
Definition: DatabaseBody.h:62
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::DatabaseBody::value_type::closing_
bool closing_
Definition: DatabaseBody.h:69
ripple::DatabaseBody::value_type::c_
std::condition_variable c_
Definition: DatabaseBody.h:66
std::condition_variable
ripple::DatabaseBody::reader::body_
value_type & body_
Definition: DatabaseBody.h:121
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::reader::MAX_ROW_SIZE_PAD
static constexpr std::uint16_t MAX_ROW_SIZE_PAD
Definition: DatabaseBody.h:125
ripple::DatabaseBody::value_type::path_
boost::filesystem::path path_
Definition: DatabaseBody.h:61
ripple::DatabaseBody::reader
Algorithm for storing buffers when parsing.
Definition: DatabaseBody.h:119
ripple::DatabaseBody::value_type::part_
std::uint64_t part_
Definition: DatabaseBody.h:68
ripple::DatabaseBody::value_type::handlerCount_
std::uint64_t handlerCount_
Definition: DatabaseBody.h:67
ripple::DatabaseBody::value_type::batch_
std::string batch_
Definition: DatabaseBody.h:63
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:80