Groonga 3.0.9 Source Code Document
Main Page
Related Pages
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
groonga
lib
hash.h
Go to the documentation of this file.
1
/* -*- c-basic-offset: 2 -*- */
2
/* Copyright(C) 2009-2012 Brazil
3
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License version 2.1 as published by the Free Software Foundation.
7
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Lesser General Public License for more details.
12
13
You should have received a copy of the GNU Lesser General Public
14
License along with this library; if not, write to the Free Software
15
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
*/
17
#ifndef GRN_HASH_H
18
#define GRN_HASH_H
19
20
#ifndef GROONGA_IN_H
21
#include "
groonga_in.h
"
22
#endif
/* GROONGA_IN_H */
23
24
#ifndef GRN_CTX_H
25
#include "
ctx.h
"
26
#endif
/* GRN_CTX_H */
27
28
#ifdef __cplusplus
29
extern
"C"
{
30
#endif
31
32
/**** grn_tiny_array ****/
33
34
/*
35
* grn_tiny_array_init() accepts a logical OR of the following flags.
36
* Note that other flags, such as (1 << 30), will be ignored.
37
*
38
* - GRN_TINY_ARRAY_CLEAR specifies to initialize a new block with zeros.
39
* It is valid only iff specified with GRN_TINY_ARRAY_USE_MALLOC.
40
* - GRN_TINY_ARRAY_THREADSAFE specifies to create a critical section when
41
* allocating memory.
42
* - GRN_TINY_ARRAY_USE_MALLOC specifies to use GRN_MALLOC/CALLOC/FREE instead
43
* of GRN_CTX_ALLOC/FREE.
44
*/
45
#define GRN_TINY_ARRAY_CLEAR (1 << 0)
46
#define GRN_TINY_ARRAY_THREADSAFE (1 << 1)
47
#define GRN_TINY_ARRAY_USE_MALLOC (1 << 2)
48
49
/*
50
* - GRN_TINY_ARRAY_FACTOR is the global parameter of grn_tiny_array.
51
* - GRN_TINY_ARRAY_GET_OFFSET() returns the offset of a specified block.
52
* - GRN_TINY_ARRAY_BASE_BLOCK_SIZE is the number of elements in the first
53
* block.
54
* - GRN_TINY_ARRAY_GET_BLOCK_SIZE() returns the number of elements in a
55
* specified block.
56
* - GRN_TINY_ARRAY_NUM_BLOCKS is the maximum number of blocks.
57
*/
58
#define GRN_TINY_ARRAY_FACTOR 0
59
#define GRN_TINY_ARRAY_GET_OFFSET(block_id) \
60
(1 << ((block_id) << GRN_TINY_ARRAY_FACTOR))
61
#define GRN_TINY_ARRAY_BASE_BLOCK_SIZE \
62
(GRN_TINY_ARRAY_GET_OFFSET(1) - GRN_TINY_ARRAY_GET_OFFSET(0))
63
#define GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id) \
64
(GRN_TINY_ARRAY_BASE_BLOCK_SIZE * GRN_TINY_ARRAY_GET_OFFSET(block_id))
65
#define GRN_TINY_ARRAY_NUM_BLOCKS (32 >> GRN_TINY_ARRAY_FACTOR)
66
67
/*
68
* grn_tiny_array uses several blocks to emulate an array.
69
* The k-th block, blocks[k - 1], consists of 2^(k-1) elements.
70
*/
71
typedef
struct
_grn_tiny_array
grn_tiny_array
;
72
73
struct
_grn_tiny_array
{
74
grn_ctx
*
ctx
;
75
grn_id
max
;
76
uint16_t
element_size
;
77
uint16_t
flags
;
78
void
*
blocks
[
GRN_TINY_ARRAY_NUM_BLOCKS
];
79
grn_critical_section
lock
;
80
};
81
82
#define GRN_TINY_ARRAY_EACH(array, head, tail, key, value, block) do { \
83
int _block_id; \
84
const grn_id _head = (head); \
85
const grn_id _tail = (tail); \
86
for (_block_id = 0, (key) = (_head); \
87
_block_id < GRN_TINY_ARRAY_NUM_BLOCKS && (key) <= (_tail); \
88
_block_id++) { \
89
int _id = GRN_TINY_ARRAY_GET_BLOCK_SIZE(_block_id); \
90
(value) = (array)->blocks[_block_id]; \
91
if (value) { \
92
while (_id-- && (key) <= (_tail)) { \
93
{ \
94
block \
95
} \
96
(key)++; \
97
(value) = (void *)((byte *)(value) + (array)->element_size); \
98
} \
99
} else { \
100
(key) += _id; \
101
} \
102
} \
103
} while (0)
104
105
GRN_API
void
grn_tiny_array_init
(
grn_ctx
*ctx,
grn_tiny_array
*array,
106
uint16_t element_size, uint16_t flags);
107
GRN_API
void
grn_tiny_array_fin
(
grn_tiny_array
*array);
108
GRN_API
void
*
grn_tiny_array_at
(
grn_tiny_array
*array,
grn_id
id
);
109
GRN_API
grn_id
grn_tiny_array_id
(
grn_tiny_array
*array,
110
const
void
*element_address);
111
112
/**** grn_tiny_bitmap ****/
113
114
typedef
struct
_grn_tiny_bitmap
grn_tiny_bitmap
;
115
116
struct
_grn_tiny_bitmap
{
117
grn_ctx
*
ctx
;
118
void
*
blocks
[
GRN_TINY_ARRAY_NUM_BLOCKS
];
119
};
120
121
/**** grn_array ****/
122
123
#define GRN_ARRAY_TINY (0x01<<6)
124
125
/*
126
* grn_array uses grn_io or grn_tiny_array to represent an array.
127
*
128
* To create a grn_tiny_array-based grn_array, specify the GRN_ARRAY_TINY flag
129
* to grn_array_create(). Note that a grn_tiny_array-based grn_array is not
130
* backed by a file.
131
*/
132
struct
_grn_array
{
133
grn_db_obj
obj
;
134
grn_ctx
*
ctx
;
135
uint32_t
value_size
;
136
int32_t
n_keys
;
137
grn_table_sort_key
*
keys
;
138
uint32_t *
n_garbages
;
139
uint32_t *
n_entries
;
140
141
/* For grn_io_array. */
142
grn_io
*
io
;
143
struct
grn_array_header
*
header
;
144
uint32_t *
lock
;
145
146
/* For grn_tiny_array. */
147
uint32_t
n_garbages_buf
;
148
uint32_t
n_entries_buf
;
149
grn_id
garbages
;
150
grn_tiny_array
array
;
151
grn_tiny_bitmap
bitmap
;
152
};
153
154
struct
_grn_array_cursor
{
155
grn_db_obj
obj
;
156
grn_array
*
array
;
157
grn_ctx
*
ctx
;
158
grn_id
curr_rec
;
159
grn_id
tail
;
160
unsigned
int
rest
;
161
int
dir
;
162
};
163
164
#define GRN_ARRAY_SIZE(array) (*((array)->n_entries))
165
166
grn_rc
grn_array_truncate
(
grn_ctx
*ctx,
grn_array
*array);
167
grn_rc
grn_array_copy_sort_key
(
grn_ctx
*ctx,
grn_array
*array,
168
grn_table_sort_key
*keys,
int
n_keys);
169
170
/* grn_table_queue */
171
172
typedef
struct
_grn_table_queue
grn_table_queue
;
173
174
struct
_grn_table_queue
{
175
grn_mutex
mutex
;
176
grn_cond
cond
;
177
grn_id
head
;
178
grn_id
tail
;
179
grn_id
cap
;
180
grn_bool
unblock_requested
;
181
};
182
183
GRN_API
void
grn_array_queue_lock_clear
(
grn_ctx
*ctx,
grn_array
*array);
184
GRN_API
void
grn_array_clear_curr_rec
(
grn_ctx
*ctx,
grn_array
*array);
185
GRN_API
grn_table_queue
*
grn_array_queue
(
grn_ctx
*ctx,
grn_array
*array);
186
GRN_API
uint32_t
grn_table_queue_size
(
grn_table_queue
*queue);
187
GRN_API
void
grn_table_queue_head_increment
(
grn_table_queue
*queue);
188
GRN_API
void
grn_table_queue_tail_increment
(
grn_table_queue
*queue);
189
GRN_API
grn_id
grn_table_queue_head
(
grn_table_queue
*queue);
190
GRN_API
grn_id
grn_table_queue_tail
(
grn_table_queue
*queue);
191
192
/**** grn_hash ****/
193
194
#define GRN_HASH_TINY (0x01<<6)
195
#define GRN_HASH_MAX_KEY_SIZE GRN_TABLE_MAX_KEY_SIZE
196
197
struct
_grn_hash
{
198
grn_db_obj
obj
;
199
grn_ctx
*
ctx
;
200
uint32_t
key_size
;
201
grn_encoding
encoding
;
202
uint32_t
value_size
;
203
uint32_t
entry_size
;
204
uint32_t *
n_garbages
;
205
uint32_t *
n_entries
;
206
uint32_t *
max_offset
;
207
grn_obj
*
tokenizer
;
208
grn_obj
*
normalizer
;
209
210
/* For grn_io_hash. */
211
grn_io
*
io
;
212
struct
grn_hash_header
*
header
;
213
uint32_t *
lock
;
214
// uint32_t nref;
215
// unsigned int max_n_subrecs;
216
// unsigned int record_size;
217
// unsigned int subrec_size;
218
// grn_rec_unit record_unit;
219
// grn_rec_unit subrec_unit;
220
// uint8_t arrayp;
221
// grn_recordh *curr_rec;
222
// grn_set_cursor *cursor;
223
// int limit;
224
// void *userdata;
225
// grn_id subrec_id;
226
227
/* For grn_tiny_hash. */
228
uint32_t
max_offset_
;
229
uint32_t
n_garbages_
;
230
uint32_t
n_entries_
;
231
grn_id
*
index
;
232
grn_id
garbages
;
233
grn_tiny_array
a
;
234
grn_tiny_bitmap
bitmap
;
235
};
236
237
/* Header of grn_io_hash. */
238
struct
grn_hash_header
{
239
uint32_t
flags
;
240
grn_encoding
encoding
;
241
uint32_t
key_size
;
242
uint32_t
value_size
;
243
grn_id
tokenizer
;
244
uint32_t
curr_rec
;
245
int32_t
curr_key
;
246
uint32_t
idx_offset
;
247
uint32_t
entry_size
;
248
uint32_t
max_offset
;
249
uint32_t
n_entries
;
250
uint32_t
n_garbages
;
251
uint32_t
lock
;
252
grn_id
normalizer
;
253
uint32_t
reserved
[15];
254
grn_id
garbages
[
GRN_HASH_MAX_KEY_SIZE
];
255
grn_table_queue
queue
;
256
};
257
258
struct
_grn_hash_cursor
{
259
grn_db_obj
obj
;
260
grn_hash
*
hash
;
261
grn_ctx
*
ctx
;
262
grn_id
curr_rec
;
263
grn_id
tail
;
264
unsigned
int
rest
;
265
int
dir
;
266
};
267
268
/* deprecated */
269
270
#define GRN_TABLE_SORT_BY_KEY 0
271
#define GRN_TABLE_SORT_BY_ID (1L<<1)
272
#define GRN_TABLE_SORT_BY_VALUE (1L<<2)
273
#define GRN_TABLE_SORT_RES_ID 0
274
#define GRN_TABLE_SORT_RES_KEY (1L<<3)
275
#define GRN_TABLE_SORT_AS_BIN 0
276
#define GRN_TABLE_SORT_AS_NUMBER (1L<<4)
277
#define GRN_TABLE_SORT_AS_SIGNED 0
278
#define GRN_TABLE_SORT_AS_UNSIGNED (1L<<5)
279
#define GRN_TABLE_SORT_AS_INT32 0
280
#define GRN_TABLE_SORT_AS_INT64 (1L<<6)
281
#define GRN_TABLE_SORT_NO_PROC 0
282
#define GRN_TABLE_SORT_WITH_PROC (1L<<7)
283
284
typedef
struct
_grn_table_sort_optarg
grn_table_sort_optarg
;
285
286
struct
_grn_table_sort_optarg
{
287
grn_table_sort_flags
flags
;
288
int (*
compar
)(
grn_ctx
*ctx,
289
grn_obj
*table1,
void
*target1,
unsigned
int
target1_size,
290
grn_obj
*table2,
void
*target2,
unsigned
int
target2_size,
291
void
*compare_arg);
292
void
*
compar_arg
;
293
grn_obj
*
proc
;
294
int
offset
;
295
};
296
297
GRN_API
int
grn_hash_sort
(
grn_ctx
*ctx,
grn_hash
*hash,
int
limit,
298
grn_array
*result,
grn_table_sort_optarg
*optarg);
299
300
grn_rc
grn_hash_lock
(
grn_ctx
*ctx,
grn_hash
*hash,
int
timeout);
301
grn_rc
grn_hash_unlock
(
grn_ctx
*ctx,
grn_hash
*hash);
302
grn_rc
grn_hash_clear_lock
(
grn_ctx
*ctx,
grn_hash
*hash);
303
304
#define GRN_HASH_SIZE(hash) (*((hash)->n_entries))
305
306
/* private */
307
typedef
enum
{
308
grn_rec_document
= 0,
309
grn_rec_section
,
310
grn_rec_position
,
311
grn_rec_userdef
,
312
grn_rec_none
313
}
grn_rec_unit
;
314
315
GRN_API
grn_rc
grn_hash_truncate
(
grn_ctx
*ctx,
grn_hash
*hash);
316
317
int
grn_rec_unit_size
(
grn_rec_unit
unit,
int
rec_size);
318
319
const
char
*
_grn_hash_key
(
grn_ctx
*ctx,
grn_hash
*hash,
grn_id
id
, uint32_t *key_size);
320
321
int
grn_hash_get_key_value
(
grn_ctx
*ctx,
grn_hash
*hash,
grn_id
id
,
322
void
*keybuf,
int
bufsize,
void
*valuebuf);
323
324
int
_grn_hash_get_key_value
(
grn_ctx
*ctx,
grn_hash
*hash,
grn_id
id
,
325
void
**key,
void
**value);
326
327
grn_id
grn_hash_next
(
grn_ctx
*ctx,
grn_hash
*hash,
grn_id
id
);
328
329
/* only valid for hash tables, GRN_OBJ_KEY_VAR_SIZE && GRN_HASH_TINY */
330
const
char
*
_grn_hash_strkey_by_val
(
void
*v, uint16_t *size);
331
332
const
char
*
grn_hash_get_value_
(
grn_ctx
*ctx,
grn_hash
*hash,
grn_id
id
, uint32_t *size);
333
334
grn_rc
grn_hash_remove
(
grn_ctx
*ctx,
const
char
*path);
335
grn_rc
grn_array_remove
(
grn_ctx
*ctx,
const
char
*path);
336
337
grn_id
grn_hash_at
(
grn_ctx
*ctx,
grn_hash
*hash,
grn_id
id
);
338
grn_id
grn_array_at
(
grn_ctx
*ctx,
grn_array
*array,
grn_id
id
);
339
340
void
grn_hash_check
(
grn_ctx
*ctx,
grn_hash
*hash);
341
342
#ifdef __cplusplus
343
}
344
#endif
345
346
#endif
/* GRN_HASH_H */
Generated on Sun Nov 10 2013 09:49:02 for Groonga 3.0.9 Source Code Document by
1.8.1.2