BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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, [&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 } }
fc::optional::reset
void reset()
Definition: optional.hpp:224
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
wlog
#define wlog(FORMAT,...)
Definition: logger.hpp:123
database.hpp
graphene::custom_operations::account_storage_object::value
optional< variant > value
Definition: custom_objects.hpp:47
custom_evaluators.hpp
fc::json::from_string
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
graphene::db::object_database::create
const T & create(F &&constructor)
Definition: object_database.hpp:63
graphene::custom_operations::CUSTOM_OPERATIONS_MAX_KEY_SIZE
constexpr uint16_t CUSTOM_OPERATIONS_MAX_KEY_SIZE
Definition: custom_objects.hpp:35
graphene::custom_operations::custom_generic_evaluator::_account
account_id_type _account
Definition: custom_evaluators.hpp:34
graphene::custom_operations::account_storage_map
Definition: custom_operations.hpp:35
graphene::custom_operations::custom_generic_evaluator::custom_generic_evaluator
custom_generic_evaluator(database &db, const account_id_type account)
Definition: custom_evaluators.cpp:32
graphene::custom_operations::account_storage_object
Definition: custom_objects.hpp:41
graphene::db::index::get
const object & get(object_id_type id) const
Definition: index.hpp:110
graphene::db::index::find
virtual const object * find(object_id_type id) const =0
graphene::custom_operations::account_storage_object::catalog
string catalog
Definition: custom_objects.hpp:45
graphene::custom_operations::custom_generic_evaluator::_db
database * _db
Definition: custom_evaluators.hpp:33
graphene::custom_operations::custom_generic_evaluator::do_apply
vector< object_id_type > do_apply(const account_storage_map &o)
Definition: custom_evaluators.cpp:38
custom_operations_plugin.hpp
graphene::custom_operations::account_storage_map::key_values
flat_map< string, optional< string > > key_values
Definition: custom_operations.hpp:39
graphene::db::generic_index
Definition: generic_index.hpp:43
graphene::custom_operations::account_storage_map::catalog
string catalog
Definition: custom_operations.hpp:38
graphene::custom_operations::account_storage_map::remove
bool remove
Definition: custom_operations.hpp:37
custom_objects.hpp
graphene::db::object_database::remove
void remove(const object &obj)
Definition: object_database.hpp:97
graphene::db::object_database::get_index_type
const IndexType & get_index_type() const
Definition: object_database.hpp:77
graphene::db::index
abstract base class for accessing objects indexed in various ways.
Definition: index.hpp:70
graphene
Definition: api.cpp:48
graphene::db::object_database::modify
void modify(const T &obj, const Lambda &m)
Definition: object_database.hpp:99