1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2011-2016 Brazil
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License version 2.1 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#pragma once
20
21#include "dat.hpp"
22
23namespace grn {
24namespace dat {
25
26class GRN_DAT_API Header {
27 public:
28 Header()
29 : file_size_(0),
30 total_key_length_(0),
31 next_key_id_(grn::dat::MIN_KEY_ID),
32 max_key_id_(0),
33 num_keys_(0),
34 max_num_keys_(0),
35 num_phantoms_(0),
36 num_zombies_(0),
37 num_blocks_(0),
38 max_num_blocks_(0),
39 next_key_pos_(0),
40 key_buf_size_(0),
41 status_flags_(0) {
42 for (UInt32 i = 0; i <= MAX_BLOCK_LEVEL; ++i) {
43 leaders_[i] = INVALID_LEADER;
44 }
45 for (UInt32 i = 0; i < (sizeof(reserved_) / sizeof(*reserved_)); ++i) {
46 reserved_[i] = 0;
47 }
48 }
49
50 UInt64 file_size() const {
51 return file_size_;
52 }
53 UInt32 total_key_length() const {
54 return total_key_length_;
55 }
56 UInt32 min_key_id() const {
57 return MIN_KEY_ID;
58 }
59 UInt32 next_key_id() const {
60 return next_key_id_;
61 }
62 UInt32 max_key_id() const {
63 return max_key_id_;
64 }
65 UInt32 num_keys() const {
66 return num_keys_;
67 }
68 UInt32 max_num_keys() const {
69 return max_num_keys_;
70 }
71 UInt32 num_nodes() const {
72 return num_blocks() * BLOCK_SIZE;
73 }
74 UInt32 num_phantoms() const {
75 return num_phantoms_;
76 }
77 UInt32 num_zombies() const {
78 return num_zombies_;
79 }
80 UInt32 max_num_nodes() const {
81 return max_num_blocks() * BLOCK_SIZE;
82 }
83 UInt32 num_blocks() const {
84 return num_blocks_;
85 }
86 UInt32 max_num_blocks() const {
87 return max_num_blocks_;
88 }
89 UInt32 next_key_pos() const {
90 return next_key_pos_;
91 }
92 UInt32 key_buf_size() const {
93 return key_buf_size_;
94 }
95 UInt32 status_flags() const {
96 return status_flags_;
97 }
98 UInt32 ith_leader(UInt32 i) const {
99 GRN_DAT_DEBUG_THROW_IF(i > MAX_BLOCK_LEVEL);
100 return leaders_[i];
101 }
102
103 void set_file_size(UInt64 x) {
104 GRN_DAT_DEBUG_THROW_IF(x > MAX_FILE_SIZE);
105 file_size_ = x;
106 }
107 void set_total_key_length(UInt32 x) {
108 GRN_DAT_DEBUG_THROW_IF(x > MAX_TOTAL_KEY_LENGTH);
109 total_key_length_ = x;
110 }
111 void set_next_key_id(UInt32 x) {
112 GRN_DAT_DEBUG_THROW_IF((x - 1) > MAX_KEY_ID);
113 next_key_id_ = x;
114 }
115 void set_max_key_id(UInt32 x) {
116 GRN_DAT_DEBUG_THROW_IF(x > MAX_KEY_ID);
117 max_key_id_ = x;
118 }
119 void set_num_keys(UInt32 x) {
120 GRN_DAT_DEBUG_THROW_IF(x > MAX_NUM_KEYS);
121 num_keys_ = x;
122 }
123 void set_max_num_keys(UInt32 x) {
124 GRN_DAT_DEBUG_THROW_IF(x > MAX_NUM_KEYS);
125 max_num_keys_ = x;
126 }
127 void set_num_phantoms(UInt32 x) {
128 GRN_DAT_DEBUG_THROW_IF(x > max_num_nodes());
129 num_phantoms_ = x;
130 }
131 void set_num_zombies(UInt32 x) {
132 GRN_DAT_DEBUG_THROW_IF(x > max_num_nodes());
133 num_zombies_ = x;
134 }
135 void set_num_blocks(UInt32 x) {
136 GRN_DAT_DEBUG_THROW_IF(x > max_num_blocks());
137 num_blocks_ = x;
138 }
139 void set_max_num_blocks(UInt32 x) {
140 GRN_DAT_DEBUG_THROW_IF(x > MAX_NUM_BLOCKS);
141 max_num_blocks_ = x;
142 }
143 void set_next_key_pos(UInt32 x) {
144 GRN_DAT_DEBUG_THROW_IF(x > key_buf_size());
145 next_key_pos_ = x;
146 }
147 void set_key_buf_size(UInt32 x) {
148 GRN_DAT_DEBUG_THROW_IF(x > MAX_KEY_BUF_SIZE);
149 key_buf_size_ = x;
150 }
151 void set_status_flags(UInt32 x) {
152 status_flags_ = x;
153 }
154 void set_ith_leader(UInt32 i, UInt32 x) {
155 GRN_DAT_DEBUG_THROW_IF(i > MAX_BLOCK_LEVEL);
156 GRN_DAT_DEBUG_THROW_IF((x != INVALID_LEADER) && (x >= num_blocks()));
157 leaders_[i] = x;
158 }
159
160 private:
161 UInt64 file_size_;
162 UInt32 total_key_length_;
163 UInt32 next_key_id_;
164 UInt32 max_key_id_;
165 UInt32 num_keys_;
166 UInt32 max_num_keys_;
167 UInt32 num_phantoms_;
168 UInt32 num_zombies_;
169 UInt32 num_blocks_;
170 UInt32 max_num_blocks_;
171 UInt32 next_key_pos_;
172 UInt32 key_buf_size_;
173 UInt32 leaders_[MAX_BLOCK_LEVEL + 1];
174 UInt32 status_flags_;
175 UInt32 reserved_[12];
176};
177
178} // namespace dat
179} // namespace grn
180