Scorum
service_base.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 
7 
8 #include <limits>
9 
10 namespace scorum {
11 namespace chain {
12 
13 template <class T> struct base_service_i
14 {
15  using object_type = T;
16  using modifier_type = std::function<void(object_type&)>;
17  using call_type = std::function<void(const object_type&)>;
18  using object_cref_type = std::reference_wrapper<const object_type>;
19 
20  virtual ~base_service_i()
21  {
22  }
23 
24  virtual const object_type& create(const modifier_type& modifier) = 0;
25 
26  virtual void update(const modifier_type& modifier) = 0;
27 
28  virtual void update(const object_type& o, const modifier_type& modifier) = 0;
29 
30  virtual void remove() = 0;
31 
32  virtual void remove(const object_type& o) = 0;
33 
34  virtual void remove_all(const std::vector<object_cref_type>& os) = 0;
35 
36  virtual bool is_exists() const = 0;
37 
38  virtual const object_type& get() const = 0;
39 };
40 
41 template <class service_interface> class dbs_service_base : public dbs_base, public service_interface
42 {
43  friend class dbservice_dbs_factory;
44 
45 public:
47  : _base_type(db)
48  {
49  }
50 
52 
53  using service_interface::get;
54  using service_interface::is_exists;
55 
58  using object_cref_type = typename service_interface::object_cref_type;
59 
60  virtual const object_type& create(const modifier_type& modifier) override
61  {
62  return db_impl().template create<object_type>([&](object_type& o) { modifier(o); });
63  }
64 
65  virtual void update(const modifier_type& modifier) override
66  {
67  db_impl().modify(get(), [&](object_type& o) { modifier(o); });
68  }
69 
70  virtual void update(const object_type& o, const modifier_type& modifier) override
71  {
72  db_impl().modify(o, [&](object_type& c) { modifier(c); });
73  }
74 
75  virtual void remove() override
76  {
77  db_impl().remove(get());
78  }
79 
80  virtual void remove(const object_type& o) override
81  {
82  db_impl().remove(o);
83  }
84 
85  virtual void remove_all(const std::vector<object_cref_type>& os) override
86  {
87  try
88  {
89  for (const auto& o : os)
90  {
91  remove(o);
92  }
93  }
94  FC_CAPTURE_AND_RETHROW()
95  }
96 
97  virtual bool is_exists() const override
98  {
99  return nullptr != db_impl().template find<object_type>();
100  }
101 
102  virtual const object_type& get() const override
103  {
104  try
105  {
106  return db_impl().template get<object_type>();
107  }
108  FC_CAPTURE_AND_RETHROW()
109  }
110 
111  template <class... IndexBy, class Key> const object_type& get_by(const Key& arg) const
112  {
113  try
114  {
115  return db_impl().template get<object_type, IndexBy...>(arg);
116  }
117  FC_CAPTURE_AND_RETHROW()
118  }
119 
120  template <class... IndexBy, class Key> const object_type* find_by(const Key& arg) const
121  {
122  try
123  {
124  return db_impl().template find<object_type, IndexBy...>(arg);
125  }
126  FC_CAPTURE_AND_RETHROW()
127  }
128 
129  template <class... IndexBy, typename Call> void foreach_by(Call&& call) const
130  {
131  const auto& idx = db_impl()
132  .template get_index<typename chainbase::get_index_type<object_type>::type>()
133  .indices()
134  .template get<IndexBy...>();
135  for (auto it = idx.cbegin(); it != idx.cend(); ++it)
136  {
137  call(*it);
138  }
139  }
140 
141  template <class... IndexBy, class LowerBounder, class UpperBounder>
142  std::vector<object_cref_type> get_range_by(LowerBounder lower, UpperBounder upper) const
143  {
144  try
145  {
146  std::vector<object_cref_type> ret;
147 
148  const auto& idx = db_impl()
149  .template get_index<typename chainbase::get_index_type<object_type>::type>()
150  .indices()
151  .template get<IndexBy...>();
152 
153  auto range = idx.range(lower, upper);
154 
155  std::copy(range.first, range.second, std::back_inserter(ret));
156 
157  return ret;
158  }
159  FC_CAPTURE_AND_RETHROW()
160  }
161 
162  template <class... IndexBy, class LowerBounder, class UpperBounder, class UnaryPredicate>
163  std::vector<object_cref_type>
164  get_filtered_range_by(LowerBounder lower, UpperBounder upper, UnaryPredicate filter) const
165  {
166  try
167  {
168  std::vector<object_cref_type> ret;
169 
170  const auto& idx = db_impl()
171  .template get_index<typename chainbase::get_index_type<object_type>::type>()
172  .indices()
173  .template get<IndexBy...>();
174 
175  auto range = idx.range(lower, upper);
176 
177  std::copy_if(range.first, range.second, std::back_inserter(ret), filter);
178 
179  return ret;
180  }
181  FC_CAPTURE_AND_RETHROW()
182  }
183 };
184 
185 #define ALL_IDS std::numeric_limits<int64_t>::max()
186 
187 } // namespace chain
188 } // namespace scorum
dba::db_index & db_impl()
Definition: dbs_base.cpp:22
virtual void remove(const object_type &o) override
virtual const object_type & create(const modifier_type &modifier) override
typename service_interface::modifier_type modifier_type
virtual void update(const modifier_type &modifier) override
virtual void remove_all(const std::vector< object_cref_type > &os) override
typename service_interface::object_cref_type object_cref_type
virtual void update(const object_type &o, const modifier_type &modifier) override
virtual const object_type & get() const override
const object_type * find_by(const Key &arg) const
virtual void remove() override
std::vector< object_cref_type > get_filtered_range_by(LowerBounder lower, UpperBounder upper, UnaryPredicate filter) const
dbs_service_base(dba::db_index &db)
virtual bool is_exists() const override
void foreach_by(Call &&call) const
const object_type & get_by(const Key &arg) const
std::vector< object_cref_type > get_range_by(LowerBounder lower, UpperBounder upper) const
typename service_interface::object_type object_type
utils::function_view< void(TObject &)> modifier_type
Definition: db_accessor.hpp:19
Definition: asset.cpp:15
virtual bool is_exists() const =0
virtual void update(const modifier_type &modifier)=0
std::function< void(object_type &)> modifier_type
virtual void update(const object_type &o, const modifier_type &modifier)=0
virtual const object_type & get() const =0
std::reference_wrapper< const object_type > object_cref_type
virtual const object_type & create(const modifier_type &modifier)=0
virtual void remove_all(const std::vector< object_cref_type > &os)=0
std::function< void(const object_type &)> call_type
virtual void remove(const object_type &o)=0