SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
CommitLog.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_ranger_db_CommitLog_h
8 #define swcdb_ranger_db_CommitLog_h
9 
10 
12 
13 
14 
15 namespace SWC { namespace Ranger { namespace CommitLog {
16 
17 class Compact;
18 
19 class Fragments final : private Core::Vector<Fragment::Ptr> {
20 
21  /* file-format(dir-structure):
22  ../log/{N}.frag
23  */
24 
25  public:
26 
28  typedef Fragments* Ptr;
30 
33 
34  explicit Fragments(const DB::Types::KeySeq key_seq);
35 
36  void init(const RangePtr& for_range);
37 
38  Fragments(const Fragments&) = delete;
39 
40  Fragments(const Fragments&&) = delete;
41 
42  Fragments& operator=(const Fragments&) = delete;
43 
44  ~Fragments() noexcept;
45 
46  void schema_update();
47 
49  int64_t modification_ts() const noexcept {
50  return m_modification_ts.load();
51  }
52 
53  void add(const DB::Cells::Cell& cell);
54 
56  std::shared_mutex& cells_mutex() noexcept {
57  return m_mutex_cells;
58  }
59 
61  void _add(const DB::Cells::Cell& cell,
62  size_t* offset_itp, size_t* offsetp) {
63  m_cells.add_raw(cell, offset_itp, offsetp, false);
64  }
65 
67  void check_sequence(const char* msg, bool w_assert=true) {
69  m_cells.check_sequence(msg, w_assert);
70  }
71 
72  void commit() noexcept;
73 
74  size_t commit_release();
75 
76  void commit_finalize();
77 
78  void add(Fragment::Ptr& frag);
79 
80  bool is_compacting() const;
81 
82  size_t need_compact(CompactGroups& groups, const Vec& without,
83  size_t vol);
84 
85  bool try_compact(uint32_t tnum = 1);
86 
87  void finish_compact(const Compact* compact);
88 
89  std::string get_log_fragment(const int64_t frag) const;
90 
91  std::string get_log_fragment(const std::string& frag) const;
92 
93  void load(int &err);
94 
95  void expand(DB::Cells::Interval& intval);
96 
97  void expand_and_align(DB::Cells::Interval& intval);
98 
99  void load_cells(BlockLoader* loader, bool& is_final,
100  Vec& frags, uint8_t vol);
101 
102  void get(Vec& fragments);
103 
104  size_t release(size_t bytes);
105 
106  void remove(int &err, Vec& fragments_old);
107 
108  void remove(int &err, Fragment::Ptr& frag, bool remove_file);
109 
110  void remove();
111 
112  void unload();
113 
114  Fragment::Ptr take_ownership(int &err, Fragment::Ptr& frag);
115 
116  void take_ownership(int& err, Vec& frags, Vec& removing);
117 
118  bool deleting();
119 
120  size_t cells_count(bool only_current = false);
121 
122  bool empty();
123 
124  size_t size() noexcept;
125 
126  size_t size_bytes(bool only_loaded=false);
127 
128  size_t size_bytes_encoded();
129 
130  bool processing() noexcept;
131 
132  uint64_t next_id();
133 
134  void print(std::ostream& out, bool minimal);
135 
136  private:
137 
138  size_t _commit(bool finalize);
139 
140  void _add(Fragment::Ptr& frag);
141 
142  uint64_t _next_id();
143 
144  void _remove(int &err, Vec& fragments_old, Core::Semaphore* semp);
145 
146  bool _need_roll() const noexcept;
147 
148  size_t _need_compact(CompactGroups& groups,const Vec& without,
149  size_t vol);
150 
151  bool _need_compact_major();
152 
153  void _load_cells(BlockLoader* loader, Vec& frags, uint8_t& vol);
154 
155  bool _processing() const noexcept;
156 
157  size_t _size_bytes(bool only_loaded=false);
158 
159  size_t _narrow(const DB::Cell::Key& key) const;
160 
161  std::shared_mutex m_mutex_cells;
162  DB::Cells::MutableVec m_cells;
163 
164  Core::StateRunning m_commit;
166 
167  std::shared_mutex m_mutex;
169  std::condition_variable_any m_cv;
170  Core::Semaphore m_sem;
171  uint64_t m_last_id;
172  Core::Atomic<size_t> m_releasable_bytes;
173  Core::Atomic<size_t> m_modification_ts;
174 };
175 
176 
177 
178 }}} // namespace SWC::Ranger::CommitLog
179 
180 
181 #include "swcdb/ranger/db/CommitLogCompact.h"
182 
183 #endif // swcdb_ranger_db_CommitLog_h
SWC::Ranger::CommitLog::Fragments::_narrow
size_t _narrow(const DB::Cell::Key &key) const
Definition: CommitLog.cc:706
SWC::Ranger::CommitLog::Fragments::load_cells
void load_cells(BlockLoader *loader, bool &is_final, Vec &frags, uint8_t vol)
Definition: CommitLog.cc:337
SWC::Ranger::CommitLog::Fragments::empty
bool empty()
Definition: CommitLog.cc:514
SWC::Ranger::CommitLog::Fragments::take_ownership
Fragment::Ptr take_ownership(int &err, Fragment::Ptr &frag)
Definition: CommitLog.cc:441
SWC::Ranger::CommitLog::Fragments::CompactGroups
Core::Vector< Vec > CompactGroups
Definition: CommitLog.h:29
SWC::Ranger::CommitLog::Fragments::Fragments
Fragments(const DB::Types::KeySeq key_seq)
Definition: CommitLog.cc:18
SWC::Ranger::CommitLog::Compact
Definition: CommitLogCompact.h:14
SWC::Ranger::CommitLog::Fragments::_load_cells
void _load_cells(BlockLoader *loader, Vec &frags, uint8_t &vol)
Definition: CommitLog.cc:349
SWC::Core::AtomicBase< bool >
SWC::Ranger::CommitLog::Fragments::m_mutex
std::shared_mutex m_mutex
Definition: CommitLog.h:167
SWC::Ranger::CommitLog::Fragments::unload
void unload()
Definition: CommitLog.cc:427
SWC::Ranger::CommitLog::Fragments::deleting
bool deleting()
Definition: CommitLog.cc:494
SWC::Core::ScopedLock
Definition: MutexLock.h:41
SWC::Ranger::CommitLog::Fragments::m_commit
Core::StateRunning m_commit
Definition: CommitLog.h:164
SWC::Ranger::CommitLog::Fragments::m_mutex_cells
std::shared_mutex m_mutex_cells
Definition: CommitLog.h:161
SWC::Ranger::CommitLog::Fragments::print
void print(std::ostream &out, bool minimal)
Definition: CommitLog.cc:566
SWC::Ranger::CommitLog::Fragment
Definition: CommitLogFragment.h:19
SWC::Ranger::CommitLog::Fragments::Ptr
Fragments * Ptr
Definition: CommitLog.h:28
SWC::DB::Cells::Cell
Definition: Cell.h:92
SWC::Ranger::CommitLog::Fragments::processing
bool processing() noexcept
Definition: CommitLog.cc:540
SWC::Ranger::CommitLog::Fragments::stopping
Core::AtomicBool stopping
Definition: CommitLog.h:32
SWC::Ranger::CommitLog::Fragments::_processing
bool _processing() const noexcept
Definition: CommitLog.cc:685
SWC::Ranger::CommitLog::Fragments::_need_roll
bool _need_roll() const noexcept
Definition: CommitLog.cc:594
SWC::Ranger::CommitLog::Fragments::expand
void expand(DB::Cells::Interval &intval)
Definition: CommitLog.cc:322
CommitLogFragment.h
SWC::Ranger::CommitLog::Fragments::_need_compact
size_t _need_compact(CompactGroups &groups, const Vec &without, size_t vol)
Definition: CommitLog.cc:611
SWC::Ranger::CommitLog::Fragments::_commit
size_t _commit(bool finalize)
Definition: CommitLog.cc:101
SWC::Ranger::CommitLog::Fragments
Definition: CommitLog.h:19
SWC::DB::Types::KeySeq
KeySeq
Definition: KeySeq.h:13
SWC::Ranger::CommitLog::Fragments::get
void get(Vec &fragments)
Definition: CommitLog.cc:362
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Ranger::RangePtr
std::shared_ptr< Range > RangePtr
Definition: Columns.h:15
SWC::Ranger::CommitLog::Fragments::remove
void remove()
Definition: CommitLog.cc:409
SWC::Ranger::CommitLog::Fragments::load
void load(int &err)
Definition: CommitLog.cc:297
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Ranger::CommitLog::Fragments::commit_finalize
void commit_finalize()
Definition: CommitLog.cc:90
SWC::Ranger::CommitLog::Fragments::size_bytes
size_t size_bytes(bool only_loaded=false)
Definition: CommitLog.cc:528
SWC::Ranger::CommitLog::Fragments::_next_id
uint64_t _next_id()
Definition: CommitLog.cc:555
SWC::Ranger::CommitLog::Fragments::m_releasable_bytes
Core::Atomic< size_t > m_releasable_bytes
Definition: CommitLog.h:172
SWC::Ranger::CommitLog::Fragments::Fragments
Fragments(const Fragments &&)=delete
SWC::Ranger::CommitLog::Fragments::finish_compact
void finish_compact(const Compact *compact)
Definition: CommitLog.cc:263
SWC::Ranger::BlockLoader
Definition: RangeBlockLoader.h:19
SWC::Ranger::CommitLog::Fragments::_remove
void _remove(int &err, Vec &fragments_old, Core::Semaphore *semp)
Definition: CommitLog.cc:389
SWC::DB::Cells::MutableVec::add_raw
void add_raw(const Cell &cell, bool finalized)
Definition: MutableVec.cc:35
SWC::Ranger::CommitLog::Fragments::operator=
Fragments & operator=(const Fragments &)=delete
SWC::Ranger::CommitLog::Fragments::Vec
Core::Vector< Fragment::Ptr > Vec
Definition: CommitLog.h:27
SWC::Ranger::CommitLog::Fragments::m_modification_ts
Core::Atomic< size_t > m_modification_ts
Definition: CommitLog.h:173
SWC::Ranger::CommitLog::Fragments::m_sem
Core::Semaphore m_sem
Definition: CommitLog.h:170
SWC::Ranger::CommitLog::Fragments::next_id
uint64_t next_id()
Definition: CommitLog.cc:549
SWC::Core::Vector
Definition: Vector.h:14
SWC::Ranger::CommitLog::Fragments::Fragments
Fragments(const Fragments &)=delete
SWC::Ranger::CommitLog::Fragments::schema_update
void schema_update()
Definition: CommitLog.cc:41
SWC::Ranger::CommitLog::Fragments::modification_ts
SWC_CAN_INLINE int64_t modification_ts() const noexcept
Definition: CommitLog.h:49
SWC::Ranger::CommitLog::Fragments::size_bytes_encoded
size_t size_bytes_encoded()
Definition: CommitLog.cc:532
SWC::Ranger::CommitLog::Fragments::commit
void commit() noexcept
Definition: CommitLog.cc:59
SWC::Core::AtomicBase::load
constexpr SWC_CAN_INLINE T load() const noexcept
Definition: Atomic.h:42
SWC::Ranger::CommitLog::Fragments::add
void add(const DB::Cells::Cell &cell)
Definition: CommitLog.cc:54
SWC::Ranger::CommitLog::Fragments::_need_compact_major
bool _need_compact_major()
Definition: CommitLog.cc:667
SWC::Ranger::CommitLog::Fragments::size
size_t size() noexcept
Definition: CommitLog.cc:522
SWC::Ranger::CommitLog::Fragments::expand_and_align
void expand_and_align(DB::Cells::Interval &intval)
Definition: CommitLog.cc:328
SWC::Ranger::CommitLog::Fragments::need_compact
size_t need_compact(CompactGroups &groups, const Vec &without, size_t vol)
Definition: CommitLog.cc:209
SWC::Ranger::CommitLog::Fragments::get_log_fragment
std::string get_log_fragment(const int64_t frag) const
Definition: CommitLog.cc:279
SWC::Ranger::CommitLog::Fragments::m_cells
DB::Cells::MutableVec m_cells
Definition: CommitLog.h:162
SWC::Ranger::CommitLog::Fragments::m_cv
std::condition_variable_any m_cv
Definition: CommitLog.h:169
SWC::Ranger::CommitLog::Fragments::try_compact
bool try_compact(uint32_t tnum=1)
Definition: CommitLog.cc:216
SWC::Ranger::CommitLog::Fragments::~Fragments
~Fragments() noexcept
Definition: CommitLog.cc:30
SWC::Ranger::CommitLog::Fragments::range
RangePtr range
Definition: CommitLog.h:31
SWC::DB::Cells::MutableVec::check_sequence
SWC_CAN_INLINE void check_sequence(const char *msg, bool w_assert=true) const
Definition: MutableVec.h:108
SWC::Ranger::CommitLog::Fragments::m_deleting
bool m_deleting
Definition: CommitLog.h:168
SWC::Ranger::CommitLog::Fragments::init
void init(const RangePtr &for_range)
Definition: CommitLog.cc:35
SWC::Ranger::CommitLog::Fragments::is_compacting
bool is_compacting() const
Definition: CommitLog.cc:205
SWC::Ranger::CommitLog::Fragments::cells_count
size_t cells_count(bool only_current=false)
Definition: CommitLog.cc:499
SWC::Ranger::CommitLog::Fragments::_size_bytes
size_t _size_bytes(bool only_loaded=false)
Definition: CommitLog.cc:694
SWC::Ranger::CommitLog::Fragments::m_compacting
Core::AtomicBool m_compacting
Definition: CommitLog.h:165
SWC::Ranger::CommitLog::Fragments::m_last_id
uint64_t m_last_id
Definition: CommitLog.h:171
SWC::Ranger::CommitLog::Fragments::cells_mutex
SWC_CAN_INLINE std::shared_mutex & cells_mutex() noexcept
Definition: CommitLog.h:56
SWC::Ranger::CommitLog::Fragments::_add
SWC_CAN_INLINE void _add(const DB::Cells::Cell &cell, size_t *offset_itp, size_t *offsetp)
Definition: CommitLog.h:61
SWC::Ranger::CommitLog::Fragments::release
size_t release(size_t bytes)
Definition: CommitLog.cc:367
SWC::Core::AtomicBool
AtomicBase< bool > AtomicBool
Definition: Atomic.h:63
SWC::Ranger::CommitLog::Fragments::commit_release
size_t commit_release()
Definition: CommitLog.cc:82
SWC::Ranger::CommitLog::Fragments::check_sequence
SWC_CAN_INLINE void check_sequence(const char *msg, bool w_assert=true)
Definition: CommitLog.h:67