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