SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
CellStore.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_CellStore_h
8 #define swcdb_ranger_db_CellStore_h
9 
11 
12 
13 namespace SWC { namespace Ranger {
14 
15 namespace CellStore {
16 
17 /* file-format:
18  [blocks]:
19  header: i8(encoder), i32(enc-len), i32(len), i32(cells),
20  i32(checksum-data), i32(checksum)
21  data: [cell]
22  [idx-blocks]:
23  header: i8(encoder), i32(enc-len), i32(len),
24  i32(checksum-data), i32(checksum)
25  idx-data: (1st ? prev_key_end,), vi32(blocks)
26  [blocks]:
27  vi64(offset), interval,
28  i8(encoder), vi32(enc-len), vi32(len),
29  vi32(cells), vi32(checksum-data)
30  trailer : i8(version), i32(cell-revs), i32(idx-blocks), i64(idx-offset),
31  i32(checksum)
32  (trailer-offset) = fileLength - TRAILER_SIZE
33 */
34 
35 
36 static const int8_t VERSION = 1;
37 static const uint8_t TRAILER_SIZE = 21;
38 static const uint8_t IDX_BLKS_HEADER_SIZE = 17;
39 
40 
41 class Read final {
42  public:
43 
44  typedef Read* Ptr;
46 
47  static constexpr uint32_t MAX_BLOCKS = UINT32_MAX;
48 
49  inline static Ptr make(int& err, const csid_t csid, const RangePtr& range,
50  bool chk_base=false);
51 
52  static bool load_trailer(int& err, FS::SmartFd::Ptr& smartfd,
53  uint32_t& cell_revs,
54  uint32_t& blks_idx_count,
55  uint64_t& blks_idx_offset,
56  bool close_after=false, bool chk_base=false);
57 
58  static void load_blocks_index(int& err, FS::SmartFd::Ptr& smartfd,
62  Blocks& blocks,
63  uint32_t& cell_revs,
64  bool chk_base=false);
65 
66  //
67 
68  const csid_t csid;
72  const Blocks blocks;
73  const uint32_t cell_revs;
75 
76  explicit Read(const csid_t csid,
80  Blocks&& blocks,
81  const uint32_t cell_revs,
82  const FS::SmartFd::Ptr& smartfd) noexcept;
83 
84  Read(const Read&) = delete;
85 
86  Read(const Read&&) = delete;
87 
88  Read& operator=(const Read&) = delete;
89 
90  ~Read() noexcept;
91 
92  // size_t size_of() const noexcept;
93 
94  const std::string& filepath() const;
95 
96  void load_cells(BlockLoader* loader);
97 
98  void _run_queued();
99 
100  void get_blocks(int& err, Blocks& to) const;
101 
102  size_t release(size_t bytes);
103 
104  void close(int &err);
105 
106  void remove(int &err);
107 
108  bool processing() const noexcept;
109 
110  size_t size_bytes(bool only_loaded=false) const;
111 
112  size_t size_bytes_enc(bool only_loaded=false) const;
113 
114  size_t blocks_count() const;
115 
116  void print(std::ostream& out, bool minimal=true) const;
117 
118  private:
119 
120  void _release_fd();
121 
122  mutable Core::QueueSafeStated<Block::Read::Ptr> m_queue;
123 
124 };
125 
126 
127 
128 class Write final {
129  public:
130  typedef std::shared_ptr<Write> Ptr;
131 
132  const csid_t csid;
135  const uint32_t block_size;
136  const uint32_t cell_revs;
138 
139  size_t size;
141 
142  //Core::Atomic<uint32_t> completion;
143 
144  Write(const csid_t csid, std::string&& filepath,
145  const RangePtr& range, uint32_t cell_revs,
146  const DB::Cell::Key& prev_key_end);
147 
148  ~Write() noexcept { }
149 
150  void create(int& err, uint8_t blk_replicas=0);
151 
152  void block_encode(int& err, DynamicBuffer& cells_buff,
153  Block::Header&& header);
154 
155  void block_write(int& err, DynamicBuffer& blk_buff,
156  Block::Header&& header);
157 
158  void finalize(int& err);
159 
160  void remove(int &err);
161 
162  void print(std::ostream& out) const;
163 
164  private:
165 
166  void block(int& err, DynamicBuffer& blk_buff);
167 
168  void write_blocks_index(int& err, uint32_t& blks_idx_count);
169 
170  void write_trailer(int& err);
171 
172  void close_and_validate(int& err);
173 };
174 
176 typedef std::shared_ptr<Writers> WritersPtr;
177 
178 
179 
180 
181 static Read::Ptr create_initial(int& err, const RangePtr& range);
182 
183 
184 
185 }}}// namespace SWC::Ranger::CellStore
186 
187 #endif // swcdb_ranger_db_CellStore_h
SWC::Ranger::CellStore::Block::Header
Definition: CellStoreBlockHeader.h:23
SWC::Ranger::CellStore::Write::blocks
Core::Vector< Block::Write::Ptr > blocks
Definition: CellStore.h:140
SWC::Ranger::CellStore::Write::block_size
const uint32_t block_size
Definition: CellStore.h:135
SWC::Ranger::CellStore::Read::size_bytes_enc
size_t size_bytes_enc(bool only_loaded=false) const
Definition: CellStore.cc:378
SWC::Ranger::CellStore::Read::Read
Read(const csid_t csid, DB::Cell::Key &&prev_key_end, DB::Cell::Key &&key_end, DB::Cells::Interval &&interval, Blocks &&blocks, const uint32_t cell_revs, const FS::SmartFd::Ptr &smartfd) noexcept
Definition: CellStore.cc:255
SWC::Ranger::Block
Definition: RangeBlock.h:23
SWC::Ranger::CellStore::Read::get_blocks
void get_blocks(int &err, Blocks &to) const
Definition: CellStore.cc:317
SWC::Ranger::CellStore::Read::Read
Read(const Read &&)=delete
SWC::Ranger::CellStore::Read::remove
void remove(int &err)
Definition: CellStore.cc:354
SWC::Core::Encoder::Type
Type
Definition: Encoder.h:28
SWC::csid_t
uint32_t csid_t
Definition: Identifiers.h:19
SWC::Ranger::CellStore::Read::interval
const DB::Cells::Interval interval
Definition: CellStore.h:71
SWC::Ranger::CellStore::Read::key_end
const DB::Cell::Key key_end
Definition: CellStore.h:70
SWC::Ranger::CellStore::Writers
Core::Vector< Write::Ptr > Writers
Definition: CellStore.h:175
SWC::Ranger::CellStore::Write::Ptr
std::shared_ptr< Write > Ptr
Definition: CellStore.h:130
SWC::DB::Cell::Key
Definition: CellKey.h:24
SWC::Ranger::CellStore::Read::close
void close(int &err)
Definition: CellStore.cc:345
SWC::Ranger::CellStore::IDX_BLKS_HEADER_SIZE
static const uint8_t IDX_BLKS_HEADER_SIZE
Definition: CellStore.h:38
SWC::Ranger::CellStore::Read::release
size_t release(size_t bytes)
Definition: CellStore.cc:322
SWC::Ranger::CellStore::Read::Blocks
Core::Vector< Block::Read::Ptr > Blocks
Definition: CellStore.h:45
SWC::Ranger::CellStore::Write::~Write
~Write() noexcept
Definition: CellStore.h:148
SWC::Ranger::CellStore::create_initial
static Read::Ptr create_initial(int &err, const RangePtr &range)
Definition: CellStore.cc:610
SWC::Ranger::CellStore::Write::cell_revs
const uint32_t cell_revs
Definition: CellStore.h:136
SWC::Ranger::CellStore::Read::prev_key_end
const DB::Cell::Key prev_key_end
Definition: CellStore.h:69
SWC::Ranger::CellStore::VERSION
static const int8_t VERSION
Definition: CellStore.h:36
SWC::Ranger::CellStore::Read::load_trailer
static bool load_trailer(int &err, FS::SmartFd::Ptr &smartfd, uint32_t &cell_revs, uint32_t &blks_idx_count, uint64_t &blks_idx_offset, bool close_after=false, bool chk_base=false)
Definition: CellStore.cc:51
SWC::Comm::Protocol::FsBroker::Handler::create
void create(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Create.h:18
SWC::Ranger::CellStore::Read::MAX_BLOCKS
static constexpr uint32_t MAX_BLOCKS
Definition: CellStore.h:47
SWC::Ranger::RangePtr
std::shared_ptr< Range > RangePtr
Definition: Columns.h:15
CellStoreBlock.h
SWC::Ranger::CellStore::Write::smartfd
FS::SmartFd::Ptr smartfd
Definition: CellStore.h:133
SWC::Ranger::CellStore::Read::operator=
Read & operator=(const Read &)=delete
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Ranger::CellStore::Write::csid
const csid_t csid
Definition: CellStore.h:132
SWC::Ranger::CellStore::Read::load_blocks_index
static void load_blocks_index(int &err, FS::SmartFd::Ptr &smartfd, DB::Cell::Key &prev_key_end, DB::Cell::Key &key_end, DB::Cells::Interval &interval, Blocks &blocks, uint32_t &cell_revs, bool chk_base=false)
Definition: CellStore.cc:118
SWC::Ranger::CellStore::Read::make
static Ptr make(int &err, const csid_t csid, const RangePtr &range, bool chk_base=false)
Definition: CellStore.cc:13
SWC::Core::BufferDyn< StaticBuffer >
SWC::Ranger::CellStore::Read::cell_revs
const uint32_t cell_revs
Definition: CellStore.h:73
SWC::Ranger::CellStore::Read::~Read
~Read() noexcept
Definition: CellStore.cc:274
SWC::Ranger::CellStore::Read
Definition: CellStore.h:41
SWC::Ranger::CellStore::Read::load_cells
void load_cells(BlockLoader *loader)
Definition: CellStore.cc:296
SWC::Ranger::CellStore::TRAILER_SIZE
static const uint8_t TRAILER_SIZE
Definition: CellStore.h:37
SWC::Ranger::CellStore::Read::blocks
const Blocks blocks
Definition: CellStore.h:72
SWC::Ranger::CellStore::Read::smartfd
FS::SmartFd::Ptr smartfd
Definition: CellStore.h:74
SWC::Ranger::CellStore::Read::processing
bool processing() const noexcept
Definition: CellStore.cc:359
SWC::Ranger::CellStore::Write::size
size_t size
Definition: CellStore.h:139
SWC::Ranger::BlockLoader
Definition: RangeBlockLoader.h:19
SWC::Ranger::CellStore::Read::_run_queued
void _run_queued()
Definition: CellStore.cc:311
SWC::Ranger::CellStore::Read::Read
Read(const Read &)=delete
SWC::Ranger::CellStore::Read::csid
const csid_t csid
Definition: CellStore.h:68
SWC::Ranger::CellStore::Write::encoder
const DB::Types::Encoder encoder
Definition: CellStore.h:134
SWC::Ranger::CellStore::Read::size_bytes
size_t size_bytes(bool only_loaded=false) const
Definition: CellStore.cc:370
SWC::Ranger::CellStore::Read::m_queue
Core::QueueSafeStated< Block::Read::Ptr > m_queue
Definition: CellStore.h:122
SWC::Core::Vector
Definition: Vector.h:14
SWC::FS::SmartFd::Ptr
std::shared_ptr< SmartFd > Ptr
Definition: SmartFd.h:37
SWC::Ranger::CellStore::Read::Ptr
Read * Ptr
Definition: CellStore.h:44
SWC::Ranger::CellStore::Read::_release_fd
void _release_fd()
Definition: CellStore.cc:335
SWC::Ranger::CellStore::WritersPtr
std::shared_ptr< Writers > WritersPtr
Definition: CellStore.h:176
SWC::Ranger::CellStore::Read::blocks_count
size_t blocks_count() const
Definition: CellStore.cc:386
SWC::DB::Cells::Interval
Definition: Interval.h:17
SWC::Ranger::CellStore::Write::prev_key_end
const DB::Cell::Key prev_key_end
Definition: CellStore.h:137
SWC::Ranger::CellStore::Read::print
void print(std::ostream &out, bool minimal=true) const
Definition: CellStore.cc:390
SWC::Ranger::CellStore::Read::filepath
const std::string & filepath() const
Definition: CellStore.cc:291
SWC::Ranger::CellStore::Write
Definition: CellStore.h:128