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 Block {
27 public:
28 Block() : next_(0), prev_(0), first_phantom_(0), num_phantoms_(0) {}
29
30 // Blocks in the same level are stored in a doubly-linked list which is
31 // represented by the following next() and prev().
32 UInt32 next() const {
33 return next_ / BLOCK_SIZE;
34 }
35 UInt32 prev() const {
36 return prev_ / BLOCK_SIZE;
37 }
38
39 // A level indicates how easyily find_offset() can find a good offset in that
40 // block. It is easier in lower level blocks.
41 UInt32 level() const {
42 return next_ & BLOCK_MASK;
43 }
44 // A block level rises when find_offset() fails to find a good offset
45 // MAX_FAILURE_COUNT times in that block.
46 UInt32 failure_count() const {
47 return prev_ & BLOCK_MASK;
48 }
49
50 UInt32 first_phantom() const {
51 return first_phantom_;
52 }
53 UInt32 num_phantoms() const {
54 return num_phantoms_;
55 }
56
57 void set_next(UInt32 x) {
58 GRN_DAT_DEBUG_THROW_IF(x > MAX_BLOCK_ID);
59 next_ = (next_ & BLOCK_MASK) | (x * BLOCK_SIZE);
60 }
61 void set_prev(UInt32 x) {
62 GRN_DAT_DEBUG_THROW_IF(x > MAX_BLOCK_ID);
63 prev_ = (prev_ & BLOCK_MASK) | (x * BLOCK_SIZE);
64 }
65
66 void set_level(UInt32 x) {
67 GRN_DAT_DEBUG_THROW_IF(x > MAX_BLOCK_LEVEL);
68 GRN_DAT_DEBUG_THROW_IF(x > BLOCK_MASK);
69 next_ = (next_ & ~BLOCK_MASK) | x;
70 }
71 void set_failure_count(UInt32 x) {
72 GRN_DAT_DEBUG_THROW_IF(x > MAX_FAILURE_COUNT);
73 GRN_DAT_DEBUG_THROW_IF(x > BLOCK_MASK);
74 prev_ = (prev_ & ~BLOCK_MASK) | x;
75 }
76
77 void set_first_phantom(UInt32 x) {
78 GRN_DAT_DEBUG_THROW_IF(x >= BLOCK_SIZE);
79 first_phantom_ = (UInt16)x;
80 }
81 void set_num_phantoms(UInt32 x) {
82 GRN_DAT_DEBUG_THROW_IF(x > BLOCK_SIZE);
83 num_phantoms_ = (UInt16)x;
84 }
85
86 private:
87 UInt32 next_;
88 UInt32 prev_;
89 UInt16 first_phantom_;
90 UInt16 num_phantoms_;
91};
92
93} // namespace dat
94} // namespace grn
95