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 :
20 :
21 : /** \file
22 : * \brief Database file implementation.
23 : *
24 : * Each table uses one or more files. Each file is handled by a dbfile
25 : * object and a corresponding set of blocks.
26 : */
27 :
28 : // self
29 : //
30 : #include "snapdatabase/block/block_free_block.h"
31 :
32 : #include "snapdatabase/block/block_header.h"
33 : #include "snapdatabase/database/table.h"
34 :
35 :
36 : // last include
37 : //
38 : #include <snapdev/poison.h>
39 :
40 :
41 :
42 : namespace snapdatabase
43 : {
44 :
45 :
46 :
47 : namespace
48 : {
49 :
50 :
51 :
52 : // 'FREE'
53 : constexpr struct_description_t g_description[] =
54 : {
55 : define_description(
56 : FieldName("header")
57 : , FieldType(struct_type_t::STRUCT_TYPE_STRUCTURE)
58 : , FieldSubDescription(detail::g_block_header)
59 : ),
60 : define_description(
61 : FieldName("next_free_block")
62 : , FieldType(struct_type_t::STRUCT_TYPE_REFERENCE)
63 : ),
64 : // the rest of these blocks are all zeroes
65 : end_descriptions()
66 : };
67 :
68 :
69 : constexpr descriptions_by_version_t const g_descriptions_by_version[] =
70 : {
71 : define_description_by_version(
72 : DescriptionVersion(0, 1),
73 : DescriptionDescription(g_description)
74 : ),
75 : end_descriptions_by_version()
76 : };
77 :
78 :
79 :
80 : }
81 : // no name namespace
82 :
83 :
84 :
85 :
86 :
87 1 : block_free_block::block_free_block(dbfile::pointer_t f, reference_t offset)
88 1 : : block(g_descriptions_by_version, f, offset)
89 : {
90 1 : }
91 :
92 :
93 1 : reference_t block_free_block::get_next_free_block() const
94 : {
95 1 : return static_cast<reference_t>(f_structure->get_uinteger("next_free_block"));
96 : }
97 :
98 :
99 0 : void block_free_block::set_next_free_block(reference_t offset)
100 : {
101 0 : f_structure->set_uinteger("next_free_block", offset);
102 0 : }
103 :
104 :
105 0 : void block_free_block::clear_block()
106 : {
107 0 : reference_t const offset(f_structure->get_size());
108 : #ifdef _DEBUG
109 0 : if(offset == 0)
110 : {
111 0 : throw snapdatabase_logic_error("the structure of the block_free_block block cannot be dynamic.");
112 : }
113 : #endif
114 0 : std::uint32_t const page_size(get_table()->get_page_size() - offset);
115 :
116 0 : memset(data(offset), 0, page_size);
117 0 : }
118 :
119 :
120 :
121 : } // namespace snapdatabase
122 : // vim: ts=4 sw=4 et
|