summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/lib/dat/trie.hpp
blob: 8a272bb7940c1cdffe97a78a0665cc32da5caf4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* -*- c-basic-offset: 2 -*- */
/* Copyright(C) 2011-2015 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef GRN_DAT_TRIE_HPP_
#define GRN_DAT_TRIE_HPP_

#include "array.hpp"
#include "header.hpp"
#include "node.hpp"
#include "block.hpp"
#include "entry.hpp"
#include "key.hpp"
#include "file.hpp"

namespace grn {
namespace dat {

class GRN_DAT_API Trie {
 public:
  Trie();
  ~Trie();

  void create(const char *file_name = NULL,
              UInt64 file_size = 0,
              UInt32 max_num_keys = 0,
              double num_nodes_per_key = 0.0,
              double average_key_length = 0.0);

  void create(const Trie &trie,
              const char *file_name = NULL,
              UInt64 file_size = 0,
              UInt32 max_num_keys = 0,
              double num_nodes_per_key = 0.0,
              double average_key_length = 0.0);

  void repair(const Trie &trie, const char *file_name = NULL);

  void open(const char *file_name);
  void close();

  void swap(Trie *trie);

  // Users can access a key by its position or ID.
  const Key &get_key(UInt32 key_pos) const {
    GRN_DAT_DEBUG_THROW_IF(key_pos >= next_key_pos());
    return *reinterpret_cast<const Key *>(key_buf_.ptr() + key_pos);
  }
  // If a specified ID is invalid, e.g. the key is already deleted, this
  // function returns a reference to an invalid key object whose id() returns
  // INVALID_KEY_ID.
  const Key &ith_key(UInt32 key_id) const {
    if ((key_id >= min_key_id()) && (key_id <= max_key_id()) &&
        ith_entry(key_id).is_valid()) {
      return get_key(ith_entry(key_id).key_pos());
    }
    return Key::invalid_key();
  }

  bool search(const void *ptr, UInt32 length, UInt32 *key_pos = NULL) const {
    return search_key(static_cast<const UInt8 *>(ptr), length, key_pos);
  }
  // Longest prefix match search.
  bool lcp_search(const void *ptr, UInt32 length,
                  UInt32 *key_pos = NULL) const {
    return lcp_search_key(static_cast<const UInt8 *>(ptr), length, key_pos);
  }

  bool remove(UInt32 key_id) {
    const Key &key = ith_key(key_id);
    if (key.is_valid()) {
      return remove(key.ptr(), key.length());
    }
    return false;
  }
  bool remove(const void *ptr, UInt32 length) {
    return remove_key(static_cast<const UInt8 *>(ptr), length);
  }

  bool insert(const void *ptr, UInt32 length, UInt32 *key_pos = NULL) {
    return insert_key(static_cast<const UInt8 *>(ptr), length, key_pos);
  }

  bool update(UInt32 key_id, const void *ptr, UInt32 length,
              UInt32 *key_pos = NULL) {
    return update_key(ith_key(key_id), static_cast<const UInt8 *>(ptr),
                      length, key_pos);
  }
  bool update(const void *src_ptr, UInt32 src_length,
              const void *dest_ptr, UInt32 dest_length,
              UInt32 *key_pos = NULL) {
    UInt32 src_key_pos;
    if (!search(src_ptr, src_length, &src_key_pos)) {
      return false;
    }
    const Key &src_key = get_key(src_key_pos);
    return update_key(src_key, static_cast<const UInt8 *>(dest_ptr),
                      dest_length, key_pos);
  }

  const Node &ith_node(UInt32 i) const {
    GRN_DAT_DEBUG_THROW_IF(i >= num_nodes());
    return nodes_[i];
  }
  const Block &ith_block(UInt32 i) const {
    GRN_DAT_DEBUG_THROW_IF(i >= num_blocks());
    return blocks_[i];
  }
  const Entry &ith_entry(UInt32 i) const {
    GRN_DAT_DEBUG_THROW_IF(i < min_key_id());
    GRN_DAT_DEBUG_THROW_IF(i > max_key_id());
    return entries_[i];
  }

  const Header &header() const {
    return *header_;
  }

  UInt64 file_size() const {
    return header_->file_size();
  }
  UInt64 virtual_size() const {
    return sizeof(Header)
        + (sizeof(Entry) * num_keys())
        + (sizeof(Block) * num_blocks())
        + (sizeof(Node) * num_nodes())
        + total_key_length();
  }
  UInt32 total_key_length() const {
    return header_->total_key_length();
  }
  UInt32 num_keys() const {
    return header_->num_keys();
  }
  UInt32 min_key_id() const {
    return header_->min_key_id();
  }
  UInt32 next_key_id() const {
    return header_->next_key_id();
  }
  UInt32 max_key_id() const {
    return header_->max_key_id();
  }
  UInt32 max_num_keys() const {
    return header_->max_num_keys();
  }
  UInt32 num_nodes() const {
    return header_->num_nodes();
  }
  UInt32 num_phantoms() const {
    return header_->num_phantoms();
  }
  UInt32 num_zombies() const {
    return header_->num_zombies();
  }
  UInt32 max_num_nodes() const {
    return header_->max_num_nodes();
  }
  UInt32 num_blocks() const {
    return header_->num_blocks();
  }
  UInt32 max_num_blocks() const {
    return header_->max_num_blocks();
  }
  UInt32 next_key_pos() const {
    return header_->next_key_pos();
  }
  UInt32 key_buf_size() const {
    return header_->key_buf_size();
  }
  UInt32 status_flags() const {
    return header_->status_flags();
  }

  void clear_status_flags() {
    header_->set_status_flags(status_flags() & ~CHANGING_MASK);
  }

  void flush();

 private:
  File file_;
  Header *header_;
  Array<Node> nodes_;
  Array<Block> blocks_;
  Array<Entry> entries_;
  Array<UInt32> key_buf_;

  void create_file(const char *file_name,
                   UInt64 file_size,
                   UInt32 max_num_keys,
                   double num_nodes_per_key,
                   double average_key_length);
  void create_file(const char *file_name,
                   UInt64 file_size,
                   UInt32 max_num_keys,
                   UInt32 max_num_blocks,
                   UInt32 key_buf_size);

  void open_file(const char *file_name);

  void map_address(void *address);

  void build_from_trie(const Trie &trie);
  void build_from_trie(const Trie &trie, UInt32 src, UInt32 dest);

  void repair_trie(const Trie &trie);
  void build_from_keys(const UInt32 *begin, const UInt32 *end,
                       UInt32 depth, UInt32 node_id);

  void mkq_sort(UInt32 *l, UInt32 *r, UInt32 depth);
  void insertion_sort(UInt32 *l, UInt32 *r, UInt32 depth);

  inline int get_label(UInt32 key_id, UInt32 depth) const;
  inline int get_median(UInt32 a, UInt32 b, UInt32 c, UInt32 depth) const;
  inline bool less_than(UInt32 lhs, UInt32 rhs, UInt32 depth) const;
  inline static void swap_ids(UInt32 *lhs, UInt32 *rhs);

  bool search_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos) const;
  bool search_linker(const UInt8 *ptr, UInt32 length,
                     UInt32 &node_id, UInt32 &query_pos) const;

  bool lcp_search_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos) const;

  bool remove_key(const UInt8 *ptr, UInt32 length);

  bool insert_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos);
  bool insert_linker(const UInt8 *ptr, UInt32 length,
                     UInt32 &node_id, UInt32 query_pos);

  bool update_key(const Key &key, const UInt8 *ptr, UInt32 length,
                  UInt32 *key_pos);

  UInt32 insert_node(UInt32 node_id, UInt16 label);
  UInt32 append_key(const UInt8 *ptr, UInt32 length, UInt32 key_id);

  UInt32 separate(const UInt8 *ptr, UInt32 length,
                  UInt32 node_id, UInt32 i);
  void resolve(UInt32 node_id, UInt16 label);
  void migrate_nodes(UInt32 node_id, UInt32 dest_offset,
                     const UInt16 *labels, UInt32 num_labels);

  UInt32 find_offset(const UInt16 *labels, UInt32 num_labels);

  void reserve_node(UInt32 node_id);
  void reserve_block(UInt32 block_id);

  void update_block_level(UInt32 block_id, UInt32 level);
  void set_block_level(UInt32 block_id, UInt32 level);
  void unset_block_level(UInt32 block_id);

  Node &ith_node(UInt32 i) {
    GRN_DAT_DEBUG_THROW_IF(i >= num_nodes());
    return nodes_[i];
  }
  Block &ith_block(UInt32 i) {
    GRN_DAT_DEBUG_THROW_IF(i >= num_blocks());
    return blocks_[i];
  }
  Entry &ith_entry(UInt32 i) {
    GRN_DAT_DEBUG_THROW_IF(i < min_key_id());
    GRN_DAT_DEBUG_THROW_IF(i > (max_key_id() + 1));
    return entries_[i];
  }

  // Disallows copy and assignment.
  Trie(const Trie &);
  Trie &operator=(const Trie &);
};

}  // namespace dat
}  // namespace grn

#endif  // GRN_DAT_TRIE_HPP_