BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
undo_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 <deque>
28 
29 namespace graphene { namespace db {
30 
31  class object_database;
32 
33  struct undo_state
34  {
35  std::unordered_map<object_id_type, std::unique_ptr<object> > old_values;
36  std::unordered_map<object_id_type, object_id_type> old_index_next_ids;
37  std::unordered_set<object_id_type> new_ids;
38  std::unordered_map<object_id_type, std::unique_ptr<object> > removed;
39  };
40 
41 
48  {
49  public:
50  explicit undo_database( object_database& db ):_db(db){}
51 
52  class session
53  {
54  public:
55  session( session&& mv )
56  :_db(mv._db),_apply_undo(mv._apply_undo)
57  {
58  mv._apply_undo = false;
59  }
60  ~session();
61  void commit() { _apply_undo = false; _db.commit(); }
62  void undo() { if( _apply_undo ) _db.undo(); _apply_undo = false; }
63  void merge() { if( _apply_undo ) _db.merge(); _apply_undo = false; }
64 
66  { try {
67  if( this == &mv ) return *this;
68  if( _apply_undo ) _db.undo();
69  _apply_undo = mv._apply_undo;
70  mv._apply_undo = false;
71  return *this;
73 
74  private:
75  friend class undo_database;
76 
77  explicit session(undo_database& db, bool disable_on_exit = false)
78  : _db(db),_disable_on_exit(disable_on_exit) {}
79 
80  undo_database& _db;
81  bool _apply_undo = true;
82  bool _disable_on_exit = false;
83  };
84 
85  void disable();
86  void enable();
87  bool enabled()const { return !_disabled; }
88 
89  session start_undo_session( bool force_enable = false );
93  void on_create( const object& obj );
101  void on_modify( const object& obj );
110  void on_remove( const object& obj );
111 
118  void pop_commit();
119 
120  std::size_t size()const { return _stack.size(); }
121  void set_max_size(size_t new_max_size) { _max_size = new_max_size; }
122  size_t max_size()const { return _max_size; }
123  uint32_t active_sessions()const { return _active_sessions; }
124 
125  const undo_state& head()const;
126 
127  private:
128  void undo();
129  void merge();
130  void commit();
131 
132  uint32_t _active_sessions = 0;
133  bool _disabled = true;
134  std::deque<undo_state> _stack;
135  object_database& _db;
136  size_t _max_size = 256;
137  };
138 
139 } } // graphene::db
FC_CAPTURE_AND_RETHROW
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:479
graphene::db::undo_database::size
std::size_t size() const
Definition: undo_database.hpp:120
graphene::db::undo_database
tracks changes to the state and allows changes to be undone
Definition: undo_database.hpp:47
graphene::db::undo_database::enable
void enable()
Definition: undo_database.cpp:30
graphene::db::undo_database::session::merge
void merge()
Definition: undo_database.hpp:63
graphene::db::undo_database::session::operator=
session & operator=(session &&mv)
Definition: undo_database.hpp:65
graphene::db::undo_database::head
const undo_state & head() const
Definition: undo_database.cpp:301
graphene::db::undo_database::start_undo_session
session start_undo_session(bool force_enable=false)
Definition: undo_database.cpp:46
graphene::db::undo_state::old_values
std::unordered_map< object_id_type, std::unique_ptr< object > > old_values
Definition: undo_database.hpp:35
graphene::db::undo_database::session::~session
~session()
Definition: undo_database.cpp:33
graphene::db::undo_database::session::commit
void commit()
Definition: undo_database.hpp:61
graphene::db::undo_database::max_size
size_t max_size() const
Definition: undo_database.hpp:122
graphene::db::undo_database::enabled
bool enabled() const
Definition: undo_database.hpp:87
graphene::db::undo_database::session::session
session(session &&mv)
Definition: undo_database.hpp:55
graphene::db::undo_database::active_sessions
uint32_t active_sessions() const
Definition: undo_database.hpp:123
graphene::db::undo_state::old_index_next_ids
std::unordered_map< object_id_type, object_id_type > old_index_next_ids
Definition: undo_database.hpp:36
graphene::db::undo_database::session::undo
void undo()
Definition: undo_database.hpp:62
graphene::db::undo_state::new_ids
std::unordered_set< object_id_type > new_ids
Definition: undo_database.hpp:37
graphene::db::undo_database::on_create
void on_create(const object &obj)
Definition: undo_database.cpp:60
graphene::db::object_database
maintains a set of indexed objects that can be modified with multi-level rollback support
Definition: object_database.hpp:39
graphene::db::undo_database::pop_commit
void pop_commit()
Definition: undo_database.cpp:264
graphene::db::undo_database::on_remove
void on_remove(const object &obj)
Definition: undo_database.cpp:86
graphene::db::undo_database::set_max_size
void set_max_size(size_t new_max_size)
Definition: undo_database.hpp:121
exception.hpp
Defines exception's used by fc.
graphene::db::undo_database::disable
void disable()
Definition: undo_database.cpp:31
graphene::db::undo_state::removed
std::unordered_map< object_id_type, std::unique_ptr< object > > removed
Definition: undo_database.hpp:38
graphene::db::undo_database::undo_database
undo_database(object_database &db)
Definition: undo_database.hpp:50
graphene::db::undo_state
Definition: undo_database.hpp:33
object.hpp
graphene::db::undo_database::session
Definition: undo_database.hpp:52
graphene::db::undo_database::on_modify
void on_modify(const object &obj)
Definition: undo_database.cpp:73
graphene
Definition: api.cpp:48