Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Collection.hpp
1#pragma once
2
3#include "data/cassandra/impl/ManagedObject.hpp"
4
5#include <cassandra.h>
6#include <xrpl/basics/base_uint.h>
7
8#include <cstdint>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace data::cassandra::impl {
15
16class Collection : public ManagedObject<CassCollection> {
17 static constexpr auto kDELETER = [](CassCollection* ptr) { cass_collection_free(ptr); };
18
19 static void
20 throwErrorIfNeeded(CassError const rc, std::string_view const label)
21 {
22 if (rc == CASS_OK)
23 return;
24 auto const tag = '[' + std::string{label} + ']';
25 throw std::logic_error(tag + ": " + cass_error_desc(rc));
26 }
27
28public:
29 /* implicit */ Collection(CassCollection* ptr);
30
31 template <typename Type>
32 explicit Collection(std::vector<Type> const& value)
33 : ManagedObject{cass_collection_new(CASS_COLLECTION_TYPE_LIST, value.size()), kDELETER}
34 {
35 bind(value);
36 }
37
38 template <typename Type>
39 void
40 bind(std::vector<Type> const& values) const
41 {
42 for (auto const& value : values)
43 append(value);
44 }
45
46 void
47 append(bool const value) const
48 {
49 auto const rc = cass_collection_append_bool(*this, value ? cass_true : cass_false);
50 throwErrorIfNeeded(rc, "Bind bool");
51 }
52
53 void
54 append(int64_t const value) const
55 {
56 auto const rc = cass_collection_append_int64(*this, value);
57 throwErrorIfNeeded(rc, "Bind int64");
58 }
59
60 void
61 append(ripple::uint256 const& value) const
62 {
63 auto const rc = cass_collection_append_bytes(
64 *this,
65 static_cast<cass_byte_t const*>(static_cast<unsigned char const*>(value.data())),
66 ripple::uint256::size()
67 );
68 throwErrorIfNeeded(rc, "Bind ripple::uint256");
69 }
70};
71} // namespace data::cassandra::impl