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 Context file header.
24 : *
25 : * The context class manages a set of tables. This represents one _database_
26 : * in the SQL world. The context is pretty shallow otherwise. Most of our
27 : * settings are in the tables (i.e. replication, compression, compaction,
28 : * filters, indexes, etc. all of these things are part of the tables).
29 : */
30 :
31 : // self
32 : //
33 : #include "snapdatabase/data/structure.h"
34 : #include "snapdatabase/data/xml.h"
35 :
36 :
37 :
38 : namespace snapdatabase
39 : {
40 :
41 :
42 :
43 : //typedef uint64_t block_ref_t;
44 : //typedef uint8_t flag8_t;
45 : //typedef uint16_t flag16_t;
46 : typedef uint32_t flag32_t; // look into not using these, instead use the structure directly
47 : typedef uint64_t flag64_t;
48 :
49 : typedef uint16_t column_id_t;
50 : typedef std::vector<column_id_t> column_ids_t;
51 :
52 :
53 :
54 : enum class model_t
55 : {
56 : TABLE_MODEL_CONTENT,
57 : TABLE_MODEL_DATA,
58 : TABLE_MODEL_LOG,
59 : TABLE_MODEL_QUEUE,
60 : TABLE_MODEL_SEQUENCIAL,
61 : TABLE_MODEL_SESSION,
62 : TABLE_MODEL_TREE,
63 :
64 : TABLE_MODEL_DEFAULT = TABLE_MODEL_CONTENT
65 : };
66 :
67 :
68 :
69 : model_t name_to_model(std::string const & name);
70 :
71 :
72 : // SAVED IN FILE, DO NOT CHANGE BIT LOCATIONS
73 : constexpr flag64_t TABLE_FLAG_TEMPORARY = (1LL << 0);
74 : constexpr flag64_t TABLE_FLAG_SPARSE = (1LL << 1);
75 : constexpr flag64_t TABLE_FLAG_SECURE = (1LL << 2);
76 :
77 : // NEVER SAVED, used internally only
78 : constexpr flag64_t TABLE_FLAG_DROP = (1LL << 63);
79 :
80 :
81 : // Special values
82 : constexpr column_id_t COLUMN_NULL = 0;
83 :
84 :
85 : // SAVED IN FILE, DO NOT CHANGE BIT LOCATIONS
86 : constexpr flag32_t COLUMN_FLAG_LIMITED = (1ULL << 0);
87 : constexpr flag32_t COLUMN_FLAG_REQUIRED = (1ULL << 1);
88 : constexpr flag32_t COLUMN_FLAG_BLOB = (1ULL << 2);
89 : constexpr flag32_t COLUMN_FLAG_SYSTEM = (1ULL << 3);
90 : constexpr flag32_t COLUMN_FLAG_REVISION_TYPE = (3ULL << 4); // TWO BITS (see COLUMN_REVISION_TYPE_...)
91 :
92 : // Revision Types (after the shift, TBD: should we keep the shift?)
93 : constexpr flag32_t COLUMN_REVISION_TYPE_GLOBAL = 0;
94 : constexpr flag32_t COLUMN_REVISION_TYPE_BRANCH = 1;
95 : constexpr flag32_t COLUMN_REVISION_TYPE_REVISION = 2;
96 : //constexpr flag32_t COLUMN_REVISION_TYPE_unused = 3; -- current unused
97 :
98 :
99 : // SAVED IN FILE, DO NOT CHANGE BIT LOCATIONS
100 : constexpr flag32_t SECONDARY_INDEX_FLAG_DISTRIBUTED = (1LL << 0);
101 :
102 :
103 :
104 0 : class schema_complex_type
105 : {
106 : public:
107 : typedef std::map<std::string, schema_complex_type>
108 : map_t;
109 :
110 : schema_complex_type();
111 : schema_complex_type(xml_node::pointer_t x);
112 :
113 : std::string name() const;
114 : size_t size() const;
115 : std::string type_name(int idx) const;
116 : struct_type_t type(int idx) const;
117 :
118 : private:
119 0 : struct field_t
120 : {
121 : typedef std::vector<field_t> vector_t;
122 :
123 : std::string f_name = std::string();
124 : struct_type_t f_type = struct_type_t::STRUCT_TYPE_VOID;
125 : };
126 :
127 : std::string f_name = std::string();
128 : field_t::vector_t f_fields = field_t::vector_t();
129 : };
130 :
131 :
132 :
133 :
134 : class schema_table;
135 : typedef std::shared_ptr<schema_table> schema_table_pointer_t;
136 : typedef std::weak_ptr<schema_table> schema_table_weak_pointer_t;
137 :
138 0 : class schema_column
139 : {
140 : public:
141 : typedef std::shared_ptr<schema_column> pointer_t;
142 : typedef std::map<column_id_t, pointer_t> map_by_id_t;
143 : typedef std::map<std::string, pointer_t> map_by_name_t;
144 :
145 : schema_column(schema_table_pointer_t table, xml_node::pointer_t x);
146 : schema_column(schema_table_pointer_t table, structure::pointer_t s);
147 : schema_column(
148 : schema_table_pointer_t table
149 : , std::string name
150 : , struct_type_t type
151 : , flag32_t flags);
152 :
153 : void from_structure(structure::pointer_t s);
154 :
155 : schema_table_pointer_t table() const;
156 :
157 : void hash(std::uint64_t & h0, std::uint64_t & h1) const;
158 : std::string name() const;
159 : column_id_t column_id() const;
160 : void set_column_id(column_id_t id);
161 : struct_type_t type() const;
162 : flag32_t flags() const;
163 : std::string encrypt_key_name() const;
164 : buffer_t default_value() const;
165 : buffer_t minimum_value() const;
166 : buffer_t maximum_value() const;
167 : std::uint32_t minimum_length() const;
168 : std::uint32_t maximum_length() const;
169 : buffer_t validation() const;
170 :
171 : private:
172 : std::uint64_t f_hash[2] = { 0ULL, 0ULL };
173 : std::string f_name = std::string();
174 : column_id_t f_column_id = column_id_t();
175 : struct_type_t f_type = struct_type_t();
176 : flag32_t f_flags = flag32_t();
177 : std::string f_encrypt_key_name = std::string();
178 : std::int32_t f_internal_size_limit = -1; // -1 = no limit; if size > f_internal_size_limit, save in external file
179 : buffer_t f_default_value = buffer_t();
180 : buffer_t f_minimum_value = buffer_t();
181 : buffer_t f_maximum_value = buffer_t();
182 : std::uint32_t f_minimum_length = 0;
183 : std::uint32_t f_maximum_length = 0;
184 : buffer_t f_validation = buffer_t();
185 :
186 : // not saved on disk
187 : //
188 : schema_table_weak_pointer_t f_schema_table = schema_table_weak_pointer_t();
189 : std::string f_description = std::string();
190 : };
191 :
192 :
193 :
194 :
195 0 : class schema_secondary_index
196 : {
197 : public:
198 : typedef std::shared_ptr<schema_secondary_index> pointer_t;
199 : typedef std::vector<pointer_t> vector_t;
200 :
201 : std::string get_index_name() const;
202 : void set_index_name(std::string const & index_name);
203 :
204 : bool get_distributed_index() const;
205 : void set_distributed_index(bool distributed);
206 :
207 : size_t get_column_count();
208 : column_id_t get_column_id(int idx);
209 : void add_column_id(column_id_t id);
210 :
211 : private:
212 : std::string f_index_name = std::string();
213 : column_ids_t f_column_ids = column_ids_t();
214 : flag64_t f_flags = flag64_t();
215 : };
216 :
217 :
218 :
219 :
220 1 : class schema_table
221 : : public std::enable_shared_from_this<schema_table>
222 : {
223 : public:
224 : typedef std::shared_ptr<schema_table> pointer_t;
225 :
226 : void from_xml(xml_node::pointer_t x);
227 : void load_extension(xml_node::pointer_t e);
228 :
229 : void from_binary(virtual_buffer::pointer_t b);
230 : virtual_buffer::pointer_t to_binary() const;
231 :
232 : version_t version() const;
233 : std::string name() const;
234 : model_t model() const;
235 : bool is_sparse() const;
236 : bool is_secure() const;
237 : column_ids_t row_key() const;
238 : void assign_column_ids();
239 : schema_column::pointer_t column(std::string const & name) const;
240 : schema_column::pointer_t column(column_id_t id) const;
241 : schema_column::map_by_id_t columns_by_id() const;
242 : schema_column::map_by_name_t columns_by_name() const;
243 :
244 : std::string description() const;
245 : std::uint32_t block_size() const;
246 :
247 : private:
248 : version_t f_version = version_t();
249 : std::string f_name = std::string();
250 : flag64_t f_flags = flag64_t();
251 : model_t f_model = model_t::TABLE_MODEL_CONTENT;
252 : std::uint32_t f_block_size = 0;
253 : column_ids_t f_row_key = column_ids_t();
254 : schema_secondary_index::vector_t f_secondary_indexes = schema_secondary_index::vector_t();
255 : schema_complex_type::map_t f_complex_types = schema_complex_type::map_t();
256 : schema_column::map_by_name_t f_columns_by_name = schema_column::map_by_name_t();
257 : schema_column::map_by_id_t f_columns_by_id = schema_column::map_by_id_t();
258 :
259 : // not saved in database, only in XML
260 : //
261 : std::string f_description = std::string();
262 : };
263 :
264 :
265 :
266 : } // namespace snapdatabase
267 : // vim: ts=4 sw=4 et
|