BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
custom_evaluators.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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  */
25 
29 
30 namespace graphene { namespace custom_operations {
31 
33 {
34  _db = &db;
35  _account = account;
36 }
37 
39 {
40  const auto &index = _db->get_index_type<account_storage_index>().indices().get<by_account_catalog_key>();
41  vector<object_id_type> results;
42  results.reserve( op.key_values.size() );
43 
44  if (op.remove)
45  {
46  for(auto const& row: op.key_values) {
47  auto itr = index.find(make_tuple(_account, op.catalog, row.first));
48  if(itr != index.end()) {
49  results.push_back(itr->id);
50  _db->remove(*itr);
51  }
52  }
53  }
54  else {
55  for(auto const& row: op.key_values) {
56  if(row.first.length() > CUSTOM_OPERATIONS_MAX_KEY_SIZE)
57  {
58  wlog("Key can't be bigger than ${max} characters", ("max", CUSTOM_OPERATIONS_MAX_KEY_SIZE));
59  continue;
60  }
61  auto itr = index.find(make_tuple(_account, op.catalog, row.first));
62  if(itr == index.end())
63  {
64  try {
65  const auto& created = _db->create<account_storage_object>(
66  [&op, this, &row]( account_storage_object& aso ) {
67  aso.account = _account;
68  aso.catalog = op.catalog;
69  aso.key = row.first;
70  if(row.second.valid())
71  aso.value = fc::json::from_string(*row.second);
72  });
73  results.push_back(created.id);
74  }
75  catch(const fc::parse_error_exception& e) { wlog(e.to_detail_string()); }
76  }
77  else
78  {
79  try {
80  _db->modify(*itr, [&op, this, &row](account_storage_object &aso) {
81  if(row.second.valid())
82  aso.value = fc::json::from_string(*row.second);
83  else
84  aso.value.reset();
85  });
86  results.push_back(itr->id);
87  }
88  catch(const fc::parse_error_exception& e) { wlog((e.to_detail_string())); }
89  }
90  }
91  }
92  return results;
93 }
94 
95 } }
vector< object_id_type > do_apply(const account_storage_map &o)
void modify(const T &obj, const Lambda &m)
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
Definition: api.cpp:56
custom_generic_evaluator(database &db, const account_id_type account)
#define wlog(FORMAT,...)
Definition: logger.hpp:123
virtual const object * find(object_id_type id) const =0
void reset()
Definition: optional.hpp:224
flat_map< string, optional< string > > key_values
#define CUSTOM_OPERATIONS_MAX_KEY_SIZE
const object & get(object_id_type id) const
Definition: index.hpp:111
static variant from_string(const string &utf8_str, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:458
abstract base class for accessing objects indexed in various ways.
Definition: index.hpp:71
void remove(const object &obj)
const T & create(F &&constructor)
const IndexType & get_index_type() const