BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
object_database.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., 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  */
24 #pragma once
25 #include <graphene/db/object.hpp>
26 #include <graphene/db/index.hpp>
28 
29 #include <fc/log/logger.hpp>
30 
31 #include <map>
32 
33 namespace graphene { namespace db {
34 
40  {
41  public:
43  virtual ~object_database() = default;
44 
45  static constexpr uint8_t _index_size = 255;
46 
48  {
49  _index.clear();
50  _index.resize(_index_size);
51  }
52 
53  void open(const fc::path& data_dir );
54 
58  void flush();
59  void wipe(const fc::path& data_dir); // remove from disk
60  void close();
61 
62  template<typename T, typename F>
63  const T& create( F&& constructor )
64  {
65  auto& idx = get_mutable_index<T>();
66  return static_cast<const T&>( idx.create( [&](object& o)
67  {
68  assert( dynamic_cast<T*>(&o) );
69  constructor( static_cast<T&>(o) );
70  } ));
71  }
72 
76  template<typename IndexType>
77  const IndexType& get_index_type()const {
78  static_assert( std::is_base_of<index,IndexType>::value, "Type must be an index type" );
79  return static_cast<const IndexType&>( get_index( IndexType::object_type::space_id,
80  IndexType::object_type::type_id ) );
81  }
82  template<typename T>
83  const index& get_index()const { return get_index(T::space_id,T::type_id); }
84  const index& get_index(uint8_t space_id, uint8_t type_id)const;
85  const index& get_index(const object_id_type& id)const { return get_index(id.space(),id.type()); }
87 
88  const object& get_object( const object_id_type& id )const;
89  const object* find_object( const object_id_type& id )const;
90 
95 
96  const object& insert( object&& obj ) { return get_mutable_index(obj.id).insert( std::move(obj) ); }
97  void remove( const object& obj ) { get_mutable_index(obj.id).remove( obj ); }
98  template<typename T, typename Lambda>
99  void modify( const T& obj, const Lambda& m ) {
100  get_mutable_index(obj.id).modify(obj,m);
101  }
102 
104 
105  template<typename T>
106  static const T& cast( const object& obj )
107  {
108  assert( nullptr != dynamic_cast<const T*>(&obj) );
109  return static_cast<const T&>(obj);
110  }
111  template<typename T>
112  static T& cast( object& obj )
113  {
114  assert( nullptr != dynamic_cast<T*>(&obj) );
115  return static_cast<T&>(obj);
116  }
117 
118  template<typename T>
119  const T& get( const object_id_type& id )const
120  {
121  const object& obj = get_object( id );
122  assert( nullptr != dynamic_cast<const T*>(&obj) );
123  return static_cast<const T&>(obj);
124  }
125  template<typename T>
126  const T* find( const object_id_type& id )const
127  {
128  const object* obj = find_object( id );
129  assert( !obj || nullptr != dynamic_cast<const T*>(obj) );
130  return static_cast<const T*>(obj);
131  }
132 
133  template<uint8_t SpaceID, uint8_t TypeID>
134  auto find( const object_id<SpaceID,TypeID>& id )const -> const object_downcast_t<decltype(id)>* {
135  return find<object_downcast_t<decltype(id)>>(object_id_type(id));
136  }
137 
138  template<uint8_t SpaceID, uint8_t TypeID>
139  auto get( const object_id<SpaceID,TypeID>& id )const -> const object_downcast_t<decltype(id)>& {
140  return get<object_downcast_t<decltype(id)>>(object_id_type(id));
141  }
142 
143  template<typename IndexType>
144  IndexType* add_index()
145  {
146  using ObjectType = typename IndexType::object_type;
147  const auto space_id = ObjectType::space_id;
148  const auto type_id = ObjectType::type_id;
149  FC_ASSERT( space_id < _index.size(), "Space ID ${s} overflow", ("s",space_id) );
150  if( _index[space_id].size() <= type_id )
151  _index[space_id].resize( _index_size );
152  FC_ASSERT( type_id < _index[space_id].size(), "Type ID ${t} overflow", ("t",type_id) );
153  FC_ASSERT( !_index[space_id][type_id], "Index ${s}.${t} already exists", ("s",space_id)("t",type_id) );
154  _index[space_id][type_id] = std::make_unique<IndexType>(*this);
155  return static_cast<IndexType*>(_index[space_id][type_id].get());
156  }
157 
158  template<typename IndexType, typename SecondaryIndexType, typename... Args>
159  SecondaryIndexType* add_secondary_index( Args... args )
160  {
161  return get_mutable_index_type<IndexType>().template
162  add_secondary_index<SecondaryIndexType, Args...>(args...);
163  }
164 
165  void pop_undo();
166 
167  fc::path get_data_dir()const { return _data_dir; }
168 
171  protected:
172  template<typename IndexType>
173  IndexType& get_mutable_index_type() {
174  static_assert( std::is_base_of<index,IndexType>::value, "Type must be an index type" );
175  return static_cast<IndexType&>( get_mutable_index( IndexType::object_type::space_id,
176  IndexType::object_type::type_id ) );
177  }
178  template<typename T>
179  index& get_mutable_index() { return get_mutable_index(T::space_id,T::type_id); }
180  index& get_mutable_index(const object_id_type& id) { return get_mutable_index(id.space(),id.type()); }
181  index& get_mutable_index(uint8_t space_id, uint8_t type_id);
182 
183  private:
184 
185  friend class base_primary_index;
186  friend class undo_database;
187  void save_undo( const object& obj );
188  void save_undo_add( const object& obj );
189  void save_undo_remove( const object& obj );
190 
191  fc::path _data_dir;
192  std::vector< std::vector< std::unique_ptr<index> > > _index;
193  };
194 
195 } } // graphene::db
196 
197 
graphene::db::object_database::find_object
const object * find_object(const object_id_type &id) const
Definition: object_database.cpp:43
graphene::db::object_database::add_secondary_index
SecondaryIndexType * add_secondary_index(Args... args)
Definition: object_database.hpp:159
graphene::db::object::id
object_id_type id
Definition: object.hpp:69
graphene::db::undo_database
tracks changes to the state and allows changes to be undone
Definition: undo_database.hpp:47
graphene::db::object_downcast_t
typename object_downcast< ObjectID >::type object_downcast_t
Definition: object_id.hpp:100
index.hpp
graphene::db::object_database::get
const T & get(const object_id_type &id) const
Definition: object_database.hpp:119
graphene::db::object_database::insert
const object & insert(object &&obj)
Definition: object_database.hpp:96
graphene::db::object_database::flush
void flush()
Definition: object_database.cpp:81
graphene::db::object_database::cast
static const T & cast(const object &obj)
Definition: object_database.hpp:106
graphene::db::object_database::open
void open(const fc::path &data_dir)
Definition: object_database.cpp:130
graphene::db::object_database::create
const T & create(F &&constructor)
Definition: object_database.hpp:63
graphene::db::object_database::close
void close()
Definition: object_database.cpp:39
graphene::db::object_database::get_mutable_index_type
IndexType & get_mutable_index_type()
Definition: object_database.hpp:173
graphene::db::object_database::get_mutable_index
index & get_mutable_index(const object_id_type &id)
Definition: object_database.hpp:180
graphene::db::base_primary_index
Definition: index.hpp:152
graphene::db::object_database::get_mutable_index
index & get_mutable_index()
Definition: object_database.hpp:179
graphene::db::object_database::find
const T * find(const object_id_type &id) const
Definition: object_database.hpp:126
graphene::db::object_database::get
auto get(const object_id< SpaceID, TypeID > &id) const -> const object_downcast_t< decltype(id)> &
Definition: object_database.hpp:139
graphene::db::object_database::_index_size
static constexpr uint8_t _index_size
Definition: object_database.hpp:45
fc::path
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
graphene::db::object_database::add_index
IndexType * add_index()
Definition: object_database.hpp:144
graphene::db::object_database::get_object
const object & get_object(const object_id_type &id) const
Definition: object_database.cpp:47
graphene::db::object_database::get_data_dir
fc::path get_data_dir() const
Definition: object_database.hpp:167
graphene::db::index::insert
virtual const object & insert(object &&obj)=0
graphene::db::object_database::get_index
const index & get_index(const object_id_type &id) const
Definition: object_database.hpp:85
graphene::db::object_database
maintains a set of indexed objects that can be modified with multi-level rollback support
Definition: object_database.hpp:39
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::db::object_id_type
Definition: object_id.hpp:30
graphene::db::object_database::object_database
object_database()
Definition: object_database.cpp:32
graphene::db::object_id
Definition: object_id.hpp:103
logger.hpp
graphene::db::index::remove
virtual void remove(const object &obj)=0
graphene::db::object_database::cast
static T & cast(object &obj)
Definition: object_database.hpp:112
graphene::db::object_database::get_index
const index & get_index() const
Definition: object_database.hpp:83
graphene::db::index::modify
virtual void modify(const object &obj, const std::function< void(object &)> &)=0
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
object.hpp
undo_database.hpp
graphene::db::index
abstract base class for accessing objects indexed in various ways.
Definition: index.hpp:70
graphene::db::object_database::pop_undo
void pop_undo()
Definition: object_database.cpp:163
graphene
Definition: api.cpp:48
graphene::db::object_database::wipe
void wipe(const fc::path &data_dir)
Definition: object_database.cpp:122
graphene::db::object_database::~object_database
virtual ~object_database()=default
graphene::db::object_database::find
auto find(const object_id< SpaceID, TypeID > &id) const -> const object_downcast_t< decltype(id)> *
Definition: object_database.hpp:134
graphene::db::object_database::modify
void modify(const T &obj, const Lambda &m)
Definition: object_database.hpp:99
graphene::db::object_database::reset_indexes
void reset_indexes()
Definition: object_database.hpp:47
graphene::db::object_database::_undo_db
undo_database _undo_db
Definition: object_database.hpp:170