Line data Source code
1 : // Copyright (c) 2019 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdatabase
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : #pragma once
20 :
21 :
22 : /** \file
23 : * \brief The virtual buffer declarations.
24 : *
25 : * When dealing with a block, we at times have to reduce or enlarge it.
26 : * Several resizing events may occur before it settles. It is best not
27 : * to resize the entire block for each event. _To ease the damage,_ we
28 : * want to use separate memory buffer to handle growths. Once we are
29 : * done with a structure, we can then request for the final data to
30 : * be written to file.
31 : *
32 : * Another case is when a structure ends up being larger than one block.
33 : * For example, the table schema can end up requiring 2 or 3 blocks.
34 : * To handle that case, we use avirtual buffer as well. This is very
35 : * practical because that way we do not have to handle the fact that
36 : * the buffer is multiple buffers. The virtual buffer gives us one
37 : * linear offset starting at `0` and going up to `size - 1`.
38 : */
39 :
40 : // self
41 : //
42 : #include "snapdatabase/block/block.h"
43 :
44 :
45 : // C++ lib
46 : //
47 : #include <deque>
48 :
49 :
50 :
51 : namespace snapdatabase
52 : {
53 :
54 :
55 :
56 : typedef std::vector<uint8_t> buffer_t;
57 :
58 :
59 9 : class virtual_buffer
60 : {
61 : public:
62 : typedef std::shared_ptr<virtual_buffer> pointer_t;
63 :
64 : virtual_buffer();
65 : virtual_buffer(block::pointer_t b, std::uint64_t offset, std::uint64_t size);
66 :
67 : void add_buffer(block::pointer_t b, std::uint64_t offset, std::uint64_t size);
68 :
69 : bool modified() const;
70 : std::size_t count_buffers() const;
71 : std::uint64_t size() const;
72 : bool is_data_available(std::uint64_t offset, std::uint64_t size) const;
73 :
74 : int pread(void * buf, std::uint64_t size, std::uint64_t offset, bool full = true) const;
75 : int pwrite(void const * buf, std::uint64_t size, std::uint64_t offset, bool allow_growth = false);
76 : int pinsert(void const * buf, std::uint64_t size, std::uint64_t offset);
77 : int perase(std::uint64_t size, std::uint64_t offset);
78 :
79 : private:
80 63 : struct vbuf_t
81 : {
82 : typedef std::deque<vbuf_t> deque_t;
83 :
84 : vbuf_t();
85 : vbuf_t(block::pointer_t b, std::uint64_t offset, std::uint64_t size);
86 :
87 : block::pointer_t f_block = block::pointer_t();
88 : buffer_t f_data = buffer_t(); // data not (yet) in the block(s)
89 : std::uint64_t f_offset = 0;
90 : std::uint64_t f_size = 0;
91 : };
92 :
93 : vbuf_t::deque_t f_buffers = vbuf_t::deque_t();
94 : std::uint64_t f_total_size = 0;
95 : bool f_modified = false;
96 : };
97 :
98 :
99 :
100 : std::ostream & operator << (std::ostream & out, virtual_buffer const & v);
101 :
102 :
103 :
104 : } // namespace snapdatabase
105 : // vim: ts=4 sw=4 et
|