BitShares-Core  6.1.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  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 
65  session& operator = ( session&& mv )
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
Definition: api.cpp:48
std::unordered_map< object_id_type, object_id_type > old_index_next_ids
std::unordered_set< object_id_type > new_ids
void set_max_size(size_t new_max_size)
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:479
Defines exception&#39;s used by fc.
undo_database(object_database &db)
uint32_t active_sessions() const
std::unordered_map< object_id_type, std::unique_ptr< object > > old_values
std::unordered_map< object_id_type, std::unique_ptr< object > > removed
std::size_t size() const